Initial commit
Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
commit
ab73e296b5
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@ -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",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
56
Makefile
Normal file
56
Makefile
Normal file
@ -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)
|
40
README.md
Normal file
40
README.md
Normal file
@ -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
|
||||||
|
```
|
0
config/config.json
Normal file
0
config/config.json
Normal file
16
go.mod
Normal file
16
go.mod
Normal file
@ -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
|
||||||
|
)
|
19
go.sum
Normal file
19
go.sum
Normal file
@ -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=
|
104
main.go
Normal file
104
main.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
saunaservice.service
Normal file
10
saunaservice.service
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user