Easy web interface with GPIO access runs on Raspberry Pi


Here’s a Raspberry Pi hack that adds web control using PHP and MySQL. As you can see in the image, it serves up a webpage (using the Apache2 server) which allows you to change the state of the GPIO pins. It’s not super-complicated, but it is nice to see a step-by-step guide for installing and configuring the package.

Web interface GPIO control is one of the features we loved about the Adafruit Web IDE. But this offering is loaded completely from the RPi (the Adafruit package uses cloud based code) and utilizes the tools most Linux network admins will be used to. A MySQL database manages the connection between GUI commands and GPIO modification. The webpage is served up by a PHP script which takes care of polling and changing database values. Configuration requires a new database, plus the username and password which has access to it.


  1. Any says:

    Code isn’t good because it does’t separate HTML from PHP whats more store password and username in this same file. It is only concept.

  2. Jan says:
  3. Zmaster says:

    WTF, now you need mysql to drive a led…

  4. Santeri says:

    Yaeh, wtf? Wouldn’t webiopi be mutch easier?

  5. trouch says:

    WebIOPi does it since months, does more right now, and will be the perfect Pi framework : http://code.google.com/p/webiopi/
    Check http://code.google.com/p/webiopi/wiki/CHANGELOG
    For instance, just add a line in the webiopi config file to say you have a temperature sensor. Then make a HTTP GET to /bedroom/temperature to retrieve the value !

  6. trouch says:

    In the MagPi february issue, there is an article to use webiopi to build a remote control spy cam.

  7. Jon says:

    Even looking at the package, I can’t tell . . . is this using FastCGI or some other CGI gateway?

  8. jklu says:

    Doing it with node.js and wiringPi is also very easy.
    Could even use node-dirty as DB :-)

  9. jklu says:

    Just noticed that its even more easy using https://github.com/EnotionZ/GpiO
    No need to use wiringPI unless you need microsecond switch time.

  10. Andrew says:

    Ehm… Super simple would be bash + cgi to the webserver for that. But why on earth do we need mysql for that.

  11. EdgarVice says:

    Topic should be: Ugly web interface…

  12. CalcProgrammer1 says:

    Or just use the simple CGI HTTP server from Python. I’m using this to control lamps using a wireless outlet remote I made. It’s hooked to my home server via USB to Serial (not a Pi in this case, just a PC) and serves a control page in Python using the built-in server. It’s stupid simple, just click a button to run the script that switches the corresponding light on/off, and it took all of an hour to write (coming from very little background in either CGI or Python). Sure, having a fancy MySQL database, authentication, and such may be useful in some cases, but sometimes a bare-bones approach is better.

  13. hans says:

    This should be submitted to thedailywtf. There is a PHP script that upon a button press changes the value of a db entry. Then there is a bash script that – in a poll loop – runs some command line db-queries, parses the result and updates the io. Why??

    Really, just drop the db and the shell script, and write the state to the gpio files directly (although you need to chmod those gpio files on start-up, it seems). Problem solved.

    The scary thing is that a lot of people on instructables seem to think this is a great tutorial. So we might be seeing some more clutch based on this in the future.

  14. g.a says:

    Well, MySQL is a little bit overkill for the task…

  15. Polling MySQL?? Use Beanstalk and write a PHP shell script that waits on the FIFO for instructions from the web interface.

  16. Matt says:

    What’s all the hate for mysql? I think the idea is brilliant. Not only does it keep the PHP from requiring system level permissions, but it has the potential for so much more. With sql, you can tag pins with attributes and arrange them into relational groups…and those attributes can be changed dynamically.

    To extend the blinky-led example…tag half the pins as blue and half as red. Now you can toggle all red or all blue LEDs with single SQL queries. If your layout changes, you update the database and the code still works. The only way to do that without sql would be manually editing define statements in the PHP or using a flat-file database….both of which really are databases, just not sql.

    • spacevs says:

      Because MySQL is a huge amount of overhead for such a simple application, if you want a database, use SQLite and Beanstalk for the FIFO to avoid polling the database. This combination would be more responsive and consume less power.

      • Jimmy says:

        But isnt the pi suppost to be an educational tool? So as MySQL is common with the PHP in the common LAMP system, it is first thing to think of.

        Or maybe thats just me.

  17. Jeff says:

    Please, for all those hating on MySQL, give me a simpler solution. I’d love to be able to control a solid state relay from my smartphone (by forwarding all port 80 traffic to my pi) without the bs here, but I, never having worked with the pi or GPIO before… can’t seem to find an easy way to do this. Help me out!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 96,421 other followers