#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#include "ThingSpeak.h"
// Wi-Fi Credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// ThingSpeak Credentials
const char* THINGSPEAK_API_KEY = "D88QSHNQJZSM9SZI";
unsigned long THINGSPEAK_CHANNEL_ID = 2838022;
// Twilio Credentials (for SMS Alerts)
const char* TWILIO_SID = "ACfa444b0fc581e0e3c17088d0f406c3ce";
const char* TWILIO_AUTH_TOKEN = "5a2d7e830d8765218864167c37c78238";
const char* TWILIO_PHONE_NUMBER = "+16402303109";
const char* YOUR_PHONE_NUMBER = "+916369474178";
// Sensor and Buzzer Setup
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define DOOR_SENSOR_PIN 5 // Push button for fridge door
#define BUZZER_PIN 19 // Buzzer connected to GPIO19
bool fridgeDoorOpen = false;
unsigned long doorOpenStartTime = 0;
const unsigned long DOOR_OPEN_THRESHOLD = 60000; // 60 sec
WiFiClient client;
// Alert tracking variables
bool doorAlertSent = false;
bool tempAlertSent = false;
bool humidityAlertSent = false;
unsigned long lastDoorAlertTime = 0;
unsigned long lastTempAlertTime = 0;
unsigned long lastHumidityAlertTime = 0;
const unsigned long ALERT_COOLDOWN = 600000; // 10 minutes cooldown
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi!");
dht.begin();
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
ThingSpeak.begin(client);
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
bool doorStatus = digitalRead(DOOR_SENSOR_PIN) == LOW;
if (doorStatus) {
if (!fridgeDoorOpen) {
fridgeDoorOpen = true;
doorOpenStartTime = millis();
}
} else {
fridgeDoorOpen = false;
}
sendToThingSpeak(temperature, humidity, fridgeDoorOpen);
handleAlerts(temperature, humidity);
delay(30000);
}
void sendToThingSpeak(float temperature, float humidity, bool doorStatus) {
if (WiFi.status() == WL_CONNECTED) {
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
ThingSpeak.setField(3, doorStatus ? 1 : 0);
int responseCode = ThingSpeak.writeFields(THINGSPEAK_CHANNEL_ID, THINGSPEAK_API_KEY);
if (responseCode == 200) {
Serial.println("Data sent to ThingSpeak!");
} else {
Serial.println("ThingSpeak Error: " + String(responseCode));
}
}
}
void handleAlerts(float temperature, float humidity) {
unsigned long currentMillis = millis();
bool alertActive = false;
// Fridge Door Alert
if (fridgeDoorOpen && (currentMillis - doorOpenStartTime > DOOR_OPEN_THRESHOLD)) {
if (!doorAlertSent || (currentMillis - lastDoorAlertTime > ALERT_COOLDOWN)) {
sendSMSAlert("Fridge Door Open Too Long! Close it ASAP!");
doorAlertSent = true;
lastDoorAlertTime = currentMillis;
}
alertActive = true;
} else if (doorAlertSent) {
sendSMSAlert("Fridge Door Issue Resolved.");
doorAlertSent = false;
}
// Temperature Alert
if (temperature > 10) {
if (!tempAlertSent || (currentMillis - lastTempAlertTime > ALERT_COOLDOWN)) {
sendSMSAlert("Temperature Alert! Fridge is warming: " + String(temperature) + "°C");
tempAlertSent = true;
lastTempAlertTime = currentMillis;
}
alertActive = true;
} else if (tempAlertSent) {
sendSMSAlert("Temperature Back to Normal.");
tempAlertSent = false;
}
// Humidity Alert
if (humidity > 80) {
if (!humidityAlertSent || (currentMillis - lastHumidityAlertTime > ALERT_COOLDOWN)) {
sendSMSAlert("Humidity Alert! Too much moisture: " + String(humidity) + "%");
humidityAlertSent = true;
lastHumidityAlertTime = currentMillis;
}
alertActive = true;
} else if (humidityAlertSent) {
sendSMSAlert("Humidity Back to Normal.");
humidityAlertSent = false;
}
// Control Buzzer
if (alertActive) {
digitalWrite(BUZZER_PIN, HIGH); // Buzzer ON
} else {
digitalWrite(BUZZER_PIN, LOW); // Buzzer OFF
}
}
void sendSMSAlert(String message) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://api.twilio.com/2010-04-01/Accounts/" + String(TWILIO_SID) + "/Messages.json";
String postData = "To=" + String(YOUR_PHONE_NUMBER) +
"&From=" + String(TWILIO_PHONE_NUMBER) +
"&Body=" + message;
http.begin(url);
http.setAuthorization(TWILIO_SID, TWILIO_AUTH_TOKEN);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int responseCode = http.POST(postData);
if (responseCode > 0) {
Serial.println("SMS Sent: " + message);
} else {
Serial.println("SMS Failed!");
}
http.end();
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4