def AttachTemperature(databasepath, serialNumber): def onAttachHandler(event): logString = "Temperature Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Temperature Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Temperature Error " + str( event.device.getSerialNum()) + ", Error: " + event.description #print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "Temperature Server Connect " + str( event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Temperature Server Disconnect " + str( event.device.getSerialNum()) #print(logString) def temperatureChangeHandler(event): logString = "Temperature Changed " + str(event.device.getSerialNum()) try: conn = sqlite3.connect(databasepath) conn.execute( "INSERT INTO TEMPERATURE_CHANGE VALUES(NULL, DateTime('now'), ?, ?, ?, ?)", (event.device.getSerialNum(), event.index, event.temperature, event.potential)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = TemperatureSensor() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnTemperatureChangeHandler(temperatureChangeHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
class PhidgetTemp(): def __init__(self): self.temperatureSensor = TemperatureSensor() self.temperatureSensor.setOnAttachHandler(TemperatureSensorAttached) self.temperatureSensor.setOnDetachHandler(TemperatureSensorDetached) self.temperatureSensor.setOnErrorhandler(TemperatureSensorError) self.temperatureSensor.setOnTemperatureChangeHandler( TemperatureSensorTemperatureChanged) # self.data_to_be_sent_bridge = [] # self.times_to_be_sent_bridge = [] def open(self, waitTimeMS): self.temperatureSensor.openPhidget() try: self.temperatureSensor.waitForAttach(waitTimeMS) time.sleep(0.5) self.temperatureSensor.setThermocoupleType( 0, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) self.temperatureSensor.setThermocoupleType( 1, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) self.temperatureSensor.setThermocoupleType( 2, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) self.temperatureSensor.setThermocoupleType( 3, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) self.temperatureSensor.setTemperatureChangeTrigger(0, 0.1) # self.temperatureSensor.setTemperatureChangeTrigger(1, 0.1) # self.temperatureSensor.setTemperatureChangeTrigger(2, 0.1) # self.temperatureSensor.setTemperatureChangeTrigger(3, 0.1) return 1 except: return -1 # def BridgeData(self,e): # timeStamp = time.time() # self.data_to_be_sent_bridge.append(e.value) # self.times_to_be_sent_bridge.append(timeStamp) def getData(self): #return the data and times and update the lists thermocouple0 = self.temperatureSensor.getTemperature(0) thermocouple1 = self.temperatureSensor.getTemperature(1) thermocouple2 = self.temperatureSensor.getTemperature(2) thermocouple3 = self.temperatureSensor.getTemperature(3) timeStamp = time.time() data_to_return = [ thermocouple0, thermocouple1, thermocouple2, thermocouple3, timeStamp ] data_to_be_sent_temperature = [] times_to_be_sent_temperature = [] return data_to_return def close(self): self.temperatureSensor.setEnabled(0, False) time.sleep(2) self.temperatureSensor.closePhidget()
def AttachTemperature(databasepath, serialNumber): def onAttachHandler(event): logString = "Temperature Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Temperature Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Temperature Error " + str(event.device.getSerialNum()) + ", Error: " + event.description #print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "Temperature Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Temperature Server Disconnect " + str(event.device.getSerialNum()) #print(logString) def temperatureChangeHandler(event): logString = "Temperature Changed " + str(event.device.getSerialNum()) try: conn = sqlite3.connect(databasepath) conn.execute("INSERT INTO TEMPERATURE_CHANGE VALUES(NULL, DateTime('now'), ?, ?, ?, ?)", (event.device.getSerialNum(), event.index, event.temperature, event.potential)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = TemperatureSensor() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnTemperatureChangeHandler(temperatureChangeHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
class PhidgetTemp(): def __init__(self): self.temperatureSensor = TemperatureSensor() self.temperatureSensor.setOnAttachHandler(TemperatureSensorAttached) self.temperatureSensor.setOnDetachHandler(TemperatureSensorDetached) self.temperatureSensor.setOnErrorhandler(TemperatureSensorError) self.temperatureSensor.setOnTemperatureChangeHandler(TemperatureSensorTemperatureChanged) # self.data_to_be_sent_bridge = [] # self.times_to_be_sent_bridge = [] def open(self,waitTimeMS): self.temperatureSensor.openPhidget() try: self.temperatureSensor.waitForAttach(waitTimeMS) time.sleep(0.5) self.temperatureSensor.setThermocoupleType(0, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) self.temperatureSensor.setThermocoupleType(1, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) self.temperatureSensor.setThermocoupleType(2, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) self.temperatureSensor.setThermocoupleType(3, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) self.temperatureSensor.setTemperatureChangeTrigger(0, 0.1) # self.temperatureSensor.setTemperatureChangeTrigger(1, 0.1) # self.temperatureSensor.setTemperatureChangeTrigger(2, 0.1) # self.temperatureSensor.setTemperatureChangeTrigger(3, 0.1) return 1 except: return -1 # def BridgeData(self,e): # timeStamp = time.time() # self.data_to_be_sent_bridge.append(e.value) # self.times_to_be_sent_bridge.append(timeStamp) def getData(self): #return the data and times and update the lists thermocouple0 = self.temperatureSensor.getTemperature(0) thermocouple1 = self.temperatureSensor.getTemperature(1) thermocouple2 = self.temperatureSensor.getTemperature(2) thermocouple3 = self.temperatureSensor.getTemperature(3) timeStamp = time.time() data_to_return = [thermocouple0,thermocouple1,thermocouple2,thermocouple3,timeStamp] data_to_be_sent_temperature = [] times_to_be_sent_temperature = [] return data_to_return def close(self): self.temperatureSensor.setEnabled(0,False) time.sleep(2) self.temperatureSensor.closePhidget()
except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) def TemperatureSensorTemperatureChanged(e): try: ambient = temperatureSensor.getAmbientTemperature() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) ambient = 0.00 source = e.device print("TemperatureSensor %i: Ambient Temp: %f -- Thermocouple %i temperature: %f -- Potential: %f" % (source.getSerialNum(), ambient, e.index, e.temperature, e.potential)) #Main Program Code try: temperatureSensor.setOnAttachHandler(TemperatureSensorAttached) temperatureSensor.setOnDetachHandler(TemperatureSensorDetached) temperatureSensor.setOnErrorhandler(TemperatureSensorError) #temperatureSensor.setOnTemperatureChangeHandler(TemperatureSensorTemperatureChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: temperatureSensor.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....")
except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) def TemperatureSensorTemperatureChanged(e): try: ambient = temperatureSensor.getAmbientTemperature() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) ambient = 0.00 source = e.device print("TemperatureSensor %i: Ambient Temp: %f -- Thermocouple %i temperature: %f -- Potential: %f" % (source.getSerialNum(), ambient, e.index, e.temperature, e.potential)) #Main Program Code try: temperatureSensor.setOnAttachHandler(TemperatureSensorAttached) temperatureSensor.setOnDetachHandler(TemperatureSensorDetached) temperatureSensor.setOnErrorhandler(TemperatureSensorError) temperatureSensor.setOnTemperatureChangeHandler(TemperatureSensorTemperatureChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: temperatureSensor.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....")
def start_measuring(self): #Create an temperaturesensor object try: temperatureSensor = TemperatureSensor() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) #Information Display Function def DisplayDeviceInfo(): inputCount = temperatureSensor.getTemperatureInputCount() print("|------------|----------------------------------|--------------|------------|") print("|- Attached -|- Type -|- Serial No. -|- Version -|") print("|------------|----------------------------------|--------------|------------|") print("|- %8s -|- %30s -|- %10d -|- %8d -|" % (temperatureSensor.isAttached(), temperatureSensor.getDeviceName(), temperatureSensor.getSerialNum(), temperatureSensor.getDeviceVersion())) print("|------------|----------------------------------|--------------|------------|") print("Number of Temperature Inputs: %i" % (inputCount)) for i in range(inputCount): print("Input %i Sensitivity: %f" % (i, temperatureSensor.getTemperatureChangeTrigger(i))) #Event Handler Callback Functions def TemperatureSensorAttached(e): attached = e.device print("TemperatureSensor %i Attached!" % (attached.getSerialNum())) def TemperatureSensorDetached(e): detached = e.device print("TemperatureSensor %i Detached!" % (detached.getSerialNum())) def TemperatureSensorError(e): try: source = e.device if source.isAttached(): print("TemperatureSensor %i: Phidget Error %i: %s" % (source.getSerialNum(), e.eCode, e.description)) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) def TemperatureSensorTemperatureChanged(e): try: ambient = temperatureSensor.getAmbientTemperature() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) ambient = 0.00 source = e.device # print("TemperatureSensor %i: Ambient Temp: %f -- Thermocouple %i temperature: %f -- Potential: %f" % (source.getSerialNum(), ambient, e.index, e.temperature, e.potential)) if e.index == 0: #print("Temperature: %f" % (e.temperature)) self.logger.add_row(e.temperature,"-","-") #Main Program Code try: #logging example, uncomment to generate a log file #temperatureSensor.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log") temperatureSensor.setOnAttachHandler(TemperatureSensorAttached) temperatureSensor.setOnDetachHandler(TemperatureSensorDetached) temperatureSensor.setOnErrorhandler(TemperatureSensorError) temperatureSensor.setOnTemperatureChangeHandler(TemperatureSensorTemperatureChanged) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: temperatureSensor.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: temperatureSensor.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: temperatureSensor.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: DisplayDeviceInfo() print("Setting Thermocouple type...") temperatureSensor.setThermocoupleType(0, ThermocoupleType.PHIDGET_TEMPERATURE_SENSOR_K_TYPE) print("Setting sensitivity of the thermocouple....") temperatureSensor.setTemperatureChangeTrigger(0, 0.10) sleep(5) #sleep for 5 seconds print("Sensitivity of thermocouple index 0 is now %f" % (temperatureSensor.getTemperatureChangeTrigger(0))) print("Press Enter to quit....") #chr = sys.stdin.read(1) while True: #print("sensing") a = 1 #print("Closing...") try: temperatureSensor.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Done.") exit(0)