From 585bf846149bb5bdaeb1c620893531c957004af4 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Tue, 21 Feb 2023 09:20:44 +0100 Subject: [PATCH] Send samples latest after one minute --- .gitignore | 1 + main.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/main.go b/main.go index 7711d5f..dc20eda 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,15 @@ import ( "log" "time" + // "periph.io/x/conn/v3/gpio" "periph.io/x/conn/v3/onewire" "periph.io/x/conn/v3/physic" "periph.io/x/devices/v3/ds18b20" "periph.io/x/host/v3" "periph.io/x/host/v3/netlink" + // "periph.io/x/host/v3/rpi" + mqtt "github.com/eclipse/paho.mqtt.golang" ) @@ -80,6 +83,8 @@ func main() { Unit: "°C", } var last_value = float64(0.0) + update_interval := time.Minute + next_update := time.Now().Add(update_interval) for { sensor.Sense(&res) log.Print(res.Temperature.String()) @@ -87,7 +92,7 @@ func main() { measure.Value = float64(res.Temperature.Celsius()) topic := "sauna/temperature" - if measure.Value != last_value { + if measure.Value != last_value || next_update.Before(time.Now()) { res, err := json.Marshal(measure) if err != nil { logger.Print(err) @@ -95,9 +100,16 @@ func main() { token := client.Publish(topic, 0, false, res) token.Wait() } + next_update = time.Now().Add(update_interval) } - 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) }