gobot.io/x/gobot/v2@v2.1.0/examples/raspi_ccs811.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"time"
    12  
    13  	"gobot.io/x/gobot/v2"
    14  	"gobot.io/x/gobot/v2/drivers/i2c"
    15  	"gobot.io/x/gobot/v2/platforms/raspi"
    16  )
    17  
    18  func CCS811BootData(a *i2c.CCS811Driver) {
    19  	v, err := a.GetHardwareVersion()
    20  	if err != nil {
    21  		fmt.Printf(err.Error())
    22  	}
    23  
    24  	fmt.Printf("Hardare Version %#x\n", v)
    25  
    26  	d, err := a.GetFirmwareBootVersion()
    27  	if err != nil {
    28  		fmt.Printf(err.Error())
    29  	}
    30  
    31  	fmt.Printf("Boot Version %#x\n", d)
    32  
    33  	d, err = a.GetFirmwareAppVersion()
    34  	if err != nil {
    35  		fmt.Printf(err.Error())
    36  	}
    37  
    38  	fmt.Printf("App Version %#x\n\n", d)
    39  }
    40  
    41  func main() {
    42  	r := raspi.NewAdaptor()
    43  	ccs811Driver := i2c.NewCCS811Driver(r)
    44  
    45  	work := func() {
    46  		CCS811BootData(ccs811Driver)
    47  		gobot.Every(1*time.Second, func() {
    48  			s, err := ccs811Driver.GetStatus()
    49  			if err != nil {
    50  				fmt.Printf("Error fetching data from the status register: %+v\n", err.Error())
    51  			}
    52  			fmt.Printf("Status %+v \n", s)
    53  
    54  			hd, err := ccs811Driver.HasData()
    55  			if err != nil {
    56  				fmt.Printf("Error fetching data from the status register: %+v\n", err.Error())
    57  			}
    58  
    59  			if hd {
    60  				ec02, tv0C, _ := ccs811Driver.GetGasData()
    61  				fmt.Printf("Gas Data - eco2: %+v, tvoc: %+v \n", ec02, tv0C)
    62  
    63  				temp, _ := ccs811Driver.GetTemperature()
    64  				fmt.Printf("Temperature %+v \n\n", temp)
    65  			} else {
    66  				fmt.Println("New data is not available")
    67  			}
    68  		})
    69  	}
    70  
    71  	robot := gobot.NewRobot("adaFruitBot",
    72  		[]gobot.Connection{r},
    73  		[]gobot.Device{ccs811Driver},
    74  		work,
    75  	)
    76  
    77  	robot.Start()
    78  }