Metrika
Ivy Knob

ESP8266: Solving random connection problems

ESP8266: Solving random connection problems

When working on our distributed sensor network project, I sometimes got strange increases in periods of data. Our project firmware is based on Arduino SDK for ESP8266 board (2.4.0 version when bug was found). After digging a little, I found that sometimes connection to MQTT was lost randomly. It did not bother us for the first time and we put the task to the backlog to solve this situation later.

When we returned to this problem, we had a lot of information:

  1. This errors occurred in 3 different wifi networks with 3 different boards.
  2. Errors was random and lasted random time.

Logs showed same message on all of the boards:

[HTTP] GET... failed, error: connection refused

After turning on the debug, I finally found interesting message in the logs:

Host: google.com lookup error: -5!

Googling the problem showed similar issues in the Github, but no information about how to solve it. The were some advices to downgrade Arduino SDK version to 2.3.0, but it did not compile in PlatformIO. There were also blurry messages about lwIP.

lwIP - is a lightweight TCP/IP stack implementation, that is widely used in embedded devices. ESP8266 Arduino SDK uses it under the hood.

Solution

I tried to play with lwIP versions and instantly got results. I changed the version to V2 and all my problems was solved.

If you use PlatformIO, just add this line to your platformio.ini file:

build_flags =
-D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH

If you are using Arduino IDE, you should set this build flag over UI:

Arduino ESP8266 connection problem fix