-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermistor.go
182 lines (148 loc) · 3.74 KB
/
thermistor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"fmt"
"log"
"math"
"net/http"
"os"
"path/filepath"
"time"
"github.com/brutella/hc"
"github.com/brutella/hc/accessory"
"github.com/brutella/hc/service"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/raspi"
)
// From the Happy Feet datasheet:
//
// ohms degC
// 64000 -10
// 38000 0
// 23300 10
// 14800 20
// 9700 30
const (
// Voltage in
vin = 3.3
// Fixed resistor, in ohms
rfixed = 10000
// Reference values from the Happy Feet datasheet. Resistance
// of 64000 ohms at -10C.
refr = 64000
reft = -10
// Thermistor beta coefficient, used in Steinhart–Hart
// equation. This is derived from the values in the
// datasheet:
//
// ln(r1/r2) / (1 / (273.15+t1) - 1 / (273.15+t2))
//
// Where r1 and r2 are resistance values and t1 and t2 are
// temperature values. Taking the average of all of the
// possible values from the refernece values gives us this
// beta value.
beta = 3765
)
func main() {
board := raspi.NewAdaptor()
ads1015 := i2c.NewADS1015Driver(board)
if err := ads1015.Start(); err != nil {
log.Fatal(err)
}
info := accessory.Info{
Name: "Heated floor",
Manufacturer: "Happy Feet",
}
acc := accessory.New(info, accessory.TypeThermostat)
sensors := make([]*service.TemperatureSensor, 2)
for i := 0; i < 2; i++ {
sensors[i] = service.NewTemperatureSensor()
sensors[i].CurrentTemperature.SetMinValue(-10)
sensors[i].CurrentTemperature.SetMaxValue(50)
acc.AddService(sensors[i].Service)
}
cfg := hc.Config{
Pin: "00102003",
StoragePath: filepath.Join(os.Getenv("HOME"), ".homecontrol", "thermistors"),
}
ipt, err := hc.NewIPTransport(cfg, acc)
if err != nil {
log.Fatal(err)
}
gauge := promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "thermistor_temperature",
Help: "Current thermistor temperature in Fahrenheit.",
},
[]string{"number"},
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hc.OnTermination(func() {
cancel()
<-ipt.Stop()
})
go func() {
t := time.NewTicker(20 * time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
for i := 0; i < 2; i++ {
const nSamples = 10
var total float64
for j := 0; j < nSamples; j++ {
v, err := ads1015.ReadWithDefaults(i)
if err != nil {
log.Fatal(err)
}
total += v
time.Sleep(100 * time.Millisecond)
}
v := total / nSamples
log.Printf("A%d voltage: %f", i, v)
// Use our fixed 10 kohm
// resistor to convert voltage
// to resistance.
r := (v * rfixed) / (vin - v)
log.Printf("A%d resistance: %f", i, r)
// Steinhart-Hart equation
tc := 1/(math.Log(r/refr)/beta+1/(reft+273.15)) - 273.15
// Fahrenheit for display
tf := tc*9/5 + 32
log.Printf("A%d temperature: %f", i, tf)
sensors[i].CurrentTemperature.SetValue(tc)
gauge.WithLabelValues(fmt.Sprint(i)).Set(tf)
}
}
}
}()
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/metrics", http.StatusMovedPermanently)
})
mux.Handle("/metrics", promhttp.Handler())
log.Printf("Starting Prometheus exporter on :9526")
s := http.Server{
Addr: ":9526",
Handler: mux,
}
go func() {
<-ctx.Done()
s.Shutdown(context.Background())
}()
if err := s.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
return
}
log.Fatalf("cannot start Prometheus exporter: %v", err)
}
}()
log.Println("Starting transport...")
ipt.Start()
}