def serve(): try: import mbug_2810 thermo = mbug_2810.open() serial = thermo.serial() global temp, max, min temp = max = min = thermo.read() except: print('Thermometer not found. Webserver mode only.') thermo = None server = HTTPServer(('', port), MyHandler) server_thread = Thread(target=server.serve_forever,args=(0.1,)) f = open(datafile,'a') f.write('\n') f.close() try: server_thread.start() print("Start server on port %s. Hit Ctrl+C to stop." % port) next_read = next_store = time() while(1): now = time() if now>=next_read and thermo: temp = thermo.read() if temp>max: max=temp if temp<min: min=temp next_read += upd_read if now>=next_store: f = open(datafile,'a') f.write("%.2f\t%.3f\n"%(now,temp)) f.close() create_plot() next_store += upd_store sleep(0.1) except: server.shutdown() server.socket.close() if thermo: thermo.close() os.remove('cmd.gp') raise
# # Demo script for the MBUG_2810 device interface class # import mbug_2810 import time # Open the first device and print the current temperature therm = mbug_2810.open(1) # Open file file = open('temperature.dat', 'a', 0) # Read until break while(1): T = therm.read() t = time.time() print t, T file.write("%.2f\t%.3f\n" % (t, T) ) file.flush() time.sleep(10)