Created
January 31, 2015 19:25
-
-
Save jnsdbr/7ae040015fb6070aefed to your computer and use it in GitHub Desktop.
The threading is dirty but it works.
This file contains 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 threading | |
import serial | |
import json | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
value = 0 | |
ser = serial.Serial('/dev/tty.usbmodemXYZ', 9600) | |
def read_value(): | |
global value | |
while True: | |
value = ser.readline().rstrip() | |
t = threading.Thread(target=read_value) | |
t.start() | |
class Handler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
self.request.sendall(json.dumps({'led': value})) | |
return | |
httpd = HTTPServer(('127.0.0.1', 5000), Handler) | |
print('HTTPServer is running') | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment