tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/dht/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"machine"
     6  	"time"
     7  	"tinygo.org/x/drivers/dht"
     8  )
     9  
    10  func main() {
    11  	pin := machine.D6
    12  	dhtSensor := dht.New(pin, dht.DHT11)
    13  	for {
    14  		temp, hum, err := dhtSensor.Measurements()
    15  		if err == nil {
    16  			fmt.Printf("Temperature: %02d.%d°C, Humidity: %02d.%d%%\n", temp/10, temp%10, hum/10, hum%10)
    17  		} else {
    18  			fmt.Printf("Could not take measurements from the sensor: %s\n", err.Error())
    19  		}
    20  		// Measurements cannot be updated only 2 seconds. More frequent calls will return the same value
    21  		time.Sleep(time.Second * 2)
    22  	}
    23  }