2

Ok, my idea is for my raspberry pi to email me when the power goes out.

Some things to note:

  1. the RPi is powered by battery backup, it will still be on when the power goes out.
  2. all of my network equipment is powered by battery backup, so i will be able to send emails.
  3. it is already configured to email, and it works.
  4. I have a 12v AC adaptor plugged into the wall (no battery), and then a relay connected to the end, ready to connect to GPIO.
  5. I have never done any programming involving GPIO on the RPi

What I need to know:

  • how to cause the RPi to trigger an email when two GPIO pins lose connection with each other

I don't really know how to start researching this, does anyone have suggestions on how to do this?

  • 1
    Don't forget to use an internet medium that won't fail when the power goes out. – einnocent Feb 8 '14 at 14:51
  • Yea make sure that internet will still work if your area is affected. Sometimes the exchanges can do down. – Piotr Kula Feb 10 '14 at 12:35
  • i have bgp across multiple providers, all of which have their own backup power solution. i am not worried about network failure. – Ace Feb 10 '14 at 15:38
2

APC uninterruptible power supply (UPS) can be accessed over USB and there's ready to use software apcupsd:

$ sudo apt-get install apcupsd

that could send e-mail or other types of notification. works perfectly for me.

0

If you like coding in C, you should look at wiringpi, if you prefer python, then look at RPi.GPIO. Either library will let you easily access the GPIO pins and trigger anything you want when something happens to your GPIO pins.

Basically all you need to do is choose one GPIO pin to use, and tell the library that that one should be an input. Then you should set up some kind of callback or interrupt so that some function of yours is called when the value of that input pin goes either high or low. In your case, your relay could connect the input pin to the Ground pin, and you'd use an internal pullup resistor so that the normal state of the pin is high.

This way, low means that the relay has connected those two pins, and high means that it hasn't. Just set up a callback to notify you when the input value goes high (a rising edge), and that will tell you that the relay has stopped connecting them.

Once you set this running, it doesn't actually load the Pi much at all - just waiting for the edge doesn't require any noticeable work this way. So the Pi can be doing all your other stuff at the same time.

0

You can detect if you are booting after power failure using a script similar to the one in the last message here. No need for GPIO or UPS. The only condition is that file system is intact. You can make sure it is intact if you make it read only, and only make writable before writing just a partition where you put your power failure flag file. Using battery backup makes it all just a little harder. Use WiringPi lib to detect change on GPIO pin. On pin change just execute script and shutdown RPi and cut of battery backup. Then on power up you have to activate battery backup.

0

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:

  1. Raspberry Pi
  2. AC->DC adapter 5-12v DC
  3. low voltage relay
  4. UPS to power all network equipment and raspberry pi

Software Requirements:

  1. a gmail account (or any other way to send an email with smtplib)
  2. two scripts (provided below)
  3. 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 =    'YOURGMAILADDRESS@gmail.com'
PASSWORD =  'YOURPASSWORD'
RECIPIENTS =    ['noc@example.com', 'noc@example2.com', 'you@gmail.com', 'someoneelse@gmail.com']
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.