-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ffadd15
Showing
7 changed files
with
612 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lyviex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |