Hi everyone, i need some help, i’m doing my ioT project, which is a gas leak detector that uses the ESP8266 microcontroller(nodeMCU) and the Blynk IoT platform to notify the user of potential gas leaks. However, i cannot fix this error. I really need your help. Thank you!
You appear to be misunderstanding the serial monitor message.
This…
tells you that the device is successfully connected to the Blynk server and that the ping time from the device to the Blynk server is 84ms.
Does the device show as Online in the web console?
You appear to be missing the Template ID and Template Name lines of code from your sketch.
You also have a logic issue with your log event code, as you will quickly exceed the 100 events per 24 hour period limit. You should read this for more info…
I suspect that you’ve somehow managed to create a device in the app that is different from the device in the web console, or that you have created two different Blynk accounts and are logged-in to a different one in the app.
You’d need to provide significantly more information and probably screenshots of both the web console and the app if you wnat assistance unpicking that part of the problem.
Not really. In the web console Device > Device info screen you’ll see three lines of firmware configuration code.
These should be copied and pasted at the VERY TOP of your sketch.
You should then delete this line:
and replace this line:
with: Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
You’ve introduced a variable called alert_sent bit it’s type and scope are not defined, so the sketch will fail to compile.
When you fix this by declaring it as a global variable of an appropriate type, and setting its initial value to false then you need to include an if test that will check the status of this variable before sending an alert.
[8526] Connecting to blynk.cloud:80
[12841] Ready (ping: 186ms).
Temperature = 2
Temperature = 2
Temperature = 3
Temperature = 2
Temperature = 2
Temperature = 2
The analogRead(Sensor) function reads the value from an analog pin (represented by the Sensor variable), and stores it in the sensor variable. The Serial.println(sensor) function prints the value of sensor to the serial monitor.
sensor = map(sensor, 0, 1024, 0, 100);
The map(sensor, 0, 1024, 0, 100) function maps the sensor value from the range of 0-1024 to the range of 0-100. This is done to make the reading more human-readable and understandable.
if (pinValue == 1) {
if (sensor <= 50) {
digitalWrite(Yellow, HIGH);
digitalWrite(Green, LOW);
digitalWrite(Buzzer, LOW);
The code then checks if the value of pinValue is 1. If it is, then it checks if the sensor value is less than or equal to 50. If it is, then the system assumes that the gas concentration level is normal and turns on the yellow LED and turns off the green LED and the buzzer.
} else if (sensor > 50) {
Blynk.logEvent("gas_leak_detected", String("The gas concentration value is ") + sensor);
bool alert_sent = true;
Serial.println("Blynk Alert Notification sent!");
Serial.println();
digitalWrite(Yellow, LOW);
digitalWrite(Green, HIGH);
digitalWrite(Buzzer, HIGH);
}
If the sensor value is greater than 50, then it assumes that a gas leak has been detected, triggers a notification using the Blynk.logEvent() function, sets the alert_sent flag to true, and turns on the green LED and the buzzer while turning off the yellow LED.
Blynk.virtualWrite(V1, sensor);
The Blynk.virtualWrite(V1, sensor) function writes the sensor value to a virtual pin
(represented by V1 ).
If the pinValue value is not 1, then the system assumes that the gas concentration level is normal, turns off all LEDs and the buzzer, and resets the alert_sent flag to false.
And how does all of this prevent your code from sending a Blynk notification each time the void notification() function is called and the gas sensor level is >50 ?
If you’re calling the function every 10 seconds then after 1000 seconds (just under 17 minutes) of sensor level >50 you will have reached your 24 hour limit for events, and the device will be of no further use during that time.
I modified the code. It will prevents sending a Blynk notification each time the notification() function is called and the gas sensor level is greater than 50.
The code checks if the gasLeakDetected flag is false and if the current sensor value is greater than the previous sensor value plus the threshold.
It also checks the time elapsed since the last notification to ensure that notifications are not sent too frequently.
If all these conditions are met, the code sends a notification and sets the gasLeakDetected flag to true to avoid sending another notification for the same gas leak.
Current code:
#define BLYNK_TEMPLATE_ID "xxxxxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxxxxx"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char wifiSSID[] = "xxxxxxxxxxxx";
char wifiPassword[] = "xxxxxxxxxxxx";
BlynkTimer timer;
int gasConcentration = 0;
bool gasLeakDetected = false;
#define BUZZER_PIN D5
#define GREEN_LED_PIN D6
#define RED_LED_PIN D7
#define GAS_SENSOR_PIN A0
int previousSensorValue = 0; // variable to store the previous sensor value
const int SENSOR_THRESHOLD = 20; // minimum difference between current and previous sensor value to trigger notification
const int NOTIFICATION_INTERVAL = 3600000L; // time interval in milliseconds between notifications (1 hour)
unsigned long lastNotificationTime = 0; // variable to store the time of the last notification
void setup() {
Serial.begin(9600);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(GAS_SENSOR_PIN, INPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, wifiSSID, wifiPassword);
timer.setInterval(NOTIFICATION_INTERVAL, notification);
}
BLYNK_WRITE(V0) {
gasLeakDetected = param.asInt();
}
void notification() {
int sensorValue = analogRead(GAS_SENSOR_PIN);
Serial.print("Gas Concentration = ");
Serial.println(sensorValue);
gasConcentration = map(sensorValue, 0, 1024, 0, 100);
if (gasLeakDetected) {
if (gasConcentration <= 50) {
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
gasLeakDetected = false; // reset the flag if the sensor level falls below the threshold
} else if (gasConcentration > 50 && !gasLeakDetected && gasConcentration >= previousSensorValue + SENSOR_THRESHOLD) {
// send notification if the current sensor value is significantly higher than the previous value
unsigned long currentTime = millis();
if (currentTime - lastNotificationTime >= NOTIFICATION_INTERVAL) {
Blynk.logEvent("gas_leak_detected", String("The gas concentration value is ") + gasConcentration);
bool alertSent = true;
Serial.println("Blynk Alert Notification sent!");
Serial.println();
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
gasLeakDetected = true; // set the flag to true if the sensor level exceeds the threshold for the first time
lastNotificationTime = currentTime; // update the time of the last notification
}
}
previousSensorValue = gasConcentration; // store the current sensor value as the previous value
} else {
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
gasLeakDetected = false; // reset the flag if notifications are turned off
}
}
void loop() {
Blynk.run();
timer.run();
}
That sounds way too complicated and appears to mean that you could have to wait anywhere between 1 and 2 hours before your first gas leak notification is sent.
What’s the point in posting “modified code” that you haven’t even tried to compile, never mind test on your device?
The same question needs asking as before…
and the answer is the same - it doesn’t.
I took quite a lot of time writing the “Events, Notifications and use of “Flag” variables” tutorial. You appear to have plucked some lines of code out of that, and modified them to make the “alert_sent” variable local - so that it’s useless, without actually understanding the principal of how this variable should be used in an if statement to limit the number of notifications sent.
I’d suggest that you re-read that tutorial and try to fully understand it, then apply the principals to your sketch, compile it, test iot, and ask further questions if it doesn’t work as expected.
I’d also recommend changing this…
to a more realistic interval, such as 1000ms rather than 60000ms if you want to build a useable device, as well as fixing the spelling.
I’ve already attempted to verify and compile it. It appears to be able to connect to Blynk, but I am unable to obtain the gas consumption value, which remains zero despite my efforts to trigger it.
Well, the code that you posted wouldn’t compile, because this line of code…
has two issues. It’s missing the semicolon at the end, and it tries to call a non-existent function called notifiaction (note that it’s spelled with an “ac” here, but a “ca” in the actual function name)…
so no, you couldn’t have successfully verified and uploaded this code.
I have no idea, I’m not familiar with this sensor or the libraries that you’re attempting to use.
The sensible approach is to get your system working without Blynk, then add the Blynk code later.
It can connect to wifi and also to the blynk cloud but the serial monitor did not send any notification. Maybe because the smoke value is not above the smoke threshold
The gas concentration level is still 0 even though I try to trigger it. I don’t know which part is wrong, maybe the coding or I just can’t trigger the mq2 sensor(?)
Also, you appear to be building a gas leak detector, but rather than reading the LPG ppm youre reading smoke particle ppm, the referring to this as a gas leak then and also mentioning temperature, which isn’t being monitored by this device at all.