gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_bme280.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/tinkerboard"
    16  )
    17  
    18  // Wiring
    19  // PWR  Tinkerboard: 1 (+3.3V, VCC), 2(+5V), 6, 9, 14, 20 (GND)
    20  // I2C1 Tinkerboard: 3 (SDA-ws), 5 (SCL-gn)
    21  // BME280 plate: VIN (2.5..5V), GND, SDL, SDA, SDO to GND
    22  func main() {
    23  	a := tinkerboard.NewAdaptor()
    24  	bme280 := i2c.NewBME280Driver(a, i2c.WithAddress(0x76),
    25  		i2c.WithBME280PressureOversampling(0x05),
    26  		i2c.WithBME280TemperatureOversampling(0x02),
    27  		i2c.WithBME280HumidityOversampling(0x01),
    28  		i2c.WithBME280IIRFilter(0x05))
    29  
    30  	work := func() {
    31  		gobot.Every(2*time.Second, func() {
    32  			t, e := bme280.Temperature()
    33  			fmt.Println("Temperature [°C]", t)
    34  			if e != nil {
    35  				fmt.Println(e)
    36  			}
    37  
    38  			p, e := bme280.Pressure()
    39  			fmt.Println("Pressure [Pa]", p) // 100hPa = 1Pa
    40  			if e != nil {
    41  				fmt.Println(e)
    42  			}
    43  
    44  			a, e := bme280.Altitude()
    45  			fmt.Println("Altitude [m]", a)
    46  			if e != nil {
    47  				fmt.Println(e)
    48  			}
    49  
    50  			h, e := bme280.Humidity()
    51  			fmt.Println("Humidity [%]", h)
    52  			if e != nil {
    53  				fmt.Println(e)
    54  			}
    55  			fmt.Println("-------------")
    56  		})
    57  	}
    58  
    59  	robot := gobot.NewRobot("bme280bot",
    60  		[]gobot.Connection{a},
    61  		[]gobot.Device{bme280},
    62  		work,
    63  	)
    64  
    65  	err := robot.Start()
    66  	if err != nil {
    67  		fmt.Println(err)
    68  	}
    69  }