Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: WiFi Outage
- - Source Code compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-12-16 06:15:02
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* monitor power outages frequency and length */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <WiFi.h>
- #include <HTTPClient.h>
- #include <ArduinoJson.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void checkPowerOutage(void);
- /****** GLOBAL VARIABLES *****/
- unsigned long lastCheckTime = 0; // Last time the check was performed
- const unsigned long checkInterval = 5000; // Check every 5 seconds
- int outageCount = 0; // Count of outages
- unsigned long outageStartTime = 0; // Start time of the outage
- bool isOutage = false; // Flag to indicate if an outage is occurring
- void setup(void)
- {
- Serial.begin(115200); // Initialize serial communication
- WiFi.begin("yourSSID", "yourPASSWORD"); // Connect to WiFi
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- }
- void loop(void)
- {
- checkPowerOutage(); // Check for power outages
- // Other main code can go here
- }
- void checkPowerOutage(void)
- {
- if (millis() - lastCheckTime >= checkInterval) {
- lastCheckTime = millis(); // Update the last check time
- if (WiFi.status() != WL_CONNECTED) {
- if (!isOutage) {
- // Outage just started
- isOutage = true;
- outageStartTime = millis(); // Record the start time
- outageCount++; // Increment outage count
- Serial.println("Power outage detected!");
- }
- } else {
- if (isOutage) {
- // Outage just ended
- isOutage = false;
- unsigned long outageDuration = millis() - outageStartTime; // Calculate duration
- Serial.print("Power outage ended. Duration: ");
- Serial.print(outageDuration / 1000); // Print duration in seconds
- Serial.println(" seconds.");
- }
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement