commit ab73e296b5f7e4ba7b50a031cd3b794cc3cc81ab Author: Thomas Klaehn Date: Tue Jan 31 13:11:32 2023 +0100 Initial commit Signed-off-by: Thomas Klaehn diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b994667 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}/main.go", + } + ] +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fa9d9d3 --- /dev/null +++ b/Makefile @@ -0,0 +1,56 @@ +PROJECT_NAME := saunaservice + +PREFIX ?= /usr/bin + +CONFIG_DIR := /etc/$(PROJECT_NAME) +SYSTEM_DIR := /usr/lib/systemd/system + +CONFIG_FILE := config/config.json +BIN_FILE := bin/$(PROJECT_NAME) +UNIT_FILE := $(PROJECT_NAME).service +README_FILE := README.md + +.PHONY: all +all:service + +.PHONY: service +service: + mkdir -p bin + go clean + go mod tidy + go build -o $(BIN_FILE) + +.PHONY: clean +clean: + go clean + rm -rf bin + +.PHONY: install +install: all + # Config file + @if [ -f $(CONFIG_DIR)/$(notdir $(CONFIG_FILE)) ]; then \ + echo "$(CONFIG_DIR)/$(notdir $(CONFIG_FILE)) already exists - skipping..."; \ + else \ + install -d $(CONFIG_DIR); \ + install -m 0644 $(CONFIG_FILE) $(CONFIG_DIR); \ + echo "install -d $(CONFIG_DIR)"; \ + echo "install -m 0644 $(CONFIG_FILE) $(CONFIG_DIR)"; \ + fi + + # Binary + install -d $(PREFIX) + install -m 0755 $(BIN_FILE) $(PREFIX) + + # System unit + install -d $(SYSTEM_DIR) + install -m 0644 $(UNIT_FILE) $(SYSTEM_DIR) + +.PHONY: uninstall +uninstall: + rm -rf $(CONFIG_DIR) + rm -rf $(SYSTEM_DIR)/$(UNIT_FILE) + rm -rf $(PREFIX)/$(PROJECT_NAME) + +.PHONY: package +package: all + tar cvzf $(PROJECT_NAME).tar.gz $(CONFIG_FILE) $(BIN_FILE) $(UNIT_FILE) $(README_FILE) diff --git a/README.md b/README.md new file mode 100644 index 0000000..4640b7e --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Saunaservice + +Collect sauna temperature and transmit it via `MQTT`. + +## Installation + +```shell +make install +``` + +Default install location for the executable is `/usr/bin`. This can be +modyfied by changing the `PREFIX` variable. + +```shell +PREFIX=/usr/local/bin make install +``` + +## Uninstallation + +```shell +make uninstall +``` + +When `PREFIX` was modyfied for installation it needs to be changed for +uninstalling as well. + +```shell +PREFIX=/usr/local/bin make uninstall +``` + +## Configuration + +Default configuration file is installed in `/etc/saunaservice/config.json` + +## systemd service + +```shell +systemctl enable saunaservice.service +systemctl start saunaservice.service +``` diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..83ada80 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module temperature + +go 1.19 + +require ( + github.com/eclipse/paho.mqtt.golang v1.4.2 + periph.io/x/conn/v3 v3.7.0 + periph.io/x/devices/v3 v3.7.0 + periph.io/x/host/v3 v3.8.0 +) + +require ( + github.com/gorilla/websocket v1.4.2 // indirect + golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b68d0d7 --- /dev/null +++ b/go.sum @@ -0,0 +1,19 @@ +github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4= +github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2b7upDZMK9HRbFvCA= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U= +golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +periph.io/x/conn/v3 v3.7.0 h1:f1EXLn4pkf7AEWwkol2gilCNZ0ElY+bxS4WE2PQXfrA= +periph.io/x/conn/v3 v3.7.0/go.mod h1:ypY7UVxgDbP9PJGwFSVelRRagxyXYfttVh7hJZUHEhg= +periph.io/x/devices/v3 v3.7.0 h1:9SqKZ9lcTpND36jDV6ADrtdhAvx7r+PqoFKzpQI+HuM= +periph.io/x/devices/v3 v3.7.0/go.mod h1:K24UH9Y0Zh/8Ib24QK56YyRDLrZFlPEMEH+aea5Xmyw= +periph.io/x/host/v3 v3.8.0 h1:T5ojZ2wvnZHGPS4h95N2ZpcCyHnsvH3YRZ1UUUiv5CQ= +periph.io/x/host/v3 v3.8.0/go.mod h1:rzOLH+2g9bhc6pWZrkCrmytD4igwQ2vxFw6Wn6ZOlLY= diff --git a/main.go b/main.go new file mode 100644 index 0000000..7711d5f --- /dev/null +++ b/main.go @@ -0,0 +1,104 @@ +package main + +import ( + "encoding/json" + "log" + "time" + + "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" + + mqtt "github.com/eclipse/paho.mqtt.golang" +) + +type sample struct { + Value float64 `json:"value"` + Unit string `json:"unit"` +} + +var ( + logger log.Logger = *log.Default() +) + +func init() { + logger.SetPrefix("Temperature: ") +} + +var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { + logger.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic()) +} + +var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) { + logger.Println("Connected") +} + +var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) { + logger.Printf("Connect lost: %v", err) +} + +func main() { + logger.Println("Starting") + + // MQTT connection + opts := mqtt.NewClientOptions() + opts.AddBroker("tcp://nuc:1883") + opts.SetClientID("saunaservice") + opts.SetDefaultPublishHandler(messagePubHandler) + opts.OnConnect = connectHandler + opts.OnConnectionLost = connectLostHandler + client := mqtt.NewClient(opts) + if token := client.Connect(); token.Wait() && token.Error() != nil { + panic(token.Error()) + } + + var addr onewire.Address = 0x8f01193726f5fd28 + if _, err := host.Init(); err != nil { + logger.Fatal(err) + } + + // get 1wire bus + bus, err := netlink.New(1) + if err != nil { + logger.Fatal(err) + } + defer bus.Close() + + // res, err := bus.Search(false) + // log.Print(res) + + sensor, err := ds18b20.New(bus, addr, 9) + if err != nil { + log.Fatal(err) + } + + var res physic.Env + var measure = sample{ + Value: 0.0, + Unit: "°C", + } + var last_value = float64(0.0) + for { + sensor.Sense(&res) + log.Print(res.Temperature.String()) + + measure.Value = float64(res.Temperature.Celsius()) + + topic := "sauna/temperature" + if measure.Value != last_value { + res, err := json.Marshal(measure) + if err != nil { + logger.Print(err) + } else { + token := client.Publish(topic, 0, false, res) + token.Wait() + } + } + + last_value = measure.Value + time.Sleep(time.Second) + } + +} diff --git a/saunaservice.service b/saunaservice.service new file mode 100644 index 0000000..15e7e39 --- /dev/null +++ b/saunaservice.service @@ -0,0 +1,10 @@ +[Unit] +Description=saunaservice service +After=multi-user.target + +[Service] +Type=idle +ExecStart=/usr/bin/saunaservice -c /etc/saunaservice/config.json + +[Install] +WantedBy=multi-user.target