[SOLVED] How to make Easy Deployment Mode enable just before shutdown!

In a quest not to need an external button press, have the system boot as soon as DC power is available, and allow the system to shutdown gracefully, I have developed the following method and want to share for those that will benefit.

This aims to:

  1. Safely (gracefully) shutdown the pi
  2. Enter EDM mode, just before halt, reboot, or poweroff (as catch alls, the behavior is the same)
  3. Allow the system to boot without external intervention as soon as power is applied (no button press)

As mentioned in other posts, a “shutdown” using this method will result in a system reboot. This is intentional and desired behavior in my use case. Alter as needed. When no DC power is connected, the system will shutdown (into EDM) and power down all connected peripherals.; if DC is (re)connected, the system will boot.

The EDM function does NOT shut the system down or bring the runlevel down to 0 gracefully, it simply cuts power and puts the HAT into EDM. That’s bad, and this is how to avoid it.

The method works by starting a service that stays running that must be stopped before the systemd reboot.target, halt.target, etc., are allowed to be entered. This is when the stop command runs. This is the last possible moment to execute something on the system and the system is considered to be in a safe state. All other programs have safely shutdown (and files closed), by design. This is the safest possible method to enter EDM.

The basics of this method consist of:

  1. Writing an enter_edm.py script
  2. Writing a service file to call the script when the service is killed (just before shutdown)
  3. Enabling the service
  4. Verifying behavior

** EDIT: ** Sometimes the ‘remove_all_scheduled_events’ fails and the script exits erroneously, I have had better luck without it, but I still remove the events in a different script, but strictly speaking it is not necessary to make that function call. I have a ‘watch’ script that monitors inputs, signals temp to the system (so that fan automation works), and initiates a shutdown sequence when DC current is 0 and battery powered (high power use can draw battery power, or has for me in the past). This script may interfere with the shutdown command in my use case, but this change helps make my system work as expected.

1. Writing an enter_edm.py script
$ sudo nano /usr/local/sbin/enter_edm.py
and add:

#! /usr/bin/python3
import sys
sys.path.append(’/home/pi/gits/sixfab-power-python-api/’)
from power_api import SixfabPower, Definition, Event
api=SixfabPower()
#api.remove_all_scheduled_events(200) # removed because if can fail
while True: api.set_edm_status(1, 1000)

2. Writing a service file to call the script when the service is killed (just before shutdown)
$ sudo nano /lib/systemd/system/shutdown_edm.service
and add:

[Unit]
Description=script to put UPS HAT in Easy Deployment Mode
Before=reboot.target halt.target poweroff.target unmount.target

[Service]
User=pi
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/true
ExecStop=/bin/sh -c ‘/usr/bin/python3 /usr/local/sbin/enter_edm.py’
TimeoutStopSec=10

[Install]
WantedBy=default.target reboot.target halt.target poweroff.target

3. Enabling the service
$ sudo systemctl enable shutdown_edm.service
$ sudo systemctl start shutdown_edm.service

3a. Disabling the service
$ sudo systemctl disable shutdown_edm.service
(Don’t run the stop, it will immediately poweroff/reboot the system)

4. Verifying behavior
$ sudo systemctl status shutdown_edm.service

The command should show that the service is active.

When shutdown on DC power, the system will reboot. This is by design of the Easy Deployment Mode.

When shutdown not on DC power, the system will shutdown and all attached hardware will power down (including GPIO, USB, everything).

When power is reapplied to the UPS hat, the system will immediately boot by leaving EDM.

Hope this helps others. I don’t think anything needs to be made executable, at minimum, everything needs to be
chmod 644’d

Shawn

1 Like

I have noticed, when running a USB hub (for which the power is cut at the same moment) it sinks power that does not allow the HAT to switch fast enough, or cleanly enough, to battery power to keep the pi powered; it immediately shuts down. In a limited number of cases in tests without the USB (and attached devices like Wifi adapter and phone) hub connected the system was able to enter battery mode quickly enough and the system did not lose power.

I mention this to elicit additional testing if this method does not work perfectly.

Because the hub can also be DC powered I plan to build a “no DC over USB adapter” from USB->terminal block connectors. I am hoping this will sufficiently isolate the USB hub and prevent the power spike that it causes to the system at the critical moment.

Turns out, disconnecting the +5V between the Pi and the powered hub and powering the +5V line from another source solves all of my problems (so far).

The +5V still needs to be hooked up to something, even if the hub is powered (by 12V, or other); the +5V signal provides the signal to begin communication. The Pi has no way of knowing that it’s 5V out has been interrupted and the hub doesn’t care where the 5V is coming from.

The problem is the amount of current that the hub tried to sink from the Pi in the moment that 12V is cut to both the HAT and the hub. The HAT not able to make the switch to battery power, power, the Pi and provide for the spike in USB current to the hub.

Bonus is, the USB devices charge quickly, and the above describe method works perfectly to get the HAT into Easy Deployment Mode successfully. If you too have an issue with high current killing the pi after DC disconnect, try this method.

Along with the change to the enter_edm.py script noted above.