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 MyThermo: def __init__(self, hub_serial, hub_port, thermal_channel): self.sensor = TemperatureSensor() self.sensor.setHubPort(hub_port) self.sensor.setChannel(thermal_channel) self.sensor.setDeviceSerialNumber(hub_serial) self.sensor.openWaitForAttachment(5000) self.previous_temp = 0 self.thermal_channel = thermal_channel def get_temp(self): try: temp = self.sensor.getTemperature() self.previous_temp = temp return temp except PhidgetException as exception: print('Error on thermal channel', self.thermal_channel, exception) return self.previous_temp def __del__(self): self.sensor.close()
class PhidgetTemperature(object): def __init__(self, hub_port=0, hub_channel=1, serial_number=-1, use_hub=False): self.use_hub = use_hub self.hub_port = hub_port self.hub_channel = hub_channel try: self.ch = TemperatureSensor() self.ch.setDeviceSerialNumber(serial_number) if use_hub: self.ch.setHubPort(hub_port) self.ch.setChannel(hub_channel) print('HUB, PORT:%d CHANNEL: %d' % (hub_port, hub_channel)) except PhidgetException as e: sys.stderr.write( "Runtime Error -> Creating TemperatureSensor: \n\t") DisplayError(e) self.ch.close() raise except RuntimeError as e: sys.stderr.write( "Runtime Error -> Creating TemperatureSensor: \n\t" + e) self.ch.close() raise logging.info("Phidget: Opening and Waiting for Attachment...") try: self.ch.openWaitForAttachment(5000) except PhidgetException as e: PrintOpenErrorMessage(e, self.ch) self.ch.close() raise EndProgramSignal("Program Terminated: Open Failed") time.sleep(1) logging.info("Phidget: Ready!") def getTemperature(self, fahrenheit=False): if fahrenheit: return (self.ch.getTemperature() * 9 / 5.0) + 32 else: return self.ch.getTemperature() def closeConnection(self): return self.ch.close()
ch1.openWaitForAttachment(5000) ch2.openWaitForAttachment(5000) ch3.openWaitForAttachment(5000) # ch0.setDataInterval(30) # print(str(ch0.getTemperatureChangeTrigger())) # print(str(ch0.getDataInterval())) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Press Enter to Exit...\n") readin = sys.stdin.read(1) exit(1) print("Gathering data for ?? seconds...") time.sleep(21) try: ch0.close() ch1.close() ch2.close() ch3.close() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Press Enter to Exit...\n") readin = sys.stdin.read(1) exit(1) print("Closed TemperatureSensor device") exit(0)
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()