Skip to content

Instantly share code, notes, and snippets.

@GennadySpb
Created February 17, 2023 22:21
Show Gist options
  • Save GennadySpb/5adddfcf5a45a96956ba73854b419b07 to your computer and use it in GitHub Desktop.
Save GennadySpb/5adddfcf5a45a96956ba73854b419b07 to your computer and use it in GitHub Desktop.
# do not forget to provide permisson to your device like:
# $ sudo chmod go+rw /dev/bus/usb/001/005
#
#
# or create udev rule in file /etc/udev/rules.d/50-local-raw-usb.rules:
# SUBSYSTEM=="usb", ATTRS{idVendor}=="04d9", ATTRS{idProduct}=="a052", MODE="0666"
#
# and restart udev
# $ sudo udevadm control --reload-rules && sudo udevadm trigger
#
import hid
vid = 0x04d9
pid = 0xa052
try:
print("Opening the device")
h = hid.device()
h.open(vid, pid)
print("Manufacturer: %s" % h.get_manufacturer_string())
print("Product: %s" % h.get_product_string())
print("Serial No: %s" % h.get_serial_number_string())
print("Send feature report:")
h.send_feature_report(bytearray([0xc4, 0xc6, 0xc0, 0x92, 0x40, 0x23, 0xdc, 0x96]))
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
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