Tech Gyan Set
Published © MIT

IoT Smart WiFi Power Cut Detection & Alert System

An IoT-based smart system that detects power cuts instantly and sends real-time WiFi alerts to keep homes and businesses informed. ⚡📡

BeginnerFull instructions provided8 hours117
IoT Smart WiFi Power Cut Detection & Alert System

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

⚡ IoT Smart WiFi Power Cut Detection & Alert System ✅ Complete ESP32 Code

C/C++
🔍 How This Code Works

1️⃣ AC detection module mains present hone par HIGH deta hai
2️⃣ Power cut hone par LOW ho jata hai
3️⃣ ESP32 status change detect karta hai
4️⃣ MQTT par "Power OFF" ya "Power ON" publish karta hai
5️⃣ Serial monitor par debug message show karta hai
#include <WiFi.h>
#include <PubSubClient.h>

// ----------------------
// Pin Configuration
// ----------------------
#define AC_DETECT_PIN 4   // AC detection module output

// ----------------------
// WiFi Credentials
// ----------------------
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

// ----------------------
// MQTT Configuration
// ----------------------
const char* mqtt_server = "broker.hivemq.com";
const char* topic_status = "powercut/status";

WiFiClient espClient;
PubSubClient client(espClient);

// ----------------------
// Variables
// ----------------------
bool powerStatus = true;       // true = power ON
bool lastPowerStatus = true;

// ----------------------
// WiFi Setup
// ----------------------
void setup_wifi() {
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nWiFi Connected");
}

// ----------------------
// MQTT Reconnect
// ----------------------
void reconnect() {
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if (client.connect("PowerCutDetector")) {
      Serial.println("MQTT Connected");
    } else {
      delay(2000);
    }
  }
}

// ----------------------
// Setup Function
// ----------------------
void setup() {

  Serial.begin(115200);

  pinMode(AC_DETECT_PIN, INPUT);

  setup_wifi();
  client.setServer(mqtt_server, 1883);
}

// ----------------------
// Main Loop
// ----------------------
void loop() {

  if (!client.connected()) {
    reconnect();
  }

  client.loop();

  // Read AC Detection Pin
  powerStatus = digitalRead(AC_DETECT_PIN);

  // If power status changes
  if (powerStatus != lastPowerStatus) {

    if (powerStatus == HIGH) {
      Serial.println("⚡ Power Restored");
      client.publish(topic_status, "Power ON");
    } 
    else {
      Serial.println("🚨 Power Cut Detected!");
      client.publish(topic_status, "Power OFF");
    }

    lastPowerStatus = powerStatus;
  }

  delay(1000);
}

Credits

Tech Gyan Set
32 projects • 14 followers
Tech Gyan Set | IoT & Embedded Systems Creator | Arduino, ESP32, GSM & NodeMCU Projects | Smart Home & Real-Life Automation Tutorials
Thanks to Tech Gyan Set .

Comments