{"id":631,"date":"2015-05-28T15:29:52","date_gmt":"2015-05-28T22:29:52","guid":{"rendered":"http:\/\/internetofhomethings.com\/homethings\/?p=631"},"modified":"2015-07-05T15:30:20","modified_gmt":"2015-07-05T22:30:20","slug":"esp8266-wifi-dropout-proof-connectivity","status":"publish","type":"post","link":"https:\/\/internetofhomethings.com\/homethings\/?p=631","title":{"rendered":"ESP8266 WIFI dropout-proof connectivity"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong>How to eliminate ESP8266 WIFI drop-outs<\/strong><\/h4>\n<p>Just when it seemed like\u00a0the ESP8266 was operating rock-solid, indeed running continuously for days now without resetting, or worse, fatally crashing, a sign that something was wrong appeared&#8230;<\/p>\n<p><a href=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/wifi-sticker.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-641\" src=\"http:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/wifi-sticker-300x300.jpg\" alt=\"wifi\" width=\"300\" height=\"300\" srcset=\"https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/wifi-sticker-300x300.jpg 300w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/wifi-sticker-150x150.jpg 150w, https:\/\/internetofhomethings.com\/homethings\/wp-content\/uploads\/2015\/05\/wifi-sticker.jpg 800w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Out of the corner of my eye, a flash of light seemed to originate from the up &#8217;till now reliably running ESP8266 based circuit. As\u00a0my gaze\u00a0turned directly towards the module, the blue LED started flashing. Not just once, but several times with a silent couple of seconds between illuminations&#8230;<\/p>\n<p>&#8220;Uh-Ohh&#8221;, I thought, &#8220;looks like the ESP8266 is once again in a reset loop!&#8221; First thing to\u00a0check was the ESP8266 web server response to a http GET request. The server did respond with a JSON string, just like it was programmed to do. And much to my surprise, the status (seconds running since last reset),\u00a0indicated the module had been continuously running for days.<\/p>\n<p>So why the flashing blue LED?<\/p>\n<p>I noticed that sending a http GET request around the time the flashing occurs required considerable time (up to 10 seconds) before the ESP8266 returned a JSON response string. This could only mean one thing&#8230;<\/p>\n<h4 style=\"text-align: center;\"><strong>A WIFI Connectivity Check and Recover is Essential<\/strong><\/h4>\n<p>The application firmware had detected a loss in WIFI connectivity and was attempting to reconnect. I had added a WIFI check to the top of the Arduino IDE loop() function. It never seemed to detect a loss of WIFI. That is, until now&#8230;<\/p>\n<p>While the following code was developed using the Arduino IDE, the same check should be included for the SDK or NodeMCU code as well.<\/p>\n<pre>\/\/connect wifi if not connected\r\nif (WiFi.status() != WL_CONNECTED) {\r\n    delay(1);\r\n    startWIFI();\r\n    return;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So the setup() function initially establishes the WIFI conection with a call to startWIFI(). The WIFI status is subsequently checked each iteration of the loop() to maintain the connection.<\/p>\n<p>Fortunately, I had included some status messages in\u00a0the startWIFI() function sent out the serial port just in case some troubleshooting was needed. This came in handy to confirm the root cause of the flashing blue LED.<\/p>\n<pre>void startWIFI(void) {\r\n    \/\/set IP if not correct\r\n    IPAddress ip = WiFi.localIP();\r\n    if( ip!= ipadd) {\r\n        WiFi.config(ipadd, ipgat, ipsub); \/\/dsa added 12.04.2015\r\n        Serial.println();\r\n        delay(10);\r\n        Serial.print(\"ESP8266 IP:\");\r\n        delay(10);\r\n        Serial.println(ip);\r\n        delay(10);\r\n        Serial.print(\"Fixed IP:\");\r\n        delay(10);\r\n        Serial.println(ipadd);\r\n        delay(10);\r\n        Serial.print(\"IP now set to: \");\r\n        delay(10);\r\n        Serial.println(WiFi.localIP());\r\n        delay(10);\r\n    }\r\n    \/\/ Connect to WiFi network\r\n    Serial.println();\r\n    delay(10);\r\n    Serial.println();\r\n    delay(10);\r\n    Serial.print(\"Connecting to \");\r\n    delay(10);\r\n    Serial.println(ssid);\r\n    delay(10);\r\n    WiFi.begin(ssid, password);\r\n\r\n    while (WiFi.status() != WL_CONNECTED) {\r\n        Serial.print(\".\");\r\n        delay(500);\r\n    }\r\n    Serial.println(\"\");\r\n    Serial.println(\"WiFi connected\");\r\n\r\n    \/\/ Start the server\r\n    server.begin();\r\n    Serial.println(\"Server started\");\r\n\r\n    \/\/ Print the IP address\r\n    Serial.print(\"ESP8266 IP: \");\r\n    Serial.println(WiFi.localIP());\r\n\r\n    \/\/ Print the server port\r\n    Serial.print(\"ESP8266 WebServer Port: \");\r\n    Serial.println(SVRPORT);\r\n    delay(300);\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>As I mentioned, the my ESP8266 web server returns status, along with sensor readings in a JSON string upon receiving http GET request. This data is logged to a mySQL database, once every hour. A CRON php script pulls the ESP8266 data and saves it to the database.<\/p>\n<h4 style=\"text-align: center;\"><strong>The next step<\/strong><\/h4>\n<p>In order to assess how frequent WIFI connectivity is lost and restarted, I am going to add a static counter to my startWIFI() function. This will identify the number of times the WifFi connection was lost and reestablished since the last reset. That value will be recorded to mySQL along with the other values. It will be interesting to see how often this occurs. Once a day? Several times per hour? We shall see&#8230;<\/p>\n<p>If you have had any issues maintaining WIFI connectivity, or your server stops responding to request, consider this as a solution.<\/p>\n<p>I hope you find this information useful&#8230;<\/p>\n<h4 style=\"text-align: center;\"><strong>Update: July 5, 2015<\/strong><\/h4>\n<p style=\"text-align: left;\">The ESP8266 supporting my weather sensor webserver did reset 23 days ago. I took that opportunity to add a\u00a0static counter to the startWIFI() function. A <a href=\"https:\/\/thingspeak.com\/channels\/33251#publicview\" target=\"_blank\">ThingSpeak<\/a>\u00a0channel has also been established to share the live results publicly. \u00a0As of this update, the Wifi connectivity was lost 537\u00a0times.<\/p>\n<p style=\"text-align: left;\">Recovery was successful every time as no ESP resets have occurred yet.<\/p>\n<p style=\"text-align: left;\">This\u00a0empirical data shows the Wifi connectivity is lost on average 23 times per day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to eliminate ESP8266 WIFI drop-outs Just when it seemed like\u00a0the ESP8266 was operating rock-solid, indeed running continuously for days now without resetting, or worse, fatally crashing, a sign that &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,10,19],"tags":[38,37],"class_list":["post-631","post","type-post","status-publish","format-standard","hentry","category-alltheposts","category-esp8266","category-internet-of-things","tag-esp8266-wifi-connectivity","tag-esp8266-wifi-drop-outs"],"_links":{"self":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=631"}],"version-history":[{"count":15,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/631\/revisions"}],"predecessor-version":[{"id":679,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=\/wp\/v2\/posts\/631\/revisions\/679"}],"wp:attachment":[{"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/internetofhomethings.com\/homethings\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}