From b0e9dfea36657138c2f2c7fe8d3a2b880c750832 Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 10 Apr 2025 22:22:57 +0300 Subject: [PATCH] using open-drain for power on signal, added option for sleep time. --- README.md | 3 ++- src/src.ino | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5241e70..28dc37b 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,10 @@ cp src/secrets.h.template src/secrets.h nano src/secrets.h ``` If you wish to change GPIO pins, please, do it in ``src/src.info`` + Flash the sketch in your board 2. Connect pin STATUS_PIN to anything in your server that has voltage when the server is running (usually some led on the forward panel) 3. Connect pin WAKE_PIN to power pin on your motherboard 4. Connect GND to motherboard's ground. -5. Make sure that the board has some power supply (usually you want to use 18650 Li-ion batteries) to be online when the power is down. \ No newline at end of file +5. Make sure that the board has some power supply (usually you want to use 18650 Li-ion batteries) to be online when the power is down. diff --git a/src/src.ino b/src/src.ino index f2d2825..49dd4ef 100644 --- a/src/src.ino +++ b/src/src.ino @@ -10,13 +10,13 @@ #define STATUS_PIN D5 #define WAKE_PIN D6 +#define SLEEP_TIME 5 // seconds static bool wifi_lost = false; static bool server_is_off = false; void setup() { Serial.begin(9600); - WiFi.mode(WIFI_STA); WiFi.disconnect(); @@ -24,16 +24,16 @@ void setup() { Serial.println("Connecting to a Wi-Fi network"); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); - while (WiFi.status() == WL_IDLE_STATUS) delay(100); + while (WiFi.status() == WL_IDLE_STATUS) delay(1000); Serial.println("Wi-Fi connected"); pinMode(STATUS_PIN, INPUT); - pinMode(WAKE_PIN, OUTPUT); + pinMode(WAKE_PIN, INPUT); } void loop() { - wifi_lost = WiFi.status() == 1 || WiFi.status() == 5 || WiFi.status() == 7; + wifi_lost = WiFi.status() == WL_NO_SSID_AVAIL || WiFi.status() == WL_CONNECTION_LOST || WiFi.status() == 7; server_is_off = digitalRead(STATUS_PIN) != HIGH; Serial.println("Checking the status"); @@ -46,13 +46,15 @@ void loop() { Serial.println("The power seems to be down. Waiting for the power to restore and send turn on signal to the server."); // Wait untill Wi-Fi is available (i.e. the power is back) while (WiFi.status() != WL_CONNECTED) delay(100); - digitalWrite(WAKE_PIN, HIGH); + pinMode(WAKE_PIN, OUTPUT); delay(100); - digitalWrite(WAKE_PIN, LOW); + pinMode(WAKE_PIN, INPUT); Serial.println("Signal sent"); } else { - Serial.println("Everyting's okay, sleeping 5 secs"); + Serial.print("Everyting's okay, sleeping "); + Serial.print(SLEEP_TIME); + Serial.println(" secs."); } - delay(5000); + delay(SLEEP_TIME * 1000); } \ No newline at end of file