Skip to content

Instantly share code, notes, and snippets.

@koteq
Last active June 28, 2016 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koteq/340f8136daecc56512b8b888a878e906 to your computer and use it in GitHub Desktop.
Save koteq/340f8136daecc56512b8b888a878e906 to your computer and use it in GitHub Desktop.
ht2000 ht-2000 co2 monitor pooler
import os
import json
import time
from struct import unpack_from
from datetime import datetime
import usb.core
import usb.backend.libusb1
script_dir = os.path.dirname(os.path.realpath(__file__))
libusb_path = os.path.join(script_dir, 'libusb-1.0.dll')
backend = usb.backend.libusb1.get_backend(find_library=lambda x: libusb_path)
dev = usb.core.find(idVendor=0x10c4, idProduct=0x82cd, backend=backend)
bmRequestType = usb.util.build_request_type(
usb.util.CTRL_IN,
usb.util.CTRL_TYPE_CLASS,
usb.util.CTRL_RECIPIENT_INTERFACE
)
while True:
buffer = dev.ctrl_transfer(
bmRequestType=bmRequestType,
bRequest=1,
wValue=0x0105,
wIndex=0,
data_or_wLength=61
)
# https://github.com/eschava/HT2000-java/blob/master/src/com/eschava/ht2000/usb/HT2000State.java
timestamp = int(time.time())
(T, RH, CO2) = unpack_from('>Bh13xh', buffer, 8)
T += 112
T /= 10.0
RH /= 10.0
# Write current values to last.json
last_json_path = os.path.join(script_dir, 'data', 'last.json')
if not os.path.exists(os.path.dirname(last_json_path)):
os.makedirs(os.path.dirname(last_json_path))
with open(last_json_path, 'w') as f:
f.write(json.dumps({
'timestamp': timestamp,
'CO2': CO2,
'T': T,
'RH': RH,
}))
# Write current values to csv storage
now = datetime.now()
storage_file = os.path.join(
script_dir,
'data',
now.strftime('%Y'),
now.strftime('%Y%m%d.csv')
)
if not os.path.exists(os.path.dirname(storage_file)):
os.makedirs(os.path.dirname(storage_file))
with open(storage_file, 'a') as f:
f.write('%d,%d,%.1f,%.1f\n' % (timestamp, CO2, T, RH))
time.sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment