Nodemcu goes offline after a few minutes

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());
}

First of all, I’d scrap all of the millis(), time_now and period stuff, its redundant if you use your timer correctly.
Simply change the timer from every second (1000ms) to the period you want. You should bear in mid that the DHT range of sensors are very slow, and probably work best if polled every 5 seconds or more.

Are your fans being controlled via some additional hardware (motor controller, relays etc)?
If so, how are these devices powered? I’ve seen may occasions where NodeMCUs reboot because they have relays connected to them and powered off of the same supply. When the relays switch they either starve the NodeMCU of power or cause back-EMF that the NodeMCU doesn’t like.

Pete.

First I let someone write the code from Freelancer. and the fans are controlled by relays and yes they get power from the nodemcu.

I’d ask for a refund!

I think that’s your problem then. If you disconnect the fans does it stay online?

Are these an off the shelf relay board like this:

Pete.

Yes thats my plan he worked for month on this and its not working right. and I have not tested whether it works properly when I turn off the relays. but it goes offline even if the ralais don’t switch anything. and yes such relays

ELEGOO 8 Kanal DC 5V Relaismodul mit Optokoppler für Arduino UNO R3 MEGA 2560 1280 DSP ARM PIC AVR STM32 Raspberry Pi https://www.amazon.de/dp/B01M61VVGV/ref=cm_sw_r_cp_api_i_OceNFbQN83BF5?_encoding=UTF8&psc=1

Okay, well the best approach is to power the relays separately, or to use a good 5v power supply to power both the relays and the NodeMCU via the Vin pin.
If you try to power the NodeMCU and the relays via the USB connector on the NodeMCU then that’s when you’ll have real issues.

Pete.

Oh ok i use the usb connector from the nodemcu to give him power. Ok then i try to give the relais a separately power. Tanks for your help. Can you maybe please edit the code what you say in your other post?

Okay, I’ve had a play around with t6he code and changed from SimpleTimer to BlynkTimer, removed the unnecessary millis(0) commands, changed the DHT22 frequency to 5 seconds and tidied up t6he code layout a bit.
I’ve also re-enabled the serial print processes, as these provide valuable feedback in the serial monitor.

I’m not sure what purpose of these variables is meant to be:

int fan1_state = HIGH;
int fan2_state = HIGH;
int humid_state = LOW;

They don’t really do anything, but I’ve left them in there for now.

There are currently no variables to track when the V12, 13 & 14 buttons are manually activated, so if you use these they will change the state if the fans, but this will be overridden the next time a temperature and humidity reading is taken (a maximum of 5 seconds) and that will be set back to the state dictated by the if statements.
Without knowing what your functional specification for the project was, it’s difficult to go much further.

/**************************************************************
   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    // Provides useful info, keep this in if possible
//#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h> //We'll use the BlynkTimer instead that is included in the Blynk library
#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    // D4
#define fan_1       12    // D6
#define fan_2       16    // D0
#define humid        4    // D2

#define V_fan_1     V12   // Styled Button "Lüfter Klein"
#define V_fan_2     V13   // Stuled Button "Lüfter Groß"
#define V_humid     V14   // Styled button "Luftbefeuchter"
#define V_min_humid V15   // Numeric Input "Min Luftfeuchte"
#define V_max_humid V16   // Numeric Input "Max Luftfeuchte"

// Other Blynk Widgets...
// V5 = Labelled Value Display "Luftfeuchte" & Superchart datastream
// V6 = Labelled Value Display "Temperatur" & Superchart datastream
// Notification Widget


DHT dht(DHTPIN, DHTTYPE);
//SimpleTimer timer // Not Used Now
BlynkTimer timer;

int fan1_state = HIGH;
int fan2_state = HIGH;
int humid_state = LOW;

int max_humid;
int min_humid;
float h, t;


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);
  if (fan1_state)
  {
    Serial.println("fan_1 On manually");
  }
  else
   {
    Serial.println("fan_1 Off manually");
  } 
}

BLYNK_WRITE(V_fan_2)
{
  fan2_state = param.asInt();
  digitalWrite(fan_2, fan2_state);
  if (fan2_state)
  {
    Serial.println("fan_2 On manually");
  }
  else
   {
    Serial.println("fan_2 Off manually");
  } 
}

BLYNK_WRITE(V_humid)
{
  humid_state = param.asInt();
  digitalWrite(humid, humid_state);
  if (humid_state)
  {
    Serial.println("Humid On manually");
  }
  else
   {
    Serial.println("Humid Off manually");
  } 
}

BLYNK_WRITE(V_min_humid)
{
  min_humid = param.asInt();
}

BLYNK_WRITE(V_max_humid)
{
  max_humid = param.asInt();
}

void sendSensor()
{
  h = dht.readHumidity();
  t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  if (isnan(h) || isnan(t))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity");
  Serial.print("\t");
  Serial.println(h);
  
  Serial.print("Temperature");
  Serial.print("\t");
  Serial.println(t);

  // 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(74880);
  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 5 seconds
  timer.setInterval(5000L, sendSensor);
}

void loop()
{
  Blynk.run(); // Feeds the Blynk Library
  timer.run(); // Feeds the BlynkTimer
}

Pete.

ok i’ll explain it briefly, the humidifier should go on at 50% and off again at 70%, and fan 1 on at 27 degrees and off again at 26 and fan 2 on at 28 and off again and the 3 buttons should only be there to visualize that the fans are on or off. and to set the min / max limits of the humidifier. Thanks for your work and help!

So the buttons could be replaced with LEDs?

Pete.

if it is possible that the switches can switch in spite of the regulation then it can be buttons but if this is not possible then LEDs are sufficient.

I don’t understand what you’re saying.

Pete.

Sorry for my bad english the guy from freelancer say It is not possible to switch the fan manually when the rule is active.

It would be possible, but you need to have thought through your requirements fully.
Would you want to be able to turn the fans on and they stay on until you turn them off - regardless of what happens to the temperature/humidity?
This sounds like a bad idea.
I guess one option is to have a timer, where you can override the automatic operation for either a fixed time (60 minutes maybe) or a time specified by widget (a slider maybe) in the app.

You also need to think about what should happen if the NodeMCU can’t connect to WiFi or the Blynk app. Should it still work in autonomous mode?

The first thing to do is to get your hardware working correctly, and think-through the desired functional specification.

Pete.

ok i think its enough when the buttons replaced with leds.

and it would be perfect if it had no connection to WiFi or Blynk, it would work autonoumous.

and yes the first thing is the hardware but thanks for your help

@PeteKnight

Can you please replace the buttons with led. and maybe also that it works autonomously even if it has no wifi or blynk connection? I would be very grateful to you and would even give you a tip

Have you solved the problem with the hardware disconnecting from the Blynk server?

Pete.

no i do that the days i just thought the code is already ready :sweat_smile:

:rofl:

so i have tested, once without a relay and with your rewritten code and it still shows me after a few minutes that it is offline and online again. and then i tested it with the 5v power supply on the nodemcu and the relay via the vpin but still it went offline.