This is what I did, based on my experience (not a lot) and research (not enough?). There may be better ways to do it. Feel free to tell me what I'm doing wrong in the comments ;-)
Basics:
- Using Ubuntu MATE 18.04 for RPi, downloaded 22 May 2020 (lost password and hadn't done anything with previous ROS install so didn't mind starting over).
- 16GB Sandisk µSD
- Wired Ethernet connection (already setup). Otherwise, pre-configure WiFi with wpa_supplicant.conf
- KVM switch for easy use of GUI.
Install OS
- Unzip (7-zip)
- Flash .img to SD (balenaEtcher)
- Insert & power-on
Check, set and update via config tool
Boot to GUI or CLI etc…
sudo raspi-config
May as well update everything…
sudo apt-get update && sudo apt-get upgrade
Not necessary, but useful
Edit /boot/config.txt and add/change both of these...
dtparam=pwr_led_trigger=heartbeat #nice load indicator dtparam=watchdog=on
Create/copy-in the mUPS.py file
(filenames are case-sensitive!)
#########################################################################################
# mUPS.py handles UPS/main-power switch-over and RPi shutdown for Droidbot.
# P.Crouch
# Rev3
# May 2020
#
# *** Developed for my own use and provided as-is, with no guarantees ***
#
# RPi signals to mUPS when it is up. mUPS tells Pi to shutdown and waits for RPi_UP
# to go low before removing power.
#########################################################################################
RPi_UP=21 # pin 40
RPi_SD=27 # pin 11
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import time
import sys
from subprocess import call
GPIO.setup(RPi_UP, GPIO.OUT)
GPIO.setup(RPi_SD, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # active-high from UPS (RPi_SD)
#########################################################################################
try:
GPIO.output(RPi_UP, GPIO.HIGH) # signal when PI is up
while 1:
if GPIO.input(RPi_SD):
call(['shutdown', '-h', 'now'], shell=False)
time.sleep(5) # sleep to reduce unnecessary CPU usage
#########################################################################################
finally:
GPIO.cleanup()
Create Bash script
Create new text file called mUPS.sh with the following contents:
#!/bin/sh
cd /home/yourusername/
sudo python mUPS.py
IIRC it wouldn't run before login (no good if running headless and not logging-in during normal operation), so python code needed to run with sudo.
Make both files executable (for all users)…
chmod u+x mPSU.py
chmod u+x mPSU.sh
Test it:
./mUPS.sh
RPi_UP (pin 21) should go high.
Create Cron job to run script at startup
(on newer distros without the rc.local that I used before - workarounds didn't work for me)
Create a cron job that will wait 10 seconds after system startup, then execute the specified script. Sudo is required again here, else you'll edit the user crontab.
sudo crontab -e
…enter the following:
@reboot (sleep 10; sh /home/yourusername/mUPS.sh)
A reboot is considered to be the point at which the daemon starts, it may be before other system daemons so use/test a suitable sleep period.
sudo crontab -l
To confirm that the cron job has been inserted.
Reboot to test it.
.
.
.
At this point the mUPS works almost flawlessly. Almost. I noticed some relay chatter at Arduino power-on that I need to investigate...
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.