Created
March 13, 2013 14:06
-
-
Save sunng87/5152427 to your computer and use it in GitHub Desktop.
tornado + websocket + rpi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
<link rel="dns-prefetch" href="//1-ps.googleusercontent.com"></head> | |
<body> | |
<div class="main"> | |
<h2>Shake your phone</h2> | |
</div> | |
<script> | |
ws = new WebSocket("ws://192.168.1.100:8888/ledcontrol"); | |
ws.onopen = function() { | |
} | |
function deviceMotionHandler(eventData) { | |
// Grab the acceleration including gravity from the results | |
var acceleration = eventData.accelerationIncludingGravity; | |
msg = []; | |
if (acceleration.x > 19) { | |
if (Math.random() < 0.2) { | |
msg.push('x') | |
} | |
} | |
if (acceleration.y > 17) { | |
if (Math.random() < 0.2) { | |
msg.push('y') | |
} | |
} | |
if (acceleration.z > 17) { | |
if (Math.random() < 0.3) { | |
msg.push('z') | |
} | |
} | |
if (msg.length > 0) { | |
ws.send(msg.join('')); | |
} | |
} | |
if (window.DeviceMotionEvent) { | |
window.addEventListener('devicemotion', deviceMotionHandler, false); | |
} else { | |
document.getElementById("dmEvent").innerHTML = "Not supported on your device." | |
} | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
import tornado.web | |
import tornado.websocket | |
import threading | |
import Queue | |
import RPi.GPIO as GPIO | |
import time,random | |
GPIO.setmode(GPIO.BCM) | |
mq = Queue.Queue() | |
pins = {'x':23, 'y':24, 'z':25} | |
for i in pins.values(): GPIO.setup(i, GPIO.OUT) | |
def monitor(): | |
while True: | |
print mq.qsize() | |
time.sleep(10) | |
def leds_loop(): | |
while True: | |
m = mq.get() | |
for i in m: | |
GPIO.output(pins[i], GPIO.HIGH) | |
time.sleep(0.08) | |
for i in m: | |
GPIO.output(pins[i], GPIO.LOW) | |
class LEDControlWebScoket(tornado.websocket.WebSocketHandler): | |
def open(self): | |
print "Connection established" | |
def on_message(self, message): | |
mq.put(message) | |
def on_close(self): | |
print "Connection lost" | |
application = tornado.web.Application([ | |
(r"/ledcontrol", LEDControlWebScoket), | |
(r"/(.*)", tornado.web.StaticFileHandler, | |
{'path': './', 'default_filename': 'index.html'}) | |
]) | |
def start_daemon(f): | |
t = threading.Thread(target=f) | |
t.setDaemon(True) | |
t.start() | |
if __name__ == "__main__": | |
application.listen(8888) | |
start_daemon(leds_loop) | |
start_daemon(monitor) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment