Device offline of the app after the first time of working

Hello, I am doing my first project with Blynk, it is to move a servo motor and when it moves I send an email and a notification to my cell phone, at the beginning it works and then I get message in the application that the device is offline and it keeps happening while the program is running.

I’ve been trying to solve this for a long time and I do not succeed, I work with a LoLin NodeMCU which is an ESP8266, which connects to a WiFi network and my cell phone connected to the data network, im using the Arduino IDE and a LCD.

If someone can help me, please, thank you.

The code is this:

#define BLYNK_PRINT Serial    //Comente esto para desactivar impresiones y ahorrar espacio

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <LiquidCrystal.h>
#include <Servo.h>

LiquidCrystal lcd(5,4,0,2,14,12);     //Pin LCD: 4-RS, 6-ENABLE, 11-D4, 12-D5, 13-D6, 14-D7)... (1,3,5- GND, 2-Vcc, );
Servo servo;  //cafe - GND, rojo - 5V, amarillo - PIN(3)

int pos = 0;   //Posicion del servo
long tiempoEntre = 10000; //10000 ms hours between feeding
long tiempoFinal; 
long tiempoActual;
int cont = 0;
bool aux = true;

byte pez[8]{
B00000,
B00000,
B00000,
B10110,
B01111,
B10110,
B00000,
};

byte pez1[8]{
B10110,
B01111,
B10110,
B00000,
B00000,
B00000,
B00000,
};

byte pez2[8]{
B00000,
B00000,
B00000,
B10110,
B01111,
B10110,
B00000,
};

//Su authtoken generado por la aplicacion Blynk
char auth[] = "6454ff8067274e8a8e8403a415a8eddd";

//Datos para la conexion de Red Wifi.
char ssid[] = "wl-fmat-ccei-b"; //Nombre de la red WIFI
char pass[] = ""; //contraseña de la red WIFI

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

void setup() {
    Serial.println("---- 1----");
  Serial.begin(9600);
    Serial.println("---- 2 ----");
  servo.attach(3); //GPIO10, SD3
  lcd.begin(16, 2);
  lcd.clear();
  
  lcd.createChar(1,pez);
  lcd.createChar(2,pez1);
  lcd.createChar(3,pez2);
  Serial.println("---- Antes ----");
  Blynk.begin(auth, ssid, pass);
    Serial.println("---- Despues ----");

  // Envia correo electrónico cuando el hardware se conecte al servidor de Blynk
  //Parametros por defecto: "Correo electronico", "Asunto", "Mensaje a enviar"
  
  Blynk.email("andrea.ortiz.pavon@gmail.com","INICIO FOODFISH", "Se ha alimentado a los peces :)");
  
  //pinMode(5, INPUT);// pin D1(GPIO5) como entrada del sensor
  pinMode(13,OUTPUT);// pin D7(GPIO13) como Salida LED Rojo
    pinMode(15,OUTPUT);// pin D8(GPIO15) como Salida LED Verde
  
}

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

void loop() {

  
  FoodFish();
  Alarma(aux);// llama a ejecutar la funcion
  aux = true;
  Blynk.run();
  
}


void FoodFish(){
  Serial.println();
  Serial.println("---- Iniciamos ----");
  Blynk.notify("INICIAMOS");
  
  lcd.setCursor(2,0);
  lcd.print("FOODFISH ");
  
  lcd.setCursor(12,0);
  lcd.write(1);
  lcd.setCursor(13,0);
  lcd.write(2);
  lcd.setCursor(14,0);
  lcd.write(3);
  
  lcd.setCursor(0,1);
  lcd.print("No. de veces: ");
  Serial.println("-----------NO. VECES");
  
  tiempoActual = millis();
  
      Serial.print("Tiempo actual: ");
      Serial.println(tiempoActual/1000);
      
      Serial.print("* Tiempo entre: ");
      Serial.println(tiempoEntre/1000);
      
  tiempoFinal = tiempoActual + tiempoEntre;
  
      Serial.print("-> Tiempo final: ");
      Serial.println(tiempoFinal/1000);
  
  while(tiempoActual < tiempoFinal) {
   servo.write(0);
   delay(1000);
   tiempoActual = millis();  
  }
  
       Serial.print("Tiempo actual: "); 
       Serial.println(tiempoActual/1000);
  
  for(pos = 0; pos < 180; pos += 1) { // goes from 0 degrees to 180 degrees 
                                     // in steps of 1 degree 
    servo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  Serial.println(".....MOVIO 1");
  } 
  
       Serial.print("Tiempo actual: "); 
       Serial.println(tiempoActual/1000);
  
  for(pos = 180; pos>=1; pos-=1)  {   // goes from 180 degrees to 0 degrees                                
    servo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15); 
    Serial.println(".....MOVIO 2"); // waits 15ms for the servo to reach the position 
  } 

  cont = cont + 1;
  digitalWrite(13,HIGH);
  delay(3000);
  digitalWrite(13,LOW);
  Serial.println();
  Serial.print("No. de veces: ");
  Serial.println(cont);

  lcd.setCursor(14,1);   //1 es la segunda linea
  lcd.print(cont);
}

void Alarma(bool aux){
  // si la alarma se activa este envia un correo y espera 15 segundos.

  if (aux == true) // si aux es true
  {
    Serial.println("Alarma Activada"); //Imprime por el monitor serial
     Blynk.email("andrea.ortiz.pavon@gmail.com","FOODFISH", "Se ha alimentado a los peces :)");
      Blynk.notify("SE ALIMENTO");
    delay(200);
  }

  aux = false;

@AndreaOrtiz go back and edit your sketch section post with backticks so we can read it.

Suggest you study PUSH DATA example very closely and look to have just 2 lines of code in loop() until you become an experienced Blynker. Also try the simple stuff first.

I modified it, i hope it gets better, i’m new here :frowning: the Loop looks better

At first it works correctly, but then the device disconnects in the app.

This is when the first time works, the end of the code:

image

Then, the second time i got this, when its stop working:

image

loop() needs to use Blynk timer to call your functions like the PUSH DATA example

I do not understand why I need that function, I already implement it and I still have the error

Let’s just say Blynk sketches are not simply Arduino sketches. In Blynk we don’t use delay() we use the Blynk timer (known to Arduino users as Simple Timer).

Can you helpe me for that, im not sure where i need to put de Timer in the code :frowning:

Take a look how the timer is used in PUSH DATA at https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FPushData

Sorry, I’ve been reading it but I still do not understand what part of my code I should implement this, the function of the timer, since the delays I use is specifically for counting the feeding time for my fish (it’s a fish feeder my program), I do not understand how to implement it or in what way it would serve me :frowning:

You have at least one very long delay of 3000ms which is not acceptable for WiFi devices running Blynk.

If I were you I would run the PUSH DATA sketch before trying servos etc.

After playing with that code, i finally did what i want it, really thank you very much for your help :smiley:

The code for my fish feeder is this:

//-----------------------------------------------------------------------------------------------------------------------
//  Autora: Andrea E. Ortiz Pavón
//  Alimentador de peces automatico, el cual te manda una notificación a la aplicacion de Blynk cuando se alimenta
//
//   COMPONENTES:
//      - Display LCD
//      - Servo motor SG90
//      - NodeMCU LoLin (ESP8266)
//
//    El servo motor se mueve cada determinado tiempo para alimentar a los peces, al terminar los movimientos
//    se manda una noticiación por medio de la App de Blynk para avisar al usuario, tiene un widget en la app
//    donde te muestra el contador de las veces que se ha alimentado
//-----------------------------------------------------------------------------------------------------------------------

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <LiquidCrystal.h>
#include <Servo.h>

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

byte pez[8]{
B00000,
B00000,
B00000,
B10110,
B01111,
B10110,
B00000,
};

byte pez1[8]{
B10110,
B01111,
B10110,
B00000,
B00000,
B00000,
B00000,
};

byte pez2[8]{
B00000,
B00000,
B00000,
B10110,
B01111,
B10110,
B00000,
};

LiquidCrystal lcd(5,4,0,2,14,12);     //Pin LCD: (4-RS, 6-ENABLE, 11-D4, 12-D5, 13-D6, 14-D7)... (1,3,5- GND, 2-Vcc, );
Servo servo;  //cafe - GND, rojo - 5V, amarillo - PIN(3)
BlynkTimer timer;

int pos = 0;   //Posicion del servo
int cont = 0;

char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
char ssid[] = "xxxxxxxxxxxxxxxxxxxx"; //Nombre de la red WIFI
char pass[] = "xxxxxxxxxxxxxxxxxxx"; //contraseña de la red WIFI

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

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.

  void myTimerEvent(){
  
    Serial.println();
    Serial.println("---- Iniciamos ----");
    
    
    lcd.setCursor(2,0);
    lcd.print("FOODFISH ");
    
    lcd.setCursor(12,0);
    lcd.write(1);
    lcd.setCursor(13,0);
    lcd.write(2);
    lcd.setCursor(14,0);
    lcd.write(3);
    
    lcd.setCursor(0,1);
    lcd.print("No. de veces: ");

    servo.write(0);

    //----- Primer Movimiento
    for(pos = 0; pos < 180; pos += 1) {  // goes from 0 degrees to 180 degrees, in steps of 1 degree 
      servo.write(pos);                  // tell servo to go to position in variable 'pos' 
      delay(15);                         // waits 15ms for the servo to reach the position 
      Serial.println(".....MOVIO 1");
    } 

    //----- Segundo Movimiento
    for(pos = 180; pos>=1; pos-=1)  {   // goes from 180 degrees to 0 degrees, in steps of 1 degree                               
      servo.write(pos);                 // tell servo to go to position in variable 'pos' 
      delay(15);                        // waits 15ms for the servo to reach the position 
      Serial.println(".....MOVIO 2"); 
    } 
  
    cont = cont + 1;
    
    digitalWrite(13,HIGH);
    delay(3000);
    digitalWrite(13,LOW);
    
    Serial.println();
    Serial.print("No. de veces: ");
    Serial.println(cont);
  

    Blynk.notify("SE ALIMENTO");
    Blynk.virtualWrite(V5, cont);
  }

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

  void setup(){
    
      Serial.begin(9600);
      servo.attach(3); //GPIO10, SD3
      
      lcd.begin(16, 2);
      lcd.clear();
      
      lcd.createChar(1,pez);
      lcd.createChar(2,pez1);
      lcd.createChar(3,pez2);
      
      Blynk.begin(auth, ssid, pass);
      Serial.println("---- Conectado!! ----");
  
      pinMode(13,OUTPUT);// pin D7(GPIO13) como Salida LED Rojo
      
    // Setup a function to be called every second, Call function f every d milliseconds. The callback function must be declared as void f()
    timer.setInterval(120000, myTimerEvent); //Cada 2 minutos se ejecute
  }

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

  void loop(){
    lcd.setCursor(2,0);
    lcd.print("FOODFISH ");
    
    lcd.setCursor(12,0);
    lcd.write(1);
    lcd.setCursor(13,0);
    lcd.write(2);
    lcd.setCursor(14,0);
    lcd.write(3);
    
    lcd.setCursor(0,1);
    lcd.print("No. de veces: ");
    lcd.setCursor(14,1);   //1 es la segunda linea
    lcd.print(cont);

    Blynk.run();
    timer.run(); // Initiates BlynkTimer
  }