Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RaspberryPi Serving Sensor Data over HTTP
#!/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