bash + javascript + html + css = gpiohttpd

magic people, voodoo people

(с) Prodigy

I recently saw a post on HaD  where a guy on (sic!) Raspberry Pi, blinks leds using apache2 + php + mysql.
I didn’t even want to find out what the heck did he need mysql for, since a more insane idea immediately came to my mind…

shot

A loong time ago, I read an article, that described making a httpd with bash in just one line. So, that gave me the inspiration, and a starting point.

So, to run this black magic yourself, you’ll need:

  • Some hardware with leds, buttons and linux. Anything that has gpios avaliable via gpiolib would do (I used my  MW150R, yep, the very one that got his VxWorks to OpenWRT upgrade)
  • OpenWRT or any other linux distro with full netcat and bash (busybox’s applets won’t do)

gpiohttpd is created with the following magics:

  • bash
  • css
  • javascript
  • html

All fitted into something like 500 lines of ONE file, making it look like utter hackery and black magic. (Honestly, who cares? It’s proof-of-concept, nothing more!)

So, it works really simple. Start it without arguments, and it will fire up netcat in an endless loop, waiting for incoming connection. For each connection an instance of this script is spawned by netcat, that renders the page, or sends out data for ajax. Netcat closes connection on EOF and exits, to be started again waiting for a new connection.

The rest of the magic is done in javascript, that polls server using ajax when we need to update anything. Any changes in the form are instantly committed via ajax. It’s just simpler.

The  obvious usage – poking around a newly obtained piece of hardware, in search for gpio numbers for leds, buttons and etc., while porting OpenWRT to a new board. (Honestly – looking for buttons has never been easier!)

Known issues.

It looks like we can’t use busybox’s ash, since netcat can’t start any script, not can it pass arguments. In other words, even

netcat -vlp 8080 -e “gpiohttpd.sh”

won’t work at all with busybox’s ash. Fail. If anyone can think of a workaround – please send me a patch.

The source code of this thing can be downloaded at my github page

Leave a Reply

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