#include <WiFi.h>
#include <HTTPClient.h>
#include "gas_sensor_chip.h"
// Defining pins in ESP32
#define FLAME_SENSOR_PIN 5
#define GAS_SENSOR_PIN 34
#define LED_PIN 2
#define BUZZER_PIN 4
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThinkSpeak key and ID settings
const char* writeAPIKey = "RAJ7W6B3G23BD3A9";
const char* readAPIKey = "IMIFBX7FT9E5B4VD";
const char* server = "http://api.thingspeak.com";
const int channelID = 2811928;
// Twilio credentials
const char* accountSID = "ACad1af54adb440a915506228c2b050eeb";
const char* authToken = "712e76d434d0c5f813a1bcd8d3a3bb3b";
const char* twilioNumber = "+15677087640"; // Twilio phone number
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);
// **Initialize the custom gas sensor chip**
gasSensorChip_init(); // Custom initialization function for your chip
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() {
// **Read sensor values from custom chip**
int flameStatus = digitalRead(FLAME_SENSOR_PIN);
// **Use custom function to read gas level from the custom chip**
int gasLevel = gasSensorChip_read(); // Use the custom chip's read function
// Alert is being generated if the gas sensor exceeds the threshold value
if (flameStatus == LOW || gasLevel > 500) {
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
// Send SMS Alert
sendSMSAlert("Alert: Fire or Gas Leak detected! Take immediate action.");
} else {
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
// Sends sensor data to ThinkSpeak
sendDataToThinkSpeak(flameStatus, gasLevel);
// Read data from ThinkSpeak if required
readDataFromThinkSpeak();
// Delay for 10 seconds before the next reading
delay(10000);
}
void sendDataToThinkSpeak(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 ThinkSpeak successfully.");
} else {
Serial.print("Error sending data to ThinkSpeak: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Wi-Fi not connected. Data not sent to ThinkSpeak.");
}
}
void readDataFromThinkSpeak() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(server) + "/channels/" + String(channelID) + "/feeds.json?api_key=" + readAPIKey + "&results=1";
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
Serial.println("Data retrieved from ThinkSpeak:");
Serial.println(payload);
} else {
Serial.print("Error reading data from ThinkSpeak: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Wi-Fi not connected. Data not retrieved from ThinkSpeak.");
}
}
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.");
}
}