Ok, by gathering info here on stack exchange, and some basic googling, I have come up with some working scripts in case someone wishes to replicate my setup.
Hardware Requirements:
- Raspberry Pi
- AC->DC adapter 5-12v DC
- low voltage relay
- UPS to power all network equipment and raspberry pi
Software Requirements:
- a gmail account (or any other way to send an email with smtplib)
- two scripts (provided below)
- install wiringpi from: https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/
Now we set it up:
Power all network equipment and RPi with battery backup solution.
Plug the AD adapter into the wall (no battery).
Terminate the end of the AC adapter into the relay, to power it. This will cause the relay to always be ON when the building power is energized, and it will cause the relay to power OFF when the building power is lost. Wire RPi pins 14 and 16 to the NC (Normally-Closed) and the COM (Common) outputs on the relay. Now the RPi can detect when the relay trips on those pins.
Use the bash script below to wait for the relay to trigger on wiringpi pin4 aka pin 16:
#!/bin/bash
while true
do
gpio wfi 4 rising
python /home/user/sendNotification.py
sleep 300 #this will send an email every 5 mins
done
Use the python script below and place it in /home/user/sendNotification.py:
#! /usr/bin/python
import smtplib
from email.mime.text import MIMEText
SERVER = 'smtp.gmail.com'
PORT = 587
SENDER = '[email protected]'
PASSWORD = 'YOURPASSWORD'
RECIPIENTS = ['[email protected]', '[email protected]', '[email protected]', '[email protected]']
SUBJECT = 'Power Outage!'
BODY = """
<html>
<head></head>
<body>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-0ord{text-align:right}
.tg .tg-qnmb{font-weight:bold;font-size:16px;text-align:center}
</style>
<table class="tg">
<tr>
<th class="tg-qnmb" colspan="2">POWER OUTAGE DETECTED!!!</th>
</tr>
<tr>
<td class="tg-0ord">Location:</td>
<td class="tg-031e">Annex Office</td>
</tr>
<tr>
<td class="tg-0ord">Address:</td>
<td class="tg-031e">123 name st<br>City, State<br>00000</td>
</tr>
<tr>
<td class="tg-0ord">Contact Info:</td>
<td class="tg-031e">YOU - 1800,123,1234</td>
</tr>
</table>
</body>
</html>
"""
session = smtplib.SMTP(SERVER, PORT)
session.set_debuglevel(1)
session.ehlo()
session.starttls()
session.ehlo
session.login(SENDER, PASSWORD)
msg = MIMEText(BODY, 'html')
msg['Subject'] = SUBJECT
msg['From'] = SENDER
msg['To'] = ", ".join(RECIPIENTS)
session.sendmail(SENDER, RECIPIENTS, msg.as_string())
session.quit()
Now run the bash script and it should wait for a trip from the relay. You can simulate power outage by unplugging the AC adapter and then it should email every 5 mins until power is restored.