Skip to content

Instantly share code, notes, and snippets.

@brianz
Created May 7, 2025 23:08
Show Gist options
  • Select an option

  • Save brianz/224b1b7f0b531cd818da69ca10639e15 to your computer and use it in GitHub Desktop.

Select an option

Save brianz/224b1b7f0b531cd818da69ca10639e15 to your computer and use it in GitHub Desktop.

Revisions

  1. brianz created this gist May 7, 2025.
    43 changes: 43 additions & 0 deletions phidgets-temperature.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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")