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.

30 thoughts on “Easy Web Interface With GPIO Access Runs On Raspberry Pi

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

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

    1. Although it is easier to write the state to the GPIO files from a script directly, this method allows for future client expandability. By implementing a MySQL database, it is possible to design android or iOS applications that interact with GPIO pins without giving them full access to the files. You are also able to run the database and GPIO pins on separate machines or include multiple raspberry pis with other functions by linking them to the database and interacting through one web interface. I am not aware of the goal the author of the instructables had in mind, and if this were it, then I agree it is inefficient, but these are some of the reasons he or she may have used a database.

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

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

  4. 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!

  5. instructable has removed almost all the pages which had informations about how to control raspberry pi gpio over the web.
    do not know what was the purpose behind this action.
    does any one know who is the author of this program? or have downloaded this instructable?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.