Instructables

Web Control of Raspberry Pi GPIO (UPDATED)

Hi there,
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
 
Remove these adsRemove these ads by Signing Up

Step 3: PhpMyAdmin & Database Setup

This solution relies on a MySQL Database, so let's set it up!

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 is the part that runs on your Raspberry Pi checking for the values in the MySQL Database.

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

Okay - the final step before you can use this is to set up the webpage.

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 7: Overview

This concludes the Instructable.

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.
1-40 of 146Next »

Hello, How are you doing? i got one question im trying to do this but whit the UDOO, but im finding some problems with the GPIOServer.sh, i had never used a Raspberry Pi before... but i don't think in the UDOO you have to invoke the GPIO, this is how it works in the UDOO http://www.udoo.org/ProjectsAndTutorials/linux-gpio-manipulation/ so if you could help me I will be more than grateful. Thanks

bejot1 year ago
"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"

Hi, I just did what you wrote and I probably have boot loop and can't login to RPI by SSH. How can I undo it?

I think I have read and avoidet, that starting a script on the raspberry automaticalli with a too litle delay before starting could end in a endless loop and stops the Pi of booting, I´ve taken a delay of 30s.

If this is your probably problem, try the Pi on a display and you hopefully are able to stop the scipt with Ctr+C.

(the Answer 12 Months too late, but if someone els has the problem ;))

viggen1 month ago

Awesome, Is it possible to set up a schedual to control pins seconds per day?

NewHouseTM2 months ago

Great project

Help a lot

Just a demonstration of my automation

Thank you

NewHouseTM2 months ago
joedix4 months ago

Great instruction... no problems yet. How would I be able to add GPIO from the MCP23017 chip into this system?

dpaterson1 joedix3 months ago

I haven't played with this chip, but from what I can tell it uses the i2c interface, and comes with it's own "i2cset" commands.

You'd need to add database entries to represent the additional pins (so you can track thier state and show on webpage), and you'd need to modify GPIOServer.sh to use i2cset commands instead of the /sys/class/gpio commands used for regular GPIO pins.

scorpionx5 months ago

Hi ~ I meet a problem by running shell script

On / Off is sure OK , I press to "Off" , and relay will open , and show the red LED will be light , and press 'On' , relay will close.

But I find a problem , when 'On' , the red let will a little light, sometimes the relay will be open ....

just running shell script will be this problem .

@@

Hi - I'm not sure I entirely understand your problem and unfortunately your video isn't working for me.

Two things to check

1. Is your relay designed to work with 3.3 volt from the GPIO pins? I found a lot of 5volt relays but not all work reliably with the RPI. This could cause inconsistent results.

2. The relay I use inverts my logic. If I send 0 volts to the RPI GPIO pin, this turns the Relay on (and powers my connected device). If I set the GPIO HIGH (3.3 volts) it turns my relay off.

acsooley4 months ago

How can I start the ./GPIOServer.sh on startup of the raspberry PI? I cant seem to figure it out. Any help please.

Did you try putting the script path into /etc/rc.local ?

Make sure you specify the complete directory path /full/path/GPIOServer.sh ./GPIOServer.sh is a relative path and likely won't work.

dpaterson1 made it!4 months ago

Thank you so much for putting up this instructable - this was a great starting point for my own GPIO control webpage.

I made a few modifications as I wanted a real-time response (instead of waiting for the GPIOserver.sh to loop around), and a simple way to group pins together with time delays (turning on an Amp before the speakers in an auditorium etc)

I ended up using WiringPI to do this, after the "TurnOn" button was pressed it calls a Shell Script that calls WiringPI commands. I still track the state of the pins in the SQL database though. http://wiringpi.com/

As my system is used by many people I also added a login page, it also sends daily emails via shell script cron job that summarises which users turned items on and off during the day.

As some of my TurnOn/TurnOff scenario's add delay into the webpage reloading, I also used this guys Javascript loading image code to the mix to stop form resumbissions. http://www.willmaster.com/library/manage-forms/rep...

Once again - thanks for this example, it got a noob at RPI up and started in no time!

PClayout.jpgMobilelayout.jpg
hilbo dpaterson14 months ago

The first script that worked ou of the box!

Thank you for sharing!

@ dpaterson1: Hi, this is exactly what I am working on. Would you mind sharing your script as well?

dpaterson1 hilbo3 months ago

Here's an example shell script I stored in /opt/scripts/onTest.sh

Make sure onTest.sh is executable "chmod 755 onTest.sh"

*****onTest.sh contents*****

#!/bin/sh

#WiringPi GPIO 1 = BCM 18 = Physical Header 12

#http://wiringpi.com/pins/

#Set GPIO 1 = PIN 12 as an Output

gpio mode 1 out

#Change GPIO 1 voltage to High

gpio write 1 1

*******PHP Code*****

$groupOnScript = "/opt/scripts/onTest.sh";

shell_exec($groupOnScript);

m3mn0t dpaterson13 months ago

Can you help me ?Can you provide your code? or simple help me with something?

m3mn0t3 months ago

Hi how can i talk with you

drcurzon??

imohammad5 months ago

hello there, thank you for the tutorial~!! i want to ask, if the gpio is set to input. If i gave the input a signal (on) does the web page update the input status?

I don't believe it does - GPIOServer.sh would need to be modified to read the status of the GPIO pins, then load it into the mysql database.

Then the web page would read the database and show the status.

thanks for the info ^^

Hello, Wondering if you can help. Great tutorial by the way however i have followed everything through but when i type ./GPIOServer.sh and enter a value it automatically turns on any GPIO pins that are connected and then nothing plays ball after. Any ideas of where i am going wrong? Cheers
drcurzon (author)  aztecthegeordie7 months ago
Hi there - can you send me the GPIOServer.sh file?
Thanks - Dan.
dhill287 months ago
I'm struggling to get the actual pins to work. From what I can tell, things are installed correctly, and if I go into the actual database, I can see that the pin status setting in the database is changing when I click the buttons, but I don't get anything out of the pins at all. Is there anything particular I should be looking at?
drcurzon (author)  dhill287 months ago
Hi there, Have you tried manually setting the GPIO pins?
Try the following commands (should set pin 21 high)

echo "21" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio21/direction
echo "1" > /sys/class/gpio/gpio21/value

To turn off:
echo "0" > /sys/class/gpio/gpio21/value
echo "21" > /sys/class/gpio/unexport

Let us know how it turns out - Dan.
jeryhawj8 months ago
when i put in http://raspberryPi/control.php(raspberryPi=my ip address), all i get is an invalid request. can someone tell me if i need to do something else. i have followed every instruction on here... and even went back to all the old pages to make sure i did everything correct. i even have access to "phpmyadmin" account. i dont know why i cannot get into this last part. any help and or reply will be appreciated. thank you.
winsum10 months ago
Hi drcurzon,
your instructable is very great!

I have only one problem. When i execute the ./GPIOServer.sh, it return two errors:

line 54: /sys/class/gpio/gpio4/direction: File or directory not exist.
line 59: /sys/class/gpio/gpio4/value: File or directory not exist.
GPIO 4 Turned Off
GPIO 17 Turned Off
GPIO 18 Turned Off
GPIO 21 Turned Off
GPIO 22 Turned Off
GPIO 23 Turned Off
GPIO 24 Turned Off
GPIO 25 Turned Off

Why this error?
Very thanks.
Mickey_Boy winsum8 months ago
i have had a similar error, turned out that i had some errors when exporting and selecting direction for the gpio#.
winsum winsum10 months ago
Hi boys,
i have seeking that the file gpio4 is not present in the folder /sys/class/gpio/

...mmmm
i not know because!
tmcisaac10 months ago
Quick query, just picked up a Raspberry Pi, got this setup and I was wondering. Can the other pins on the GPIO be addressed to add more buttons? (minus the DNC pins of course)
Rather n00b question, I know.
Thanks
buteman10 months ago
Running control.php doesn't seem to do anything. I have edited control.php and put a username and password in but when I enter them on running control.php it just goes straight back to asking for user name and password. I tried refreshing the web page to ensure I was running the modified version, I restarted both mysqld and apache2 but it just doesn't work. Any ideas?
lionikus10 months ago
Great tutorial. But... My Raspberry Pi rev2.. This GPIOserver.sh does not include pin2..
http://elinux.org/RPi_BCM2835_GPIOs ... I added in GPIOserver.sh :
direction2=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinDirection FROM pinDirection WHERE pinNumber='2'";)
and:
status2=$(mysql -B --disable-column-names --user=$mysqlusername --password=$mysqlpassword gpio -e "SELECT pinStatus FROM pinStatus WHERE pinNumber='2'";)
and:
if [ "$direction2" == "out" ]; then
echo "out" > /sys/class/gpio/gpio2/direction
if [ "$status2" == "1" ]; then
echo "0" > /sys/class/gpio/gpio2/value
echo "GPIO 2 Turned On"
else
echo "1" > /sys/class/gpio/gpio2/value
echo "GPIO 2 Turned Off"
fi
and... I added in SQLbase gpio respective lines...
then it worked... But...
My led is flashing every "How long do you want the wait time to be? "...
Weak, but flashes...
How to fix this problem?
Sorry for my english :)
lionikus lionikus10 months ago
Who could be useful it:
bash:
sudo su -
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction
echo "0" > /sys/class/gpio/gpio4/value
echo "1" > /sys/class/gpio/gpio4/value
lionikus lionikus10 months ago
Sorry... It : This GPIOserver.sh does not include pin2.. read as: This GPIOserver.sh does not include gpio2..
lionikus lionikus10 months ago
Note that 0 and 1 interchanged in Turned on and turned off... I do not know why but check first to bash...
maco171710 months ago
Hi, Great tutorial! at last something that work for me. was trying with webGPIO for ages to then realize that the new version dont support to run from apache anymore so can use on my solution.

anyway was wondering what that sleep time? and it takes around 2s for the led to change state when i do trough web is this a normal delay time? would it be quicked with another solutions?

thanks.
drcurzon (author)  maco171710 months ago
Hi there,
Glad you got it working. :)
the sleep time is the time before it updates each of the variables - the smaller the number, then more CPU + RAM usage, etc...
The delay is unfortunately due to the way the script works - there are other solutions - I believe you can call scripts directly from Python.
Many thanks, Dan.
maco1717 drcurzon10 months ago
yeah that's what i was trying, well first i tried php exec() with a bash script but would execute i guess because of permissions i was going to try python but i read that the script also need to be executed by admin so didnt try... is there any way around this?
the bash script was very basic, basically
===
#!/bin/bash

# Set up GPIO 4 and set to output
echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction

# Write output
echo "1" > /sys/class/gpio/gpio4/value
===

and "0" to turn off...

all this in a .sh that was called from a button

==

ON
===

also tried implementing a read status off led

==
exec('sudo cat /sys/class/gpio/gpio4/value',$outputArray);
print_r($outputArray);
?>
===

but as is said no luck...
:(

should i try with python?

exec(sudo python scriptname.py)

???
maco1717 maco171710 months ago
i changed the visudo file to allow www-data apache user to edit files this allowed me to run
==
exec('sudo cat /sys/class/gpio/gpio4/value',$outputArray);
print_r($outputArray);
==

but the buttons are not working...
maco1717 maco171710 months ago
at last got it working.
my problem wasnt permission was not good web programing...

the page is like this now...
===

exec('cat /sys/class/gpio/gpio4/value',$outputArray);
print_r($outputArray);
?>


if ($_GET['on']) {
# This code will run if ?run=true is set.
exec("/var/www/teston.sh");
}
if ($_GET['off']) {
exec("/var/www/testoff.sh");
}
?>


ON

OFF
===

incredibly responsive
drcurzon (author)  maco171710 months ago
Hi there,
Nice to see you got it working :)
Dan.
1-40 of 146Next »