Keep working even offline

I’m creating a system using a ESP8266 and a SIM800. And the function is just to control the lights, either by the physical buttons connected directly to the ESP or by the Blynk app, however, if the connection drops, the ESP8266 restarts, and it keeps trying to connect to the Blynk servers, and, as there is no connection, ends up getting into a loop of trying to connect to Blynk servers > Failed to connect > restart > trying to connect to Blynk servers and so on until you can connect again. That way, I’m also unable to control the lights through the physical buttons, because the system never starts.

Is there any way to prevent this automatic restart? For example, if there is no connection, my system continues to work offline and, after a few moments, try to connect again (So I could call the Blynk.begin function from time to time until the connection is re-established, without my system restarting).

Yes.

Blynk.begin is a blocking function, and if there’s no connection to either the internet or the Blynk server then all code processing will halt at that point.

It’s impossible to suggest a solution without more info, including your code.

Pete.

I would be interested in this also if there is a way to run “offline”, what part of code would you need to see for a suggested solution?

It depends exactly what you are trying to achieve.

But, rather than hijacking this topic with details of your own objectives, it would be better if you started a new topic and explained in detail what it is that you are trying to achieve, what hardware you are using, what functionality you’d like when offline and why, and any other relevant information including existing code etc.

Pete.

The code was pretty messed up, so I cleaned it up leaving only the parts that really matter.
(The status of the lamp switch was already created by me and is working in another version that uses Wifi)

As I said before, I did a validation to check the connection status, and if it is no longer connected, start a new connection, however, if Blynk can’t connect, ESP is forced to restart, staying in this loop until it gets a connection with Blynk. How can I prevent this restart and make the code run normally when it fails to connect (Since I already have a validation for it to try to connect again)?

My code below:

#define TINY_GSM_MODEM_SIM800
//------------------------------------------------------------------

#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>
#include "Wire.h"
#include <FS.h>
//GPS Module RX pin to NodeMCU D3
//GPS Module TX pin to NodeMCU D4
#define rxPin D4
#define txPin D3
#define FONA_RST 13
SoftwareSerial Sim800L(txPin,rxPin);
SoftwareSerial *fonaSerial = &Sim800L;
//Hardware serial is also possible! for ESP32
//HardwareSerial *fonaSerial = &Serial2;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);


//------------------------------------------------------------------
#define BLYNK_TEMPLATE_ID "TMPL_aJxEwab"
#define BLYNK_DEVICE_NAME "SmartAstra v01"
#define BLYNK_AUTH_TOKEN "TOKEN"
//------------------------------------------------------------------

#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
//------------------------------------------------------------------
char auth[] = BLYNK_AUTH_TOKEN;
// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "zap.vivo.com.br";
char user[] = "vivo";
char pass[] = "vivo";
//------------------------------------------------------------------
TinyGsm modem(fona);
BlynkTimer timer;
//------------------------------------------------------------------

#define pin_rele_lamp D5

//------------------------------------------------------------------
int rele_lamp = 0;

//------------------------------------------------------------------
//Change the virtual pins, as you have set in the blynk account.
#define virtual_pin_radio    V1
//------------------------------------------------------------------
String gsm_buff ="";
char sendsms[15];
char caller_id[32];
int len=0;
unsigned long temporizador1 = millis();
int lampApp = 0;
int temporizador3 = 0;
int reiniciaConexao = 1;


//------------------------------------------------------------------
BLYNK_WRITE(virtual_pin_lamp) {

  if (interrup == 0 ){
  rele_lamp = param.asInt();
  lampApp = param.asInt();
  }
  //digitalWrite(pin_rele_lamp, rele_lamp);
   Serial.print("Relay 1 is ");
   if(rele_lamp==0)
   Serial.println("OFF");
   else
   Serial.println("ON");
}
//------------------------------------------------------------------

void statusLamp(){

  
  if(digitalRead(D6)==HIGH){
   Serial.println("DESLIGADO");
   valor = "DESLIGADO";
  }
  else{
    if(digitalRead(D6)==LOW){
   Serial.println("LIGADO");
   valor = "LIGADO";
    }
  }

  Blynk.virtualWrite(virtual_pin_status_lamp, valor);
}

//------------------------------------------------------------------
void controlaLamp(){



  //Se interruptor ligado > ligar lampada 
  if(interrup ==1){
    Serial.println("LIGOU PELO INTERRUPTOR");
    Blynk.virtualWrite(virtual_pin_lamp, 1);
    rele_lamp = 1;
    digitalWrite(pin_rele_lamp, HIGH);

    }
    //Se no app estiver como ligado > ligar lampada
    else if(rele_lamp == 1){
    Serial.println("LIGOU PELO APP");
    digitalWrite(pin_rele_lamp, HIGH);
    Blynk.virtualWrite(virtual_pin_lamp, 1);
    
    }
      
      //Se no app estiver como desligado > desligar radio
      else if(rele_lamp == 0){
        digitalWrite(pin_rele_lamp, LOW);
        Serial.println("DESLIGOU PELO APP");
        Blynk.virtualWrite(virtual_pin_lamp, 0);
       }

  
}
//------------------------------------------------------------------

void verifyConnection(){

  if( Blynk.connected() ){
    
    Serial.println("Connection OK");
    }
    else if(!Blynk.connected()){
      Serial.println("NO CONNECTION");

        Serial.println("INITIATING CONNECTION");
        modem.restart();
        Blynk.begin(auth, modem, apn, user, pass);
        Serial.println("CONNECTED");
      }
  
  }


//------------------------------------------------------------------
 

 

void setup()
{
  Serial.begin(115200);
  
 //--------------------------------------------------------------------
  pinMode(pin_rele_lamp, OUTPUT);
  pinMode(pin_rele_status_lamp, INPUT_PULLUP);
  //--------------------------------------------------------------------
  //During Starting all Relays should TURN OFF
  digitalWrite(pin_rele_lamp, LOW);
  digitalWrite(pin_rele_status_lamp, LOW);
  //--------------------------------------------------------------------
  delay(2000);
  
   fonaSerial->begin(9600);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }
  //--------------------------------------------------------------------
  Serial.println(F("FONA is OK"));
  fona.println("AT+CMGF=1"); // Configuring TEXT mode
  delay(1000);
  fona.print ("AT+CSMP=17,167,0,0\r");// Configuring TEXT mode
  delay(1000);
  fona.print("AT+CNMI=2,1\r\n");  //set up the fona to send a +CMTI notification when an SMS is received
 
  //fona.println(F("AT+CMGDA=\"DEL ALL\""));
  //delay(5000);
  delay(1000);
  Serial.println("FONA Ready");
  //--------------------------------------------------------------------
  modem.restart();

    Blynk.begin(auth, modem, apn, user, pass);

  
  Blynk.virtualWrite(virtual_pin_lamp, rele_lamp);
  Blynk.virtualWrite(virtual_pin_status_lamp, valor);
  //--------------------------------------------------------------------
  timer.setInterval(1L, statusLamp);
  timer.setInterval(1L, controlaLamp);


}

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

Please edit your post, and add triple backticks ``` before and after your whole sketch.

1 Like

If you mean the verifyConnection function then how is this being called?

Do you realise that the “1L” in these commands means 1 millisecond?
You are trying to call two functions one thousands times per second, and each function contains a Blynk.virtualWrite command. So you are trying to do 2000 Blynk.virtualWrites per second, when the allowable limit is 10 Blynk.virtualWrites per second.

You ned to fix these issues before you go any further!

Pete.

Must be on the top of your sketch, the first lines.

also, I’d suggest that you read this article

Small mistakes I made to clean the code and also translate the functions to English (My code is written with a lot of Portuguese).

In this case, the verifyConnection function is called by the loop, and where 1L is actually 1000L

If you want constructive feedback then you need to post valid code.

Pete.