gobot.io/x/gobot/v2@v2.1.0/examples/tinkerboard_bmp280.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  // BMP280 plate: VIN (2.5..5V), GND, SDL, SDA, SDO to GND
    22  func main() {
    23  	a := tinkerboard.NewAdaptor()
    24  	bmp280 := i2c.NewBMP280Driver(a, i2c.WithAddress(0x76),
    25  		i2c.WithBME280PressureOversampling(0x05),
    26  		i2c.WithBME280TemperatureOversampling(0x02),
    27  		i2c.WithBME280IIRFilter(0x05))
    28  
    29  	work := func() {
    30  		gobot.Every(2*time.Second, func() {
    31  			t, e := bmp280.Temperature()
    32  			fmt.Println("Temperature [°C]", t)
    33  			if e != nil {
    34  				fmt.Println(e)
    35  			}
    36  
    37  			p, e := bmp280.Pressure()
    38  			fmt.Println("Pressure [Pa]", p) // 100hPa = 1Pa
    39  			if e != nil {
    40  				fmt.Println(e)
    41  			}
    42  
    43  			a, e := bmp280.Altitude()
    44  			fmt.Println("Altitude [m]", a)
    45  			if e != nil {
    46  				fmt.Println(e)
    47  			}
    48  			fmt.Println("-------------")
    49  		})
    50  	}
    51  
    52  	robot := gobot.NewRobot("bmp280bot",
    53  		[]gobot.Connection{a},
    54  		[]gobot.Device{bmp280},
    55  		work,
    56  	)
    57  
    58  	err := robot.Start()
    59  	if err != nil {
    60  		fmt.Println(err)
    61  	}
    62  }