Advertisement
pleasedontcode

**WiFi Monitor** rev_01

Dec 15th, 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 Monitor**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-16 02:36:58
  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. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <WiFi.h>
  27. #include <HTTPClient.h>
  28. #include <ArduinoJson.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void checkPowerOutage(void);
  34.  
  35. /****** GLOBAL VARIABLES *****/
  36. unsigned long lastCheckTime = 0; // Last time the check was performed
  37. const unsigned long checkInterval = 5000; // Check every 5 seconds
  38. int outageCount = 0; // Count of outages
  39. unsigned long outageStartTime = 0; // Start time of the outage
  40. bool isOutage = false; // Flag to indicate if an outage is occurring
  41.  
  42. void setup(void)
  43. {
  44.     Serial.begin(115200); // Initialize serial communication
  45.     WiFi.begin("yourSSID", "yourPASSWORD"); // Connect to WiFi
  46.  
  47.     // Wait for connection
  48.     while (WiFi.status() != WL_CONNECTED) {
  49.         delay(1000);
  50.         Serial.println("Connecting to WiFi...");
  51.     }
  52.     Serial.println("Connected to WiFi");
  53. }
  54.  
  55. void loop(void)
  56. {
  57.     checkPowerOutage(); // Check for power outages
  58.     // Other main code can go here
  59. }
  60.  
  61. void checkPowerOutage(void)
  62. {
  63.     if (millis() - lastCheckTime >= checkInterval) {
  64.         lastCheckTime = millis(); // Update the last check time
  65.  
  66.         if (WiFi.status() != WL_CONNECTED) {
  67.             if (!isOutage) {
  68.                 // Outage just started
  69.                 isOutage = true;
  70.                 outageStartTime = millis(); // Record the start time
  71.                 outageCount++; // Increment outage count
  72.                 Serial.println("Power outage detected!");
  73.             }
  74.         } else {
  75.             if (isOutage) {
  76.                 // Outage just ended
  77.                 isOutage = false;
  78.                 unsigned long outageDuration = millis() - outageStartTime; // Calculate duration
  79.                 Serial.print("Power outage ended. Duration: ");
  80.                 Serial.print(outageDuration / 1000); // Print duration in seconds
  81.                 Serial.println(" seconds.");
  82.             }
  83.         }
  84.     }
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement