Created
May 7, 2025 23:08
-
-
Save brianz/224b1b7f0b531cd818da69ca10639e15 to your computer and use it in GitHub Desktop.
Phidgets temperature example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Phidget22.Devices.TemperatureSensor import TemperatureSensor | |
from Phidget22.ThermocoupleType import ThermocoupleType | |
import time | |
# Create a temperature sensor object | |
temperature_sensor = TemperatureSensor() | |
# Set the channel number | |
temperature_sensor.setChannel(1) | |
def handle_attach(sensor: TemperatureSensor): | |
print("Device attached") | |
sensor.setThermocoupleType(ThermocoupleType.THERMOCOUPLE_TYPE_J) | |
# Important: Set event handlers BEFORE opening | |
temperature_sensor.setOnAttachHandler(handle_attach) | |
# Open the connection | |
temperature_sensor.open() | |
# Wait for attachment before attempting to set thermocouple type | |
print("Waiting for the device to be attached...") | |
temperature_sensor.openWaitForAttachment(5000) | |
try: | |
# Main loop to continuously read temperature | |
while True: | |
try: | |
current_temp = temperature_sensor.getTemperature() | |
current_temp = (current_temp * 9 / 5) + 32 | |
print(f"Temperature: {current_temp:.2f} °F") | |
except Exception as e: | |
print(f"Error reading temperature: {e}") | |
time.sleep(1) | |
except KeyboardInterrupt: | |
print("\nExiting program...") | |
finally: | |
temperature_sensor.close() | |
print("Connection closed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment