Nodemcu with blynk constantly keeps connecting and disconnecting and not send data to blynk

Hi everyone,

This is my first project where I’ve used an ESP8266 to monitor gas levels and send alerts via email and push notifications if the value exceeds a certain volume. I’ve implemented this using BlynkEdgent. Initially, the code was working fine, but recently, the node connected to Blynk started restarting at short intervals and stopped sending readings.

Here are the troubleshooting steps I’ve tried:

  1. Changed the gas sensor.
  2. Replaced the ESP8266 node.
  3. Provided separate power supplies to both the node and sensor (both are commonly grounded).
  4. Reconfigured the device.
  5. Adjusted delays with BlynkTimer.

Unfortunately, none of the above solutions resolved my issue.

Here’s my code:

#define BLYNK_TEMPLATE_ID "TMPL3DqEbXn9A"
#define BLYNK_TEMPLATE_NAME "test"
#define BLYNK_FIRMWARE_VERSION "0.1.0"

#define BLYNK_PRINT Serial

#include "BlynkEdgent.h"

#define MQ2 A0  // MQ2 gas sensor pin

// Number of readings to average
#define NUM_READINGS 10

BlynkTimer timer; // Create a BlynkTimer object

void sensorDataSend() {
  // Array to store gas level readings
  int gasReadings[NUM_READINGS];

  // Take multiple readings and store them in the array
  for (int i = 0; i < NUM_READINGS; i++) {
    gasReadings[i] = analogRead(MQ2);
    delay(10); // Delay between readings
  }

  // Calculate the average gas level
  int gasLevelTotal = 0;
  for (int i = 0; i < NUM_READINGS; i++) {
    gasLevelTotal += gasReadings[i];
  }
  int gasLevel = gasLevelTotal / NUM_READINGS;

  // Map the gas level to a percentage (0 to 100)
  // Map the range 0-1023 to 0-60 instead of 0-100
  gasLevel = map(gasLevel, 0, 1023, 0, 60);
  
  // Send gas level to Blynk
  Blynk.virtualWrite(V1, gasLevel); // Send gas level to pin V1
  
  // Display gas level on serial monitor
  Serial.print("Gas Level: ");
  Serial.println(gasLevel);
  
  // Check if gas level exceeds 50
  if (gasLevel > 50) {
    // Trigger gas leakage event
    Blynk.logEvent("gas", "Gas Leakage is detected");
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(MQ2, INPUT); // Set MQ2 pin as input
  BlynkEdgent.begin();
  
  // Setup timer to call sensorDataSend() function every 1000 milliseconds
  timer.setInterval(1000L, sensorDataSend);
}

void loop() {
  BlynkEdgent.run();
  timer.run(); // Run the timer to execute scheduled tasks
}

In the serial monitor, I’m not seeing any errors or unusual behavior. Any suggestions or insights would be greatly appreciated. Thanks!

   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


----------------------------------------------------
 Device:    Blynk test-3D47
 Firmware:  0.1.0 (build May  7 2024 11:26:32)
 Token:     YGgY - •••• - •••• - ••••
 Platform:  ESP8266 @ 80MHz
 Boot ver:  31
 SDK:       2.2.2-dev(38a443e)
 ESP Core:  3.1.2
 Flash:     4096K
 Free mem:  30808
----------------------------------------------------

>[7670] Using Dynamic IP: 192.168.45.79
Free heap memory: 29528
Gas Level: 0
[10084] Current time: Tue May  7 05:59:20 2024
[10084] Connecting to blynk.cloud:443
[11205] Ready (ping: 12ms).
⸮⸮[295] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.3.2 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


----------------------------------------------------
 Device:    Blynk test-3D47
 Firmware:  0.1.0 (build May  7 2024 11:26:32)
 Token:     YGgY - •••• - •••• - ••••
 Platform:  ESP8266 @ 80MHz
 Boot ver:  31
 SDK:       2.2.2-dev(38a443e)
 ESP Core:  3.1.2
 Flash:     4096K
 Free mem:  30808
----------------------------------------------------

>[10192] Using Dynamic IP: 192.168.45.79
Free heap memory: 29424
Gas Level: 0
[28025] Current time: Tue May  7 05:59:56 2024
[28026] Connecting to blynk.cloud:443
[29138] Ready (ping: 12ms).

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

Also, please don’t post screenshots of your serial output. Instead copy the text and paste it, also with triple backticks at the beginning and end.

Pete.

id i try to upload serial monitor in test An error occurred: Sorry, new users can only put 2 links in a post. What should i do

That’s because you haven’t added the triple backticks around your code properly. They need to be on a separate line, and as I said before, you also need triple backticks around your serial monitor text.

Pete.

i try to upload serial monitor in text An error occurred: Sorry, new users can only put 2 links in a post. What should i do

As I said already…

Pete.

Thank you for the guidance.

Okay, you clearly haven’t grasped the concept of triple backticks. On this occasion I’ve edited your post to put the required triple backticks in the correct places.
Please go back into your post (using the edit option) and see where the triple backticks are, so that you fully understand how to do this correctly in future.

What happens if you downgrade your ESP8266 core to 2.7.4 ?

Pete.