Tuesday, September 03, 2013

Raspberry Pi and “sudo poweroff”

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).

Illuminated LED shutdown switch

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.

3 comments:

ddurdle said...

sudo poweroff is just doing a sudo shutdown -P now. Neither a poweroff or a shutdown will power off the PI completely unless you use this switch device. Poweroff and shutdown both will poweroff the system when you have this switch attached.

İlker ilgen said...

Hi

I want flip screen or application (exam
image viewer) GPIO connected button.

How can I do this ?

Anonymous said...

I just bought a raspberry pi and am in the process of installing it in my car to get the gas mileage. I'd like to install this exact power switch. I am new to this and was wondering if you could walk me through how you set it up. Is it as simple as connecting it to to pins 23 and 24 and booting up my pi? The system I using is the raspbian debian. Will the code be similar or the same as what you used?

Also, how does the sudo shutoff factor into this? Will I still need to use that command if I have the switch installed, or can I just press the button?

Thanks!