Instructables
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 172Next »
LlewellynR8 days ago

Thank you for a great instructable!

I was wondering - is it difficult to adjust one or more of the GPIO buttons so that it's not a "LATCH" but a "NON-LATCH" button...?

seb13gtt1 month ago
Hello, thank you very much for that detailled tutorial.
Is there a way to avoid running the GPIOServer script ?
I would like to run the script once inside the control.php
That would avoid continious requests to the DB.
But as I'm a noob with PHP, I don't know if that is so simple ?
Regards
seb13gtt1 month ago
Hello, thank you very much for that detailled tutorial.
Is there a way to avoid running the GPIOServer script ?
I would like to run the script once inside the control.php
That would avoid continious requests to the DB.
But as I'm a noob with PHP, I don't know if that is so simple ?
Regards
MTec0071 month ago
It is public the address is https://github.com/linwiz/Raspberry-Pi-Web-GPIO
schosch MTec0071 month ago

hey thanks for sharing your update!!

i set all up and it works :)

I am using the MCP23017 GPIO extender, do you know what changes I have to configure to make it run with that too?

MTec007 schosch1 month ago
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

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
schosch MTec0071 month ago

thanks for your reply!
I opened an issue.
So this has moved to:

https://github.com/linwiz/Raspberry-Pi-Web-GPIO/is...

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.

MTec0075 months ago

I've made some serious improvements to this. Upgraded to use mysqli, a stronger password hash algorithm, and better interface.

photo 1.PNGphoto 2.PNGphoto 3.PNGphoto 4.PNG

nice job MTec...can you make it public for other , or just for you??yhx

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?

Whenever I use ./GPIOServer.sh and enter in 5 for the wait time it returns with:
./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?
drcurzon (author)  checkers3131 year ago
Hi there.
Does this only happen with a wait time of 5?
Many thanks, Dan.

Hi.

I have the same problem with every wait time :(

What shall I do?
Thanks in advance

You have to change the code (replacing '==' with '='). Even though you are using bash (check with 'echo $SHELL') the '==' can cause unexpected errors.

if["$direction4"="out"];then

echo"out"> /sys/class/gpio/gpio4/direction

if["$status4"="1"];then

echo"1"> /sys/class/gpio/gpio4/value

echo"GPIO 4 Turned On"

else

echo"0"> /sys/class/gpio/gpio4/value

echo"GPIO 4 Turned Off"

fi

else

Hi.

I have the same problem with every wait time :(

What shall I do?
Thanks in advance

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 ?

notworks.jpg

superb tutorial but the biggest problem is when we reboot the device we have to run "./GPIOServer.sh" code again and again. what can i do for that?

to start the script aumatically you have to move it into /etc/init.d

ps. sorry for my english i am italian

I had completed step 5 but when i type username "admin" and password "gpio" it incorrect and can't login

NigelT14 months ago

7.9.2014

Hello.

This is a good tutorial. However I am puzzled why it is neccessary

to write a GPIO value to MySQl then read the value out again.

This seem to be a high overhead. Can one not read a GPIO value

via Python in Apache and then display it? This would be much faster.

Perhaps the SPI and I2C data must be stored in MySQL.

Please comment - I need to learn much more about Web and displaying variables from C , Java or Python.

Thank you

MTec007 NigelT13 months ago

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.

Bonzadog MTec0072 months ago

5.11.2014

Thank you for your input.

Now, one can use hmtl and php5 under Apache to create a Website,

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.

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.

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.

Thank you for your assistance and help

BG

AidanC13 months ago

Great tutorial. Everything works perfectly.

Keep up the good work.

matthias2t3 months ago

Hi !

Thanks for the tutorial for doomies (as I am), but...

How would it be with Sqlite?

yashmehta73 months ago
What is the port for the ipaddress/control.php website?
MTec0075 months ago

Here is the link to my version of this, for anyone who is interested

https://github.com/linwiz/Raspberry-Pi-Web-GPIO

johngriswold5 months ago

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.

Thank you very much!

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 ;))

viggen7 months ago

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

NewHouseTM8 months ago

Great project

Help a lot

Just a demonstration of my automation

Thank you

NewHouseTM8 months ago
joedix10 months ago

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

dpaterson1 joedix9 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.

scorpionx11 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.

acsooley10 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.

1-40 of 172Next »