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

Step 1: Knowledge Base

You must have the following running on your Raspberry Pi:
- 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

Okay - Skip this if you have the root account enabled already, otherwise continue reading.

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

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:

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

To start the GPIO programs, do the following:

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

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.
<p>Very good - followed all the instructions, put a led and resistor on and... only some of the pins work....the lower ones - this is on the Raspberry Pi 2 - can't imagine they've changed much?</p>
<p>Great instructable, just what I was looking for. How hard would it be to incorporate a game controller in this. I have one of the old XBOX 1 controllers that has been converted to USB how would I get the data from this controller displayed on my web page and thus use it to control things on my Pi ? Thanks again ...</p>
<p>Thank you for a great instructable!</p><p>I was wondering - is it difficult to adjust one or more of the GPIO buttons so that it's not a &quot;LATCH&quot; but a &quot;NON-LATCH&quot; button...?</p>
<p>i would guess that in GPIOServer.sh for the pin you wanted to trigger, instead of having </p><p>if [ &quot;$status(number)&quot; = &quot;1&quot; ]; then<br> echo &quot;1&quot; &gt; /sys/class/gpio/gpio(number)/value<br> echo &quot;GPIO 23 Turned On&quot;<br> else<br> echo &quot;0&quot; &gt; /sys/class/gpio/gpio23/value<br> echo &quot;GPIO 23 Turned Off&quot;</p><p>you would have </p><p>if [ &quot;$status(number)&quot; = &quot;1&quot; ]; then<br> echo &quot;1&quot; &gt; /sys/class/gpio/gpio(number)/value</p><p>sleep 1</p><p>echo &quot;0&quot;&gt;/sys/class/gpio/gpio(number)/value<br> else<br> echo &quot;0&quot; &gt; /sys/class/gpio/gpio(number)/value</p>
<p>Hi there! I followed your tutorial, and can control leds, but they.blink when on. Is there a software fix, or should i attach capacitors to the lights? Great article btw, really useful for a noob at web development</p>
<p>Used an 8 channel solid state relay wired to my pi to control outlets remotely</p>
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 &quot;phpmyadmin&quot; account. i dont know why i cannot get into this last part. any help and or reply will be appreciated. thank you.
<p>for mine, I have to put in ipaddress/control.php</p>
<p>7.9.2014</p><p>Hello.</p><p>This is a good tutorial. However I am puzzled why it is neccessary</p><p>to write a GPIO value to MySQl then read the value out again.</p><p>This seem to be a high overhead. Can one not read a GPIO value</p><p>via Python in Apache and then display it? This would be much faster.</p><p>Perhaps the SPI and I2C data must be stored in MySQL.</p><p>Please comment - I need to learn much more about Web and displaying variables from C , Java or Python.</p><p>Thank you</p>
<p>You could do it either way, but in my opinion you use the right tools for the job... You let apache do what it does best, and you let the OS do what it does best. The GPIO files are not in the www directory, so it shouldn't be apache's job to read them.. The OS can read the GPIO files, so let it.</p>
<p>5.11.2014</p><p>Thank you for your input.</p><p> Now, one can use hmtl and php5 under Apache to create a Website,</p><p>Can one not then access the system using HTML or PHP? Is it possible then to start a Python Programme under HMTL/PHP5 and so access to GPIO but I am unsure about passing that variable to Apache for display. </p><p>When SQL is used writing/reading is performed from the SD Card and so is very dependant on the speed of that SD Card - if other programmes/threads also use SQL then it will take longer.</p><p>This is to help me learn about the interaction between Apache and the sysem.I still need to get more information - any tips here most welcome.</p><p>Thank you for your assistance and help</p><p>BG</p>
<p>Yes, you can definitely access the system using PHP. I tried the method from this instructible, but it's just got too much overhead for my setup. I'm running an original B1, only 256MB RAM, and a small SD card. I used wiringPi to get command-line reading and setting of the gpio, and then wrote a short PHP page that kicks off gpio commands as needed. With that solution there's no need for mysql. A python solution could work similarly, but I'm much more familiar with PHP, so that's the route I went.</p><p>This instructible is still very much appreciated. It got me closer to where I wanted to go.</p>
Hello, thank you very much for that detailled tutorial.<br>Is there a way to avoid running the GPIOServer script ?<br>I would like to run the script once inside the control.php<br>That would avoid continious requests to the DB.<br>But as I'm a noob with PHP, I don't know if that is so simple ? <br>Regards
Hello, thank you very much for that detailled tutorial.<br>Is there a way to avoid running the GPIOServer script ?<br>I would like to run the script once inside the control.php<br>That would avoid continious requests to the DB.<br>But as I'm a noob with PHP, I don't know if that is so simple ? <br>Regards
It is public the address is https://github.com/linwiz/Raspberry-Pi-Web-GPIO
<p>hey thanks for sharing your update!!<br><br>i set all up and it works :)<br><br>I am using the MCP23017 GPIO extender, do you know what changes I have to configure to make it run with that too?</p>
I do not know about the MCP23017, sorry. It is best to take questions like this over to github; https://github.com/linwiz/Raspberry-Pi-Web-GPIO/issues<br><br>I am sure that this can be modified for MCP23017 and I am willing to help, I just need to get information on how to control the GPIO pins with that chip
<p>thanks for your reply!<br>I opened an issue.<br>So this has moved to: </p><p><a href="https://github.com/linwiz/Raspberry-Pi-Web-GPIO/issues/1" rel="nofollow">https://github.com/linwiz/Raspberry-Pi-Web-GPIO/is...</a><br></p>
<p>Im having this same problem but I can't get into any files to change anything as it boots up and then asks for the wait time. Please help, I really don't want to have to start over and reimage it. </p>
<p>I've made some serious improvements to this. Upgraded to use mysqli, a stronger password hash algorithm, and better interface.</p>
<p>nice job MTec...can you make it public for other , or just for you??yhx</p>
<p>Hi there, I just did this tutorial and it is fabulous! I am trying to make a website where you can log in and the following page will have a button and a live feed from a camera. How can I adjust this to make it so the user can only see one button and feed from the pi camera? any ideas?</p>
Whenever I use ./GPIOServer.sh and enter in 5 for the wait time it returns with:<br> ./GPIOServer.sh: 53: [: out: unexpected operator<br> ./GPIOServer.sh: 65: [: out: unexpected operator<br> ./GPIOServer.sh: 77: [: out: unexpected operator<br> ./GPIOServer.sh: 89: [: out: unexpected operator<br> ./GPIOServer.sh: 101: [: out: unexpected operator<br> ./GPIOServer.sh: 113: [: out: unexpected operator<br> ./GPIOServer.sh: 125: [: out: unexpected operator<br> ./GPIOServer.sh: 137: [: out: unexpected operator<br> <br> Any ideas why?
Hi there. <br>Does this only happen with a wait time of 5?<br>Many thanks, Dan.
<p>Hi.</p><p>I have the same problem with every wait time :(</p><p>What shall I do?<br>Thanks in advance</p>
<p>You have to change the code (replacing '==' with '='). Even though you are using bash (check with 'echo $SHELL') the '==' can cause unexpected errors.</p><p>if[<strong>&quot;$direction4&quot;</strong>=<strong>&quot;out&quot;</strong>];then</p><p><strong>echo</strong><strong>&quot;out&quot;</strong>&gt; /sys/class/gpio/gpio4/direction</p><p>if[<strong>&quot;$status4&quot;</strong>=<strong>&quot;1&quot;</strong>];then</p><p><strong>echo</strong><strong>&quot;1&quot;</strong>&gt; /sys/class/gpio/gpio4/value</p><p><strong>echo</strong><strong>&quot;GPIO 4 Turned On&quot;</strong></p><p>else</p><p><strong>echo</strong><strong>&quot;0&quot;</strong>&gt; /sys/class/gpio/gpio4/value</p><p><strong>echo</strong><strong>&quot;GPIO 4 Turned Off&quot;</strong></p><p>fi</p><p>else</p>
<p>Hi.</p><p>I have the same problem with every wait time :(</p><p>What shall I do?<br>Thanks in advance</p>
<p>Hi i have same problem... I remake it for banana pi and first day it works. And second day i turn on same script and it write same erors. Did you find why ? </p>
<p>superb tutorial but the biggest problem is when we reboot the device we have to run &quot;<strong>./GPIOServer.sh</strong>&quot; code again and again. what can i do for that? </p>
<p>to start the script aumatically you have to move it into /etc/init.d</p><p>ps. sorry for my english i am italian</p>
<p>I had completed step 5 but when i type username &quot;admin&quot; and password &quot;gpio&quot; it incorrect and can't login </p>
<p>Great tutorial. Everything works perfectly.</p><p>Keep up the good work.</p>
<p>Hi !</p><p>Thanks for the tutorial for doomies (as I am), but...</p><p>How would it be with Sqlite?</p>
What is the port for the ipaddress/control.php website?
<p>Here is the link to my version of this, for anyone who is interested</p><p>https://github.com/linwiz/Raspberry-Pi-Web-GPIO</p>
<p>Very nice tutorial, as always, Daniel. I just have to learn to be a stickler for following instructions. You present a lot of material - i just have to pay attention.</p><p>Thank you very much!</p>
<p>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</p>
&quot;sudo cp /root/GPIOServer.sh /etc/init.d/GPIOServer.sh <br>sudo chmod +x /etc/init.d/GPIOServer.sh <br>sudo update-rc.d GPIOServer.sh defaults&quot; <br> <br>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?
<p>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&acute;ve taken a delay of 30s.</p><p>If this is your probably problem, try the Pi on a display and you hopefully are able to stop the scipt with Ctr+C.<br><br>(the Answer 12 Months too late, but if someone els has the problem ;))</p>
<p>Awesome, Is it possible to set up a schedual to control pins seconds per day?</p>
<p>Great project</p><p>Help a lot</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/7Up5ecGMdoE" width="500"></iframe></p><p>Just a demonstration of my automation</p><p>Thank you</p>
<p>Great instruction... no problems yet. How would I be able to add GPIO from the MCP23017 chip into this system?</p>
<p>I haven't played with this chip, but from what I can tell it uses the i2c interface, and comes with it's own &quot;i2cset&quot; commands.</p><p>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.</p>
<p>Hi ~ I meet a problem by running shell script</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/LDznhMrQmnk" width="500"></iframe></p><p>On / Off is sure OK , I press to &quot;Off&quot; , and relay will open , and show the red LED will be light , and press 'On' , relay will close.</p><p>But I find a problem , when 'On' , the red let will a little light, sometimes the relay will be open ....</p><p>just running shell script will be this problem .</p><p>@@</p>
<p>Hi - I'm not sure I entirely understand your problem and unfortunately your video isn't working for me. </p><p>Two things to check</p><p>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.</p><p>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. </p>
<p>How can I start the <strong>./GPIOServer.sh</strong> on startup of the raspberry PI? I cant seem to figure it out. Any help please.</p>
<p>Did you try putting the script path into /etc/rc.local ?</p><p>Make sure you specify the complete directory path /full/path/GPIOServer.sh ./GPIOServer.sh is a relative path and likely won't work.</p>
<p>Thank you so much for putting up this instructable - this was a great starting point for my own GPIO control webpage.</p><p>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)</p><p>I ended up using WiringPI to do this, after the &quot;TurnOn&quot; 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. <a href="http://wiringpi.com/" rel="nofollow">http://wiringpi.com/</a></p><p>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.</p><p>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. <a href="http://www.willmaster.com/library/manage-forms/replacing-submit-button-with-loading-image.php" rel="nofollow">http://www.willmaster.com/library/manage-forms/rep...</a></p><p>Once again - thanks for this example, it got a noob at RPI up and started in no time!</p>
<p>The first script that worked ou of the box!</p><p>Thank you for sharing!</p><p>@ dpaterson1: Hi, this is exactly what I am working on. Would you mind sharing your script as well? </p>

About This Instructable




Bio: Hi there, I'm an engineering apprentice with a passion for computing.
More by drcurzon:
Add instructable to: