Board : WeMos D1 R1
Problem : The Red LED is always on, Yellow and Green LED is blinking
HELP ME TO FIGURE IT OUT TRYING FOR THE WHOLE DAY STILL SAME !!
Coding : #define BLYNK_TEMPLATE_ID "TMPL6H2vkmbht"
#define BLYNK_TEMPLATE_NAME "GAS LEAKAGE"
#define BLYNK_AUTH_TOKEN "6sbdPPTorwsTHJnlA9KGLoUIIkeknC8w"
#define BLYNK_PRINT Serial
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "UrbanSanctuaries"; // Type your WiFi name
char pass[] = "UrbanSancA3211"; // Type your WiFi password
int sensorPin = A0;
int buzzer = D6;
int G_led = D1;
int Y_led = D2;
int R_led = D5;
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int total = 0;
int average = 0;
int mappedMin = 0;
int mappedMax = 99;
int thresholdGreen = 20;
int thresholdYellow = 30;
int thresholdRed = 45;
bool systemEnabled = true;
bool mediumLeakAlertSent = false;
bool highLeakAlertSent = false;
BlynkTimer timer;
void setup() {
pinMode(sensorPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(R_led, OUTPUT);
pinMode(G_led, OUTPUT);
pinMode(Y_led, OUTPUT);
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" WELCOME To ");
lcd.setCursor(0, 1);
lcd.print(" GAS Detector ");
delay(2000);
lcd.clear();
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(2500L, readGasSensor);
// Initialize the readings array
for (int i = 0; i < numReadings; i++) {
readings[i] = 0;
}
}
void loop() {
Blynk.run();
timer.run();
}
void readGasSensor() {
if (!systemEnabled) {
digitalWrite(R_led, LOW);
digitalWrite(Y_led, LOW);
digitalWrite(G_led, LOW);
digitalWrite(buzzer, LOW);
lcd.setCursor(0, 1);
lcd.print("System Disabled ");
Serial.println("System Disabled");
return;
}
// Calculate the moving average
total -= readings[readIndex];
readings[readIndex] = analogRead(sensorPin);
total += readings[readIndex];
readIndex = (readIndex + 1) % numReadings;
average = total / numReadings;
int mappedReading = map(average, 0, 1023, mappedMin * 100, mappedMax * 100);
float gasLevel = (float)mappedReading / 100.0;
lcd.setCursor(0, 0);
lcd.print("Gas Level: ");
lcd.print(gasLevel, 1);
lcd.print(" ");
Blynk.virtualWrite(V0, gasLevel);
// Debugging information
Serial.print("Mapped Reading: ");
Serial.println(mappedReading);
Serial.print("Gas Level: ");
Serial.println(gasLevel);
if (mappedReading >= thresholdRed * 100) {
digitalWrite(R_led, HIGH);
digitalWrite(Y_led, LOW);
digitalWrite(G_led, LOW);
lcd.setCursor(0, 1);
lcd.print("....Alert....!!!");
digitalWrite(buzzer, HIGH);
if (!highLeakAlertSent) {
Blynk.logEvent("high_gas_leak", "High Gas Leak Detected, Evacuate Immediately!");
highLeakAlertSent = true;
}
Serial.println("High Gas Alert!");
} else if (mappedReading >= thresholdYellow * 100) {
digitalWrite(R_led, LOW);
digitalWrite(Y_led, HIGH);
digitalWrite(G_led, LOW);
lcd.setCursor(0, 1);
lcd.print(".....Warning.....");
digitalWrite(buzzer, HIGH);
if (!mediumLeakAlertSent) {
Blynk.logEvent("medium_gas_leak", "Medium Gas Leak Detected");
mediumLeakAlertSent = true;
}
Serial.println("Medium Gas Alert!");
} else {
digitalWrite(R_led, LOW);
digitalWrite(Y_led, LOW);
digitalWrite(G_led, HIGH);
lcd.setCursor(0, 1);
lcd.print(".....Normal.....");
digitalWrite(buzzer, LOW);
mediumLeakAlertSent = false;
highLeakAlertSent = false;
Serial.println("Gas Level Normal");
}
delay(100);
}
BLYNK_WRITE(V4) {
int switchState = param.asInt();
if (switchState == 1) {
systemEnabled = true;
} else {
systemEnabled = false;
}
// Debugging information
Serial.print("System Enabled: ");
Serial.println(systemEnabled ? "Yes" : "No");
}