-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (81 loc) · 1.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"time"
"github.com/alexcesaro/statsd"
"github.com/goburrow/modbus"
)
func main() {
server := flag.String("statsd-server", "", "statsd UDP server")
flag.Parse()
c, err := statsd.New(
statsd.Address(*server),
statsd.Prefix("solar"),
)
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
handler := modbus.NewRTUClientHandler("/dev/ttyUSB0")
handler.BaudRate = 9600
handler.DataBits = 8
handler.Parity = "N"
handler.StopBits = 2
handler.SlaveId = 1
handler.Timeout = 5 * time.Second
if err := handler.Connect(); err != nil {
fmt.Println("error conecting: ", err)
os.Exit(1)
}
defer handler.Close()
client := modbus.NewClient(handler)
fb := 32768.0
conv := 100.0 / fb
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
for {
select {
case <-s:
fmt.Println("shutting down")
return
default:
results, err := client.ReadHoldingRegisters(8, 44)
if err != nil {
fmt.Println("error reading registers:", err)
return
}
// modbus uses 16bit registers. Results is a slice of uint8. Every two
// registers need to be combined into a uint16 value for their real
// value.
// Get value for Adc_vb_f
hb := results[0]
lb := results[1]
b := uint16(uint16(hb)<<8 | uint16(lb))
v := float64(b) * conv
c.Gauge("adcvbf", v)
// Get value for Adc_va_f
hb = results[2]
lb = results[3]
b = uint16(uint16(hb)<<8 | uint16(lb))
v = float64(b) * conv
c.Gauge("adcvaf", v)
// Get value for Ahc_daily
hb = results[74]
lb = results[75]
b = uint16(uint16(hb)<<8 | uint16(lb))
v = float64(b) * 0.1
c.Gauge("ahcdaily", v)
// Get value for Ahl_daily
hb = results[76]
lb = results[77]
b = uint16(uint16(hb)<<8 | uint16(lb))
v = float64(b) * 0.1
c.Gauge("ahldaily", v)
}
time.Sleep(5 * time.Second)
}
}