Web Control of Raspberry Pi GPIO (UPDATED) by drcurzon
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
1-40 of 92Next »
vantanevn says: May 1, 2013. 8:23 AM
MySql data is installed on SD card. But SD card only rewrite about <1000 times. So if you other file (data) on SDRAM it will work perfect.
gwiguna says: Apr 18, 2013. 7:10 AM
this great i has complete it,
but in output GPIO with LED not working?
anyone can fix this problem?
vantanevn says: May 1, 2013. 8:21 AM
My raspberry is type B version 2. So GPIO 21 not work (it is GPIO 27). But GPIO17 too. So you can test other pin.
i has complete it and it work verry good.
vantanevn says: May 1, 2013. 8:21 AM
My raspberry is type B version 2. So GPIO 21 not work (it is GPIO 27). But GPIO17 too. So you can test other pin.
i has complete it and it work verry good.
tunafreedolphin says: Apr 30, 2013. 6:11 AM
To make mine start up automatically, I put this line just before the "exit 0" in the /etc/rc.local file,

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.
looks_456 says: Apr 15, 2013. 3:38 AM
Hi,
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
thecank says: Apr 6, 2013. 1:19 PM
Great tutorial!! I finally got this working as well as my wireless adapter!!
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!
drcurzon (author) says: Apr 6, 2013. 1:21 PM
Hi there,
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.
jduffas says: Apr 6, 2013. 4:17 AM
Hi,

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.
drcurzon (author) says: Apr 6, 2013. 8:35 AM
Hi there.
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.
jduffas says: Apr 4, 2013. 2:49 PM
hi !
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
checkers313 says: Apr 1, 2013. 6:20 PM
Sorry, no it does it with any wait time I try
drcurzon (author) says: Apr 1, 2013. 11:18 PM
Hi there.
Are you running the script as root?
Many thanks, Dan.
checkers313 says: Mar 31, 2013. 5:14 PM
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) says: Apr 1, 2013. 2:31 AM
Hi there.
Does this only happen with a wait time of 5?
Many thanks, Dan.
ashah8 says: Mar 23, 2013. 5:47 PM
Is there an easy way to convert the control webpage so its more compatible with mobile browser?
Nechako says: Mar 20, 2013. 6:49 AM
Thanks for the quick reply, I'll give it a go!! Awesome tutorial.
thecank says: Mar 19, 2013. 7:44 AM
Great instructable! I am going to have fun with this. I do have one question that I can't figure out even after some research; When I view the page on my iPhone the text is very small. I have not coded HTML since the 90's and don't know anything about CSS or PHP. Is there a way to adjust resolution in your control.php script based on browser or screen size?
Nechako says: Mar 18, 2013. 6:44 PM
Can this be adapted to work with the piface board?
drcurzon (author) says: Mar 19, 2013. 2:50 AM
Hi there.
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.
javilondo says: Mar 16, 2013. 2:11 AM
Thanks a lot for this great instructable!
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?
pd5dj says: Mar 10, 2013. 1:17 PM
Hi all, i have tried to let it auto start during boot.
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...
drcurzon (author) says: Mar 10, 2013. 2:13 PM
Hi there,
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.
Palmerfarmer says: Mar 10, 2013. 11:09 AM
Great Instructable, was my first attempt at Linux/pi/configuring etc and managed to get it working too! was wondering if you had any plans to do a version that incorporated looking at inputs too? Many Thanks Ian
keep up the good work!
drcurzon (author) says: Mar 10, 2013. 11:29 AM
Hi there,
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.
H_C says: Mar 10, 2013. 4:56 AM
Thanks for great tutorial :)
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 :)
drcurzon (author) says: Mar 10, 2013. 9:15 AM
Hi there,
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.
frankdyring says: Feb 21, 2013. 3:22 PM
First of all: great tutorial. Works perfect (just have to add "i" in "service" somewhere, but figured that out ;-)

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?
drcurzon (author) says: Feb 22, 2013. 1:50 PM
Hi there - to get the script to run at boot:
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.
drcurzon (author) says: Feb 21, 2013. 4:03 PM
Hi there.
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.
abishur says: Feb 3, 2013. 8:28 PM
Hey this is abishur over on the r-pi forum I had two questions. One I wanted to add some features to the code you've provided, do you have any restrictions on reuse of your code and if you're okay with it, how would you like to be notated as the originator of the code?

Second, I *really* don't know much about php, how secure is the transmission of the password?
drcurzon (author) says: Feb 3, 2013. 11:27 PM
Hi there.
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.
abishur says: Feb 5, 2013. 7:35 AM
Thanks, I've made a post over on the pi forums with links to the work I've done with it. I confess that my one lament is I have no idea how to set the main page to auto update on the pin status changing, ah well :-)
drcurzon (author) says: Feb 5, 2013. 10:34 AM
Hi there.
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.
abishur says: Feb 21, 2013. 1:47 PM
Did you have any luck with this? I've been working on it, but without any success. Web coding *really* isn't my forte ;-)
drcurzon (author) says: Feb 21, 2013. 4:00 PM
Hi there.
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.
abishur says: Feb 5, 2013. 11:03 AM
Wow that would be amazing thanks!
drcurzon (author) says: Feb 22, 2013. 1:57 PM
Hi there,
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.
drcurzon (author) says: Feb 22, 2013. 2:03 PM
Sorry - editor hid the text. If you PM me your email address, I'll email you the code.
Thanks, Dan.
rikohz says: Feb 10, 2013. 7:35 PM
Hi, thanks a lot for this tutorial, it worked perfectly. I made a few changes to make it work as a service and reduce the number of SQL queries. I used a Python script for that with a crontab, i chose in this case to have an update every 2 seconds. Here is the script:

### 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
1-40 of 92Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!