Projects / UDP sensor broadcast on Ethernet

UDP sensor broadcast on Ethernet

Intermediate1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the module

    SPI and a proper 3.3 V supply.

  2. NANO

    Get an address

    Print the DHCP address.

  3. NANODHT2226.6°C

    Read the DHT22

    Print values.

  4. NANO

    Send one packet

    To the broadcast address.

  5. NANO

    Listen on a computer

    Use the Python line.

  6. NANO

    Add a device name

    Several nodes, one listener.

  7. NANO

    Compare with TCP

    Note what is lost.

In short

How do you build an UDP sensor broadcast on Ethernet?

A wired sensor broadcasts readings over UDP so any computer on the network can listen. You need Arduino Nano, ENC28J60 Ethernet module, DHT22 Sensor, Resistor 10 kΩ and Ethernet cable Cat6 (3 m). It takes 7 steps and about 90 minutes.

Level
Intermediate
Time
About 90 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ENC28J60 CSNano D10Chip select
ENC28J60 SI, SO, SCKNano D11, D12, D13SPI
ENC28J60 VCC and GND3.3 V supply and GNDNeeds about 160 mA: do not use the Nano 3V3 pin (use the module's own regulator if it has one)
ENC28J60 RSTLeave openOptional
DHT22 dataNano D3, with 10 k to 5VSensor signal
DHT22 VCC and GNDNano 5V and GNDPower
Ethernet cableRouter or switchNetwork

Code

UDP is the simplest way to send data: no connection, no reply, one message goes out and whoever is listening picks it up. A broadcast goes to every machine on the network at once, so one sensor can feed a PC, a Raspberry Pi and a laptop at the same time with no setup on the sender. The sketch works out the broadcast address from the network's own mask, so it works on any home network. UDP has no delivery guarantee, so a message can be lost, which is fine for a reading repeated every few seconds but not for something that must arrive; compare with TCP by writing the same data to a server. The ENC28J60 needs a steady 3.3 V at about 160 mA, more than the Nano's own 3.3 V pin can give, so power it from its own regulator or a separate supply. The Nano has little memory, so keep the messages short. Keep the node inside your own network: broadcasts are not encrypted and anyone on the network can read them. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// UDP broadcast of temperature and humidity. Libraries: UIPEthernet (ENC28J60 with the Ethernet API), DHT sensor library.
// Board: Arduino Nano. Listen on a computer with Python:
//   python3 -c "import socket; s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.bind(('', 9999)); print(s.recvfrom(200))"
#include <UIPEthernet.h>
#include <DHT.h>
DHT dht(3, DHT22);
EthernetUDP udp;
byte mac[] = {0x02, 0xAB, 0xCD, 0x00, 0x00, 0x09};     // locally administered; change it if you build several
const char* NAME = "nano1";                            // device name in each message
const unsigned int PORT = 9999;
void setup() {
  Serial.begin(115200);
  dht.begin();
  if (Ethernet.begin(mac) == 0) { Serial.println("No DHCP address"); while (true) delay(1000); }
  IPAddress ip = Ethernet.localIP();
  Serial.print("IP "); Serial.println(ip);
  udp.begin(PORT);
}
void loop() {
  float t = dht.readTemperature(), h = dht.readHumidity();
  if (!isnan(t) && !isnan(h)) {
    IPAddress ip = Ethernet.localIP(), mask = Ethernet.subnetMask();
    IPAddress bc(ip[0] | ~mask[0], ip[1] | ~mask[1], ip[2] | ~mask[2], ip[3] | ~mask[3]);   // network broadcast address
    char msg[64];
    snprintf(msg, sizeof(msg), "name=%s;t=%d.%d;h=%d", NAME, (int)t, abs((int)(t * 10) % 10), (int)h);
    udp.beginPacket(bc, PORT);
    udp.write(msg);
    udp.endPacket();
    Serial.println(msg);
  }
  Ethernet.maintain();
  delay(5000);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
BootPrints an IP address
Python listenerReceives a line every 5 seconds
Two nodesBoth appear, with different names
Cable pulledMessages stop, resume on plug

If it does not work

No DHCP

Check the cable and the 3.3 V supply.

Resets randomly

The Nano 3V3 pin cannot power the module.

Nothing received

Check the firewall on the listening computer.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the UDP sensor broadcast on Ethernet?

Arduino Nano, ENC28J60 Ethernet module, DHT22 Sensor, Resistor 10 kΩ and Ethernet cable Cat6 (3 m).

How long does the UDP sensor broadcast on Ethernet take to build?

About 90 minutes for a intermediate maker, in 7 steps.

How do I know the UDP sensor broadcast on Ethernet works?

Boot: Prints an IP address. Python listener: Receives a line every 5 seconds. Two nodes: Both appear, with different names.

No DHCP: what should I check?

Check the cable and the 3.3 V supply.

Resets randomly: what should I check?

The Nano 3V3 pin cannot power the module.

Nothing received: what should I check?

Check the firewall on the listening computer.