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: http://www.3cc.org/blog/2013/01/raspberry-pi-shutdown-switch-safely-turning-off-the-pi/. Perfect! i will use the code reported there to turn off my very special media player! let me see the code... 







                                                                                         
[code]
import RPi.GPIO as GPIO
import time
import os
[/code]

some imports...



[code]
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
[/code]

some settings....



[code]
while True:
   if(GPIO.input(17)):
[/code]

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
cd
mkdir temp_gpio_python
cd temp_gpio_python

download this file :
wget raspberry-gpio-python.googlecode.com/files/RPi.GPIO-0.5.2a.tar.gz

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

install it:
sudo python setup.py 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 setup.py install
Finished processing dependencies for RPi.GPIO==0.5.2a

 yuppy! :)

download the python module...
cd
cd temp_gpio_python
wget raspberry-gpio-python.googlecode.com/files/python-rpi.gpio_0.5.2a-1_armhf.deb
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
GPIO.VERSION

the response must be something like:
'0.5.2a'
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 http://raspi.tv/wp-content/uploads/2013/03/RasPi.TV-interrupt1_bb.jpg 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.setmode(GPIO.BCM)
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")
exit(0)



let's try it!

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

yuppy! :)

save the script in /usr/bin/shutdown.py 

phase 4: make it automatic :P

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


that's all! :D

Nessun commento:

Posta un commento