giovedì 2 maggio 2013

Almost every Raspberri Pi owner uses it without a monitor/keyboard attached to it... One simple option is controlling the (mini)computer by ssh or directly by an iphone (thanks to xbmc + airplay, GREAT invention!). Anyway, after listening hour of justin bieber music you could think... "and now, how the hell I can turn this off?" after some moments of hard-thinking you end up with the simplest solution: unplug the power, no matter if the (poor) system tells you every time "the system was not shut down properly".

Next move: use google to search for "raspberry power off button". One of the most interesting site you could find is: Perfect! i will use the code reported there to turn off my very special media player! let me see the code... 

import RPi.GPIO as GPIO
import time
import os

some imports...

GPIO.setup(17, GPIO.IN)

some settings....

while True:

some while true..... WHAT!?!?!?!?!? "WHILE TRUE"!?!?!?

NO, i will not waste the time of my processor with this stupid polling for button pressed... and another things... what is the reason why you use a couple of resistor if some of the raspberry pins actually HAVE an integrated pull-up resistor?

ok, let's start from the beginning. I'll assume in the following that you have some background on how the interrupt and the pull-up resistor works. otherwise, use google or wikipedia.
phase 0:

assume that you have a *working* linux installation (I'm gonna use xbmc) on your Raspberry pi and you know how to open a consolle or use ssh.

(on xbmc)
default user: pi
default password: raspberry

phase 1: install the GPIO python library

create a temp directory
mkdir temp_gpio_python
cd temp_gpio_python

download this file :

unrar it:
tar zxf RPi.GPIO-0.5.2a.tar.gz
cd RPi.GPIO-0.5.2a

install it:
sudo python install
error: command 'gcc' failed with exit status 1

OH FU.... ok... step back... (just to be sure, gonna install everything)
sudo apt-get install build-essential python python-dev

install it (second try):
sudo python install
Finished processing dependencies for RPi.GPIO==0.5.2a

 yuppy! :)

download the python module...
cd temp_gpio_python
sudo dpkg -i python-rpi.gpio_0.5.2a-1_armhf.deb

now check if the installation works... type:
sudo python
import RPi.GPIO as GPIO

the response must be something like:
phase 2: the hardware  

simply connect a momentary open switch between pin 23 and GND (pin 6). No resistor needed as I said before I'll use the pull-up resistor of the Raspberry.

 (refer to for a bigger version)

phase 3: the software 

this is the python script. It's simple. And there are comments in it.

#!/usr/bin/env python2.7.3

import RPi.GPIO as GPIO
import os

#set GPIO as input and enable the internat pull up resistor
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#enable interrupt on falling edge
GPIO.wait_for_edge(23, GPIO.FALLING)

#power off the system
os.system("sudo shutdown -h now")

let's try it!

sudo python 
--- press the button ---
The system is going down for halt NOW!

yuppy! :)

save the script in /usr/bin/ 

phase 4: make it automatic :P

sudo pico /etc/rc.local
insert the line
   sudo /usr/bin/python /usr/bin/
right before "exit 0"

that's all! :D

Nessun commento:

Posta un commento