Page 1 of 1

I need help please - power cut notification

Posted: Wed Sep 05, 2018 10:06 am
by Levin Rainey
Hi
Thank you very much for reading this!
Anyway, I program just for fun, but I don't really do any physical projects (except I did the 'Pi Music Box' thing in a past Magpi).
A few weeks ago we had a holiday for two weeks and when we came back we found out we had had a power cut. When we checked the fridge, everything was rotten!
Please help on how to program something so that when we have a power cut it automatically sends a notification to a phone. My initial ideas are:
Something to do with a UPS (uninterruptible power supply/source) and send notification the via the internet??
Thanks alot,
Levin :)

Re: I NEED HELP PLEASE IT'S A SIMPLE PROJECT

Posted: Wed Sep 05, 2018 1:22 pm
by thagrol
First, the obvious question: if the power is out how will you reach the internet? No power = no router = no internet.

One option is a UPS that can power both the Pi and your router. There others but all of them depnd on how much you want to speend.

Whatever you use for off grid power, it needs to be able to signal the Pi that mains power is out.

Re: I NEED HELP PLEASE IT'S A SIMPLE PROJECT

Posted: Wed Sep 05, 2018 1:45 pm
by scotty101
I'd propose a 3G phone module like the Adafruit FONA.

With this you could either call your phone with a pre-recorded message, send a text message or access the internet to send a message.

Re: I need help please - power cut notification

Posted: Fri Sep 07, 2018 2:05 pm
by default_user8
Look at the script that emails you the IP address of your PI on power up. When i get a chance I plan on modifying it to send me an email that my power has come back on since my PI will boot up when power is restored. Right now if my power goes out I just try to log into my pivpn every so often and when it connects i know my power is back on.

Re: I need help please - power cut notification

Posted: Fri Sep 07, 2018 9:12 pm
by ajw
Why not write status information to a file, and check it on startup?

It could be as simple as create a file ('running') at startup, and delete it at normal shutdown.
At startup, test if it's there; if it is, you had an abnormal shutdown (so assume power failure).
At abnormal shutdown, send a notification to you.

That's not very flexible, and rather ugly, though.

You could write it as a INI file using configparser (Python 3.x) or ConfigParser (Python 2.x).
That way it's a simple text file, easily updated, and you can change values yourself to test different conditions.
You can add other settings to it as you want them (e.g., start time, notification methods, phone number, email address, etc)
(I prefer ConfigObj because it preserves comments and is more powerful, but it's more difficult to use; the documentation could be better. Google usually finds answers though.)

For example:

Code: Select all

    running = True
    started = '2018-0907-1628'
    qtyNotifications = 2

    [notify_1]
    active = True
    method = text
    address = +1-123-555-1234

    [notify_2]
    active = False
    method = email
    address = me@somewhere.xyz
Like before, when you start, if 'running' is True, you had an abnormal shutdown (assume it's a power failure; send notifications)
If it's False, you start up normally, writing the started (date and time) and running (True) values.
If running is missing, it's the first time you've started, so same as running = False; normal startup.
When you shut down, set running to False.

You might wrap your code in a try/except so you can write running = False in the event of a code bug. Bugs are abnormal shutdowns, but they're not power failures. You could even send notifications of the exception.
Wrap all that in a while loop to restart it again. (use a flag; set to True at startup, set to False for normal shutdown:

Code: Select all

    restart = True

    while restart:
        try:
            # do my stuff
        except Exception as e:
            # handle the exception - at least print() it, or log it somewhere
            # leave restart True, so we *will* restart
        except:
            # treat other exceptions (KeyboardInterrupt, SystemExit, etc) as normal shutdown
            restart = False
        finally:
            # write False to INI file  (so we won't send an 'abnormal shutdown'
            # notification if we restart)
            # do other cleanup

Re: I need help please - power cut notification

Posted: Sat Mar 16, 2019 11:33 am
by Levin Rainey
Thank you very much everyone for replying. I will take a look and make further developments. Thanks again,
Levin