Blynk devices goes offline and online randomly

Hej,
I have two different devices(esp8266 and esp32 running on Blynk iot) in separate places connected to different wifi`s and they both have the same issues…they goes online and offline after a few minutes.
I put one screenshot form of both devices timeline,
and one device code here.!
Screenshot_1|225x500

#define BLYNK_TEMPLATE_ID           "#######"
#define BLYNK_TEMPLATE_NAME         "#######"
#define BLYNK_AUTH_TOKEN            "#############"
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>

const int oneWireBus = 4;

OneWire oneWire(oneWireBus);

DallasTemperature sensors(&oneWire);

char ssid[] = "#######";
char pass[] = "#######";

#define DHTPIN 15      // 
#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float t1 = sensors.getTempCByIndex(0);

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V7, t1);
}

void prindimulle()
{
      Serial.print(F("  Niiskus väli = "));
      Serial.print(dht.readHumidity());
      Serial.println(F("%"));
      delay(50);
      Serial.print(F("  Temperatuur väli = "));
      Serial.print(dht.readTemperature());
      Serial.println(F("ºC"));
      delay(50);
  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);
  Serial.print(F("    Kompostri temp = "));
  Serial.print(temperatureC);
  Serial.println("ºC");
      Serial.println();
      delay(50);
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  // You can also specify server:
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();
  sensors.begin();
  Serial.println("KomposterVesrioon:3.1");
  timer.setInterval(2010L, sendSensor);
  delay(200);
  timer.setInterval(10028L, prindimulle);
  delay(200);
}

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

}

What does your serial monitor show?

Pete.

Don`t know yet, because device begin do it after a few day since I turned it on and device is far away . But now i got code to send Serial info into Blynk terminal widget and tomorrow I can upload it to my device ,and then we see what it shows after a few day :slight_smile:

I don’t think that’s going to help. You device can’t send data to the terminal widget if it’s offline, and the useful info will be appearing when the device boots - before it can connect to Blynk - and when it disconnects.

Pete.

This time devise was working normally more then week…some times only a few hours, some times a few days… I can`t figure out what is causes this, perhaps 4G wifi router looses connection some times and my device gets stuck there…other devises like : cameras ,raspberry pi(acting Victron Venus devise) are working well on same router…
This time i was able to get some serial monitor info… Devise was not rebooting - it was working normally ,but on every turn there was : [18786874] Connecting to blynk.cloud:80

...
Niiskus = 49.00%
 Temperatuur = 20.90*C

[18786874] Connecting to blynk.cloud:80
Kysin andmeid...
OLEMAS!
Sensor 1: 16.56*C Andur 3m peal
Sensor 2: 21.00*C Andur 9m peal
Sensor 3: 25.50*C Andur 15m peal
Sensor 4: 29.00*C Andur 21m peal
Sensor 5: 32.00*C Andur 27m peal
Sensor 6: 20.90*C Andur Garaazis
Running olek = true
Led on
 Niiskus = 48.90%
 Temperatuur = 21.00*C

[18797196] Connecting to blynk.cloud:80
Kysin andmeid...
OLEMAS!
Sensor 1: 16.50*C Andur 3m peal
Sensor 2: 21.00*C Andur 9m peal
...
´´´

It would be useful if you had…

#define BLYNK_PRINT Serial

Near the top of your sketch.

Pete.

Hej,
Actually there is…! I have two devices making strange thing (i think one of them maybe doing it therefore ,it works at the end of wifi coverage in my garden) ,but previous Serial monitor info was from second devise(esp8266) which works on another place and it has “# define BLYNK_PRINT serial” in the sketch. Sry, I didn’t say that.

#define BLYNK_TEMPLATE_ID "********"

#define BLYNK_DEVICE_NAME "Õhkküte"

#define BLYNK_AUTH_TOKEN "*********************"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial

#define APP_DEBUG

#define USE_NODE_MCU_BOARD

#define VENT D8 // ventilaatori juhtPin..hetkel relee selle taga..

//////////////////

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <OneWire.h>

#include <DallasTemperature.h>

#include <AM2320.h>
 // Garaazi I2C temp- ja niiskusandur AM2320b kasutab D1 ja D2 pinne ,kui pole määratud teisiti..Arvatavasti tuleb I2C tempandur ka ühendada D3 ja D4

#define ONE_WIRE_BUS D6 // Paneeli tempandurid // vcc ja anduri data otsa vahele 4.7k takisti

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

/////////////////

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 32

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)

#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10

#define LOGO_HEIGHT   16

#define LOGO_WIDTH    16

static const unsigned char PROGMEM logo_bmp[] =

{ 0b00000000, 0b11000000,

  0b00000001, 0b11000000,

  0b00000001, 0b11000000,

  0b00000011, 0b11100000,

  0b11110011, 0b11100000,

  0b11111110, 0b11111000,

  0b01111110, 0b11111111,

  0b00110011, 0b10011111,

  0b00011111, 0b11111100,

  0b00001101, 0b01110000,

  0b00011011, 0b10100000,

  0b00111111, 0b11100000,

  0b00111111, 0b11110000,

  0b01111100, 0b11110000,

  0b01110000, 0b01110000,

  0b00000000, 0b00110000 };

String messages[] = {"Tere Hommikust"};

////////////////

//Andurite aadressid

DeviceAddress sensor1 = { 0x28, 0xFF, 0xA1, 0x4A, 0x30, 0x18, 0x2, 0xB5 };

DeviceAddress sensor2 = { 0x28, 0xAA, 0xF2, 0xE8, 0x17, 0x13, 0x2, 0xC8 };

DeviceAddress sensor3 = { 0x28, 0x65, 0x77, 0x77, 0x91, 0x10, 0x2, 0xF7 };

DeviceAddress sensor4 = { 0x28, 0x67, 0x7,  0x77, 0x91, 0x12, 0x2, 0x65 };

DeviceAddress sensor5 = { 0x28, 0xCA, 0x42, 0x77, 0x91, 0xB,  0x2, 0x16 };

AM2320 th(&Wire);

bool running = false;  //läheb käima "false" olekus

float Celcius = 0;

char ssid[] = "******";

char pass[] = "*******";

BlynkTimer timer;

WidgetLED led(8);

WidgetTerminal terminal(V11);

//////////////////////////////////////////////

 // float h = th.Humidity; // Garaazi Niiskus

 // float t = th.cTemp;   // Garaazi Temperatuur

 // float t1 = (sensors.getTempC(sensor1));  // Andur 3m peal

 // float t2 = (sensors.getTempC(sensor2));  // Andur 9m peal

 // float t3 = (sensors.getTempC(sensor3));  // Andur 15m peal

 // float t4 = (sensors.getTempC(sensor4));  // Andur 21m peal

 // float t5 = (sensors.getTempC(sensor5));  // Andur 27m peal

//////////////////////////////////////////////

// Blynk appist vendi juhtimine

BLYNK_WRITE(V9)

{

  switch (param.asInt()) {

    case 1: {

        digitalWrite(VENT, LOW); //Lülitab vendi sisse.

        (running = true);

        Serial.println("VENT APIST ON ");

        break;

      }

    case 2: {

        digitalWrite(VENT, HIGH); //Lülitab vendi välja.

        (running = true);

        Serial.println("VENT APIST OFF ");

        break;

      }

    case 3: {

        Serial.println("VENT APIST AUTOMAATNE"); //Lülitab vendi automaatse peale.

        (running = false);

        break;

      }

   }

}

 

void sendSensor()

{

  Blynk.virtualWrite(V5, th.Humidity );

  Blynk.virtualWrite(V6, th.cTemp );

  Blynk.virtualWrite(V21, sensors.getTempC(sensor1) );

  Blynk.virtualWrite(V22, sensors.getTempC(sensor2) );

  Blynk.virtualWrite(V23, sensors.getTempC(sensor3) );

  Blynk.virtualWrite(V24, sensors.getTempC(sensor4) );

  Blynk.virtualWrite(V25, sensors.getTempC(sensor5) );

}

void prindimulle()

{

 

  Serial.println("Kysin andmeid...");

  sensors.requestTemperatures();

delay(200);

  Serial.println("OLEMAS!");

 

  Serial.print("Sensor 1: ");

  Serial.print(sensors.getTempC(sensor1));

  Serial.print("*C ");

  Serial.println("Andur 3m peal");

delay(200);    

  Serial.print("Sensor 2: ");

  Serial.print(sensors.getTempC(sensor2));

  Serial.print("*C ");

  Serial.println("Andur 9m peal");

delay(200);

  Serial.print("Sensor 3: ");

  Serial.print(sensors.getTempC(sensor3));

  Serial.print("*C ");

  Serial.println("Andur 15m peal");

delay(200);

  Serial.print("Sensor 4: ");

  Serial.print(sensors.getTempC(sensor4));

  Serial.print("*C ");

  Serial.println("Andur 21m peal");

delay(200);

  Serial.print("Sensor 5: ");

  Serial.print(sensors.getTempC(sensor5));

  Serial.print("*C ");

  Serial.println("Andur 27m peal");

delay(200);

  Serial.print("Sensor 6: ");

  Serial.print (th.cTemp);

  Serial.print("*C ");

  Serial.println("Andur Garaazis");

delay(200);

}

void saadaekraan()

{

  display.clearDisplay();

  display.setTextSize(2);            

  display.setTextColor(WHITE);        

  display.setCursor(0,0);

  display.print(F("  3m temp = "));

  display.print(sensors.getTempC(sensor1));

  display.print(F(" C"));

  display.display();

delay(1500);

  display.clearDisplay();

  display.setTextSize(2);            

  display.setTextColor(WHITE);        

  display.setCursor(0,0);

  display.print(F("  9m temp = "));

  display.print(sensors.getTempC(sensor2));

  display.print(F(" C"));

  display.display();

delay(1500);

  display.clearDisplay();

  display.setTextSize(2);            

  display.setTextColor(WHITE);        

  display.setCursor(0,0);

  display.print(F(" 15m temp = "));

  display.print(sensors.getTempC(sensor3));

  display.print(F(" C"));

  display.display();

delay(1500);

  display.clearDisplay();

  display.setTextSize(2);            

  display.setTextColor(WHITE);        

  display.setCursor(0,0);

  display.print(F(" 21m temp = "));

  display.print(sensors.getTempC(sensor4));

  display.print(F(" C"));

  display.display();

delay(1500);

  display.clearDisplay();

  display.setTextSize(2);            

  display.setTextColor(WHITE);        

  display.setCursor(0,0);

  display.print(F(" 27m temp = "));

  display.print(sensors.getTempC(sensor5));

  display.print(F(" C"));

  display.display();

delay(1500);

  display.clearDisplay();

  display.setTextSize(2);            

  display.setTextColor(WHITE);        

  display.setCursor(0,0);

  display.print(F("Gar. temp = "));

  display.print(th.cTemp);

  display.print(F(" C"));

  display.display();

  display.clearDisplay();

delay(100);

}

void Sent_serial() {

     

       String content = "";

       char character;

       while(Serial.available()) {

            character = Serial.read();

            content.concat(character);

             

       }

       if (content != "") {

            Blynk.virtualWrite (V11, content);

       }  

}

void arvuta()

{

  float h = th.Humidity; // Garaazi Niiskus

  float t = th.cTemp;   // Garaazi Temperatuur

  float t4 = (sensors.getTempC(sensor4));  // Andur 21m peal

  Serial.print("Running olek = ");

  if (running == true) {

    Serial.println("true");

  }

      else {

       Serial.println ("false");

  }

  // t = garaazi temp

  // t4 = paneeli temp 21m peal

  // D0 = kontroll pin (kui akupinge üle 12,5v siis D0 = 0 ja kui akupinge alla 12,5v siis D0 = 1) - pole kasutuses hetkel enam(oli vana Blynk legasi ajal)

  if (running == false && t4-t >= 15) {

    digitalWrite (VENT, LOW);                                                       //kui blynk äpist on VENT väljalülitatud ja t1 > 24 ja t < 22 ja digitalRead(D0) == LOW,//

    Serial.println("VENT ON ");                                                                   //siis lülita VENT sisse ja pridi VENT sees//

  }

        else if (running == false && t4-t < 10 || running == false && t >= 27 || running == false && t4 <= t ) {

          digitalWrite (VENT, HIGH);

          Serial.println("VENT OFF");

  }

  if (digitalRead(D8) == LOW) { //Ventilaatori oleku ledmärgutuli blynk appis

    led.on();

    Serial.println("Led on");

  }

       else {

         led.off();

         Serial.println("Led off");

  }

delay(300);

  switch (th.Read()) {

    case 2:

      Serial.println(" CRC failed");

      break;

    case 1:

      Serial.println(" Sensor offline");

      break;

    case 0:

      Serial.print(" Niiskus = ");

      Serial.print(th.Humidity);

      Serial.println("%");

      Serial.print(" Temperatuur = ");

      Serial.print(th.cTemp);

      Serial.println("*C");

      Serial.println();

      break;

  }

  }

//////////////////////////////////////////////////////////

void setup()

{

  pinMode(VENT, OUTPUT);

  digitalWrite (VENT, HIGH);

  //Wire.pins(0,2); // SDA, SCL

  Serial.begin(115200);

  Serial.println("NodeMCU AM2320b 17.02.19 ");

  Serial.println("DHTxx test!");

  Wire.begin();

  sensors.begin();

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  Serial.println("Tere hommikust Eestimaa :)");

  Serial.println("Uus Blynk proov v:7.3");

 

  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {

    Serial.println(F("SSD1306 allocation failed"));

   for(;;); // Don't proceed, loop forever

  }

terminal.println(F("------terminal-------"));

terminal.println(F("    "));

delay(200);

terminal.println(F("Esp32 booting up"));

terminal.println(F("........."));

delay(100);

terminal.println(F("........"));

delay(100);

terminal.println(F("......."));

delay(100);

terminal.println(F("......"));

delay(100);

terminal.println(F("....."));

delay(100);

terminal.println(F("...."));

delay(100);

terminal.println(F("..."));

delay(100);

terminal.println(F(".."));

delay(100);

terminal.println(F("   Ready!   "));

delay(100);

terminal.println(F("    "));

terminal.flush();

 display.display();

delay(2000);

  display.clearDisplay();

  display.drawPixel(10, 10, SSD1306_WHITE);

  display.display();

delay(2000);

 timer.setInterval(1010L, sendSensor);

delay(100);

 timer.setInterval(1028L, prindimulle);

delay(1500);

 timer.setInterval(10000L, saadaekraan);

delay(500);

 timer.setInterval(1033L, arvuta);

delay(400);

timer.setInterval(1013L, Sent_serial);

delay(200);

 //digitalWrite (VENT, LOW);

  }

////////////////////////////////////////////////////////////////

void loop()

{

  Blynk.run();

  timer.run();

}
´´´

I’m not surprised you’re getting disconnections with all those delays,

When this device boots do you actually see the messages in void setup?

Pete.

Thanks,
Yes ,I see the messages what are in void setup.
I understand that “loop” should be clean as possible, how delays in “setup” affect blynk running? I understand that you recommend remove these delays from void setup?

Strange thing is, on Blynk legacy same sketch ran years and years like charm and “void loop” was crowded delays and “if” statements and ect. - i had no ideas that “loop” should be clean :smiley:

Any delay after Blynk.begin() will cause problems.

You have delays in your other functions too.

Pete.

Thank You! :wink:

The delays are your biggest problem atm, but which sensors are you using?