ESP32 WROOM 32D disconnect after few minutes

I’m working on a project that requires daily analysis of gases and air quality in a room. I’m using an ESP32 WROOM 32D development board along with MQ9, MQ135, and DHT22 sensors. I’m encountering some connection issues with Blynk. After turning the device on, it disconnects from Blynk within a few minutes. Here’s the code for reference:

#define BLYNK_TEMPLATE_ID "XXXXX"
#define BLYNK_AUTH_TOKEN "XXXXX"
#define BLYNK_TEMPLATE_NAME "XXXXX"
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#include <DHT.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>  

#define ST77XX_GREEN  0x07E0
#define ST77XX_YELLOW 0xFFE0
#define ST77XX_ORANGE 0xFD20
#define ST77XX_RED    0xF800
#define ST77XX_PURPLE 0x780F
#define ST77XX_MAROON 0x7800
#define ST77XX_WHITE  0xFFFF
#define ST77XX_BLUE   0x001F
#define ST77XX_BLACK  0x0000



#define TFT_CS     5
#define TFT_RST    4
#define TFT_DC     2
#define MQ135_PIN  34
#define MQ9_PIN    35 
#define DHTPIN     32
#define DHTTYPE    DHT22

char auth[ ] = "XXXXX";
char ssid[] = "XXXXX";
char pass[] = "XXXXX";

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

unsigned long lastReconnectAttempt = 0;
int reconnectInterval = 10000; 
int gasesMQ135[7]; 
int gasesMQ9[4];  

void setup() {
  Serial.begin(115200);
  tft.init(135, 240); 
  tft.fillScreen(ST77XX_BLUE); 
  dht.begin();
  connectToWifi();
  Blynk.config(auth);
  Blynk.connect(); 

  timer.setInterval(5000L, readSensors); 
  timer.setInterval(6000L, sendToBlynk); 

  Serial.println("Setup complete. Sensors warming up...");
}

void connectToWifi() {
  WiFi.begin(ssid, pass);
  int attempts = 0;
  while (WiFi.status() != WL_CONNECTED && attempts < 30) {
    delay(500); // Short delay to avoid spamming connection attempts
    Serial.print(".");
    attempts++;
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nConnected to WiFi");
  } else {
    Serial.println("\nFailed to connect to WiFi");
  }
}


float convertToPPm(int raw, int gasType) {

  switch (gasType) {
    case 0:
      return raw * (1000.0 / 4095.0); 
    case 1:
      return raw * (10000.0 / 4095.0); 
    case 2:
      return raw * (10000.0 / 4095.0); 
    case 3:
      return raw * (10000.0 / 4095.0); 
    case 4:
      return raw * (10.0 / 4095.0); 
    case 5:
      return raw * (10.0 / 4095.0); 
    default:
      return raw / 10.0; 
  }
}


int calculateAQI(float concentration, float concLow, float concHigh, int indexLow, int indexHigh) {
  if (concentration < concLow) concentration = concLow;
  return ((indexHigh - indexLow) / (concHigh - concLow)) * (concentration - concLow) + indexLow;
}

int getAQI() {
  float CO_ppm = convertToPPm(gasesMQ9[0], 0);
  float NO2_ppm = convertToPPm(gasesMQ135[3], 4);
  float O3_ppm = convertToPPm(gasesMQ135[4], 5);

  Serial.print("CO ppm: ");
  Serial.println(CO_ppm);
  Serial.print("NO2 ppm: ");
  Serial.println(NO2_ppm);
  Serial.print("O3 ppm: ");
  Serial.println(O3_ppm);


  int AQI_CO = calculateAQI(CO_ppm, 0.0, 50.0, 0, 50);   
  int AQI_NO2 = calculateAQI(NO2_ppm, 0.0, 1.0, 0, 200); 
  int AQI_O3 = calculateAQI(O3_ppm, 0.0, 1.0, 0, 200);  
  int AQI = max(max(AQI_CO, AQI_NO2), AQI_O3);

  Serial.print("AQI_CO: ");
  Serial.println(AQI_CO);
  Serial.print("AQI_NO2: ");
  Serial.println(AQI_NO2);
  Serial.print("AQI_O3: ");
  Serial.println(AQI_O3);
  Serial.print("Max AQI: ");
  Serial.println(AQI);

  return AQI;
}

void displayAQI(int AQI) {
  uint16_t bgColor;
  uint16_t textColor = ST77XX_WHITE;
  String aqiText;

  if (AQI <= 50) {
    bgColor = ST77XX_GREEN;
    aqiText = "Good";
  } else if (AQI <= 100) {
    bgColor = ST77XX_YELLOW;
    aqiText = "Moderate";
    textColor = ST77XX_BLACK; 
  } else if (AQI <= 150) {
    bgColor = ST77XX_ORANGE;
    aqiText = "Unhealthy for Sensitive Groups";
  } else if (AQI <= 200) {
    bgColor = ST77XX_RED;
    aqiText = "Unhealthy";
  } else if (AQI <= 300) {
    bgColor = ST77XX_PURPLE;
    aqiText = "Very Unhealthy";
  } else {
    bgColor = ST77XX_MAROON;
    aqiText = "Hazardous";
  }

  tft.fillRect(0, 190, 135, 60, bgColor); 
  tft.setCursor(0, 200);
  tft.setTextColor(textColor);
  tft.setTextSize(1);
  tft.println("AQI: " + String(AQI));
  tft.setCursor(0, 210);
  tft.println(aqiText);
}

void displaySensorData() {
  tft.fillRect(0, 0, 135, 200, ST77XX_BLUE); 
  tft.setTextColor(ST77XX_WHITE);
  tft.setTextSize(1);
  int y = 0;
  tft.setCursor(0, y);
  tft.println("*Benzene: " + String(convertToPPm(gasesMQ135[0], 0)) + " ppm"); 
  y += 10;
  tft.setCursor(0, y);
  tft.println("*Toluene: " + String(convertToPPm(gasesMQ135[5], 0)) + " ppm"); 
  y += 10;
  tft.setCursor(0, y);
  tft.println("*Xylenes: " + String(convertToPPm(gasesMQ135[6], 0)) + " ppm"); 
  y += 10;
  tft.setCursor(0, y);
  tft.println("*Formaldehyde: " + String(convertToPPm(gasesMQ135[1], 0)) + " ppm");
  y += 10;
  tft.setCursor(0, y);
  tft.println("---------------------");
  y += 10;
  tft.setCursor(0, y);
  tft.println("CO: " + String(convertToPPm(gasesMQ9[0], 0)) + " ppm"); 
  y += 10;
  tft.setCursor(0, y);
  tft.println("Ammonia: " + String(convertToPPm(gasesMQ135[2], 0)) + " ppm");
  y += 10;
  tft.setCursor(0, y);
  tft.println("NO2: " + String(convertToPPm(gasesMQ135[3], 4)) + " ppm"); 
  y += 17;
  tft.setCursor(0, y);
  tft.println("Ozone: " + String(convertToPPm(gasesMQ135[4], 5)) + " ppm"); 
  y += 10;
  tft.setCursor(0, y);
  tft.println("Methane: " + String(convertToPPm(gasesMQ9[1], 1)) + " ppm"); 
  y += 10;
  tft.setCursor(0, y);
  tft.println("LPG: " + String(convertToPPm(gasesMQ9[2], 2)) + " ppm"); 
  y += 17;
  tft.setCursor(0, y);
  tft.println("Hydrogen: " + String(convertToPPm(gasesMQ9[3], 3)) + " ppm"); 
  y += 10;
  tft.setCursor(0, y);
  tft.println("CO2: " + String(convertToPPm(gasesMQ135[4], 5)) + " ppm"); CO2 reading from MQ135
  y += 10;
  tft.setCursor(0, y);
  tft.println("Nitric Oxide: " + String(convertToPPm(gasesMQ135[3], 4)) + " ppm"); 
  y += 10;
  tft.setCursor(0, y);
  tft.println("---------------------");
  float temp = dht.readTemperature();
  float humidity = dht.readHumidity();
  y += 13;
  tft.setCursor(0, y);
  tft.println("Temp: " + String(temp) + " C");
  y += 10;
  tft.setCursor(0, y);
  tft.println("Humidity: " + String(humidity) + "%");
  y += 20;
  tft.setCursor(0, y);
  int AQI = getAQI();
  displayAQI(AQI);
}

void readSensors() {
  int rawMQ135 = analogRead(MQ135_PIN);
  int rawMQ9 = analogRead(MQ9_PIN);

  Serial.print("Raw MQ135: ");
  Serial.println(rawMQ135);
  Serial.print("Raw MQ9: ");
  Serial.println(rawMQ9);

  for (int i = 0; i < 7; i++) {
    gasesMQ135[i] = rawMQ135 / (i + 1); 
  }
  for (int j = 0; j < 4; j++) {
    gasesMQ9[j] = rawMQ9 / (j + 1);
  }
  
  displaySensorData();
}

> Blockquote
void sendToBlynk() {
  Blynk.virtualWrite(V0, convertToPPm(gasesMQ135[0], 0)); 
  Blynk.virtualWrite(V1, convertToPPm(gasesMQ9[0], 0));  
  Blynk.virtualWrite(V2, convertToPPm(gasesMQ135[1], 0)); 
  Blynk.virtualWrite(V3, convertToPPm(gasesMQ135[2], 0)); 
  Blynk.virtualWrite(V4, convertToPPm(gasesMQ135[3], 4)); 
  Blynk.virtualWrite(V5, convertToPPm(gasesMQ135[4], 5)); 
  Blynk.virtualWrite(V6, convertToPPm(gasesMQ135[5], 0));
  Blynk.virtualWrite(V7, convertToPPm(gasesMQ135[6], 0)); 
  Blynk.virtualWrite(V8, convertToPPm(gasesMQ9[1], 1)); 
  Blynk.virtualWrite(V9, convertToPPm(gasesMQ9[2], 2));  
  Blynk.virtualWrite(V10, convertToPPm(gasesMQ9[3], 3));  
  Blynk.virtualWrite(V11, convertToPPm(gasesMQ135[4], 5));
  Blynk.virtualWrite(V12, convertToPPm(gasesMQ135[3], 4));
  Blynk.virtualWrite(V13, dht.readTemperature());        
  Blynk.virtualWrite(V14, dht.readHumidity());            
  Blynk.virtualWrite(V15, getAQI());                      
}
void loop() {
  Blynk.run(); // Allow Blynk to process events
  
  if (WiFi.status() != WL_CONNECTED) {
    connectToWifi();
  }
  timer.run(); // Run the Blynk timer
}

@Abdullah91 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.

Thanks for letting me know, Pete. I’ve made the edit to my previous post so the code displays correctly.

You really ought to be doing a Blynk.connected() logical test and only expecting Blynk.run() if true and doing a Blynk.connect() if false

You’re doing 16 consecutive Blynk.virtualWrites, but you’re only supposed to do a maximum of 10 per second.
I don’t really understand why you aren’t sending each sensor separately once the reading is obtained, and splitting the readings into several timed functions if necessary.

Pete.

1 Like

Thanks a lot for the reply. I really appreciate you pointing out the code sections and suggesting the changes.

I’m happy to report that after adjust the code you mentioned. The Blynk connection issue seems to be resolved. The device is staying connected now.

I truly appreciate your time and assistance.

Thanks again, Pete

1 Like