We have already discussed previously how to control hardware signals using our Raspberry Pi GPIO port, but today we will discuss the way to do it in a more convenient way (via a high level programming language) and, in addition, how to simplifying the «export» process will be also discussed on this entry. And for convenience of our followers, we will offer the code downloading to perform the described examples …
Note: this post is based on Raspberry Pi hardware and Raspbian software (eg Wheezy)
The summary of what we will discuss:
(1) Description of the rc.local file.
(2) Automation of GPIO port «export».
(3) Simple PHP application for GPIO control.
(4) Simple script to export GPIO port.
Note: You can download the example code (ZIP format) at the end of this entry.
1 – Description of the rc.local file.
Something we described in the Raspberry Pi and GPIO input (1) entry was the need to manage our GPIO port via file, we need to «export» (using root privileges) the hardware pins to a «file «in path /sys/class/gpio
First idea to any Raspberry Pi user would be to automate this process in order to avoid having to export this hardware every time we want to use our Raspi GPIO port, especially if you want to use this hardware permanently connected to the card.
and, logically, the Raspberry Pi has to be «informed» about the input/output layout used by this little piece of hardware, as :
Pin 8 (GPIO14) digital output
Pin 24 (GPIO8): digital input
but this time we will not only execute the export commands from terminal, but we will tell Linux to run it at startup automatically, and for this reason we need to use the file «rc.local«.
Typically, Linux allows us to execute a series of «custom» commands just at the end of booting process. In order to do this we must use the /etc/rc.local file and place inside the commands that we want to execute on startup.
So, the first thing we will do is to take a look inside the mentioned file (/etc/rc.local) and we observe that contains the following:
2 – Automation of GPIO port «export».
Now we can modify our rc.local file by adding our «export» commands, but to be in the safe side, before we make a backup of the original file using the command:
sudo cp /etc/rc.local /etc/rc.local.org
Now, we modify the file using the command
sudo nano /etc/rc.local
and edit the end part (just before «exit 0«) in this way:
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will «exit 0» on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ «$_IP» ]; then
printf «Mi direccion IP es %s\n» «$_IP»
# Pin GPIO14 (OUT)
echo «14» > /sys/class/gpio/export
chmod 777 -R /sys/class/gpio/gpio14
echo «out» > /sys/class/gpio/gpio14/direction
# Pin GPIO8 (IN)
echo «8» > /sys/class/gpio/export
chmod 777 -R /sys/class/gpio/gpio8
echo «in» > /sys/class/gpio/gpio8/direction
After saving the rc.local file we can restart our Raspberry Pi and verify that files associated with the I/O already exist. To do this we run:
ls -la /sys/class/gpio/
3 – Simple PHP application for GPIO control.
Now, with exported ports, our Raspberry can execute commands from the terminal and control the I/O signals, as we described in the Raspberry Pi and GPIO input (1) previous post.
But let’s go one step further and use a high level programming language and, moreover, this is one of the most used language in the Web world. Using an small PHP application we can manage our Raspberry Pi hardware via network, either from our local network or (if we make the proper settings) from any Web browser capable to access our Raspberry from Internet.
A prerequisite for use this program is to have the PHP enviroment installed on our Raspberry as well a Web server. In Internet there are very detailed descriptions to install it, but we can do so by following the described instructions at DIVERTEKA previous post : HTTP y PHP en Raspberry .
As soon we enabled in our Raspberry the ability to run PHP programs we can create a small application – for example – like this one:
We’ve named xue1.php, and must be located in the folder where tipically Raspi serve Web pages (by default this is /var/www/)
Just placed the file, we need access to our Raspi using their IP by:
Note: Our IP is 192.168.0.22 in this example, but we must use our Raspi IP.
After running the program you can activate (write 1/0) to GPIO output, or check (read 1/0) in the GPIO input.
4 – Simple script to export GPIO port.
Now we know how to automatically export (via the rc.local file) the GPIO hardware, but we can do it in a smarter way. In order to do this we will create a specific script to be called from rc.local file. This way we can – in example – easily enable or disable the call to exporting (co/uncommenting the associated line in the rc.local file) or, in a future, edit the external script for change the hardware configuration controlled by our Raspberry.
To create this script we must execute
sudo touch /etc/gpio.sh
and now, we can edit it with
sudo nano /etc/gpio.sh
Now, all we have to do is give correct permissions to the script
sudo chmod 777 /etc/gpio.sh
and finally, edit the rc.local file again to insert the line that call to our script. Something like this:
sudo nano /etc/rc.local
And now … ¡¡ a DIVERTIRSE !! ( Have Fun !!)