Webify Your Pi Projects

Would you like to control your pi projects from the internet?  Here is some simple instructions that will turn on and off an LED from anywhere in the world.

Supplies needed:
A Raspberry Pi
An LED
A resistor
A computer

First off, I'm going to assume that you have done a little work with your Pi already.  If you're not sure how to set up your Pi, Adafruit has excellent guides.

Next, we install the Apache web server.  Disclaimer: I am by no means a web security expert.  If you intend to make your Pi project public, you will want to secure it.

To install Apache, ssh into your Pi and type:

sudo apt-get install apache2 -y

Find your Pi's local IP address:

ifconfig | grep addr

And look for the inet addr that is not 127.0.0.1

wlan0       Link encap:Ethernet  HWaddr 00:0f:60:01:78:73                    
                 inet addr:192.168.1.4  Bcast:10.0.0.255  Mask:255.255.255.0

From a web browser you should be able to see the default webpage.  Browse to your Pi's IP address.

It works!

This is the default web page for this server.
The web server software is running but no content has been added, yet.


The default location for webpages is: /var/www

Now we need to add python capabilities to Apache.  From your shell type:

sudo apt-get install libapache2-mod-python

Next we need to modify Apache to execute your .py files. (substitute vi for your preferred text editor. Type:

sudo vi /etc/apache2/sites-enabled/000-default

Look for the line:  <Directory "/usr/lib/cgi-bin">
And somewhere below it add:

AddHandler cgi-script .py

Save your file and exit.  Restart Apache:

sudo /etc/init.d/apache2 restart

Let's create a simple python script.  Save this file to your /usr/lib/cgi-bin directory.

#hello.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb

cgitb.enable()

print "Content-Type: text/plain;charset=utf-8""
print "Hello World!"



Make the file executable:

sudo chmod a+x hello.py

And browse to: http://your_pi_ip/cgi-bin/hello.py

If you see Hello World!  It worked!

Now let's get Apache talking to your GPIO pins with wiringPi.
First we will need to install 'git'

sudo apt-get install git-core
sudo apt-get update
sudo apt-get upgrade

Then we use git to get wiringPi:

git clone git://get.drogon.net/wiringPi

Once that finishes:

cd wiringPi
git pull origin
./build

Test out the new program:  Connect a resistor to pin 26 and an LED to the resistor.  Here's a great guide if you're not sure how to do that. Then type:

gpio -g mode 26 out
gpio -g write 26 1

Your LED should light up.  Sending a 0 (zero) to the pin will turn it off.

gpio -g write 26 0

We now have all the building blocks we need to control your Pi project from the web!

Change back to your /var/www directory and create a new file: light.html

<html>
<head>
<title>Light</title>
</head>
<body>

<form action="/cgi-bin/light.py" method="POST">
  <P>Light</P>
  <input type="submit" value="On" name="On">
  <input type="submit" value="Off" name="Off">
</form>
</body>
</html>


We're almost there, change to your /usr/lib/cgi-bin directory and create a new file: light.py

#!/usr/bin/env python

import cgi
import cgitb
import subprocess

cgitb.enable()

print "Content-type: text/html\n\n"
print "<html>"
print "<head>"
print "<title>Light</title>"
print '<meta http-equiv="refresh" content="4;url=/../light.html" />'
print '<link rel="stylesheet" type="text/css" href="/../light.css">'
print "</head>"
print "<body>"

form=cgi.FieldStorage()

print "<P>"
print "The light is now "
if "On" in form:
    subprocess.call(['gpio', '-g', 'write', '26', '1'])
    print "on"
elif "Off" in form:
    subprocess.call(['gpio', '-g', 'write', '26', '0'])
    print "off"
else:
    print "derp"
print "</P>"


Browse to http://your_pi_ip/light.html and you should be able to turn on and off with a click of the mouse!

To make your Pi accessible from the internet you will need to open and redirect a port on your router's firewall to the Pi.  This could be dangerous as it opens your Pi up to everyone on the internet, do so at your own risk.  It may also violate the terms of service with your ISP.

No comments:

Post a Comment