gobot.io/x/gobot/v2@v2.1.0/examples/raspi_grove_pi_dht.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  const (
    19  	dhtPin        = "4"
    20  	dhtModel      = 1 // white DHT22, 0 for the blue DHT11
    21  	delayMillisec = 10
    22  )
    23  
    24  func main() {
    25  	r := raspi.NewAdaptor()
    26  	gp := i2c.NewGrovePiDriver(r)
    27  
    28  	work := func() {
    29  		gobot.Every(1*time.Second, func() {
    30  			if temp, hum, err := gp.DHTRead(dhtPin, dhtModel, delayMillisec); err != nil {
    31  				fmt.Println(err)
    32  			} else {
    33  				fmt.Println("Temperature [°C]", temp)
    34  				fmt.Println("rel. Humidity [%]", hum)
    35  			}
    36  		})
    37  	}
    38  
    39  	robot := gobot.NewRobot("dhtBot",
    40  		[]gobot.Connection{r},
    41  		[]gobot.Device{gp},
    42  		work,
    43  	)
    44  
    45  	robot.Start()
    46  }