-
Notifications
You must be signed in to change notification settings - Fork 0
/
squirrel.go
121 lines (105 loc) · 3.11 KB
/
squirrel.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
package main
import (
movingaverage "github.com/RobinUS2/golang-moving-average"
"github.com/inktomi/squirrel/hardware"
"github.com/inktomi/squirrel/telemetry"
log "github.com/sirupsen/logrus"
"math"
"os"
"time"
)
func main() {
file, err := os.OpenFile("/var/log/squirrel.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
log.SetOutput(file)
} else {
log.Info("Failed to log to file, using default stderr")
}
if err := hardware.Setup(); err != nil {
log.Fatal("Failed to setup HX711", err)
}
if adafruitClient, err := telemetry.CreateClient(); err != nil {
log.Fatal("Failed to setup & connect to MQTT Topic", err)
} else {
// Clean up MQTT
defer func(client *telemetry.Adafruit) {
if err := adafruitClient.Disconnect(); err != nil {
log.Error(err, "Failed to shut down Adafruit Client.")
}
}(adafruitClient)
// Clean up HX711
defer func() {
if err := hardware.Shutdown(); err != nil {
log.Error(err, "Failed to shut down HX711")
}
}()
// Set up the loop to track weights in
var lastReported int64 = 0
var movingAverage = movingaverage.New(1200)
if err := hardware.SingleBeep(); err != nil {
log.Error("Failed to beep", err)
}
for {
time.Sleep(100 * time.Millisecond)
if weight, err := hardware.GetWeight(); err != nil {
log.Error(err, "Failed to retrieve weight value")
} else {
// 10 weights per second
// 600 weights per minute
// 1200 weights for calibration
if movingAverage.SlotsFilled() {
var zeroValue = movingAverage.Avg()
var variance = math.Abs(zeroValue - weight)
if variance > 500 {
//if err := hardware.Alarm(); err != nil {
// log.Error("Failed to alarm: ", err)
//}
} else {
movingAverage.Add(weight)
}
if reportTime, err := ReportWeightIfNeeded(lastReported, adafruitClient, variance); err != nil {
log.Error(err, "Failed to send telemetry data to Adafruit")
} else {
// We don't want to change the value if we didn't report anything.
if reportTime > 0 {
lastReported = reportTime
}
}
} else {
// We're calibrating.
movingAverage.Add(float64(weight))
log.WithFields(log.Fields{
"weight": weight,
"calibration_count": movingAverage.Count(),
"calibration_value": movingAverage.Avg(),
}).Info("Added weight to calibration")
}
}
}
}
}
func ReportWeightIfNeeded(lastReported int64, adafruitClient *telemetry.Adafruit, weight float64) (int64, error) {
var now = time.Now().Unix()
var interval = now - lastReported
if interval > 20 {
if err := adafruitClient.SendDataPoint(weight); err != nil {
return now, err
} else {
log.WithFields(log.Fields{
"weight": weight,
"now": now,
"lastReported": lastReported,
"interval": interval,
}).Info("Reported variance to adafruit.")
return now, nil
}
} else {
log.WithFields(log.Fields{
"weight": weight,
"now": now,
"lastReported": lastReported,
"interval": interval,
}).Trace("Did not report, time interval was not long enough.")
}
return -1, nil
}