UDP packets not sent from ESP-8266 - solved
I’m now playing with ESP-8266, Wemos D1 mini to be precise. One think I’m now using for fun/debugging/communication is broadcasting UDP packets. For some reason the packets were not coming lately.
You might say: “Jiri, it’s UDP. Packets will get lost.”. True. But my packets were lost in 99% cases, on my home network. Something is going on.
My code was pretty simple.
1 2 3 4
WiFiUDP client; client.beginPacket(...); client.write(...); client.endPacket();
I was playing with different IP addresses - unicast, broadcast. Nothing was working. As I was digging deeper I faced few times WDT reset. Good night sleep and I was able to connect the dots. I changed my code to use
ESP.deepSleep (as I was prototyping more real world scenario) from
delay one day ago. And this call was right after returning from the sending method (the one above). Could it be, that the board didn’t had time to finish everything before going to sleep?
So I modified the code slightly (I tried calling
yield blindly first before going safely to
delay - I’m still learning, although this seemed like a reasonable function to have ;)).
1 2 3 4 5
WiFiUDP client; client.beginPacket(...); client.write(...); client.endPacket(); yield();
With this change the code now works like a charm. I don’t understand the real reason deep behind, but that’s the beauty of learning. Also now thinking about it. I’m not sure it should be logically after sending the packet of before entering deep sleep. Let me know if you understand it.