Created
December 30, 2021 12:22
-
-
Save matsuu/df7935b3cdd169888c1d00e6cd3ce594 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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