#include #include #include // Include for secure connections // Replace with your network credentials const char* ssid = "SSIDHERE"; const char* password = "PASSWORDHERE"; // Replace with your ntfy server URL const String ntfyServerUrl = "https://ntfy.sh/TOPIC"; // GPIO pin connected to optocoupler output const int powerStatusPin = 5; // GPIO5 is D1 const int ledPin = 2; // GPIO2 is the blue onboard LED // Variable to store the previous state of the power status bool previousPowerStatus = false; // Assume power is off initially WiFiClientSecure wifiClient; // Create a WiFiClientSecure object void setup() { // Start serial communication Serial.begin(115200); // Initialize the GPIO pin as an input with pull-up pinMode(powerStatusPin, INPUT_PULLUP); // Initialize the LED pin as an output pinMode(ledPin, OUTPUT); // Connect to Wi-Fi - The last 'true' indicates hidden SSID WiFi.begin(ssid, password, 0, NULL, true); // Connect to Wi-Fi, using non-hidden SSID // WiFi.begin(ssid, password); Serial.print("Connecting to Wi-Fi"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("Connected to Wi-Fi"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // Blink the blue LED every second digitalWrite(ledPin, LOW); // Turn the LED on (LED is active low) delay(500); // Wait for half a second digitalWrite(ledPin, HIGH); // Turn the LED off delay(500); // Wait for half a second // Read the current power status bool currentPowerStatus = digitalRead(powerStatusPin); // If power status has changed if (currentPowerStatus != previousPowerStatus) { if (currentPowerStatus == LOW) { // Assume LOW means power is on // Power restored Serial.println("Power Restored."); sendNotification("Power is on! Control MDF is back on mains power."); } else { // Power outage detected Serial.println("Power Outage Detected!"); sendNotification("Power is off! Control MDF is on battery power."); } // Update the previous power status previousPowerStatus = currentPowerStatus; } // Add a small delay to prevent rapid state changes delay(1000); } // Function to send a notification to ntfy void sendNotification(String message) { if (WiFi.status() == WL_CONNECTED) { // Check Wi-Fi connection HTTPClient http; // Allow the use of insecure connections if needed (for testing) wifiClient.setInsecure(); // Use this for testing; in production, use valid certificates // Prepare HTTP POST request with WiFiClientSecure http.begin(wifiClient, ntfyServerUrl); // Pass the WiFiClientSecure object http.addHeader("Content-Type", "text/plain"); // Print the message being sent for debugging Serial.print("Sending notification: "); Serial.println(message); // Send the message int httpResponseCode = http.POST(message); // Print response code for debugging Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); // Close the connection http.end(); } else { Serial.println("Wi-Fi Disconnected. Cannot send notification."); } }