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()
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()
#Add Phidgets library from Phidget22.Phidget import * from Phidget22.Devices.TemperatureSensor import * from Phidget22.Devices.PressureSensor import * #Required for sleep statement import time #Create temperatureSensor = TemperatureSensor() pressureSensor = PressureSensor() #Open temperatureSensor.openWaitForAttachment(1000) pressureSensor.openWaitForAttachment(1000) #Use your Phidgets while (True): #Update user print("Logging data...") #Write data to file in CSV format with open('data.csv', 'a') as datafile: datafile.write( time.strftime("%Y-%m-%d %H:%M:%S") + "," + str(temperatureSensor.getTemperature()) + "," + str(pressureSensor.getPressure()) + "\n") time.sleep(1.0)
# code for humidity sensor humidity = HumiditySensor() humidity.setDeviceSerialNumber(561266) humidity.setHubPort(5) humidity.setIsHubPortDevice(False) humidity.setChannel(0) humidity.openWaitForAttachment(5000) try: humidity.getHumidity() except Exception as e: pass time.sleep(2) print(humidity.getHumidity()) # code for temperature sensor temp = TemperatureSensor() temp.setDeviceSerialNumber(561266) temp.setHubPort(5) temp.setIsHubPortDevice(False) temp.setChannel(0) temp.openWaitForAttachment(5000) try: temp.getTemperature() except Exception as e: pass time.sleep(2) print(temp.getTemperature())