ESP8266 keep disconnecting from server

Hello everyone thanks for reading this post and helping me.

BASICS:

I wrote a small program to use my Lolin Nodemcu V3 to read a Temperature/humidity sensor ( HTU21D ).
The goal of my project is to read the sensor values once every 7 seconds display them on a small oled screen ( SSD1306 ), then send the values to the Blynk server to plot them on the SuperChart widget.

I’m using the Blynk_Release_v0.4.8, the app is updated and running on an Android (samsung S6), i’m using cloud server, powering the NodeMcu with an usb and battery power bank 12V, i’m using Arduino IDE 1.6.13.

THE PROBLEM:
So after sending two or three times the data to the Blynk server the ESP8266 gets disconnected… After a while it reconnects then disconnects again and again… Help please

I spent the past two days trying to solve the problem with no success, i’ve red tens of other posts of people having the same problem i have, but i wasn’t able to fix the code…

Here the code :wink:


>     // Lolin NodeMcu V.3
>     // HTU21D i2c sensor
>     // SSD 1306  i2c  OLED screen
>     // Connect SDA to  (D2)
>     // Connect SCL to  (D1)
> 
>     #define BLYNK_PRINT Serial
>     #include <SPI.h>
>     #include <ESP8266WiFi.h>
>     #include <BlynkSimpleEsp8266.h>
>     #include <Wire.h>
>     #include "Adafruit_HTU21DF.h"    // library for Sensor
>     #include <ACROBOTIC_SSD1306.h>   // library for OLED: SCL ==> D1; SDA ==> D2
> 
>     //char auth[] = "---------------------------------";
>     //char ssid[] = "--------------";
>     //char pass[] = "---------";
> 
> 
>     int h=0;       // variable for humidity
>     int t =0;      // variable for temperature
>     //int ledPin = 2;  // led pin 
> 
>     Adafruit_HTU21DF htu = Adafruit_HTU21DF();  
>     BlynkTimer timer;
> 
>     void setup()
>     {
>       // Debug console
>     Serial.begin(9600);
>      // pinMode(ledPin, OUTPUT);
>       oledStart();
>       Blynk.begin(auth, ssid, pass);
> 
>     // --------Setup a function to be called every.....------
>       timer.setInterval (7000L, sensing); 
>     }
> 
>     void loop()
>     {   
>       Blynk.run();
>       timer.run();
>        }
> 
>     void sensing(){  
>         h = htu.readHumidity();
>         t = htu.readTemperature();
>        displayData();      
>     }
> 
>     /***************************************************
>      * Display data at Serial Monitora & OLED Display
>      **************************************************/
>     void displayData(void)
>     { 
>       oled.setTextXY(3,0);              // Set cursor position, start of line 2
>       oled.putString("Temp: " + String(t) + "C");
>       oled.setTextXY(4,0);              // Set cursor position, start of line 2
>       oled.putString("Hum:  " + String(h) + "%");
>     //  oled.setTextXY(5,0);              // Set cursor position, start of line 2
>      // oled.putString("Soil: " + String(soilMoister)");
> 
>      sendSensor();
>     }
> 
>     /***************************************************
>      * Start OLED
>      **************************************************/
>     void oledStart(void)
>     {
>       Wire.begin();  
>       oled.init();                      // Initialze SSD1306 OLED display
>       clearOledDisplay();
>       //oled.setFont(font5x7);            // Set font type (default 8x8)
>       oled.clearDisplay();              // Clear screen
>       oled.setTextXY(0,0);              
>       oled.putString("I still love u"); 
>     }
> 
>     /***************************************************
>      * Clear OLED Display
>      **************************************************/
>     void clearOledDisplay()
>     {
>       oled.setFont(font8x8);
>       oled.setTextXY(0,0); oled.putString("                ");
>       oled.setTextXY(1,0); oled.putString("                ");
>       oled.setTextXY(2,0); oled.putString("                ");
>       oled.setTextXY(3,0); oled.putString("                ");
>       oled.setTextXY(4,0); oled.putString("                ");
>       oled.setTextXY(5,0); oled.putString("                ");
>       oled.setTextXY(6,0); oled.putString("                ");
>       oled.setTextXY(7,0); oled.putString("                ");
>       oled.setTextXY(0,0); oled.putString("                ");              
>     }
> 
>     /***************************************************
>      * Send data to Blynk server
>      **************************************************/
>     void sendSensor()
>     {
> 
>       oled.setTextXY(7,0);              // Set cursor position, start of line 7
>       oled.putString("Sending data");
>       
>       Blynk.virtualWrite(V11, h);
>       Blynk.virtualWrite(V10, t);
>       
>       oled.setTextXY(7,0); oled.putString("                ");
>       oled.setTextXY(7,0);              // Set cursor position, start of line 7
>       oled.putString("Data sent");
>     }

Here the serial monitor:

   ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.8 on ESP8266

[21377] Connecting to blynk-cloud..com:8442
[21520] Ready (ping: 3ms).
[46839] Connecting to blynk-cloud..com:8442
[53463] Connecting to blynk-cloud..com:8442
[60712] Connecting to blynk-cloud..com:8442
[67838] Connecting to blynk-cloud..com:8442
[75036] Connecting to blynk-cloud..com:8442
[82211] Connecting to blynk-cloud..com:8442
[89386] Connecting to blynk-cloud..com:8442
[96535] Connecting to blynk-cloud..com:8442
[103711] Connecting to blynk-cloud..com:8442
[110784] Connecting to blynk-cloud..com:8442
[111780] Ready (ping: 0ms).
[127059] Connecting to blynk-cloud..com:8442
[134433] Connecting to blynk-cloud..com:8442
[141657] Connecting to blynk-cloud..com:8442
[148832] Connecting to blynk-cloud..com:8442
[156006] Connecting to blynk-cloud..com:8442

hm, strange issue…
try to put a serial.println(millis()) at the begin and end of sensing function, and see how long it takes to execute.

or maybe the sensor library or oled library interferes with the esp somehow? you should try some new versions, in the first one remove completely the oled stuff and see if it works. in the second one remove completely the sensor stuff and just send a static value and see if the issue still happens.

also, check your wifi signal strength, wireing, psu, etc

edit: update arduino ide, 1.6 is very old!

3 Likes

Thank you wanek for your reply! You helped me a lot!

I finally found out how to make it work. As it might be interesting for others i gonna share the process i went through to do that!

First i updated Arduino as you suggested.
Second i went to the sketch builder page, i copied the “Blynk Blink” code and then uploaded to the nodemcu.
I immediately noticed in the serial monitor that i was having the same connection problem as before.

So the problem wasn’t the code, i was thinking or the nodemcu is somehow broken or i have some wifi issue.
I connected the nodemcu with my uncle wifi and it worked! :slight_smile: im so happy!

1 Like

Maybe you could explain how your Uncle’s WiFi differs from yours?

glad you figured out.

regarding the wifi issue, can be soo much factors.

  • in esp library there is function to measure wifi signal strength (dbm) for reliable long term connections this needs to be higher than -80dbm. if the signal strength is ok, try the following (only if you know what you’re doing):

  • try to restart router. if not helps, then

  • do factory reset on router. if not helps, then

  • try different wifi channels, or channel bandwiths 20/40mhz

  • maybe you have a crappy router

edit:
put this in code and set widget in app to refresh 1 seconds

BLYNK_READ(V_DBM)
{
  Blynk.virtualWrite(V_DBM, WiFi.RSSI());
}

/*
  -30 dBm: amazing  (unlikely in real life)
  -67 dBm: strong   (minimum for realtime hd streaming)
  -70 dBm: okay     (minimum for reliable packet delivery)
  -80 dBm: weak     (minimum signal strength for basic connectivity. packet delivery may be unreliable)
  -90 dBm: unusable (any functionality is highly unlikely)
*/

1 Like