Send samples latest after one minute

This commit is contained in:
Thomas Klaehn 2023-02-21 09:20:44 +01:00
parent ab73e296b5
commit 585bf84614
2 changed files with 15 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin/

16
main.go
View File

@ -5,12 +5,15 @@ import (
"log" "log"
"time" "time"
// "periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/onewire" "periph.io/x/conn/v3/onewire"
"periph.io/x/conn/v3/physic" "periph.io/x/conn/v3/physic"
"periph.io/x/devices/v3/ds18b20" "periph.io/x/devices/v3/ds18b20"
"periph.io/x/host/v3" "periph.io/x/host/v3"
"periph.io/x/host/v3/netlink" "periph.io/x/host/v3/netlink"
// "periph.io/x/host/v3/rpi"
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
) )
@ -80,6 +83,8 @@ func main() {
Unit: "°C", Unit: "°C",
} }
var last_value = float64(0.0) var last_value = float64(0.0)
update_interval := time.Minute
next_update := time.Now().Add(update_interval)
for { for {
sensor.Sense(&res) sensor.Sense(&res)
log.Print(res.Temperature.String()) log.Print(res.Temperature.String())
@ -87,7 +92,7 @@ func main() {
measure.Value = float64(res.Temperature.Celsius()) measure.Value = float64(res.Temperature.Celsius())
topic := "sauna/temperature" topic := "sauna/temperature"
if measure.Value != last_value { if measure.Value != last_value || next_update.Before(time.Now()) {
res, err := json.Marshal(measure) res, err := json.Marshal(measure)
if err != nil { if err != nil {
logger.Print(err) logger.Print(err)
@ -95,9 +100,16 @@ func main() {
token := client.Publish(topic, 0, false, res) token := client.Publish(topic, 0, false, res)
token.Wait() token.Wait()
} }
next_update = time.Now().Add(update_interval)
} }
last_value = measure.Value last_value = measure.Value
// if measure.Value > 72.0 {
// rpi.P1_40.Out(gpio.Low)
// } else if measure.Value < 68.0 {
// rpi.P1_40.Out(gpio.High)
// }
time.Sleep(time.Second) time.Sleep(time.Second)
} }