gobot.io/x/gobot@v1.16.0/examples/raspi_ccs811.go (about)

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