Page 1 of 1

Interfacing website hosted on Raspberry Pi with GPIO pins

Posted: Sat Mar 16, 2013 7:36 am
by DoubleBrowne
Hi there,
I'm a total linux noob embarking on a very ambitious project with my raspberry pi and I'm hoping someone can point me in the right direction for the information that I'm looking for.

I currently have the PbPi set up in a remote location with internet connectivity and have a ddns client installed on it. I'm hoping to use it to host a (very) simple website that will allow me to log in and utilise the GPIO pins to drive relays etc. I'm ok with the electrical side of the whole thing but I have no idea where to start, a) With the website (I'm guessing a simple HTML based one would do the trick) and b) How to set up the website so that I can say, click a button that will trigger a voltage output on one of the GPIO pins for a specified length of time.

Any help and guidance is much appreciated.

Re: Interfacing website hosted on Raspberry Pi with GPIO pin

Posted: Tue Mar 19, 2013 9:31 am
by DBryant
You don't say whether you have written any code to exercise the GPIO and your electronics. If not, then you need to do this before any exact detail could be given to the way you might put your server together. If you have written some code, and you wish to re-use that and integrate into your plans, then this will determine some routes for you.

Your code could be a bash script, in some ways the most basic, through to C++ or Java at the other end of the spectrum. These all have their relative merits, and downsides too, it depends on where you are and what sort of learning curve you wish to embark upon.

If you were to start with a bash script you should consider installing wiringPi https://projects.drogon.net/raspberry-p ... d-install/, this will introduce a new command (gpio) which will provide access to the GPIO pins. This will simplify the look of your script by de-cluttering it of sudo, redirection and so on, making it easier to develop and appreciate what's going on. It hides all the access to the underlying system registers, which may be what you'd like to see, so it may not be for you. So develop a script to do something simple, maybe

Code: Select all

./activateLED.sh -p 10 -s on
where it accepts a pin number (-p 10) and sets a state (-s on). Not the simplest possible, but a few twists like getting options into the script. Obviously you can add more things, like check that the stated pin is valid, the state is only on or off, is case-insensitve and so on, just to brush up the script writing skills.

The script will be invoked as a CGI by the web-server. So you will need Apache2 installed (other ones are available if required) and depending on your distro of choice this may well already be done. This server will define a directory where web pages are to be sourced from, it might be something like /var/www (or not), check you own documentation.

To develop web pages you can use your favourite text editor (vi, vim, nano,...). You could write these on a remote system with more familiar tools and then copy the pages across to the correct location on your Pi.

Create a simply piece of HTML containing some text. Install it to the correct location and see that you can access it will a browser. You may decide you want a Pi local browser, but personally I'd use one on a remote system. Point the browser at your web page and see the result.

You can now think about your LED-activatiing script and running it as a CGI with URLS like

Code: Select all

http://mypi.local/cgi-bin/activeLED.sh?p=10&s=on
The bit from ? onwards in known as the QUERY_STRING and this can be accessed by your script at run-time. I've made the arguments roughly the same as before but the code would have to be modified to handle the QUERY_STRING rather that accept command line arguments. Its not beyond the wit of man to write it so it handles both! Take a look at http://www.yolinux.com/TUTORIALS/BashShellCgi.html for pointers on how to get the CGI up and running. The installation of your cgi script should be verified in the documentation of for your web-server; this scripts can pose a security risk and so are usually coralled quite strongly.

You can write web pages to include things like:

Code: Select all

<a href="http://mypi.local/cgi-bin/activeLED.sh?p=10&s=off">Pin 10 OFF</a>
<a href="http://mypi.local/cgi-bin/activeLED.sh?p=10&s=on">Pin 10 ON</a>
Clicking the line will result in the LED being turned oon or off

You will need to look at Javascript in order to develop the sort of pages that are in your minds eye. It would be tedious to write code for the above for 4 pins (say) and you could do the same with option menu, check/radio boxes and so on. These can be constructed in basic Javascript, or you might take a look at JQuery, a library of wondrous things you never new you wanted!

And then you're away and running!

HTH

Re: Interfacing website hosted on Raspberry Pi with GPIO pin

Posted: Wed Mar 20, 2013 6:15 am
by DoubleBrowne
Wow thank you very much. This is exactly what I'm looking for, I have a lot to digest now. I'm hoping this will make a good first project for my RasPi.