-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod-bmp280.go
executable file
·84 lines (71 loc) · 2.19 KB
/
mod-bmp280.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"context"
"fmt"
"log"
"sync"
"github.com/parMaster/rpid/config"
"github.com/parMaster/rpid/storage"
"github.com/parMaster/rpid/storage/model"
"periph.io/x/conn/v3/i2c"
"periph.io/x/conn/v3/physic"
"periph.io/x/devices/v3/bmxx80"
)
const (
HectoPascal physic.Pressure = 100 * physic.Pascal
)
type Bmp280Reporter struct {
data map[string][]ShortFloat
cfg config.BMP280
bmp280Data physic.Env
bmp280Device *bmxx80.Dev
i2cBus i2c.BusCloser
mx sync.Mutex
store storage.Storer
}
func LoadBmp280Reporter(cfg config.BMP280, i2cBus i2c.BusCloser, store storage.Storer) (*Bmp280Reporter, error) {
if !cfg.Enabled {
return nil, fmt.Errorf("Bmp280Reporter is not enabled")
}
data := map[string][]ShortFloat{
"pressure": {}, // Atmospheric pressure from BMP280 in hPa
"temp": {}, // Temperature from BMP280 in mC
}
bmp280Device, err := bmxx80.NewI2C(i2cBus, cfg.Bmp280Addr, &bmxx80.DefaultOpts)
if err != nil {
return nil, fmt.Errorf("[ERROR] failed to initialize bmp280: %w", err)
}
b := &Bmp280Reporter{data: data, bmp280Device: bmp280Device, i2cBus: i2cBus, cfg: cfg}
if store != nil {
log.Printf("[DEBUG] Bmp280Reporter: using storage (%T)", store)
b.store = store
}
return b, nil
}
func (r *Bmp280Reporter) Name() string {
return "bmp280"
}
func (r *Bmp280Reporter) Collect(ctx context.Context) error {
if err := r.bmp280Device.Sense(&r.bmp280Data); err != nil {
return err
}
pressurePa := ShortFloat(r.bmp280Data.Pressure / physic.Pascal)
tempMilliC := ShortFloat(r.bmp280Data.Temperature-physic.ZeroCelsius) / 1000000000
r.mx.Lock()
r.data["pressure"] = append(r.data["pressure"], pressurePa/100)
r.data["temp"] = append(r.data["temp"], tempMilliC)
r.mx.Unlock()
log.Printf("[DEBUG] BMP280: %8s | %s hPa \n", r.bmp280Data.Temperature, pressurePa)
if r.store != nil {
err := r.store.Write(ctx, model.Data{Module: r.Name(), Topic: "pressure", Value: fmt.Sprint(pressurePa / 100)})
if err != nil {
return fmt.Errorf("[ERROR] Bmp280Reporter: failed to write to storage: %v", err)
}
}
return nil
}
func (r *Bmp280Reporter) Report() (interface{}, error) {
r.mx.Lock()
defer r.mx.Unlock()
return r.data, nil
}