Skip to content

Instantly share code, notes, and snippets.

@davstott
Last active February 26, 2016 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davstott/5183828 to your computer and use it in GitHub Desktop.
Save davstott/5183828 to your computer and use it in GitHub Desktop.
Raspberry Pi, GPIO over the Network
#!/usr/bin/pythonRoot
# bring in the libraries
import RPi.GPIO as G
from flup.server.fcgi import WSGIServer
import sys, urlparse
# set up our GPIO pins
G.setmode(G.BCM)
G.setup(18, G.OUT)
# all of our code now lives within the app() function which is called for each http request we receive
def app(environ, start_response):
# start our http response
start_response("200 OK", [("Content-Type", "text/html")])
# look for inputs on the URL
i = urlparse.parse_qs(environ["QUERY_STRING"])
yield (' ') # flup expects a string to be returned from this function
# if there's a url variable named 'q'
if "q" in i:
if i["q"][0] == "w":
G.output(18, True) # Turn it on
elif i["q"][0] == "s":
G.output(18, False) # Turn it off
#by default, Flup works out how to bind to the web server for us, so just call it with our app() function and let it get on with it
WSGIServer(app).run()
<html>
<head>
<title>Hello from the Pi</title>
</head>
<body>
<h1>Hello world from the Raspberry Pi</h1>
</body>
</html>
<head>
<title>Hello from the Pi</title>
<script src="//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>
</head>
<body>
<h1>Hello world from the Raspberry Pi</h1>
<form>
<input type="button" value="On" onclick="go('w')" style="font-size:200%;"><br />
<input type="button" value="Off" onclick="go('s')" style="font-size:200%;">
</form>
<script type="text/javascript">
function go(qry) {
new Ajax.Request('doStuff.py?q=' + qry,
{method: 'GET'}
);
}
</script>
</body>
</html>
#!/usr/bin/python
import RPi.GPIO as G # reference the GPIO library
G.setmode(G.BCM) # use the 'BCM' numbering scheme for the pins
G.setup(18, G.OUT) # Set pin 18 as an Output
G.output(18, True) # Turn it on
raw_input('Press return to exit')
G.cleanup() # Tidy up after ourselves so we don't generate warnings next time we run this
#!/usr/bin/python
import RPi.GPIO as G # reference the GPIO library
G.setmode(G.BCM) # use the 'BCM' numbering scheme for the pins
G.setup(18, G.OUT) # Set pin 18 as an Output
while (True): # keep going around this loop until we're told to quit
key = raw_input("Enter 'w' for On, 's' for Off and any other key to quit. You'll need to press enter after each character: ")
if key == "w":
G.output(18, True) # Turn it on
elif key == "s":
G.output(18, False) # Turn it off
else:
break # leave our loop
G.cleanup() # Tidy up after ourselves so we don't generate warnings next time we run this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment