Immediate power up with external power, safe shutdown when removed

TL;DR - How to make the unit boot immediately when external power is applied and safely shutdown when external power is removed?

Use case:

Stationary, mounted Rpi (no direct access to circuit board and buttons), with touchscreen.

I need the Rpi to boot ** as soon as ** external power is applied.
I need the Rpi to safely shutdown ** as soon as ** external power is removed.

How do I get the UPS HAT to behave in this way using the PMS Python API? The unit will not have regular wifi access so the Power Software is not a good solution either.

I can accomplish the shutdown by querying the mode every few seconds and creating a shutdown event. Regardless of which way it is shutdown, the Rpi does not boot again when external power is re-applied.

The only way I have gotten it to boot again is by using the ‘battery level solution’, but this is a poor solution because it requires a 5% point rise in the battery level before the Rpi powers up. This can take 5-10 minutes depending on charge state. Worse yet, a safe shutdown setting between 95-100% causes the unit to continuously boot/shutdown.

S

So, no real fix from sixfab, so I came up with something that does the trick.

My solution is to simulate a button press when power comes on with hardware using a 555 timer IC. This works for me, it may work for you, but use it at your own risk. :wink:

The circuit operates by sensing voltage on the power source and uses a mosfet to simulate a quick press on the button. Since the ‘short press’ has no effect when the system is already on, power can be applied/removed in cases where the normal safe shutdown (battery level) is being used. I do not use it in this way.

I added some code to read_sensors.py that responds to the battery state and initiates a shutdown. To boot from a hard shutdown, use S1, from soft shutdown, use S2.

To run the read_sensor.py script at boot up, add:

@/home/pi/startup.sh

to the end of:

/etc/xdg/lxsession/LXDE-pi/autostart

and place the startup.sh file in the appropriate place. It will run:

x-terminal-emulator -e ‘cd /home/pi/Gits/sixfab-power-python-api/ && watch python3 ./example/read_sensors.py’

to launch a window at startup to monitor the operating mode It monitors system input power, since during high power usage, it will be in BATTERY_POWERED even when plugged in.

(This would be more correctly described as ‘BATTERY_DISCHARGING’)

The schematic, cnc, and all the needed files can be obtained from my git:

1 Like