Saturday, January 10, 2015

Web CGI Based Raspberry Pi GPIO LED Toggler

Finally, a setup that works, and it was a joy to go to mypi.freedns.org:NONCOXBLOCKEDPORT/cgi-bin/script.pl on my phone and see the LED toggle, and refresh the page and see it toggle again, and again and again...

So, on to how :-

What you need :
  • pigpio library (RPIO and RPi.GPIO don't cut it because they need root access)
  • lighttpd (other solutions possible, but this recipe is based on lite) an account with a free DNS service that will give you a static URL (you can't do 192.168.0.114 on your phone unless you're on the WiFi - and it's wimpy anyway)
  • Simple perl script that will call the python GPIO script (Yes, I tried calling the python script through CGI and gave up on that) 


1) Install lighttpd - consult online tutorials for that.
2) Once lighttpd is installed, enable CGI and perl by editing /etc/lighttpd/lighttpd.conf to have the relevant portions look like :
server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_cgi",
        "mod_redirect",
#       "mod_rewrite",
)
and
$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" ,
                        ".pl" => "/usr/bin/perl")
}
And, of course, if your ISP (Eg. Cox) is blocking port 80, you can change server.port to something else.
3) In the /var/www directory, create a cgi-bin directory (ownership can be root, no problem).
4) Install the pigpio library by following these steps.
5) Put the line "sudo /usr/local/bin/pigpiod &" in your /etc/rc.local so this daemon runs @ boot.
6) In a convenient place, put this script which will set port 17 (CPU view, not Board View) as an output port (name it meaningfully - I use caution_set_out_17.py :
#!/usr/bin/python
import RPIO as GPIO
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)

7) Run this script using "sudo /path/to/caution_set_out_17.py". It's named that way because it's not a good idea to blindly set a port to output - what if some app had set it as input and a voltage source was driving it on the board? Yes, until you know what you're doing with your Pi, it's not a bad idea to run this command manually :)
8) Put this script in a convenient place, which actually does the toggling of the logic-level on Port 17 :
#!/usr/bin/python
import pigpio
pi = pigpio.pi()
if 1 ==pi.get_mode(17) :
        pi.write(17, not pi.read(17))
9) Put this script in your /var/www/cgi.bin, name it conveniently
#!/usr/bin/perl
print "Content-type: text/html\n\n";
system("/home/pi/projects/pgpio_toggle_17.py");
print "<html>Hello, World!\n</html>";
10) Ensure you do a "chmod +x" on all the scripts you just finished installing
11) Using your breadboard, connect the LED longer wire to pin 11 (header view, google it), shorter wire to 330 ohm, and other terminal of the resistor to GND on the header (you'd want to do a sanity check by first connecting the LED, through 330 ohm of course) between 3V and 5V)
12) That's it, you should first try each of the scripts standalone - just run them from the command line and ensure the effect is as desired, then you can take your smartphone and go to yourpi.dnsserver.domain:80XX/cgi-bin/script.pl and you should see the LED toggle. 

Troubleshooting - running each script standalone should show get you there - obviously you edit the script I gave you to put in the actual name of the script you created, etc.


No comments:

Post a Comment