Advertisement
pleasedontcode

WiFi Outage rev_02

Dec 16th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: WiFi Outage
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-16 06:15:02
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* monitor power outages frequency and length */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <WiFi.h>
  28. #include <HTTPClient.h>
  29. #include <ArduinoJson.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void checkPowerOutage(void);
  35.  
  36. /****** GLOBAL VARIABLES *****/
  37. unsigned long lastCheckTime = 0; // Last time the check was performed
  38. const unsigned long checkInterval = 5000; // Check every 5 seconds
  39. int outageCount = 0; // Count of outages
  40. unsigned long outageStartTime = 0; // Start time of the outage
  41. bool isOutage = false; // Flag to indicate if an outage is occurring
  42.  
  43. void setup(void)
  44. {
  45.     Serial.begin(115200); // Initialize serial communication
  46.     WiFi.begin("yourSSID", "yourPASSWORD"); // Connect to WiFi
  47.  
  48.     // Wait for connection
  49.     while (WiFi.status() != WL_CONNECTED) {
  50.         delay(1000);
  51.         Serial.println("Connecting to WiFi...");
  52.     }
  53.     Serial.println("Connected to WiFi");
  54. }
  55.  
  56. void loop(void)
  57. {
  58.     checkPowerOutage(); // Check for power outages
  59.     // Other main code can go here
  60. }
  61.  
  62. void checkPowerOutage(void)
  63. {
  64.     if (millis() - lastCheckTime >= checkInterval) {
  65.         lastCheckTime = millis(); // Update the last check time
  66.  
  67.         if (WiFi.status() != WL_CONNECTED) {
  68.             if (!isOutage) {
  69.                 // Outage just started
  70.                 isOutage = true;
  71.                 outageStartTime = millis(); // Record the start time
  72.                 outageCount++; // Increment outage count
  73.                 Serial.println("Power outage detected!");
  74.             }
  75.         } else {
  76.             if (isOutage) {
  77.                 // Outage just ended
  78.                 isOutage = false;
  79.                 unsigned long outageDuration = millis() - outageStartTime; // Calculate duration
  80.                 Serial.print("Power outage ended. Duration: ");
  81.                 Serial.print(outageDuration / 1000); // Print duration in seconds
  82.                 Serial.println(" seconds.");
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement