Alarm Panel Interface (disconnects after 30min requiring restart)

Good day Blynk Followers: :slight_smile:

I have a Project that i created to monitor my Alarm System at Home and i am having issues with my Blynk Connection.
Blynk seems To connect after 1 Minute fine but my code seems to make the device disconnect after an 30 Min or so then i need to restart my device,

This is my normal connection, i have check telnet connection to this and it seems fine.
i know it is not my router our firewall and doubled check that.

[14228] Connecting to blynk-cloud.com:8442
[19229] Connecting to blynk-cloud.com:8442
[24230] Connecting to blynk-cloud.com:8442
[29231] Connecting to blynk-cloud.com:8442

If i use the vanilla Code for the Blink virtual Led it works fine and stays connected for the entire day without any drops.

i am trying to follow best practices, as this might solve the issue but i am not a Developer as Such and don’t know a lot of coding. :cry:

I have also noticed if my code runs and i monitor the serial, it needs to reconnect to the Blynk Cloud Server every minute or so Displaying “Ready in my serial Console with the ms ping”

i then look at the app and the device indicates that it is disconnected again.

My Device
Arduino Mega
Eathernet 5100

i have two questions
a. Please let me know what i am doing wrong and where i can change to fix this?
b. if not so Perhaps a local server will help?

#include <SPI.h>
#define BLYNK_PRINT Serial
#include <BlynkSimpleEthernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 20, 12);
IPAddress dnServer(8, 8, 8, 8);
IPAddress gateway(192, 168, 20, 1);                                                                                              
IPAddress subnet(255, 255, 255, 0);                                                                                              
char auth[] = "xxxxx";

WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);
int checkalarmarmdisarmpin = 3;
int outputled = 4;
int checksirenpin = 5;
int emailoversend = 0;

void setup()
{
   Serial.begin(9600);

   Blynk.begin(auth, "blynk-cloud.com", 8442, ip, dnServer, gateway, subnet, mac);
    while (!Blynk.connect()) {
    }
      
   pinMode(checkalarmarmdisarmpin,INPUT_PULLUP);
   pinMode(outputled,OUTPUT);
   pinMode(checksirenpin,INPUT_PULLUP);
}

void loop()
{
  Blynk.run();
  CheckAramStatus();
  ChecksirenStatus();
}

void CheckAramStatus()
{
  int statusofAlarm = digitalRead(checkalarmarmdisarmpin);

  if (statusofAlarm == 0){
  led2.on();
  digitalWrite(outputled, HIGH);
  Serial.print("Status OF Alarm:");
  Serial.print(" ");
  Serial.print(statusofAlarm);
  Serial.println(" ");
  }

  else{
  led2.off();
  digitalWrite(outputled, LOW);
  Serial.print("Status OF Alarm:");
  Serial.print(" ");
  Serial.print(statusofAlarm);
  Serial.println(" ");
  }
}


void ChecksirenStatus()
{
  int sirenstatus = digitalRead(checksirenpin);

  if (sirenstatus == 0 && emailoversend == 0){
  led3.on();
  Serial.print("Siren Status:");
  Serial.print(" ");
  Serial.print(sirenstatus);
  Serial.println(" ");
  emailsend();
  emailoversend = 1;
  }
  else{
  }

  if (sirenstatus == 1){
  led3.off();
  Serial.print("Siren Status:");
  Serial.print(" ");
  Serial.print(sirenstatus);
  Serial.println(" ");
  emailoversend = 0;
  }
  else{
  }
}


void emailsend()
{
   Blynk.email("myemail@gmail.com", "Alarm", "Alarm Activated");
}

These commands may easily run hundreds of times a second, eventually something is bound to interfere with your connection and cause a disconnect.

Use timers for the two check voids, check each one every second or so, instead of constantly, and see if that helps.

1 Like

@Juanb in addition to using BlynkTimer you should add a status condition inside the 2 functions i.e. only do
something if the status has changed from the last time the digitalRead() was made. As it’s written at the moment it will be repeatedly turning the LED on / off even when the LED is already on / off etc.

2 Likes

Thanks Blynkers

I will try tonight to add this into my code, Will give you guys an update soon

:relaxed:

1 Like

Hi Team :slight_smile:

I changed the code to work on the lib “Blynk.Timer”, Like you asked.

This seems to help, but then for no reason it stopped Working again, i had a look on the serial Monitor and it displayed “Login Timeout” i then thought perhaps it is flooding the server again, so i Set the Interval to check every 70 Sec :cry:

i then had a look at the serial and after about 10 min it suddenly displayed, Connecting to blynk-cloud.com:8442
As indicated below.

Siren Status: 1
Status OF Alarm: 1
[1780100] Connecting to blynk-cloud.com:8442
[1785088] Connecting to blynk-cloud.com:8442

it then didn’t reconnect. While it didn’t connect i pinged the Blynk Server and Tested Telnet and it seems fine.
I have a Steady Ping rate of 200~ 300 Ms Ping Rate with Packet loss now and then.

Do you think a Packet Drop will disconnect the Arduino and why not doesnt it reconnect it seems like the arduino goes into a Hang state, should it try to install a Local server and see if this goes away, or do i still have a Code issue.

This is some of the questions is have perhaps you can help me in the right direction again?

Latest code?

#include <SPI.h>
#define BLYNK_PRINT Serial
#include <BlynkSimpleEthernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 20, 12);
IPAddress dnServer(8, 8, 8, 8);
IPAddress gateway(192, 168, 20, 1);
IPAddress subnet(255, 255, 255, 0);
char auth[] = "xxx";

BlynkTimer timer;

WidgetLED led1(V1);
WidgetLED led2(V2);
WidgetLED led3(V3);
int checkalarmarmdisarmpin = 3;
int outputled = 4;
int checksirenpin = 5;
int emailoversend = 0;

void ChecksirenStatus()
{
  int sirenstatus = digitalRead(checksirenpin);
  
      if (sirenstatus == 0 && emailoversend == 0){
      led3.on();
      Serial.print("Siren Status:");
      Serial.print(" ");
      Serial.print(sirenstatus);
      Serial.println(" ");
      emailsend();
      emailoversend = 1;
      }
      else{
      }
    
      if (sirenstatus == 1){
      led3.off();
      Serial.print("Siren Status:");
      Serial.print(" ");
      Serial.print(sirenstatus);
      Serial.println(" ");
      emailoversend = 0;
      }
      else{
      }
}

void CheckAramStatus()
{
  int statusofAlarm = digitalRead(checkalarmarmdisarmpin);
  
      if (statusofAlarm == 0){
      led2.on();
      digitalWrite(outputled, HIGH);
      Serial.print("Status OF Alarm:");
      Serial.print(" ");
      Serial.print(statusofAlarm);
      Serial.println(" ");
      }
    
      else{
      led2.off();
      digitalWrite(outputled, LOW);
      Serial.print("Status OF Alarm:");
      Serial.print(" ");
      Serial.print(statusofAlarm);
      Serial.println(" ");
      }
}

void blinkLedWidget()
{
  if (led1.getValue()) {
    led1.off();
    
  } else {
    led1.on();
    
  }
}

void emailsend()
{
   Blynk.email("mygmail@gmail.com", "Alarm", "Alarm Activated");
}

void setup()
{
   Serial.begin(9600);                                                                                                            // Serial Diagnose

   Blynk.begin(auth, "blynk-cloud.com", 8442, ip, dnServer, gateway, subnet, mac);
    while (!Blynk.connect()) {
    }
      
   pinMode(checkalarmarmdisarmpin,INPUT_PULLUP);
   pinMode(outputled,OUTPUT);
   pinMode(checksirenpin,INPUT_PULLUP);

    timer.setInterval(10000L, ChecksirenStatus);
    timer.setInterval(10000L, CheckAramStatus);
    timer.setInterval(5000L, blinkLedWidget);
    Blynk.email("mygmail@gmail.com", "Arduino", "Arduino Online");
}

void loop()
{
  Blynk.run();
  timer.run();
}

You shouldn’t need the while statement has Blynk.begin() was made a blocking routine around 6 months ago. What does Serial Monitor show?

Hi Costas

I will remove the While statement tonight, the Two Serial output i received was as follow

Attempt 1 - Arduino Was Working then Stopped and didnt connect again Serial indicated the following

Status OF Alarm: 1
Siren Status: 1
Status OF Alarm: 1
[1780100] Connecting to blynk-cloud.com:8442
[1785088] Connecting to blynk-cloud.com:8442

Attempt 2 - Arduino Was Working then Stopped and didnt connect again indicated

Status OF Alarm: 1
Siren Status: 1
Status OF Alarm: 1
“Login Timeout” after some time.

Some Ethernet systems are quite flakey. Perhaps check at intervals if connected to Blynk and if not send a software command to reset the Arduino.

Hi Guys

Last night i did the following :eyeglasses:

  • i removed the while Statement

  • Replaced the Network Shield

  • Built a local Blynk server

After all this i tested my Arduino remotely and i didn’t receive any issues. :slight_smile:
Thanks for all your help / feedback :trophy:

i will run it as follow and see how it works out

1 Like