A few weeks ago I ordered a power switch for the Raspberry Pi from www.mausberrycircuits.com with the aim of using this on my Raspi Fuel Miser setup. (see previous post).
When this switch is situated between your Raspberry Pi’s power jack and your phone charger and you plug in the phone charger, nothing happens. You first need to press the button before the Pi lights up. Then, once you are through using it, you keep the button pressed for 2 seconds and the Pi powers off.
You need to hook up 2 wires from the switch to the GPIO pins, suggested are 23 and 24. If you do use other ones, then you need to modify /etc/switch.sh after you run setup .sh.
I first ran it using the switch.sh configuration, but then I got to thinking, why run it separately when it can be included in my Python script, which is running continuously anyway, dutifully computing gas mileage.
So I amended my script, created a new function has_ShutdownButtonBeenPressed and inserted a call to it at the start of the main loop in the obd_capture.py program. This way, it will check for a pressed key once every 3/4 second or so, the time it takes to complete a cycle of OBDII readings. I disabled the switch.sh and remove the entry in /etc/rc.local which started up switch.sh upon start up.
The ‘sudo poweroff’ command, by the way, cuts all power to the board, so it is completely disabled, unlike the ‘shutdown’ command, which still leaves the main red LED lit.
Now, at the end of my trip, if I have used the fuel miser, all I need to do is press the power button for 2 seconds, and the Python script will close the SQlite database gracefully before issuing the os.system(“sudo poweroff”) command.