How do you safely shutdown or halt the Pi without turning off USB power?
I have an application where an Arduino is connected to a Pi via USB. The Arduino manages a battery that powers both it and the Pi. I'm now trying to setup a safe self-shutoff routine where the Pi runs
sudo halt so no files get corrupted by sudden power failure, and then the Arduino uses a transistor to cut power.
Unfortunately, it seems that after
sudo halt finishes, the Pi turns off USB power. Since I'm powering my Arduino via USB, I was stuck with this paradox where I need the Arduino to remain powered so it can turn off the battery...but first it needs to wait until the Pi safely halts itself...but by then the Arduino has lost power where it can't turn off the battery, leaving the Pi still powered but halted while the Arduino is disabled.
My hacky workaround was to modify the Arduino to track a heartbeat signal from the Pi, and once that's lost, then immediately turn off the battery. That way, when the Pi begins to execute
sudo halt, it kills the process sending this heartbeat, causing the Arduino to cut power.
However, this doesn't reliably work because it's preventing full execution of
sudo halt and therefore sometimes resulting in data corruption on the Pi SD card.
Ideally, I'd like to keep using the loss of the Pi heartbeat to tell the Arduino to shutoff, but add a 30 second delay to ensure
sudo halt has completed. However, to do that, I need to keep USB power turned on, even after the Pi has halted. How do I do that?