Blynk keeps going offline

Hello

I am having an issue with my ESP32 keeps going offline on Blynk app, it has been installed since April and have had no issues until this start of this month, after a few hours the device goes offline, it then has to be manually reset and stays on for a few hours again.

It’s not sharing authentication code or template with any other device, connection is done by Wi-Fi. Will post code later when I am on the laptop.

Anyone had similar issue, not found anything on the web.

Hello, @Daryl. Please need more information. Please provide sketch, logs from serial monitor, also need Blynk app version (platform Android or iOS).

Hello

Please find code below, i have been unable to capture the error through serial.

Blynk app 3.5.7 on iOS

#define BLYNK_TEMPLATE_ID "TMPL5NV09sZ4A"
#define BLYNK_TEMPLATE_NAME "Burstner Elegance"
#define BLYNK_AUTH_TOKEN "aRau0IucQnidN0Ri99hi6s27FUyEx64f"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define BLYNK_PRINT Serial

char ssid[] = "test";
char pass[] = "Test";

//char ssid[] = "test1";
//char pass[] = "test1";

const int Relay1 = 14;
const int Relay2 = 27;
const int Relay3 = 26;
const int Relay4 = 25;
const int SensorDataPin = 0;

int RelayState1 = LOW;
int RelayState2 = LOW;
int RelayState3 = LOW;
int RelayState4 = LOW;

int currState = -1;
int prevState = -1;
long lastChangeTime = 0;
int flag=0;

BlynkTimer timer;

// BME280 connect to ESP32 I2C (GPIO 21 = SDA, GPIO 22 = SCL)
Adafruit_BME280 bme;

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);
}
  BLYNK_WRITE(V1) {
  RelayState1 = param.asInt();
  digitalWrite(Relay1, RelayState1);
  }
  BLYNK_WRITE(V2) {
  RelayState2 = param.asInt();
  digitalWrite(Relay2, Relay2);
  }
  BLYNK_WRITE(V3) {
  RelayState3 = param.asInt();
  digitalWrite(Relay3, RelayState3);
  }
  BLYNK_WRITE(V4) {
  RelayState4 = param.asInt();
  digitalWrite(Relay4, RelayState4);
}
 
void checkPin() {
  
  int input1 = digitalRead(33);
  if (input1==1 && flag==0) {
  Blynk.virtualWrite(V21, input1);
  flag=1;
  }
  else if (input1==0)
  {
  flag=0;
  Blynk.virtualWrite(V21, input1);
}

  int input2 = digitalRead(32);
  if (input2==1 && flag==0) {
  Blynk.virtualWrite(V22, input2);
  flag=1;
  }
  else if (input2==0)
  {
  flag=0;
  Blynk.virtualWrite(V22, input2);
}

  int input3 = digitalRead(35);
  if (input3==1 && flag==0) {
  Blynk.virtualWrite(V23, input3);
  flag=1;
  }
  else if (input3==0)
  {
  flag=0;
  Blynk.virtualWrite(V23, input3);
}

  int gassensor = digitalRead(34);
  long r = millis();
  if (gassensor != prevState) {
  lastChangeTime = r;
  }
  if (r - lastChangeTime > 50) {
  if (gassensor != currState) {
  currState = gassensor;
  Blynk.virtualWrite(V24, gassensor);
    }
  }
  prevState = gassensor;
}

void checkTemps() { 
  Blynk.virtualWrite(V31, bme.readTemperature());
  Blynk.virtualWrite(V32, bme.readHumidity());
}

void displayOled() {
  
  display.clearDisplay();
  display.setTextSize(1);             
  display.setTextColor(WHITE); 
  display.setCursor(0,0);             
  display.println(WiFi.SSID());
  display.setTextSize(1);        
  display.setCursor(0,20);             
  display.println(WiFi.localIP());
  display.setTextSize(3); 
  display.setCursor(0,40);             
  display.println(WiFi.RSSI());
  display.display();
}

void setup() {

  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS));
  display.clearDisplay();
  
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  Serial.print("RRSI: ");
  Serial.println(WiFi.RSSI());

  bme.begin(0x76); 
  pinMode(33,   INPUT_PULLDOWN);
  pinMode(32,   INPUT_PULLDOWN);
  pinMode(35,   INPUT_PULLDOWN);
  pinMode(34,   INPUT_PULLUP);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);

  timer.setInterval(30000L, checkTemps);
  timer.setInterval(10000L, displayOled);
  
}

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

@Daryl Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Thank you for letting me know, new to this.

You shouldn’t be calling checkPin() from your void loop. Read this…

Pete.

Hi Pete

many thanks, I shall give this a try and see what happens.

I manged to get to the device tonight, and the Oled screen was showing no internet connection, I restarted the device and came straight back online.

Seems for some reason it loses connection and then dont reconnect.

Daryl

Hello

I removed as per adive given and now the app is not updating when the pin goes low, only high.

After doing some reading i suspect the device is losing wifi and not being able to reconnect within the timeout thats within blynks libary, so it then does noting till it is manually reset.

So how are you now calling checkPin() ?

How are you powering your OLED display?

Pete.

I have readded the check pin, but have added a timer same the temp & oled. it now updates when the pin goes low.

Tho Oled is powerd by 5V from the esp32, however the issue was piror the Oled, i only added it so could see what is happing with the wifi.

Seeing some code would be good.

Pete.

#define BLYNK_TEMPLATE_ID "TMP"
#define BLYNK_TEMPLATE_NAME "Bue"
#define BLYNK_AUTH_TOKEN "aRau"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     -1 // Reset pin
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define BLYNK_PRINT Serial

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

//char ssid[] = "test";
//char pass[] = "test";

const int Relay1 = 14;
const int Relay2 = 27;
const int Relay3 = 26;
const int Relay4 = 25;
const int SensorDataPin = 0;

int RelayState1 = LOW;
int RelayState2 = LOW;
int RelayState3 = LOW;
int RelayState4 = LOW;

int currState = -1;
int prevState = -1;
long lastChangeTime = 0;
int flag=0;

BlynkTimer timer;

// BME280 connect to ESP32 I2C (GPIO 21 = SDA, GPIO 22 = SCL)
Adafruit_BME280 bme;

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);
}
  BLYNK_WRITE(V1) {
  RelayState1 = param.asInt();
  digitalWrite(Relay1, RelayState1);
  }
  BLYNK_WRITE(V2) {
  RelayState2 = param.asInt();
  digitalWrite(Relay2, Relay2);
  }
  BLYNK_WRITE(V3) {
  RelayState3 = param.asInt();
  digitalWrite(Relay3, RelayState3);
  }
  BLYNK_WRITE(V4) {
  RelayState4 = param.asInt();
  digitalWrite(Relay4, RelayState4);
}
 
void checkPin() {
  
  int input1 = digitalRead(33);
  if (input1==1 && flag==0) {
  Blynk.virtualWrite(V21, input1);
  flag=1;
  }
  else if (input1==0)
  {
  flag=0;
  Blynk.virtualWrite(V21, input1);
}

  int input2 = digitalRead(32);
  if (input2==1 && flag==0) {
  Blynk.virtualWrite(V22, input2);
  flag=1;
  }
  else if (input2==0)
  {
  flag=0;
  Blynk.virtualWrite(V22, input2);
}

  int input3 = digitalRead(35);
  if (input3==1 && flag==0) {
  Blynk.virtualWrite(V23, input3);
  flag=1;
  }
  else if (input3==0)
  {
  flag=0;
  Blynk.virtualWrite(V23, input3);
}

  int gassensor = digitalRead(34);
  long r = millis();
  if (gassensor != prevState) {
  lastChangeTime = r;
  }
  if (r - lastChangeTime > 50) {
  if (gassensor != currState) {
  currState = gassensor;
  Blynk.virtualWrite(V24, gassensor);
    }
  }
  prevState = gassensor;
}

void checkTemps() { 
  Blynk.virtualWrite(V31, bme.readTemperature());
  Blynk.virtualWrite(V32, bme.readHumidity());
}

void displayOled() {
  
  display.clearDisplay();
  display.setTextSize(1);             
  display.setTextColor(WHITE); 
  display.setCursor(0,0);             
  display.println(WiFi.SSID());
  display.setTextSize(1);        
  display.setCursor(0,20);             
  display.println(WiFi.localIP());
  display.setTextSize(3); 
  display.setCursor(0,40);             
  display.println(WiFi.RSSI());
  display.display();
}

void setup() {

  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS));
  display.clearDisplay();
  
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  Serial.print("RRSI: ");
  Serial.println(WiFi.RSSI());

  bme.begin(0x76); 
  pinMode(33,   INPUT_PULLDOWN);
  pinMode(32,   INPUT_PULLDOWN);
  pinMode(35,   INPUT_PULLDOWN);
  pinMode(34,   INPUT_PULLUP);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);

  timer.setInterval(30000L, checkTemps);
  timer.setInterval(10000L, displayOled);
  timer.setInterval(1000L, checkPin);
  
}

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

It’s still there!
Didn’t the “keep your void loop clean” article make it clear why you can’t do this?

Using the same flag variable

What is the point in the time comparisons in this piece of code?

Using the same flag variable for tracking three different pin states seems unlikely to work well.

Pete.