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);
}
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);
#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
}
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.
#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);
}
#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
}
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
}
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 sometimes the solution can be very easy