# coding:utf-8 from __future__ import print_function import os import tornado.ioloop import tornado.web import tornado.websocket import json import time import subprocess from newBMP280_2 import read_temp def run_shell_command(command_str): proc = subprocess.run(command_str, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True) result = proc.stdout.split("=") return float(result[1].replace('\'C\n', '')) class WebSocketHandler(tornado.websocket.WebSocketHandler): def check_origin(self, origin): return True def open(self): print("WebSocket Opened") def on_message(self, message): print("Message Received") while True: temp1, temp2 = read_temp() temp3 = run_shell_command("vcgencmd measure_temp") payload = {"temp1":temp1,"temp2":temp2,"temp3":temp3} message = json.dumps(payload) self.write_message(message) print(temp1, temp2, temp3) time.sleep(5) def on_close(self): print("WebSocket Closed") return 0 class MainHandler(tornado.web.RequestHandler): def get(self): self.render('main.html') application = tornado.web.Application([ (r"/", MainHandler), (r"/websocket", WebSocketHandler), ], template_path=os.path.join(os.getcwd(), "templates"), static_path=os.path.join(os.getcwd(), "static") ) if __name__ == "__main__": application.listen(8088) print("Server is up ...") tornado.ioloop.IOLoop.current().start()