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