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:
- Changed the gas sensor.
- Replaced the ESP8266 node.
- Provided separate power supplies to both the node and sensor (both are commonly grounded).
- Reconfigured the device.
- 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).