This is an updated version of my previous Instructable: Control Stuff with your Raspberry Pi (GPIO) Over the internet.
This version is much more secure and could easily be used across the internet with very few security concerns.
A brief outline:
- A server program that runs on the Raspberry Pi to read variables
- A MySQL database to store the variables in
- An Apache2 served webpage to allow control of the variables
Step 1: Knowledge Base
- Apache web server
- PHP5
- MySQL Server
- phpMyAdmin would help
You can find instructions for this here.
It may help to read up on my other Instructable: Control Stuff with your Raspberry Pi (GPIO) so you know what;s going on.
Step 2: Enable Root Account
Login to your Raspberry Pi with your username and password, then type the following:
sudo -i
passwd root
Now, type in (and confirm) a password for the root account.
Then you need to colse the SSH session, and restart it - logging in as root.
Step 3: PhpMyAdmin & Database Setup
I am assuming that you have phpMyAdmin set up, alongside PHP5. If not, click here for an Instructable on how to do this.
Firstly, download the sql file from here.
Login to your phpMyAdmin control panel, then press the "Import" button on the top bar.
Now, under the "File to Import" heading, click the "Choose File" button, and select the file you downloaded previously (gpio.sql).
Finally, at the bottom of the page, click the "Go" button.
This will set up all of the tables needed to ensure the the script functions as it should on your Raspberry Pi.
Now, you need to add a user to the database from within phpMyAdmin. To do this;
Click the "Users" button on the top bar.
Now click the "Add User" link (about half way down the page on the left).
In the "User name" field, enter a suitable username. I went with "gpio".
In the "Host" field, enter "localhost".
Then in the two password fields, enter a suitable password. (No spaces, Hypehens or special characters). I went with "pr03ND2".
Now leave all of the rest as default, then click the "Add User" button on the bottom right.
The final part for this step is to give the user the correct privileges.
Click the "Users" button on the top bar, then scroll down until you see the user you have just added in the "Users Overview" table.
Across from the username, click the "Edit Privileges" link.
Scroll down to the heading "Database-specific privileges" and select "gpio" from the drop down list box, and click the "Go" button.
Select ALL of the check boxes, then click the "Go" button on the bottom left.
Congratulations - That's the database set up.
Step 4: Shell Script
This script is pretty simple, but does require setting up.
Firstly, download the script by inputting the commands below (on your Raspberry Pi).
sudo -i and hit Return / Enter.
wget http://raspberrypi-gpio.googlecode.com/files/GPIOServer.sh and hit Return / Enter.
Once this has downloaded, type in the following:
chmod +x GPIOServer.sh and hit Return / Enter.
nano GPIOServer.sh and hit Return / Enter.
This will allow you to edit the script.
You must change the following variables at the top of the file:
mysqlusername="USERNAME HERE"
mysqlpassword="PASSWORD HERE"
These must be changed to the username and password you created previously in phpMyAdmin.
Once these have been changed, hold the Ctrl key and press x, then release the Ctrl key and press y, then hit Return / Enter.
That's it for the shell scripting session.
Step 5: Web Page Setup
For this, type the following commands, ensuring you are logged in as root. (type sudo -i if you're not).
wget http://raspberrypi-gpio.googlecode.com/files/control.php and hit Return / Enter.
wget http://raspberrypi-gpio.googlecode.com/files/off.jpg and hit Return / Enter.
wget http://raspberrypi-gpio.googlecode.com/files/on.jpg and hit Return / Enter.
Once they have downloaded, type in:
mv control.php /var/www/control.php and hit Return / Enter.
chmod 755 /var/www/control.php and hit Return / Enter.
mv off.jpg /var/www/off.jpg and hit Return / Enter.
chmod 755 /var/www/off.jpg and hit Return / Enter.
mv on.jpg /var/www/on.jpg and hit Return / Enter.
chmod 755 /var/www/on.jpg and hit Return / Enter.
You must edit a few variables in the file before use, so type:
nano /var/www/control.php and hit Return / Enter.
Change the following variables:
$MySQLUsername = "USERNAME HERE";
$MySQLPassword = "PASSWORD HERE";
Now, navigate in your web browser to the control.php page. (mine is http://raspberryPi/control.php) where raspberryPi is your host name.
It will ask you to login with the following credentials:
Username: admin
Password: gpio
I recommend clicking the "Change Password" link at the top of the page, and changing the password for obvious reasons.
That's the end of the Web Page section.
Step 6: Usage
Start an SSH session with your Raspberry Pi, and login as root, then type in:
./GPIOServer.sh and hit Return / Enter.
It will ask you to input a wait time, this will depend upon your application needs, but the shorter the wait time, the more resources the script will use. (I generally use 5).
Now login to the web control interface, (http://HOSTNAME/control.php), login and enjoy.
Step 7: Overview
For security reasons, you should now disable the root account
To keep updated on the latest releases, or to report a bug / glitch - go to http://code.google.com/p/raspberrypi-gpio/
All comments and criticisms are welcome.
Hope this helps, Daniel.















































Visit Our Store »
Go Pro Today »




but in output GPIO with LED not working?
anyone can fix this problem?
i has complete it and it work verry good.
i has complete it and it work verry good.
sleep 30
/var/www/GPIOServer.sh > /dev/null
Also, I added a line at the very beginning of the GPIOServer.sh
#!/bin/bash
I hope this helps.
i am unable to run the script at startup
i get the following errors
update-rc.d: using dependency based boot sequencing
insserv: warning: script 'GPIOServer.sh' missing LSB tags and overrides
insserv: warning: script 'myserver' missing LSB tags and overrides
insserv: There is a loop between service rpc_shellinaboxd and GPIOServer.sh if stopped
insserv: loop involving service GPIOServer.sh at depth 2
insserv: loop involving service rpc_shellinaboxd at depth 1
insserv: Stopping GPIOServer.sh depends on rpc_shellinaboxd and therefore on system facility `$all' which can not be true!
insserv: Stopping myserver depends on rpc_shellinaboxd and therefore on system facility `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
plz help
I have a question that I have not found the answer to yet and I'm not all that familiar with PHP.
When I log on to the Control.PHP page with an iPhone, the page is very small and I have to zoom in. I also have to re-zoom every time I push a button.
Is there a way to set the screen resolution for an iPhone or other mobile device in the PHP script?
Again, thanks so much. This is so much fun!
I believe that has to be set in the HTML and CSS of the page.
Sorry I can't be of more help.
Thanks, Dan.
I discovered a bug :
with safari on mac, if I use port 8000, I can't login any more...
il I delete the line : session_regenerate_id
frome the control.php script, It works ok...
is it a problem to remove that line ?
thank you.
There's no real issue with removing that line, all it does is help towards the security of the login script, but shouldn't affect the system in any way.
Thanks, Dan.
I love your script, and I have 2 questions :
1 - where do you put the script GPIOServer.sh ? do you leave it in /root ?
2 - I would like it to start on boot of the raspberry, but I don't really know to create a init.d file... I did try but had an error when updating rc.d :
insserv: warning: script 'K01GPIOServer' missing LSB tags and overrides
insserv: warning: script 'GPIOServer' missing LSB tags and overrides
thank you
jean
Are you running the script as root?
Many thanks, Dan.
./GPIOServer.sh: 53: [: out: unexpected operator
./GPIOServer.sh: 65: [: out: unexpected operator
./GPIOServer.sh: 77: [: out: unexpected operator
./GPIOServer.sh: 89: [: out: unexpected operator
./GPIOServer.sh: 101: [: out: unexpected operator
./GPIOServer.sh: 113: [: out: unexpected operator
./GPIOServer.sh: 125: [: out: unexpected operator
./GPIOServer.sh: 137: [: out: unexpected operator
Any ideas why?
Does this only happen with a wait time of 5?
Many thanks, Dan.
I think that the piface board is a solution in its own right, however it probably could be controlled by the scripts if you really wanted.
Many thanks, Dan.
This information will be pretty usefull for mi proyect:
I am building a clock for the city hall in my town "Jalos" (in Mexico) that is able to configure via the web for people with privileges to schedule it in the departament of social communication. I built the mechanism and its controller that connects to the RPi. I use a DS1302 with its backup battery to avoid errors by power outage. Three actuators, which control the motor of hand's axis, the bell quarters and the bell of hours. This machine will play songs of my town at each hour with an amplifier, for this i use the program mpg123 that plays ogg and mp3 files.
Just need to use reading encoders that tell me the position of the hands.
I wonder how can I scan/read the pins of GPIO as inputs?
Uffortunally it boots, but then hangs in a loop of errors. after entering the wait time.
Cant find MySQL database etc bla bla...this because it is started to early.
I cant seem to kill the loop so it can continue to boot further..
Any ideas? btw im a real noob in linux...
As a work around, you could perhaps add a delay to the start of the script.
i.e. a For loop that goes for 30 seconds before allowing the rest of the script to run.
Many thanks, Dan.
keep up the good work!
Glad you got it all working :) - have fun with it.
At the moment I am planning to re-write the script to make it easier to set up, and less resource intensive (so faster updating), although this will be a few months off, as I'm quite busy with other projects at work.
Many thanks, Dan.
I've got one question about it.
Is there possibility to add another button "pulse" to each GPIO on control.php?
When I press the button I need 2 secs of signal
Many thanks :)
With the way I've set the scripts up, EXACT timing is not possible - but you could get near 2 seconds (maybe half a second out).
There are 2 ways of doing this:
Javascript in the browser
Modify the shell script and database and browser files.
The javascript method is probably the easiest and you'd have to add a button to the file with a javascript timer so that it automatically changes the database variable after 2 seconds.
For this, there is probably a better solution out there using python to perform the actions you request.
Many thanks, Dan.
1) how do I get the script to run at reboot?
2) when I reboot and run it manually, it sets all pins "high" - and updates the database. How can this be solved?
Login as your user, then input the following:
sudo cp /root/GPIOServer.sh /etc/init.d/GPIOServer.sh
sudo chmod +x /etc/init.d/GPIOServer.sh
sudo update-rc.d GPIOServer.sh defaults
Reboot and that should start the script on boot.
Many thanks, Dan.
2. (Easiest to start with) I believe it is in the architecture of the Raspberry pi to set all pins high on boot up, however I may be incorrect.
I don't know of any way of changing this (but would be very interested if you found one).
1. I'll post a reply to your comment tomorrow, although I believe it to be a simple thing. Just a bit late here :)
Many thanks, Dan.
Second, I *really* don't know much about php, how secure is the transmission of the password?
It's great that you want to build off of this! :)
No restrictions on the code - use it as you wish.
If you could link to the instructable, that'd be great thanks.
The password should be fine for transmission over the Internet as it's hashed and salted, so should be fine.
Let me know how it goes?
Many thanks, Dan.
I'll send you some code to put into the web page tomorrow that will automatically update the page if you want?
Many thanks, Dan.
Sorry about the lack of a reply, completely forgot.
I'm away on work at the moment, but I'll try my best to get that code sorted for you.
Again, sorry for no reply, Dan.
How good is your JavaScript?
The simplest way I can think of doing this is to refresh the page every x seconds, so that the updates will show.
In the header of the file, add:
NOTE: Change the 3000 for any millisecond value you want.
Hope this helps, Dan.
Thanks, Dan.
### Script Python ###
#!usr/bin/python
from time import sleep;
import RPi.GPIO as GPIO;
import MySQLdb
GPIO.setmode(GPIO.BCM)
conn = MySQLdb.connect('localhost','user','password','table_name');
while True:
curs = conn.cursor();
result = curs.execute("select * from pinStatus");
result = curs.fetchall()
for row in result:
pinNumber = int(row[1])
pinStatus = True if int(row[2]) == 1 else False
GPIO.setup(pinNumber, GPIO.OUT)
GPIO.output(pinNumber, pinStatus)
curs.close()
sleep(2)
##############
And the crontab looks like:
@reboot /home/toto/script.py
(@reboot means that the following script will be launch everytime the computer is started)
Any comments or advice are welcome