From 3cd3051149a99e742cb2d82efc06d7407ce4bb91 Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 10 Apr 2025 00:32:30 +0300 Subject: [PATCH] initial commit --- .gitignore | 3 +++ README.md | 20 ++++++++++++++- src/secrets.h.template | 8 ++++++ src/src.ino | 58 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/secrets.h.template create mode 100644 src/src.ino diff --git a/.gitignore b/.gitignore index e257658..d8fc7dd 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ *.out *.app +# Secrets + +src/secrets.h diff --git a/README.md b/README.md index 27cc421..5241e70 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,21 @@ # deadmans_hand -Simple program for ESP-32 to power up my server in case of power cut \ No newline at end of file +Simple program for ESP-32 to power up my server in case of power cut. + +## Usage +**/!\ WARNING /!\ The author (leca) is not responsible for any damage you've caused to your hardware with your own hand /!\ WARNING /!\\** +1. Set up the sketch +```bash +git clone https://git.foxarmy.org/leca/deadmans_hand +cd deadmans_hand +cp src/secrets.h.template src/secrets.h +# Open the file with your favorite editor and insert Wi-Fi SSID and password. +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 diff --git a/src/secrets.h.template b/src/secrets.h.template new file mode 100644 index 0000000..02e9902 --- /dev/null +++ b/src/secrets.h.template @@ -0,0 +1,8 @@ +/* +* This is a template for secrets. +* Copy this file as secrets.h and +* adjust it to your needs +*/ + +#define WIFI_SSID "your_wifi_ssid" +#define WIFI_PASSWORD "your_wifi_password" diff --git a/src/src.ino b/src/src.ino new file mode 100644 index 0000000..f2d2825 --- /dev/null +++ b/src/src.ino @@ -0,0 +1,58 @@ +#include "secrets.h" + +#if defined(ARDUINO_ARCH_ESP32) || defined(ESP32) +# include +#elif defined(ARDUINO_ARCH_ESP8266) || defined(ESP8266) +# include +#else +# error "Unsupported board, please contact the developer" +#endif + +#define STATUS_PIN D5 +#define WAKE_PIN D6 + +static bool wifi_lost = false; +static bool server_is_off = false; + +void setup() { + Serial.begin(9600); + + + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + + Serial.println("Connecting to a Wi-Fi network"); + + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + while (WiFi.status() == WL_IDLE_STATUS) delay(100); + + Serial.println("Wi-Fi connected"); + + pinMode(STATUS_PIN, INPUT); + pinMode(WAKE_PIN, OUTPUT); +} + +void loop() { + wifi_lost = WiFi.status() == 1 || WiFi.status() == 5 || WiFi.status() == 7; + server_is_off = digitalRead(STATUS_PIN) != HIGH; + + Serial.println("Checking the status"); + Serial.print("WiFi lost: "); + Serial.println(wifi_lost? "true" : "false"); + Serial.print("Server is off: "); + Serial.println(server_is_off? "true" : "false"); + + if (wifi_lost && server_is_off) { + 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); + delay(100); + digitalWrite(WAKE_PIN, LOW); + Serial.println("Signal sent"); + } else { + Serial.println("Everyting's okay, sleeping 5 secs"); + } + + delay(5000); +} \ No newline at end of file