I have a Pi Model B Rev 2.0 (I think) and I intend to use it for a Home Automation system. As I have a prepayment meter on my electricity supply I occasionally run out of electrons to go around my home's Mains Supply!
To avoid problems I have obtained a UPS in the form of a UPis Basic made by PiModules. I have configured it so that I can poll the supply voltages via the Pi's own serial port (not the default configuration but a supported set-up documented in the manual).
At present it uses a dedicated GPIO pin (pin 13 on the header, GPIO27 I believe) and the built-in micro-controller uses that and a python script running from rc.local to tell the Pi to
shutdown -h now when the pin is taken low - which happens when the UPS "Shutdown" button is pressed or if the battery supply runs to critically low in the event of main supply failure. For the record the script is:
#!/usr/bin/python # import the libraries to use time delays, send os commands and access GPIO pins import PRi.GPIO as GPIO import time import os GPIO.setmode(GPIO.BCM) # Set pin numbering to board numbering GPIO.setup(27, GPIO.IN, pull_up_don=GPIO.PUD_UP) # Setup pin 27 as an input while True: # Setup a whille loopto wait for a button press if(GPIO.input(27)==0): # Setup an if loop to run a shutdown command when button press sensed os.system("sudo shutdown -h now") # Send shutdown command to os break time.sleep(1) # Allow a sleep time of 1 second to reduce CPU usage
This strikes me as a little inelegant given that
init has the built-in capability to handle power events. I should reiterate that I am using sysVinit on Raspbian Jessie NOT the default systemd for that release (for personal preference and familiarity reasons).
I would like to, initially, replace the above script with something that tells init to do its
powerfailnow when the pin goes low - and to do a
powerokwait if it subsequently returns high. Eventually I also want to poll the serial port and keep an eye on the responses to
@ups which return the current voltages on the Pi's 5V rail, the LiPo battery and the UPis own microUsb input respectively - so that the Pi can give an alert on/handle a power-fail (it should result in a
powerfail init action and let the Pi report the power loss status to me, the user - assuming I haven't ready noticed!)
However I am having difficulty finding out how Linux UPS devices signal to
init that the
powerokwait commands defined in
\etc\inittab should be performed.
Can anyone advise me, for instance, how "grown-up" UPS tell a Linux kernel on a normal PC that "power" events are happening and how I might reproduce the same in this system on a Pi?