def whattemp(): global temp9 ch2 = TemperatureSensor() ch2.setDeviceSerialNumber(118651) ch2.setChannel(0) ch2.setOnAttachHandler(onAttachHandler) ch2.setOnTemperatureChangeHandler(onTemperatureChangeHandler) ch2.openWaitForAttachment(5000) temp9 = ch2.getTemperature() * 9 / 5 + 32 temp8 = temp9 #time.sleep(10) ch2.close() print("i got the temperature, its ", temp9) return temp8
class ThermoCouple(Sensor): '''Class used to handle any thermcouple data ''' def __init__(self, deviceSN, channelNo, dataInterval, refreshPeriod, sensorType, sensorName=None): ''' Constructor for creating thermcouple sensors. Takes standard sensor arguments. Sensor type here is enum for different thermcouples. J,K,E and T types are represented by 1,2,3,4. ''' self.channelNo = channelNo self.sensorType = sensorType self.sensorUnits = None Sensor.__init__(self, deviceSN, dataInterval, refreshPeriod, sensorName) def attachSensor(self): ''' Connects the sensor to the application ''' self.channel = TemperatureSensor() self.channel.setDeviceSerialNumber(self.deviceSN) self.channel.setChannel(self.channelNo) self.channel.openWaitForAttachment(100) print("\n***** {} Sensor Attached *****".format(self.sensorName)) self.attached = True self.channel.setThermocoupleType(self.sensorType) self.channel.setDataInterval(self.dataInterval) self.sensorUnits = "degrees C" def activateDataListener(self): ''' Sets up the event which triggers when the sensor updates its outputs ''' self.startTime = time.time() def onTempChange(channelObject, temp): rawTime = time.time() deltaTime = rawTime - self.startTime self.dataQ.put([temp, deltaTime, rawTime]) self.channel.setOnTemperatureChangeHandler(onTempChange)
class IRTemperatureSensor(Sensor): ''' Class for connecting to Phidget IR temperature sensors. Extends base Sensor class ''' def __init__(self, deviceSN, dataInterval, refreshPeriod, sensorName=None): ''' Constructor for IR Temp class. Takes same arguments as Sensor ''' Sensor.__init__(self, deviceSN, dataInterval, refreshPeriod, sensorName) self.sensorUnits = "Degrees C" def attachSensor(self): ''' Connects sensor to application. Sets the "channel" parameter which is the handle to the phidget ''' self.channel = TemperatureSensor() self.channel.setDeviceSerialNumber(self.deviceSN) self.channel.setChannel(0) self.channel.openWaitForAttachment(5000) print("***** {} Sensor Attached *****".format(self.sensorName)) self.attached = True self.channel.setDataInterval(self.dataInterval) def activateDataListener(self): ''' Sets up the event which triggers when the sensor updates its output values ''' self.startTime = time.time() def onTemperatureChange(channelObject, temperature): # logs time and sensor value to the thread safe dataQ rawTime = time.time() deltaTime = rawTime - self.startTime self.dataQ.put([temperature, deltaTime, rawTime]) self.channel.setOnTemperatureChangeHandler(onTemperatureChange)
ch1.setOnErrorHandler(ErrorEvent) ch2.setOnAttachHandler(TemperatureSensorAttached) ch2.setOnDetachHandler(TemperatureSensorDetached) ch2.setOnErrorHandler(ErrorEvent) ch3.setOnAttachHandler(TemperatureSensorAttached) ch3.setOnDetachHandler(TemperatureSensorDetached) ch3.setOnErrorHandler(ErrorEvent) start0 = time.time() start1 = start0 start2 = start0 start3 = start0 ch0.setOnTemperatureChangeHandler(TemperatureChangeHandler0) ch0.setChannel(0) ch1.setOnTemperatureChangeHandler(TemperatureChangeHandler1) ch1.setChannel(1) ch2.setOnTemperatureChangeHandler(TemperatureChangeHandler2) ch2.setChannel(2) ch3.setOnTemperatureChangeHandler(TemperatureChangeHandler3) ch3.setChannel(3) # Please review the Phidget22 channel matching documentation for details on the device # and class architecture of Phidget22, and how channels are matched to device features. # Specifies the serial number of the device to attach to.
def main(): try: """ * Allocate a new Phidget Channel object """ try: ch = TemperatureSensor() except PhidgetException as e: sys.stderr.write("Runtime Error -> Creating TemperatureSensor: \n\t") DisplayError(e) raise except RuntimeError as e: sys.stderr.write("Runtime Error -> Creating TemperatureSensor: \n\t" + e) raise """ * Set matching parameters to specify which channel to open """ #You may remove this line and hard-code the addressing parameters to fit your application channelInfo = AskForDeviceParameters(ch) ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber) ch.setHubPort(channelInfo.hubPort) ch.setIsHubPortDevice(channelInfo.isHubPortDevice) ch.setChannel(channelInfo.channel) if(channelInfo.netInfo.isRemote): ch.setIsRemote(channelInfo.netInfo.isRemote) if(channelInfo.netInfo.serverDiscovery): try: Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE) except PhidgetException as e: PrintEnableServerDiscoveryErrorMessage(e) raise EndProgramSignal("Program Terminated: EnableServerDiscovery Failed") else: Net.addServer("Server", channelInfo.netInfo.hostname, channelInfo.netInfo.port, channelInfo.netInfo.password, 0) """ * Add event handlers before calling open so that no events are missed. """ print("\n--------------------------------------") print("\nSetting OnAttachHandler...") ch.setOnAttachHandler(onAttachHandler) print("Setting OnDetachHandler...") ch.setOnDetachHandler(onDetachHandler) print("Setting OnErrorHandler...") ch.setOnErrorHandler(onErrorHandler) #This call may be harmlessly removed PrintEventDescriptions() print("\nSetting OnTemperatureChangeHandler...") ch.setOnTemperatureChangeHandler(onTemperatureChangeHandler) """ * Open the channel with a timeout """ print("\nOpening and Waiting for Attachment...") try: ch.openWaitForAttachment(5000) except PhidgetException as e: PrintOpenErrorMessage(e, ch) raise EndProgramSignal("Program Terminated: Open Failed") print("Sampling data for 10 seconds...") print("You can do stuff with your Phidgets here and/or in the event handlers.") time.sleep(10) """ * Perform clean up and exit """ #clear the TemperatureChange event handler ch.setOnTemperatureChangeHandler(None) print("\nDone Sampling...") print("Cleaning up...") ch.close() print("\nExiting...") return 0 except PhidgetException as e: sys.stderr.write("\nExiting with error(s)...") DisplayError(e) traceback.print_exc() print("Cleaning up...") ch.setOnTemperatureChangeHandler(None) ch.close() return 1 except EndProgramSignal as e: print(e) print("Cleaning up...") ch.setOnTemperatureChangeHandler(None) ch.close() return 1 finally: print("Press ENTER to end program.") readin = sys.stdin.readline()