#include <WiFi.h> // Including WiFi.h library to handle WiFi related tasks
#include <HTTPClient.h> // Including HTTPClient.h library to use HTTP requests
#include "DHT.h" // Including DHT library for temperature and humidity sensor
/* ______________________________________________________
configuration wifi et connection whatsapp
__________________________________________________________*/
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST"; // Add your WiFi SSID
const char* password = ""; // Add your WiFi password
// WhatsApp API credentials
String apiKey = // Add your Token number that the bot has sent you on WhatsApp
String phone_number = // Add your WhatsApp registered phone number
// URL String to store the final generated URL
String url;
/*___________________________________________________
definir les pins aux quelles on relie les capteurs
_____________________________________________________*/
// DHT Sensor
#define DHTPIN 14 // Pin where the DHT11 is connected
#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
// PIR Sensor
#define PIRPIN 15 // Pin where the PIR sensor is connected
// Flame Sensor
#define FLAME_PIN 34 // Pin where the flame sensor is connected
#define FLAME_BUZZER_PIN 25 // Pin where the flame buzzer is connected
// Gas Sensor
#define GAS_PIN 35 // Pin where the gas sensor is connected
#define GAS_BUZZER_PIN 26 // Pin where the gas buzzer is connected
// Light Sensor (Photoresistor)
#define LIGHT_PIN 32 // Pin where the photoresistor is connected
#define LIGHT_LED_PIN 27
void setup() {
Serial.begin(115200);
// Initialize WiFi connection
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
unsigned long startAttemptTime = millis();
// Timeout for WiFi connection attempt (e.g., 10 seconds)
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 10000) {
delay(500);
Serial.print(".");
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect to WiFi");
return;
}
Serial.println();
Serial.println("Connected to the WiFi network");
// Initialize DHT sensor
dht.begin();
// Initialize PIR sensor
pinMode(PIRPIN, INPUT);
// Initialize flame sensor and buzzer
pinMode(FLAME_PIN, INPUT);
pinMode(FLAME_BUZZER_PIN, OUTPUT);
// Initialize gas sensor and buzzer
pinMode(GAS_PIN, INPUT);
pinMode(GAS_BUZZER_PIN, OUTPUT);
// Initialize light sensor and LED
pinMode(LIGHT_PIN, INPUT);
pinMode(LIGHT_LED_PIN, OUTPUT);
// Send an initial message
message_to_whatsapp("ESP32 is connected and ready.");
}
void loop() {
// Read sensor data
float temperature = dht.readTemperature(); // Read temperature as Celsius
float humidity = dht.readHumidity(); // Read humidity
int pirValue = digitalRead(PIRPIN); // Read PIR sensor value
int flameValue = analogRead(FLAME_PIN); // Read flame sensor value
int gasValue = analogRead(GAS_PIN); // Read gas sensor value
int lightValue = analogRead(LIGHT_PIN); // Read light sensor value
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Handle flame sensor
if (flameValue < 100) { // Adjust the threshold as necessary
digitalWrite(FLAME_BUZZER_PIN, HIGH);
Serial.println("Flame detected!");
} else {
digitalWrite(FLAME_BUZZER_PIN, LOW);
}
// Handle gas sensor
if (gasValue > 300) { // Adjust the threshold as necessary
digitalWrite(GAS_BUZZER_PIN, HIGH);
Serial.println("Gas detected!");
} else {
digitalWrite(GAS_BUZZER_PIN, LOW);
}
// Handle light sensor
if (lightValue < 500) { // Adjust the threshold as necessary
digitalWrite(LIGHT_LED_PIN, HIGH);
Serial.println("Low light detected!");
} else {
digitalWrite(LIGHT_LED_PIN, LOW);
}
// Create message
String message = "Temperature: " + String(temperature) + "°C\n";
message += "Humidity: " + String(humidity) + "%\n";
message += "Motion detected: " + String(pirValue == HIGH ? "Yes" : "No") + "\n";
message += "Flame detected: " + String(flameValue < 100 ? "Yes" : "No") + "\n";
message += "Gas detected: " + String(gasValue > 300 ? "Yes" : "No") + "\n";
message += "Low light detected: " + String(lightValue < 500 ? "Yes" : "No");
// Send message to WhatsApp
message_to_whatsapp(message);
// Wait for 10 minutes before sending the next reading
delay(60000); // 60000 milliseconds = 1 minutes
}
void message_to_whatsapp(String message) {
// Adding all number, your API key, your message into one complete URL
url = "https://api.callmebot.com/whatsapp.php?phone=" + phone_number + "&apikey=" + apiKey + "&text=" + urlencode(message);
postData(); // Calling postData to run the above-generated URL once so that you will receive a message
}
void postData() {
int httpCode; // Variable used to get the response HTTP code after calling API
HTTPClient http; // Declare object of class HTTPClient
http.begin(url); // Begin the HTTPClient object with generated URL
httpCode = http.POST(""); // Finally, POST the URL with this function and it will store the HTTP code
if (httpCode == 200) { // Check if the response HTTP code is 200
Serial.println("Sent ok."); // Print message sent ok message
} else { // If response HTTP code is not 200, it means there is some error
Serial.println("Error."); // Print error message
}
http.end(); // After calling API, end the HTTP client object
}
String urlencode(String str) { // Function used for encoding the URL
String encodedString = "";
char c;
char code0;
char code1;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == ' ') {
encodedString += '+';
} else if (isalnum(c)) {
encodedString += c;
} else {
code1 = (c & 0xf) + '0';
if ((c & 0xf) > 9) {
code1 = (c & 0xf) - 10 + 'A';
}
c = (c >> 4) & 0xf;
code0 = c + '0';
if (c > 9) {
code0 = c - 10 + 'A';
}
encodedString += '%';
encodedString += code0;
encodedString += code1;
}
}
return encodedString;
}