hybridgroup.gobot/examples/raspi_ccs811.go

81 lines
1.5 KiB
Go
Raw Permalink Normal View History

//go:build example
2018-10-09 13:13:54 +08:00
// +build example
2018-10-09 13:13:54 +08:00
//
// Do not build by default.
package main
import (
"fmt"
"time"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/i2c"
"gobot.io/x/gobot/v2/platforms/raspi"
2018-10-09 13:13:54 +08:00
)
func CCS811BootData(a *i2c.CCS811Driver) {
v, err := a.GetHardwareVersion()
if err != nil {
fmt.Println(err.Error())
2018-10-09 13:13:54 +08:00
}
fmt.Printf("Hardare Version %#x\n", v)
d, err := a.GetFirmwareBootVersion()
if err != nil {
fmt.Println(err.Error())
2018-10-09 13:13:54 +08:00
}
fmt.Printf("Boot Version %#x\n", d)
d, err = a.GetFirmwareAppVersion()
if err != nil {
fmt.Println(err.Error())
2018-10-09 13:13:54 +08:00
}
fmt.Printf("App Version %#x\n\n", d)
}
func main() {
r := raspi.NewAdaptor()
ccs811Driver := i2c.NewCCS811Driver(r)
work := func() {
CCS811BootData(ccs811Driver)
gobot.Every(1*time.Second, func() {
s, err := ccs811Driver.GetStatus()
if err != nil {
fmt.Printf("Error fetching data from the status register: %+v\n", err.Error())
}
fmt.Printf("Status %+v \n", s)
hd, err := ccs811Driver.HasData()
if err != nil {
fmt.Printf("Error fetching data from the status register: %+v\n", err.Error())
}
if hd {
ec02, tv0C, _ := ccs811Driver.GetGasData()
fmt.Printf("Gas Data - eco2: %+v, tvoc: %+v \n", ec02, tv0C)
temp, _ := ccs811Driver.GetTemperature()
fmt.Printf("Temperature %+v \n\n", temp)
} else {
fmt.Println("New data is not available")
2018-10-09 13:13:54 +08:00
}
})
}
robot := gobot.NewRobot("adaFruitBot",
[]gobot.Connection{r},
[]gobot.Device{ccs811Driver},
work,
)
if err := robot.Start(); err != nil {
panic(err)
}
2018-10-09 13:13:54 +08:00
}