Possible fix for ESP8266 random hangs

Since I’ve been playing with blynk and ESP8266 modules the past few days, the modules seem to hang constantly at random intervals.

I finally discovered that the problem lies in the file: BlynkProtocol.h

When sending the data to the blynk server fails, the library might hit an infinite loop. I changed the following lines:

while (wlen < len2s) {
        const size_t chunk = BlynkMin(size_t(BLYNK_SEND_CHUNK), len2s - wlen);
        wlen += conn.write(buff + wlen, chunk);

into:

    size_t wlentmp = 0;
    while (wlen < len2s) {
        const size_t chunk = BlynkMin(size_t(BLYNK_SEND_CHUNK), len2s - wlen);
    	wlentmp = conn.write(buff + wlen, chunk);
    	if (wlentmp == 0) {
			#ifdef BLYNK_DEBUG
        		    BLYNK_LOG("Cmd not sent");
			#endif
        	return;
    	}
        wlen += wlentmp;

Hope this helps improve the code.

3 Likes

When using a generic ESP8266 module, make sure you have a 100nf capacitor or similar over the power lines, as close to the chip as possible. This solved my problems with the module crashing and sending a memory dump over the serial line.

1 Like

thanks! I will take a look on this ASAP!

@vdwel Really nice fix, thanks for sharing!!

I use my ESP8266 with blynk in standalone mode, so far so good. But after adding several sensor, thinkspeak, ntp-sync, etc. I was kind of disappointed when I saw my ESP randomly hangs (no crash, really doing nothing).
It has once successfully worked for almost 6 hours! I was suspecting the hardware but I made a try without blynk and guess what, 3 days of uptime until I added few more line of code.

I have fixed the while loop and I’m happy again, uptime with blynk for now 12h. It’s not a lot but looks like promising since it beats the max uptime I had during one of my numerous blynk trys.

@vshymanskyy Sometimes, I need to kill the Blynk android app on my LG G2 in order to re-connect to my ESP. Also I didn’t try the notify, but I’m assuming that it notifies only if the blynk app in running. It should be great to have notification even if the blynk app was not started or at least started but put in some kind of background mode.

1 Like

Why is that?

No. Notifications work when app is not started or in background.

It’s look like the app randomly hangs, app was started 10 min ago, home button, doing stuff with my phone, then I ā€œwake upā€ the blynk app from the running process manager, I push some button, slider and no response (even if I wait 1min). So I kill it, re-launch, it reconnect to the server and then works as expected.
But anyhow, not a huge problem to me, the ESP hanging was my only huge problem.

My bad, I should have tried it before making assumption.

Hm… Do you use latest Android version? This bug was fixed already.

Yep, latest on the PlayStore (1.0 RC5 fix)

Ok, we’ll have a look. Thank you for detailed steps.

I’ve included this code, thanks!

A question though, should there be some amount of retry? I’m guessing the original code wasn’t a comms retry as much as a ā€˜finish the send’ loop. I’m wondering if there were cases where this loop resulted in a successful retry and didn’t loop endlessly.

I guess I just don’t understand the bug/fix.

Just a few lines before the while loop, they check if the connection is up. If it is down, they also exit without reconnecting. It would be nice to have a return value to check if the data was sent successfully.

I guess it doesn’t make a big difference, because a retry will automatically happen with the next blynk.run()… And we wouldn’t want the program to lockup when the internet is down.

I included the above code and it seems to have helped, but I am still unable to keep the ESP online for more than 12 hours.

It is interesting that my Android application occasionally issues disconnection notifications, even though I can still access the ESP when I re-open the Blynk application…

Try adding an extra blynk.run() line after writing to each virtual wire. This somehow helped me got rid of the disconnects.

I got an LCD display with the ESP hanging onto my Arduino, but it’s really instable. The LCD flickers a lot too, so I think (quite sure actually) the ESP needs a whole lot more juice then the duino can deliver. Wifi chips eat a lot.

I ordered a seperate 3.3v step down module which can deliver 3a at 3.3v. The ESP is specced at about 500mA, so make sure you have enough power for it.

That’s right. You can’t power an esp reliably with an arduino board.

The easiest solution is to get a nodemcu board. Gives you a stable power supply and no hassle with capacitors to make the thing stable.

The esp8266 is very sensitive for noise on the power supply lines.

2 Likes

Strongly agree! Either use capacitors + pull resistors or an already assembled board like NodeMCU

Ok so with the fix, my ESP has reached about 13 days of uptime but sadly not more …
I will try to add a connection test in the main loop, something like pinging my wifi passerel and if it fails several time reset the esp …

I don’t think I am having power issue with my setup (ESP12 + TinyESP + DHT22).

1 Like

Hi i have nodemcu board esp8266 witty from ali and i have same problem. Esp was disconected ewery 10-30 sec. i run basic ino file, standalone. on witty is rgb led and ldr sensor. connection is totaly unstable.

I’ve had an ESP8266 (the original -01 version) running for months without issue, and I think there are 2 keys to that stability:

  1. Decoupling capacitors. Add a .1uF ceramic capacitor and a 10uF electrolytic capacitor between the 3.3V and GND pins.

  2. A ā€œcleanā€ void loop():

    void loop() {

    Blynk.run();
    timer.run();
    yield();
    }

All additional code should be called by the timer function. The yield() may be unnecessary, but I’ve read that it keeps the ESP’s background processes running smoothly. In any case, this combination of hardware and code has been absolutely rock solid.

You don’t need a board like nodemcu necessarily. Even a breadboard adapter like this with a decoupling cap has worked perfect for me:
esp-01 BB adapter