I have a nodemcu with blynk server and a DHT22 sensor and 2 fans and 1 humudifier but it goes after 5 or 10 or 15 minutes offline and online i thuink somethingin the code is wrong…can anyone say me whats the problem maybe??
here is the code
/**************************************************************
For this example you'll need SimpleTimer library:
https://github.com/jfturcot/SimpleTimer
and Adafruit DHT sensor library:
https://github.com/adafruit/DHT-sensor-library
App project setup:
Value Display widget attached to V5
Value Display widget attached to V6
**************************************************************/
//#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>
char auth[] = "t******"; //Enter the Auth code which was send by Blink
char ssid[] = "*******"; //Enter your WIFI Name
char pass[] = "******"; //Enter your WIFI Password
#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
#define DHTPIN 2 // Digital pin 4
#define fan_1 12 //D6
#define fan_2 16 //D0
#define humid 4 //D2
#define V_fan_1 V12
#define V_fan_2 V13
#define V_humid V14
#define V_min_humid V15
#define V_max_humid V16
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
int fan1_state = HIGH;
int fan2_state = HIGH;
int humid_state = LOW;
int max_humid;
int min_humid;
float h,t;
int period = 2000;
unsigned long time_now = 0;
BLYNK_CONNECTED() {
// Request the latest state from the server
//Blynk.syncVirtual(V_fan_1);
//Blynk.syncVirtual(V_fan_2);
//Blynk.syncVirtual(V_humid);
Blynk.virtualWrite(V_fan_1,fan1_state);
Blynk.virtualWrite(V_fan_2,fan2_state);
Blynk.virtualWrite(V_humid,humid_state);
}
BLYNK_WRITE(V_fan_1) {
fan1_state = param.asInt();
digitalWrite(fan_1, fan1_state);
//Serial.println("fan1_1 on");
//Serial.println(fan1_state);
}
BLYNK_WRITE(V_fan_2) {
fan2_state = param.asInt();
digitalWrite(fan_2, fan2_state);
//Serial.println("fan1_2 on");
//Serial.println(fan2_state);
}
BLYNK_WRITE(V_humid) {
humid_state = param.asInt();
digitalWrite(humid, humid_state);
//Serial.println("humid on");
}
BLYNK_WRITE(V_min_humid)
{
min_humid=param.asInt();
}
BLYNK_WRITE(V_max_humid)
{
max_humid=param.asInt();
}
void sendSensor()
{
if (millis() >= time_now + period) {
h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
time_now += period;
// 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); //V5 is for Humidity
Blynk.virtualWrite(V6, t); //V6 is for Temperature
if (t > 26.5)
{
// fan1_state = HIGH;
digitalWrite(fan_1, LOW);
Blynk.virtualWrite(V_fan_1, LOW);
}
if (t < 26)
{
// fan1_state = LOW;
digitalWrite(fan_1, HIGH);
Blynk.virtualWrite(V_fan_1,HIGH);
// fan2_state = ;
digitalWrite(fan_2, HIGH);
Blynk.virtualWrite(V_fan_2, HIGH);
}
if (t > 27)
{
//fan2_state = HIGH;
digitalWrite(fan_2, LOW);
Blynk.virtualWrite(V_fan_2, LOW);
}
if (h > min_humid && h <max_humid)
{
humid_state = LOW;
digitalWrite(humid, LOW);
Blynk.virtualWrite(V_humid, LOW);
}
else if (h > max_humid)
{
humid_state = HIGH;
digitalWrite(humid, HIGH);
Blynk.virtualWrite(V_humid, HIGH);
}
}
void setup()
{
//Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dht.begin();
pinMode(fan_1,OUTPUT);
pinMode(fan_2,OUTPUT);
pinMode(humid,OUTPUT);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
//Serial.print(min_humid);
//Serial.print("\t");
////Serial.println(max_humid);
//Serial.println(dht.readTemperature());
}