Skip to content

Instantly share code, notes, and snippets.

@matsuu
Created December 30, 2021 12:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsuu/df7935b3cdd169888c1d00e6cd3ce594 to your computer and use it in GitHub Desktop.
Save matsuu/df7935b3cdd169888c1d00e6cd3ce594 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"time"
"github.com/zserge/hid"
)
const (
vendorID = "04d9:a052"
co2op = 0x50
tempop = 0x42
)
var (
key = []byte{0x86, 0x41, 0xc9, 0xa8, 0x7f, 0x41, 0x3c, 0xac}
)
func monitor(device hid.Device) {
if err := device.Open(); err != nil {
log.Fatal(err)
}
defer device.Close()
if err := device.SetReport(0, key); err != nil {
log.Fatal(err)
}
for {
buf, err := device.Read(-1, 3*time.Second)
if err != nil || len(buf) == 0 {
continue
}
val := int(buf[1])<<8 | int(buf[2])
switch buf[0] {
case co2op:
log.Printf("co2:%d ppm", val)
case tempop:
temp := float64(val)/16.0 - 273.1
log.Printf("temp: %.1f", temp)
}
}
}
func main() {
hid.UsbWalk(func(device hid.Device) {
info := device.Info()
id := fmt.Sprintf("%04x:%04x", info.Vendor, info.Product)
if id != vendorID {
return
}
monitor(device)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment