Last active
August 29, 2015 14:14
-
-
Save artschwagerb/91137af51a421b600773 to your computer and use it in GitHub Desktop.
Raspberry Pi Tornado Temperature Server
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/python | |
import tornado.ioloop | |
import tornado.web | |
MIN_TEMP = 0 | |
ERROR_TEMP = -999.99 | |
TEMP_SENSORS = { | |
"28-00043a4518ff" : {"label":"A","location":"Server-1-Front","temperature":None}, | |
"28-00043b61b4ff" : {"label":"B","location":"Server-1-Back","temperature":None}, | |
"28-00043e827cff" : {"label":"C","location":"Server-2-Front","temperature":None}, | |
"28-00043a52a1ff" : {"label":"D","location":"Server-2-Back","temperature":None}, | |
"28-00043b6803ff" : {"label":"E","location":"Server Room Ambient","temperature":None}, | |
} | |
def read_temperature(sensor): | |
tfile = open("/sys/bus/w1/devices/{0}/w1_slave".format(sensor)) | |
text = tfile.read() | |
tfile.close() | |
lines = text.split("\n") | |
if lines[0].find("YES") > 0: | |
temp = float((lines[1].split(" ")[9])[2:]) | |
temp /= 1000 | |
return temp | |
return ERROR_TEMP | |
class MainHandler(tornado.web.RequestHandler): | |
def get(self): | |
self.write("Hello, world") | |
class TempHandler(tornado.web.RequestHandler): | |
def get(self): | |
temps = {} | |
for rrd in TEMP_SENSORS: | |
#temp = read_temperature(TEMP_SENSORS[rrd]) | |
TEMP_SENSORS[rrd]['temperature'] = read_temperature(rrd) | |
response = { 'controller':{'location': 'Server Room','hardware':'raspberrypib+'}, | |
'sensors': TEMP_SENSORS,} | |
self.write(response) | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
(r"/temperatures", TempHandler), | |
]) | |
if __name__ == "__main__": | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment