// Defining the information to connect with Blynk cloud
#define BLYNK_TEMPLATE_ID "TMPL6IQSNotu4"
#define BLYNK_TEMPLATE_NAME "IR Flame Sensor and MQ2"
#define BLYNK_AUTH_TOKEN "ucHeGtuZdPY4uW3Pp3Nw3L1KQi2zg9fx"
// Import all the libraries
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Authentification information with Blynk and Wokwi WiFi
char auth[] = "ucHeGtuZdPY4uW3Pp3Nw3L1KQi2zg9fx";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Define pin variables
const int mq2Pin = 33;
const int irflamePin = 13;
const int buzzerPin = 16;
const int alertLampPin = 4;
// Create variables with initial value
int gasValue = 0;
bool buzzerState = 0;
bool alertlampState = 0;
bool irflameState = 0;
String gasCondition = "";
String fireCondition = "";
// This function creates the timer object. It's part of Blynk library
BlynkTimer timer;
void sendSensor() {
// This function describes data sensor send to Blynk cloud each timer tick
// Condition for gas concetrate high/danger or close fire detected
if (gasValue > 40 || irflameState == 0) {
digitalWrite(buzzerPin, HIGH);
tone(buzzerPin, 200);
digitalWrite(alertLampPin, HIGH);
buzzerState = 1;
alertlampState = 1;
}
else {
digitalWrite(buzzerPin, LOW);
noTone(buzzerPin);
digitalWrite(alertLampPin, LOW);
buzzerState = 0;
alertlampState = 0;
}
// Condition for gas concetrate and fire detected notification in string
gasCondition = (gasValue > 40) ? "Warning! Gas Concetrate High" : "Gas Concetrate Low";
fireCondition = (irflameState == 0) ? "Warning! close Fire Detected" : "No Fire Detected";
// Print to serial
Serial.print("Gas value = ");
Serial.print(gasValue);
Serial.print(" ppm");
Serial.print(" || IR Flame state = ");
Serial.println(irflameState);
Serial.print(gasCondition);
Serial.print(" || ");
Serial.println(fireCondition);
Blynk.virtualWrite(V0, gasValue);
Blynk.virtualWrite(V1, alertlampState);
Blynk.virtualWrite(V2, buzzerState);
Blynk.virtualWrite(V3, fireCondition);
Blynk.virtualWrite(V4, gasCondition);
}
void setup() {
Serial.begin(115200);
// Configure used pin
pinMode(mq2Pin, INPUT); // Input pin for MQ2 sensor
pinMode(irflamePin, INPUT); // Input pin for IR Flame sensor
pinMode(buzzerPin, OUTPUT); // Output pin for buzzer
pinMode(alertLampPin, OUTPUT); // Output pin for alert lamp/ led
Blynk.begin(auth, ssid, pass); // Connect to Blynk cloud
// Setting interval to send data to Blynk Cloud to 1000ms (1 second).
timer.setInterval(1000L, sendSensor);
}
void loop() {
// Read in analog value. Range 0-4095 mapping to 0-100 --> if <=40 gas concetrate low/good (ppm), else >40 gas concetrate high/danger (ppm)
gasValue = map(analogRead(mq2Pin), 0, 4095, 0, 100);
// Read in digital value. Range 0-1 --> if 0/LOW close fire detected, else 1/HIGH no close fire detected
// In Wokwi simulation range is 0-100 lux --> if >=100 (0/LOW) close fire detected, else <100 (1/HIGH) no close fire detected
irflameState = digitalRead(irflamePin);
// Runs all Blynk stuff
Blynk.run();
// runs BlynkTimer
timer.run();
tone(buzzerPin, 200);
}