Skip to content

Instantly share code, notes, and snippets.

@yosida95
Created January 12, 2022 14:08
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 yosida95/e8b78e5f2b4e3d997e654f5dbcd06fb6 to your computer and use it in GitHub Desktop.
Save yosida95/e8b78e5f2b4e3d997e654f5dbcd06fb6 to your computer and use it in GitHub Desktop.
CO2-mini
$ sudo apt-get install \
    python3-dev \
    librrd-dev \
    libudev-dev \
    libusb-1.0-0-dev
$ python3 -m pip install wheel
$ python3 -m pip install hidapi rrdtool
$ mkdir data
$ rrdtool \
  create ./data/temperature.rrd \
  --step 10 \
  --start now-1h \
  DS:celsius:GAUGE:600:U:U \
  RRA:AVERAGE:0.5:1m:8d \
  RRA:AVERAGE:0.5:5m:90d \
  RRA:MIN:0.5:1h:366d \
  RRA:MAX:0.5:1h:366d \
  RRA:AVERAGE:0.5:1h:366d 
$ rrdtool \
  create ./data/co2.rrd \
  --step 10 \
  --start now-1h \
  DS:ppm:GAUGE:600:U:U \
  RRA:AVERAGE:0.5:1m:8d \
  RRA:AVERAGE:0.5:5m:90d \
  RRA:MIN:0.5:1h:366d \
  RRA:MAX:0.5:1h:366d \
  RRA:AVERAGE:0.5:1h:366d
# -*- coding: utf-8 -*-
# https://github.com/heinemml/CO2Meter/blob/master/CO2Meter.py
from datetime import datetime
import hid
import rrdtool
key = [0xc4, 0xc6, 0xc0, 0x92, 0x40, 0x23, 0xdc, 0x96]
RRD_TEMP = 'data/temperature.rrd'
RRD_CO2 = 'data/co2.rrd'
def main():
co2_last_time = 0
temp_last_time = 0
device = hid.device()
device.open(0x04d9, 0xa052)
try:
device.send_feature_report(bytearray([0] + key))
while True:
data = device.read(8)
if len(data) != 8:
print('unexpected length')
continue
if data[4] != 0x0d or (sum(data[:3]) & 0xff) != data[3]:
print('checksum')
continue
value = data[1] << 8 | data[2]
now = int(datetime.now().timestamp())
if data[0] == 0x50:
if (now - co2_last_time) < 1:
continue
rrdtool.update(RRD_CO2, f'{now:d}:{value:d}')
co2_last_time = now
elif data[0] == 0x42:
if (now - temp_last_time) < 1:
continue
value = value / 16.0 - 273.1
rrdtool.update(RRD_TEMP, f'{now:d}:{value:.1f}')
temp_last_time = now
else:
# print('unknown', data[0])
pass
finally:
device.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment