Skip to content

Instantly share code, notes, and snippets.

@librarian
Last active January 23, 2023 15:00
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Basic programm to use hidapi and report temp/co2 from Holtek Semiconductor, Inc. USB-zyTemp (hidapi is python3-hidapi in debian buster)
from hidapi import Device
vid = 0x04d9
pid = 0xa052
try:
print("Opening the device")
h = Device(vendor_id=vid, product_id=pid, blocking=False)
#h.open(vid, pid) # TREZOR VendorID/ProductID
print("Manufacturer: %s" % h.get_manufacturer_string())
print("Product: %s" % h.get_product_string())
print("Serial No: %s" % h.get_serial_number_string())
#print("Get feature report:")
#print(h.get_feature_report(bytes(1),2))
print("Send feature report:")
h.send_feature_report(bytearray([0xc4, 0xc6, 0xc0, 0x92, 0x40, 0x23, 0xdc, 0x96]), report_id=bytes(1))
try:
while True:
r = h.read(8)
if r:
# print('read: "{}"'.format(r))
metric = r[0]
value = r[1]
value_thousands = r[2]
checksum = r[3]
r4 = r[4]
if r4 != 0x0d:
continue
print("Unexpected data from device")
value_sum = metric + value + value_thousands
#if checksum != metric + value + value_thousands:
# continue
# print("Checksum error")
metric_name = ""
value_shifted = value << 8
metric_value = value_shifted + value_thousands
if metric == 0x42:
metric_name = "Temperature"
metric_value = metric_value / 16 - 273.15
elif metric == 0x50:
metric_name = "CO2"
elif metric == 0x44:
metric_name = "Humidity"
else:
continue
metric_name = "Unknown"
print(hex(metric), metric_name, "%.1f" % metric_value)
finally:
print("Closing the device")
h.close()
h.close()
except IOError as ex:
print(ex)
print("You probably don't have the hard-coded device.")
print("Update the h.open() line in this script with the one")
print("from the enumeration list output above and try again.")
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment