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

Blynk app keeps disconnecting in home automation system

esp8266
arduino
#1

Hello Guyz,
I have written a code to start my home generator automatically and with blynk on mobile but it keeps disconnecting . code is attached.

[43557] Ready (ping: 36022ms).
AT+CIPSEND=1,40
AT+CIPCLOSE=1

[69321] Login timeout
AT+CIPCLOSE=1

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "0*******************************************";
char ssid[] = "PTCL-BB";
char pass[] = "3553C8A8";
#define ESP8266_BAUD 115200
ESP8266 wifi(&Serial);




// Out For Relays //
int GPR    = 6;          
int GSR    = 7;          
int COO    = 8;          


// Digital Read Relays //
int ACCP      = 9;         
int GENP      = 10;  
int UPSP      = 11;  
int UPSBPP    = 12;     

int ACC;         
int GENS;  
int UPS;
int UPSBP;

// Variables //
int TATP     = 4;
int ATP      = 0;          
int COS      = 0;
long int DT  = 60000;



// Mobile Buttons //
int GONM   = 0;
int GOFM   = 0;
int ATS    = 1;
int CONM   = 0;
int COFM   = 0;
int UPSBPM = 0;



void setup()
{
 // Debug console
 Serial.begin(9600);
 delay(10);
 // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);
 delay(10);
 Blynk.begin(auth, wifi, ssid, pass);
 
 
 
 pinMode(GPR, OUTPUT);
 pinMode(GSR, OUTPUT);
 pinMode(COO, OUTPUT);
 
 pinMode(ACCP, INPUT);
 pinMode(GENP, INPUT);
 pinMode(UPSP, INPUT);
 pinMode(UPSBPP, INPUT);
 
 digitalWrite(GPR, HIGH);
 digitalWrite(GSR, HIGH);
 digitalWrite(COO, HIGH);


}




void generatorstart(){

if (ATP <= TATP){
ATP = ATP + 1;
Serial.println("");
Serial.println("Generator Starting!!!");
digitalWrite(GPR,  LOW);
digitalWrite(GSR,  LOW);
delay(3000);
digitalWrite(GSR,  HIGH);
Serial.println("Attempted!!!");
Serial.println("");
delay(15000);

       } else {

Serial.println(ATP);
Serial.println("");
Serial.println("Already attempted 4 times");
Serial.println("Generator Will Not Start Until Reset");
digitalWrite(GPR, HIGH);
digitalWrite(GSR, HIGH);
Serial.println("");
delay(10);
}

}

void generatoroff(){
  
Serial.println("");
Serial.println("Generator Shutting Down!!!"); 
digitalWrite(GPR,   HIGH);

 
}

void changeoveron(){
  Serial.println("COO Over on Routine");
  delay(DT);
  digitalWrite(COO,LOW);
  COS=COS+1;
}

void changeoveroff(){
  Serial.println("COO Over off Routine");
  delay(DT/2);
  digitalWrite(COO,HIGH);
  COS=COS-1;
}

void logics(){



if ( ACC == 0 && (UPSBP == 1 || UPSBPM == 1) && GOFM == 0 && GENS == 0){
Serial.println("");
Serial.println("WAPDA IS OFFLINE!");
Serial.println("UPS IS BYPASSED");
Serial.println("Starting Generator.");
generatorstart();
Serial.println(""); 
}

if ( ACC == 0 && UPS == 0 && GOFM == 0 && GENS == 0){
Serial.println("");
Serial.println("WAPDA IS OFFLINE!");
Serial.println("UPS IS NOT BYPASSED");
Serial.println("UPS IS DISCHARGED");
Serial.println("Starting Generator.");
generatorstart();
Serial.println("");
}

if ( ACC == 0 && (UPSBP == 1 || UPSBPM == 1) && GOFM == 0 && GENS == 1){
Serial.println("");
Serial.println(" Generator Started Sucessfully!!");
Serial.println("Now System Will Reset Number of Attempts");
ATP=0;
Serial.println("");
Serial.println("Now System will run Change Over Routine");
if (COS == 0){
  changeoveron();
}
}

if ( ACC == 0 && UPS == 0 && GOFM == 0 && GENS == 1){
Serial.println("");
Serial.println(" Generator Started Sucessfully!!");
Serial.println("Now System Will Reset Number of Attempts");
ATP=0;
Serial.println("");
Serial.println("Now System will run Change Over Routine");
if (COS == 0){
  changeoveron();
}
}

if ( ACC == 1 && GONM == 0 && GENS == 1){
Serial.println("");
Serial.println("WAPDA IS NOW ONLINE");
Serial.println("GENERATOR WILL NOW STOP");
if (COS == 1){
  changeoveroff();
} 
generatoroff();
}

if ( ACC == 0 && UPS == 1 && UPSBP == 0 && UPSBPM == 0 && GONM == 0 && GENS == 1){
Serial.println("");
Serial.println("UPS IS NOW ONLINE");
Serial.println("GENERATOR WILL NOW STOP");
if (COS == 1){
  changeoveroff();
} 
generatoroff();
}

if ( ACC == 1 && GONM == 0 && GENS == 0){
Serial.println("");
Serial.println("GENERATOR IN NOW SUCESSFULLY STOPPED");  
if (COS == 1){
  changeoveroff();
} 
else 
{
Serial.println("");
Serial.println("CHANGE OVER ROUTINE SUCESSFULL");
} 
  
}

if ( ACC == 0 && UPS == 1 && UPSBP == 0 && UPSBPM == 0 && GONM == 0 && GENS == 0){
Serial.println("");
Serial.println("GENERATOR IN NOW SUCESSFULLY STOPPED"); 
if (COS == 1){
  changeoveroff();
} 
else 
{
Serial.println("");
Serial.println("CHANGE OVER ROUTINE SUCESSFULL");
} 
    
}


}

void overrides(){
  
if ( GONM == 1 && GOFM == 0 && GENS == 0){ 
Serial.println("");
Serial.println("Mobile Over-Ride Activated!!");
Serial.println("Starting Generator.");
Serial.println("Warning Generator will Not Stop Automatically!");
generatorstart();
Serial.println(""); 
} 
  
if ( GONM == 1 && GOFM == 0 && GENS == 1){
Serial.println("");
Serial.println(" Generator Started Sucessfully!!");
Serial.println("Now System Will Reset Number of Attempts");
ATP=0;
Serial.println("");
Serial.println("Now System will run Change Over Routine");
if (COS == 0){
  changeoveron();
}
} 

if ( GOFM == 1 && GENS == 1){
Serial.println("");
Serial.println("MOBILE OVERRIDE");
Serial.println("GENERATOR WILL NOW STOP");
if (COS == 1){
  changeoveroff();
} 
generatoroff();
}

if ( GOFM == 1 && GENS == 0){
Serial.println("");
Serial.println("GENERATOR IN NOW SUCESSFULLY STOPPED"); 
if (COS == 1){
  changeoveroff();
} 
else 
{
Serial.println("");
Serial.println("CHANGE OVER ROUTINE SUCESSFULL");
} 
}

if ( CONM == 1 && COFM == 0){
Serial.println("");
Serial.println("Mobile Over-Ride Activated!!");
Serial.println("INIATING CHANGEOVER ON ROUTINE");
Serial.println("Warning CHANGEOVER will Not TURN OFF Automatically!");
generatorstart();
Serial.println("");   
if (COS == 0){
  changeoveron();
} 
else 
{
Serial.println("");
Serial.println("CHANGE OVER ROUTINE SUCESSFULL");
}
}

if ( CONM == 0 && COFM == 1){
Serial.println("");
Serial.println("Mobile Over-Ride Activated!!");
Serial.println("INIATING CHANGEOVER OFF ROUTINE");
Serial.println("Warning CHANGEOVER will Not TURN ON Automatically!");
generatorstart();
Serial.println("");   
if (COS == 1){
  changeoveroff();
} 
else 
{
Serial.println("");
Serial.println("CHANGE OVER ROUTINE SUCESSFULL");
}


}

}

void readings(){
  
ACC = digitalRead(ACCP); 
GENS = digitalRead(GENP);  
UPS = digitalRead(UPSP); 
UPSBP = digitalRead(UPSBPP);  
  }


void ledsmobile(){

if (ACC == 1){
Blynk.virtualWrite(V7, 255); 
}
else
{
Blynk.virtualWrite(V7, 0);   
}


if (GENS == 1){
Blynk.virtualWrite(V8, 255); 
}
else
{
Blynk.virtualWrite(V8, 0);   
}
  

if (COS == 1){
Blynk.virtualWrite(V9, 255); 
}
else
{
Blynk.virtualWrite(V9, 0);   
} 

if (UPS == 1){
Blynk.virtualWrite(V10, 255); 
}
else
{
Blynk.virtualWrite(V10, 0);   
}
  
 
  }

void loop()
{

Blynk.run();

if ( ATS == 1){
logics(); 
}

overrides();

readings();

ledsmobile();

} 






BLYNK_WRITE(V1) {
 int pinValue = param.asInt();
 if (pinValue == 1) {  
    ATS = 1;  
  } else {
    ATS = 0;
 }
}


BLYNK_WRITE(V2) {
 int pinValue = param.asInt();
 if (pinValue == 1) {  
    UPSBPM = 1;  
  } else {
    UPSBPM = 0;
 }
}


BLYNK_WRITE(V3) {
 int pinValue = param.asInt();
 if (pinValue == 1) {  
    GONM = 1;  
  } else {
    GONM = 0;
 }
}


BLYNK_WRITE(V4) {
 int pinValue = param.asInt();
 if (pinValue == 1) {  
    GOFM = 1;  
  } else {
    GOFM = 0;
 }
}


BLYNK_WRITE(V5) {
 int pinValue = param.asInt();
 if (pinValue == 1) {  
    CONM = 1;  
  } else {
    CONM = 0;
 }
}


BLYNK_WRITE(V6) {
 int pinValue = param.asInt();
 if (pinValue == 1) {  
    COFM = 1;  
  } else {
    COFM = 0;
 }
}
0 Likes

#2

You need to read this:

Pete.

0 Likes

#3

I have changed the void loop code to this
and still getting the same error

void loop()
{

Blynk.run();

timer.setInterval(1000L, overrides);

}
0 Likes

#4

tried this too
overrides is a function

void setup()
{
 // Debug console
 Serial.begin(9600);
 delay(10);
 // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);
 delay(10);
 Blynk.begin(auth, wifi, ssid, pass);
 
 
 
 pinMode(GPR, OUTPUT);
 pinMode(GSR, OUTPUT);
 pinMode(COO, OUTPUT);
 
 pinMode(ACCP, INPUT);
 pinMode(GENP, INPUT);
 pinMode(UPSP, INPUT);
 pinMode(UPSBPP, INPUT);
 
 digitalWrite(GPR, HIGH);
 digitalWrite(GSR, HIGH);
 digitalWrite(COO, HIGH);


timer.setInterval(1000L, overrides);

}

void loop()
{

Blynk.run();
timer.run();


}
0 Likes

#5

The timer initialisation:

timer.setInterval(1000L, overrides);

needs to be in void setup, not void loop.

Your void loop should look like this:

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

I’d suggest you take a little more time to read the document correctly.

Even when you do set-up your timer correctly, if you call a function that takes a long time to execute and then that calls another function, which also takes a long time to execute, you’ll starve the Blynk library of processor time.
The solution to that may be to optimise your code better and/or add Blynk.run() commands into your functions to allow the Blynk libraries to get some processor time.

Pete.

0 Likes

#6

AT+CIPSEND=1,40
AT+CIPCLOSE=1
AT+CIPCLOSE=1
AT+CIPSTART=1,“TCP”,“blynk-cloud.com”,80
AT+CIPSEND=1,37
0e305d6b5b2e42bfb7ce2116dbf99b05[158600] Login timeout

this error now

0 Likes

#7

OK Thank you sir . Thanks for the help

0 Likes