I am attempting to send sensor data from a battery-powered device using UDP. The goal is to send a datagram immediately after the wifi station awakes from sleep then immediately go back to sleep after writing the datagram to a given IP address or hostname.

I can send data to Thingspeak's API using their library so I know the device works, but when attempting to use the WiFiUDP library to send a datagram to an IP address on the same LAN I see nothing happen. My device is getting on the wifi and the udp.endPacket() command is returning successful, but I am not receiving the packets on the server. I know the server works because I can use nc to send UDP packets from other machines on the LAN.

Here is my sketch with the irrelevant bits redacted.

// https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/station-class.html
#include <ESP8266WiFi.h>
// https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/udp-examples.html
#include <WiFiUdp.h>

// Create udp class instance
WiFiUDP udp;

void setup() {
  Serial.print("Connecting to WiFi");
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {

  Serial.print("IP address:\t");


  uint8_t buffer[50] = "hello world";
  char message[] = "hello world 2";

  IPAddress staticIP(192, 168, 1, 118);
  udp.beginPacket(staticIP, 3381);
  udp.write(buffer, 11);

  // Calculate required sleep time and go to sleep
  long sleepTime = measurementInterval - millis(); // in milliseconds
  if (sleepTime < 100) sleepTime = 100; // set minimum sleep of 0.1 second
  Serial.print ("Going to sleep now for ");
  Serial.print((float)sleepTime / 1000, 3);
  Serial.println (" seconds.");
  Serial.print ("Time going to sleep=");
  Serial.print ((float)millis() / 1000, 3);
  ESP.deepSleep(sleepTime * 5000); // convert to microseconds

And my node server just to be complete here:

const dgram = require('dgram')
const server = dgram.createSocket('udp4')

server.on('error', (err) => {
  console.log(`server error:\n${err.stack}`)

server.on('message', (msg, rinfo) => {
  console.log(`got message from ${rinfo.address}:${rinfo.port} - ${msg}`)

server.on('listening', () => {
  const address = server.address()
  console.log(`server listening ${address.address}:${address.port}`)


I've also tried adding delays between each function to no avail.

  • 1
    I don't see udp.begin(). Some Arduino libraries require it even for sending.
    – Juraj
    Jul 3 '20 at 4:33
  • run wireshark on your server or on any pc on your LAN ... see what is being sent
    – jsotola
    Jul 3 '20 at 5:24
  • @Juraj udp.begin() is for listening, but I did try it and see no difference. Jul 4 '20 at 2:42
  • @jsotola wireshark captures no information from the ip of the esp8266 device, udp or otherwise. Jul 4 '20 at 2:44
  • you have to send the packets to the IP address of the PC that is running wireshark
    – jsotola
    Jul 4 '20 at 2:53

Per the esp8266 subreddit, you need to add a sufficient delay() of 50-100 milliseconds (not microseconds) before going to sleep as none of the udp functions are blocking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.