gobot.io/x/gobot/v2@v2.1.0/examples/raspi_sht3x.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 main() {
    19  	r := raspi.NewAdaptor()
    20  	sht3x := i2c.NewSHT3xDriver(r)
    21  
    22  	work := func() {
    23  		sht3x.Units = "F"
    24  		sht3x.Start()
    25  		sn, err := sht3x.SerialNumber()
    26  		fmt.Printf("Serial Number: 0x%08x, err: %v\n", sn, err)
    27  
    28  		gobot.Every(5*time.Second, func() {
    29  			temp, rh, err := sht3x.Sample()
    30  			fmt.Printf("Temp: %f F, Relative Humidity: %f, err: %v\n", temp, rh, err)
    31  		})
    32  	}
    33  
    34  	robot := gobot.NewRobot("SHT3xbot",
    35  		[]gobot.Connection{r},
    36  		[]gobot.Device{sht3x},
    37  		work,
    38  	)
    39  
    40  	robot.Start()
    41  }