tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

UDP packets not sent from ESP-8266 – solved

20 Apr 2016 1 mins .NET, Arduino, C#, ESP-8266, IoT, Network

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.

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 😉).

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 or before entering deep sleep. Let me know if you understand it.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.