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

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"strconv"
     6  	"time"
     7  
     8  	"tinygo.org/x/drivers/bme280"
     9  )
    10  
    11  func main() {
    12  
    13  	machine.I2C0.Configure(machine.I2CConfig{})
    14  	sensor := bme280.New(machine.I2C0)
    15  	sensor.Configure()
    16  
    17  	connected := sensor.Connected()
    18  	if !connected {
    19  		println("BME280 not detected")
    20  	}
    21  	println("BME280 detected")
    22  
    23  	for {
    24  		temp, _ := sensor.ReadTemperature()
    25  		println("Temperature:", strconv.FormatFloat(float64(temp)/1000, 'f', 2, 64), "°C")
    26  		press, _ := sensor.ReadPressure()
    27  		println("Pressure:", strconv.FormatFloat(float64(press)/100000, 'f', 2, 64), "hPa")
    28  		hum, _ := sensor.ReadHumidity()
    29  		println("Humidity:", strconv.FormatFloat(float64(hum)/100, 'f', 2, 64), "%")
    30  		alt, _ := sensor.ReadAltitude()
    31  		println("Altitude:", alt, "m")
    32  
    33  		time.Sleep(2 * time.Second)
    34  	}
    35  }