Nodemcu esp connection problem (solved)

hi,

my nodemcu It’s disconnecting again and again, i did not solve. please help

code is here

#define BLYNK_PRINT Serial    
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

char auth[] = "auth";

char ssid[] = "wifi";
char pass[] = "pass";

#define DHTPIN 5          // What digital pin we're connected to
int Soil1 = A0;
int min = 980;
int max = 350;

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  pinMode (Soil1, INPUT);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
  int nem1 = analogRead(Soil1);
  float toprak1 = (float)((nem1 - min ) * 100 ) / ( max - min );
  Blynk.virtualWrite(V3, toprak1);
}

I think it is the best put the blynk.run and timer.run in the loop, others should be outside the loop.

thx i try, but the problem continues

Remove this thing from void loop and create separate function.
Like this
Void send() { int nem1 = analogRead(Soil1); float toprak1 = (float)((nem1 - min ) * 100 ) / ( max - min ); Blynk.virtualWrite(V3, toprak1); }
And in void steup put
timer.setInterval(1000L, send);

thx, but

#define BLYNK_PRINT Serial  
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

char auth[] = "auth";


char ssid[] = "wifi";
char pass[] = "pass";

#define DHTPIN 5          // What digital pin we're connected to
int Soil1 = A0;
int min = 980;
int max = 350;

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

 
}

void sendMoisture()
{
 int nem1 = analogRead(Soil1);
  float toprak1 = (float)((nem1 - min ) * 100 ) / ( max - min );
  Blynk.virtualWrite(V3, toprak1);  
}

void setup()
{
  pinMode (Soil1, INPUT);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, sendMoisture);
}


void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer

}

problem continues :frowning:

What is serial monitor output??
past it.

I was in the same trouble with ESP standalone. I think it is somehow ‘connected’ with yeld () function of ESP- arduino (C++) “bridge”. I’m not a programmer, but I managed to solve it by placing most of time-consuming operations inside loop () function. Just try to use ‘raw’ timing operation inside loop () INSTEAD of Timer- called functions. (sth like: if (millis () - nowTimer > delay); nowTimer = millis (); etc. ) . For me that solved all the problems in much, much larger project.

Also, in your first post I see you tried reading analog input ( analogRead () ) in loop. That crashed my ESP too. Using (this time) timer- called function (I’m using TICKER library) just for analogRead () with frequency 10/s (100ms delay) solved this problem too.
Cheers.

i try this code but problem continues

#define BLYNK_PRINT Serial    
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "wifi";
char pass[] = "pass";

#define DHTPIN 5          // What digital pin we're connected to
int Soil1 = A0;
int min = 980;
int max = 350;

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

 
}

void sendMoisture()
{
  int nem1 = analogRead(Soil1);
  delay(100);
  float toprak1 = (float)((nem1 - min ) * 100 ) / ( max - min );
  Blynk.virtualWrite(V3, toprak1);  
}

void setup()
{
  pinMode (Soil1, INPUT);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every second

}


void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, sendMoisture);

}

Remove this from void loop and put it into void setup.
Your previous code were fine.what is serial monitor output for previous code.

serial monitor output;

ÿ5Qıÿ[309] Connecting to Next_Level_Alpha
[16813] Connected to WiFi
[16813] IP: 192.168.1.102
[16813] Blynk v0.4.0 on NodeMCU
[16814] Connecting to blynk-cloud.com:8442
[17379] Ready (ping: 1ms).
[40485] Connecting to blynk-cloud.com:8442
[42180] Ready (ping: 376ms).
[70494] Connecting to blynk-cloud.com:8442
[73655] Ready (ping: 376ms).
[80021] Connecting to blynk-cloud.com:8442

I think your network is not so good.
And for which code this serial monitor output is?

this code;

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Next_Level_Alpha";
char pass[] = "pass";

#define DHTPIN 5          // What digital pin we're connected to
int Soil1 = A0;
int min = 980;
int max = 350;

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

 
}

void sendMoisture()
{
  int nem1 = analogRead(Soil1);
  delay(100);
  float toprak1 = (float)((nem1 - min ) * 100 ) / ( max - min );
  Blynk.virtualWrite(V3, toprak1);  
}

void setup()
{
  pinMode (Soil1, INPUT);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, sendMoisture);
}


void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer


}

modem is now away but normally near.

Remove this delay from code.

It has already made the same problem with this code;

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Next_Level_Alpha";
char pass[] = "pass";

#define DHTPIN 5          // What digital pin we're connected to
int Soil1 = A0;
int min = 980;
int max = 350;

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

 
}

void sendMoisture()
{
  int nem1 = analogRead(Soil1);
  float toprak1 = (float)((nem1 - min ) * 100 ) / ( max - min );
  Blynk.virtualWrite(V3, toprak1);  
}

void setup()
{
  pinMode (Soil1, INPUT);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, sendMoisture);
}


void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer


}

Have you tried standalone example??
Does it work fine for you??
If not then 100% Network problem.

Yes, i have tried. this problem has occurred after adding the soil moisture sensor.

It seems Blynk disconnects you for some reason. Try adding:
#define BLYNK_DEBUG
you gonna see a lot more output!.

Also, as (at least for now) the interval in both cases is 1000ms, why don’t you just try:
#define Soil1 A0

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  int nem1 = analogRead(Soil1);

  if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
  }
  else {
    Blynk.virtualWrite(V5, h);
    Blynk.virtualWrite(V6, t);
  }
  
  float toprak1 = (float)((nem1 - min ) * 100 ) / ( max - min );
  Blynk.virtualWrite(V3, toprak1);
}

also why waste memory for constant ‘Soil1’? try #define, as shown

There is one more thing i’d like to mention here: With ESP and Arduino IDE, as a general “rule” (?) you should avoid any delay outside, i.e in functions called from within the loop() function! These should be completed as soon as possible. On the other hand, some delay inside loop() is nothing bad, and helps ESP complete its background tasks. BUT if you try to use long delays in Blynk projects, expect connection breaking. NO PROBLEM with long (1000ms is fine!) delays in setup() AND functions called from it… I had no problem with that.

thx for all, i solved the problem. Previously dht11 vcc pin connected to nodemcu 3.3v pin, but the nodemcu was constantly restarting. then i changed dht vcc pin to Vin (5v) pin and the problem was solved :slight_smile: sometimes the solution can be very easy

1 Like