Power outage notifier, based on an ESP32 client and a PHP web server
The client device sends a request to the server at every specified interval while it is online. Whenever the server detects a gap in pings larger than the interval length, it notifies the user of a possible power outage.
- A client device sends an HTTP request to a PHP script at a given interval
- Every time the PHP receives a ping, it notes down the timestamp
- A cron job pings another PHP script, which checks if the difference between current time and last device ping has exceeded its interval length
- If that is the case, then the device has stopped pinging. PHP will send an email to the user, then notes down that it has already done so
- If pinging returns after the outage email has been sent, another email is sent to the user to notify them that the device has come back online
- Client side
- an ESP32 Dev Kit
- Arduino IDE
- stable power and Wi-Fi connection
- Server side
- PHP web server
- Cron job
- SMTP (to send emails)
- PHPMailer
- stable power and internet connection
-
Server side
- Download PHPMailer from GitHub, rename the folder to
PHPMailer-masterand put it under/server/ - Enter SMTP information in
/server/secret.php - Change email information in
/server/emailUtils.php - The default ping interval is 3 minutes. If you want to change it, do so on
INTERVAL_SECONDSin/server/cron.php - A template is provided for a device in
/server/devices.jsonand/server/stamps.json. You can modify the device ID (1-9999) and the email addresses to send notification - Host the contents of
/server/on a PHP server - Note the path to
/server/ping.phpon your server. Format it ashttps://{your_url}/{path}/ping.php?id={device_id}. You will need this later as "ping URL" - Set up a Cron job with interval
*/3 * * * *if your interval is every 3 minutes. The cron job should fetch the path to/server/cron.php(you can usecurl) - If your server doesn't work with cron jobs, you can also use cron-job.org
- Download PHPMailer from GitHub, rename the folder to
-
Client side
- Connect the ESP32 to a computer, and download necessary softwares from the Arduino IDE's board manager
- Copy and paste the code from
/client/ping.cpp - Enter the Wi-Fi name in
SSIDNAME, password inPASSWORD, and the ping URL inENDPOINT. (Make sure theidis present in the URL) - Change the ping interval on
INTERVAL_SECONDSto match the server side, if you have modified it - Upload the code on to the ESP32
- Unplug the ESP32 and plug it into a power source, and make sure that during normal operations, both the red and blue LEDs are lit up
Made by Mu Leelawat. Contact information can be found on my website.
Fun fact: This project is live, up and running at my house!