Simple Power Loss Detector with SMS Notification

{lang: 'en-GB'}

After having a few un-expected power outages this year, it got me thinking about how I could easily (and cheaply) be alerted (preferably via SMS) should one occur. With a laptop running Linux, a little bit of Python and some easily obtainable hardware and software, I come up with a simple method of doing just that. Read on for the code and set-up instructions.

What You’ll Need

  • A Laptop or netbook running Linux (This won’t work with a Desktop PC)
  • Gammu (a management application for cellular devices)
  • The acpi system utility.
  • The Python Interpreter (Version 2.6.)
  • The commands, time and datetime Python modules
  • The Python bindings for Gammu.
  • A GSM modem or Phone that is compatible with Gammu (in my case, I’m using a Huawei E160 HSDPA USB modem with a Prepaid SIM card on the O2 network in the UK.)

Installing and Configuring the Software

Ubuntu/Debian users can install Gammu, the Gammu Python bindings and the acpi utility using the following command:

apt-get install gammu python-gammu acpi

The only configuration we’ll have to do here is to Gammu. First, determine what serial port device your GSM modem is connected to by running the dmesg command. If your using a USB dongle like me, the serial port device will be prefixed with /dev/ttyUSB

Once you’ve found the serial port device, enter the command gammu-config into the terminal to configure Gammu for use with your modem. You should be presented with something similar to the screenshot below:

Gammu Configuration Utility Screenshot
gammu-config Configuration Menu Screen (Click on the image for a full-sized version.)

The only thing you need to worry about here is the Port option. Just replace the value in this option with the serial port device that corresponds to your GSM modem, then select the Save option to save the new configuration.

The Code

The Python script (pwralarm.py) which handles the job of detecting mains power outages and sending notifications can be found on the Software page. You will need to extract the script from the zip archive and configure it for your own use before it can be used. To configure the script for your own use, just set the variable mobileno at the top of the script to the Mobile/Cellular number you would like SMS notifications to be sent to by entering the number between the quotes.

Everything should now be good to go. To use the script,, make sure you have your GSM modem connected and your laptop/netbook plugged into the mains first,  then type the command ./pwralarm.py to execute the script.

The script will run in an infinite loop and If the mains power source is disconnected (which is determined by the script running the acpi -a command and looking at the output) this will be detected by the script and an SMS notification message sent using the SendSMS() method of the Gammu module. To stop the script, just hold down the CTRL and C keys in the terminal you’re running the script in.

In Conclusion…

Ok, so this is a very, very, very ugly hack, but if you’re like me and you use a laptop as your main PC, which spends most of it’s time anchored to a desk and connected to the mains, you should find this useful as a way to alert you to possible power cuts or if your laptop is disconnected from the mains for any reason, for that matter (theft detection device, anyone?)

Hope it’s useful to someone. Feel free to leave any feedback in the comments or you can always get in touch with me via the Contact page on this site.