Startside‎ > ‎

UPS for OpenELEC on the Pi

Raspberry Pi with an UPS that runs OpenELEC

How to automatically turn off a Raspberry Pi safely with OpenELEC when the internet fails/ power cuts..

The idea is not mine. I have gathered much inspiration from this site: raspi-ups.appspot.com
Thanks to  Ingeniøren, dgaa1991 and Erik_A at HardwareOnline.dk for the code and assistance.

The problem is that the software does not run on OpenELEC and that is why I made this page.

It works like this: When ups.py can not access google.com / an IP, it will shutdown the Pi.

I am not a programmer or an engineer. I take absolutely no responsibility for anything that goes wrong !! 

The colour red indicates what needs to be written.

1.) Make a python script that will turn off the Pi when google.com is not accessible. (Here is two scripts, use one of them): 

First use nano to make the ups.py file, which will turn off the Pi:

nano ups.py

Then write (or copy..) the following (script 1):

#!/usr/bin/python
import os
import time
import urllib2 


while True:
    try:
        urllib2.urlopen("http://www.google.com").close()
    except urllib2.URLError:
        os.system("shutdown now -h")    # shutdown command
    else:
        break

Press  "ctrl o" to save and then "ctrl x" to exit.


An alternative and more simple script that uses ping instead of opening an URL (script 2):

#!/usr/bin/python
import os if os.system("ping -c 10 google.dk") != 0: os.system("shutdown -h now")

Press  "ctrl o" to save and then "ctrl x" to exit.

(OpenELEC do not need sudo before the shutdown command. On other distros they properly do)

2.) Setup cron:

In the console you write the follow command:

crontab -e

.. and write this:

*/5 * * * * python /storage/ups.py

Press  "ctrl  o" to save and then "ctrl  x" to exit. Now ups.py runs every 5 minutes.

The next command shows you your cron settings:

 crontab -l

If it is looking alright, then you are done with the software setup. 

3.) Hardware setup

Set up your hardware like this, and you are done:

1 x Raspberry Pi, 1 x powerbank which can power the Pi and be charged at the same time: I use the "Cager T10 2600mAh" powerbank, 1 x a switch.
Powerbanks and batteries can be volatile! Do your own research on which powerbank to use.



Terms And Conditions Of ekrajb.dk
All content provided on this "ekrajb.dk" site is for informational purposes only. The owner of this site makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site.
The owner of ekrajb.dk will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.