BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

My NODE MCU goes online and then off line every few minutes


#1

Hello, I have two NodeMCUs. One is writing to or controlling the other when certain alarms are detected. The “slave” NodeMCU is ok. It is all the time online, but the “master” Node MCU goes off line every so many minutes and then back on line and then again off line and so on.

I thought it might have been caused by a faulty Node MCU, so I ordered another one. Loaded the sketch again into the new one but it is still doing the same thing.

Any idea what could be causing this? Do i have too many “delays”?

This is the sketch for the “Master” NodeMCU.


/**************************************************************
With this project, which is easy to implement
we can control and activate an alarm, which sends an email
mentioning that an alarm has been detected.
 **************************************************************/

#define BLYNK_PRINT Serial    //Comment this out to saves space. 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

// authtoken generated by Blynk
char auth[] = "XXXXXXXXX";

//Wifi connection info.
char ssid[] = "ServerID"; // WIFI server name
char pass[] = "XXXX"; // WIFI password

SimpleTimer timer;

WidgetBridge bridge1(V1);

BLYNK_CONNECTED() {
  // Place the AuthToken of the second Node MCU here
  bridge1.setAuthToken("YYYYYYYYYYYYY"); 
}



void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
 
  

  // Send email when it connects to Blynk server
  //Default parameters "Email", "subject", "Message to send".  You can also send text to a at&t customer, by using the phone_number@txt.att.net
  
  Blynk.email("XXXXXX@txt.att.net","Subject", "Alarm monitoring program re-starting. A power failure could have occurred");
  pinMode(5, INPUT);// pin D1(GPIO5) as input for the Alarm
  //pinMode(4,OUTPUT);// pin D2(GPIO4) as output
  
  //timer.setInterval(1000L, myTimerEvent); 
  
  }


void emailtosend()
{ 
Serial.println("An Alarm was activated"); //Prints to the serial monitor
Blynk.email("XXXX@txt.att.net", "Alarm Activated", "Please coordinate to make sure someone gets there to fix this issue.");
delay(20);
}

// The blynk interface has an option to silence the alarm email notifications.  If they have not turned them back on after a certain amount of time, the following sends an email reminding them to turn them back on. 

void emailturnon()
{ 
Serial.println("Notifications have been turned off"); //Prints to the serial monitor
Blynk.email("XXXX@txt.att.net", "Notifications are still turned off", "If the issue(s) with the alarms have been resolved, please turn notifications back on");
delay(20);
}

int pinData = 0;

BLYNK_WRITE(V2) // reading if email notifications have been silenced in the blynk smart phone interface . 
{
  if (param.asInt())
  {
    pinData = 1;
  }
  else 
  {
    pinData = 0;
  }
Serial.println(pinData);
}
 

void Alarma()
{
  // if alarm is activated it send email.   
   
  int Sensor = digitalRead(5); // variable to store the state of the alarm
  if (Sensor == 1) // If the switches are activated, then it goes high
  {
   Serial.println("Writing to D1 in Slave NodeMCU"); //Prints to the serial monitor
     delay(20);
    bridge1.digitalWrite(5, 1000); // writes in D1 of the slave NodeMCU to also activate the alarm with the slave NodeMCU
   }else{
     bridge1.digitalWrite(5, 0);    
  }

  if (Sensor == 1) // if the swtiches are activated  
  {
   if (pinData == 0) 
    {  
    timer.setTimeout(60000, emailtosend); // send email every 60 seconds when the sensor is activated and when the alarm has not been silenced
    }  
 }
 
  if (pinData == 1)  
    {  
    timer.setTimeout(1800000, emailturnon); // send email every 30 minutes when the alarm has been silenced
    }
   }
  
  
void loop()
{
  timer.run();
  Alarma();// calls the function to be executed.
  Blynk.run();
 
}

#2

Please format posted code properly, as required in the Welcome Topic…

Blynk - FTFC


#3

Sorry about that.

I have reformatted.

Regards


#4

Calling this function directly in the main loop will have it running hundreds of times a second, causing disconnections. Put it on a timer of it’s own.


#5

Thank you so much! That makes sense.

I want Alarma() to run as often as possible. Do you think every 10 seconds would be too much?

Regards,


#6

That should be sufficient, depending on what you need to do. The other thing to watch for is any potential delays or blocking while actually running that function.


#7

Thank you so much Gunner!