Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
python for webserver that controls lights using serial to write to arduino
import os, socket, threading, time, subprocess, mimetypes, struct, wave, serial
from urlparse import parse_qs, urlparse
from wave import open
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web.static import File
#
#Grab arduino that controls the lights to write serial to
#
ser = serial.Serial('/dev/ttyACM0', 9600)
#
# Webserver MAIN
#
class webserver(Resource):
global ser
isLeaf = True
#
# Main function when you get a https request
#
def render_GET(self, request):
# Parse out URL for easy use.
this=urlparse(request.path)#scheme,netloc,path,query
root,ext=os.path.splitext(this.path)
filename=os.path.basename(request.path)
fileFolder=request.path.replace(filename,"")
self.serverRoot=os.getcwd()
arg='null'
# Get param if not empty.
if bool(request.args):
arg=request.args['param']
# Parse out API params to serial calls to the arduino.
if this.path=="/index.html":
print "loading main page.."
# Param to turn on or off light.
# Basically writing to arduino using serial based on RESTFUL parameters handed to python twisted webserver.
if arg==['light1on']:
print "turning on light one"
ser.write("one:off")
elif arg==['light1off']:
print "turning off light one"
ser.write("one:on")
elif arg==['light2on']:
print "turning on light two"
ser.write("two:off")
elif arg==['light2off']:
print "turning off light two"
ser.write("two:on")
elif arg==['light3on']:
print "turning on light two"
ser.write("three:off")
elif arg==['light3off']:
print "turning off light two"
ser.write("three:on")
elif arg==['light4on']:
print "turning on light two"
ser.write("four:off")
elif arg==['light4off']:
print "turning off light two"
ser.write("four:on")
# Return index.html main resource at end requardless of params
return """
</html>
<head>
<style>
button {
background-color: 4CAF50; /* Green */
border: none;
color: white;
margin-bottom: 25px;
padding: 20px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 3em;
text-align:center;
width:48%;
height:20%;
float:left;
}
</style>
</head>
<body style="background-color:darkgrey;">
<button style="border-top-left-radius: 2em; border-bottom-left-radius: 2em;" onclick="window.location='/index.html?param=light1on'">Light one on</button>
<button style="background-color: e60000; border-top-right-radius: 2em; border-bottom-right-radius: 2em;" onclick="window.location='/index.html?param=light1off'">Light one off</button>
<button style="border-top-left-radius: 2em; border-bottom-left-radius: 2em;" onclick="window.location='/index.html?param=light2on'">Light two on</button>
<button style="background-color: e60000; border-top-right-radius: 2em; border-bottom-right-radius: 2em;" onclick="window.location='/index.html?param=light2off'">Light two off</button>
<button style="border-top-left-radius: 2em; border-bottom-left-radius: 2em;" onclick="window.location='/index.html?param=light3on'">Light three on</button>
<button style="background-color: e60000; border-top-right-radius: 2em; border-bottom-right-radius: 2em;" onclick="window.location='/index.html?param=light3off'">Light three off</button>
<button style="border-top-left-radius: 2em; border-bottom-left-radius: 2em;" onclick="window.location='/index.html?param=light4on'">Light four on</button>
<button style="background-color: e60000; border-top-right-radius: 2em; border-bottom-right-radius: 2em;" onclick="window.location='/index.html?param=light4off'">Light four off</button>
</body>
</html>"""
# Start twisted server functions
resource = webserver()
factory = Site(resource)
reactor.listenTCP(80, factory)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment