Cannot send any UDP packets from ESP8266 Wemos D1 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 &lt;ESP8266WiFi.h&gt;
// https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/udp-examples.html
#include &lt;WiFiUdp.h&gt;

// Create udp class instance
WiFiUDP udp;

void setup() {
  Serial.begin(115200);
  Serial.print(&quot;Connecting to WiFi&quot;);
  WiFi.mode(WIFI_STA);
  WiFi.hostname(hostname);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(&quot;.&quot;);
  }

  Serial.print(&quot;SSID:\t&quot;);
  Serial.println(WiFi.SSID());
  
  Serial.print(&quot;IP address:\t&quot;);
  Serial.println(WiFi.localIP());

  Serial.print(&quot;Gateway:\t&quot;);
  Serial.println(WiFi.gatewayIP());

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

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

  // Calculate required sleep time and go to sleep
  long sleepTime = measurementInterval - millis(); // in milliseconds
  if (sleepTime &lt; 100) sleepTime = 100; // set minimum sleep of 0.1 second
  Serial.print (&quot;Going to sleep now for &quot;);
  Serial.print((float)sleepTime / 1000, 3);
  Serial.println (&quot; seconds.&quot;);
  Serial.print (&quot;Time going to sleep=&quot;);
  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) =&gt; {
  console.log(`server error:\n${err.stack}`)
  server.close()
})

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

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

server.bind(3381)

I've also tried adding delays between each function to no avail.
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.