Last active
August 29, 2015 14:11
-
-
Save HeinrichHartmann/6c9d235a594e3b5be376 to your computer and use it in GitHub Desktop.
RaspberryPi Serving Sensor Data over HTTP
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
#!/usr/bin/env python | |
# Import the ADC driver: | |
# https://gist.github.com/HeinrichHartmann/27f33798d12317575c6c | |
import ADC0832 as ADC | |
import sys | |
import BaseHTTPServer | |
import json | |
from BaseHTTPServer import BaseHTTPRequestHandler | |
port = 8081 | |
if sys.argv[1:]: | |
port = int(sys.argv[1]) | |
class myHandler(BaseHTTPRequestHandler): | |
protocol_version = "HTTP/1.0" | |
def do_GET(self): | |
payload = {'temperature' : ADC.getADC(0), 'light': ADC.getADC(1)} | |
self.send_response(200) | |
self.send_header('Content-type','text/html') | |
self.end_headers() | |
# Send the html message | |
self.wfile.write(json.dumps(payload)) | |
return | |
httpd = BaseHTTPServer.HTTPServer(('0.0.0.0', port), myHandler) | |
sa = httpd.socket.getsockname() | |
print "Serving HTTP on", sa[0], "port", sa[1] | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment