// Fridge Monitoring System (ESP32 + ThingSpeak + Twilio)
// DHT22 reads temperature & humidity
// Push button detects fridge door status
// Data is sent to ThingSpeak for remote monitoring
// Twilio sends SMS alerts for critical conditions.
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#include "ThingSpeak.h"
// Wi-Fi Credentials (Replace with your Wokwi Wi-Fi)
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"; // Twilio sender number
const char* YOUR_PHONE_NUMBER = "+916369474178"; // Your phone to receive SMS
// Sensor Setup
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define DOOR_SENSOR_PIN 5 // Push button for fridge door
bool fridgeDoorOpen = false;
unsigned long doorOpenStartTime = 0;
const unsigned long DOOR_OPEN_THRESHOLD = 60000; // 60 sec
WiFiClient client;
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n Connected to Wi-Fi!");
// Initialize DHT Sensor
dht.begin();
// Set up fridge door sensor
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP);
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
// Read Temperature & Humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println(" Failed to read from DHT sensor!");
return;
}
// Check Fridge Door Status
bool doorStatus = digitalRead(DOOR_SENSOR_PIN) == LOW; // LOW = Open, HIGH = Closed
if (doorStatus) {
if (!fridgeDoorOpen) {
fridgeDoorOpen = true;
doorOpenStartTime = millis();
}
} else {
fridgeDoorOpen = false;
}
// Send Data to ThingSpeak
sendToThingSpeak(temperature, humidity, fridgeDoorOpen);
// Trigger Alert if Necessary
if (fridgeDoorOpen && (millis() - doorOpenStartTime > DOOR_OPEN_THRESHOLD)) {
sendSMSAlert(" Fridge Door Open Too Long! Close it ASAP!");
}
if (temperature > 10) {
sendSMSAlert(" Temperature Alert! Fridge is warming: " + String(temperature) + "°C");
}
if (humidity > 80) {
sendSMSAlert(" Humidity Alert! Too much moisture: " + String(humidity) + "%");
}
delay(30000); // Wait 30 sec before next update
}
// 📡 Send Data to ThingSpeak
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));
}
}
}
// 📲 Send SMS Alert via Twilio
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();
}
}