gobot.io/x/gobot/v2@v2.1.0/examples/raspi_sht2x.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  	sht2x := i2c.NewSHT2xDriver(r)
    21  
    22  	work := func() {
    23  		gobot.Every(1*time.Second, func() {
    24  			t, _ := sht2x.Temperature()
    25  			fmt.Printf("Temperature: %v\n", t)
    26  
    27  			h, _ := sht2x.Humidity()
    28  			fmt.Printf("Humidity: %v\n", h)
    29  		})
    30  	}
    31  
    32  	robot := gobot.NewRobot("SHT2xbot",
    33  		[]gobot.Connection{r},
    34  		[]gobot.Device{sht2x},
    35  		work,
    36  	)
    37  
    38  	robot.Start()
    39  }