Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
feuerrot committed Apr 18, 2022
0 parents commit ffadd15
Show file tree
Hide file tree
Showing 7 changed files with 612 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lyviex
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# IKEA VINDRIKTNING Prometheus Exporter
![Vindriktning PCB with USB-TTY-adapter](doc/vi0.jpg)
![Vindriktning PCB with USB-TTY-adapter](doc/vi1.jpg)
Binary file added doc/vi0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/vi1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module github.com/feuerrot/lyviex

go 1.18

require (
github.com/prometheus/client_golang v1.12.1
go.bug.st/serial v1.3.5
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/creack/goselect v0.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
google.golang.org/protobuf v1.26.0 // indirect
)
471 changes: 471 additions & 0 deletions go.sum

Large diffs are not rendered by default.

116 changes: 116 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.bug.st/serial"
)

var (
hdr = []byte{0x16, 0x11, 0x0B}
pmstat = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "sensor",
ConstLabels: map[string]string{
"sensor": "PM1006",
"type": "PM",
},
}, []string{"value"})

pms1_0 prometheus.Gauge
pms2_5 prometheus.Gauge
pms10 prometheus.Gauge
)

func main() {
if len(os.Args) < 2 {
ports, err := serial.GetPortsList()
if err != nil {
log.Fatal(err)
}
if len(ports) == 0 {
log.Fatal("No serial ports found!")
}
for _, port := range ports {
log.Printf("Found port: %v\n", port)
}

log.Printf("Usage: %s [serial port]", os.Args[0])
os.Exit(1)
}

mode := &serial.Mode{
BaudRate: 9600,
}
port, err := serial.Open(os.Args[1], mode)
if err != nil {
log.Fatal(err)
}

pms1_0, _ = pmstat.GetMetricWithLabelValues("1")
pms2_5, _ = pmstat.GetMetricWithLabelValues("2.5")
pms10, _ = pmstat.GetMetricWithLabelValues("10")

go func() {

ctr := 0
buff := make([]byte, 1)
row := make([]byte, 20)
for {
n, err := port.Read(buff)
if err != nil {
log.Fatal(err)
break
}
if n == 0 {
log.Println("\nEOF")
break
}
if ctr < 3 && buff[0] != hdr[ctr] {
ctr = 0
continue
}

row[ctr] = buff[0]
ctr += 1

if ctr == 20 {
parseBuf(row)
ctr = 0
}
}
}()

http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":33141", nil)
}

func parseBuf(b []byte) {
var sum byte
var cs string
for _, e := range b {
sum += e
}
if sum == 0x00 {
cs = "Checksum OK"
} else {
cs = fmt.Sprintf("Checksum ERR: %X\n", sum)
}

log.Printf("%X (%s)", b, cs)

pm2_5 := int(b[5])*256 + int(b[6])
pm1_0 := int(b[9])*256 + int(b[10])
pm10 := int(b[13])*256 + int(b[14])

pms1_0.Set(float64(pm1_0))
pms2_5.Set(float64(pm2_5))
pms10.Set(float64(pm10))

log.Printf("PM2.5: %d\tPM1.0: %d\tPM10: %d\n", pm2_5, pm1_0, pm10)
}

0 comments on commit ffadd15

Please sign in to comment.