#include <WiFi.h>
#include <HTTPClient.h>
// Defining pins in ESP32
#define FLAME_SENSOR_PIN 5
#define GAS_SENSOR_PIN 34 // Analog pin for gas sensor
#define LED_PIN 2
#define BUZZER_PIN 4
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak settings
const char* writeAPIKey = "RAJ7W6B3G23BD3A9";
const char* server = "http://api.thingspeak.com";
// Twilio credentials
const char* accountSID = "ACad1af54adb440a915506228c2b050eeb";
const char* authToken = "712e76d434d0c5f813a1bcd8d3a3bb3b";
const char* twilioNumber = "+15677087640";
const char* userPhoneNumber = "+919385967737";
void setup() {
Serial.begin(115200);
pinMode(FLAME_SENSOR_PIN, INPUT_PULLUP);
pinMode(GAS_SENSOR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
WiFi.begin(ssid, password);
Serial.println("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Attempting to connect to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");
}
void loop() {
// Reading sensor values
int flameStatus = digitalRead(FLAME_SENSOR_PIN);
int gasLevel = analogRead(GAS_SENSOR_PIN); // Directly read from the custom gas sensor
Serial.print("Flame Status: ");
Serial.println(flameStatus);
Serial.print("Gas Level: ");
Serial.println(gasLevel);
// Trigger alerts if gas leak is detected based on threshold value
if (flameStatus == LOW || gasLevel > 500) {
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
sendSMSAlert(" Alert: Fire or Gas Leak detected! Take immediate action.");
} else {
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
// Send data to ThingSpeak
sendDataToThingSpeak(flameStatus, gasLevel);
delay(10000); // Delay for 10 seconds before the next reading
}
void sendDataToThingSpeak(int flame, int gas) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "/update?api_key=" + writeAPIKey + "&field1=" + flame + "&field2=" + gas;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingSpeak successfully.");
} else {
Serial.print("Error sending data to ThingSpeak: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Wi-Fi not connected. Data not sent to ThingSpeak.");
}
}
void sendSMSAlert(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://api.twilio.com/2010-04-01/Accounts/" + String(accountSID) + "/Messages.json";
String postData = "To=" + String(userPhoneNumber) + "&From=" + String(twilioNumber) + "&Body=" + message;
http.begin(url);
http.setAuthorization(accountSID, authToken);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
Serial.println(" SMS Alert Sent Successfully!");
} else {
Serial.print(" Error sending SMS: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Wi-Fi not connected. Unable to send SMS.");
}
}