-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZonkyScale.ino
253 lines (201 loc) · 5.66 KB
/
ZonkyScale.ino
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
#include <ESP8266HTTPClient.h>
#include <HX711.h>
#include "ZonkyScaleProperties.h"
/* PINs of the indication LED diodes */
#define RED_LED D8
#define YELLOW_LED D7
#define BLUE_LED D6
#define GREEN_LED D5
#define SCALE_DOUT_PIN D1
#define SCALE_SCK_PIN D2
float calibration_factor = 683350;
float offset = 226219; // tare of empty coffee jug
HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);
float tolerance = 0.005; // 5 grams
float lastWeight = -100000;
long lastWeightTime = -1;
boolean saturated = false;
void setup() {
Serial.begin(9600);
if (! connectWifi()) {
Serial.println("Wifi connection error. Going to sleep.");
signalErrorBlick();
} else {
Serial.println("Wifi connection OK.");
}
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(BLUE_LED, LOW);
digitalWrite(GREEN_LED, LOW);
scale.set_scale(calibration_factor);
scale.set_offset(offset); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
Serial.println(zero_factor);
lastWeight = scale.get_units();
lastWeightTime = millis();
saturated = false;
}
void loop() {
checkWeightLoop();
checkNoJugLoop();
}
unsigned long lastWeightCheck = millis();
void checkWeightLoop() {
if (millis() - lastWeightCheck < 250) {
return;
}
lastWeightCheck = millis();
float weight = scale.get_units();
Serial.println(String(weight, 3));
float diff = weight - lastWeight;
if (diff < 0) {
diff *= -1;
}
if (diff <= tolerance) {
// same weight
if (saturated == false && millis() - lastWeightTime > 2000) {
// same and saturated
saturated = true;
Serial.println("Weight is saturated.");
signalWeight(lastWeight);
}
} else {
// different weight
lastWeight = weight;
lastWeightTime = millis();
saturated = false;
Serial.println("Not saturated.");
}
}
unsigned long lastNoJugCheck = millis();
unsigned long lastNoJugBlickTime = millis();
bool on = false;
void checkNoJugLoop() {
if (millis() - lastNoJugCheck < 1000) {
return;
}
lastNoJugCheck = millis();
if (saturated && lastWeight < -0.150 && millis() - lastNoJugBlickTime > 1000) {
on = !on;
digitalWrite(RED_LED, on ? HIGH : LOW);
}
}
void signalWeight(float weight) {
if (weight < -0.150) { // less than -150g - jug is not placed
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(BLUE_LED, LOW);
digitalWrite(GREEN_LED, LOW);
} else if (weight < 0.060) { // less than 60g (60ml)
digitalWrite(RED_LED, HIGH);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(BLUE_LED, LOW);
digitalWrite(GREEN_LED, LOW);
} else if (weight < 0.300) {
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, HIGH);
digitalWrite(BLUE_LED, LOW);
digitalWrite(GREEN_LED, LOW);
} else if (weight < 0.800) {
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(BLUE_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
} else {
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(BLUE_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
}
// Send to influx
if (weight > -0.150) {
HTTPClient http;
http.begin(influxUrl);
http.setAuthorization(influxUser, influxPass);
int httpCode = http.POST((String)"zonky-coffee-scale weight=" + weight);
Serial.print("Influx DB result: ");
Serial.println(httpCode);
if (httpCode < 0) {
signalErrorBlick();
} else {
String payload = http.getString(); //Get the request response payload
Serial.println(payload);
}
}
}
/**
* Connect the Wifi
*/
bool connectWifi() {
int connWait = 15; //seconds
int delayMs = 50; //ms
int ticksWait = connWait * 1000 / delayMs;
Serial.println("\nChecking the wifi.");
if (WiFi.SSID() != ssid) {
Serial.println("\nConnecting the wifi.");
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, ssidPass);
}
while (ticksWait-- > 0 && WiFi.status() != WL_CONNECTED) {
Serial.print(".");
Serial.print(WiFi.status());
digitalWrite(LED_BUILTIN, LOW);
delay(5);
digitalWrite(LED_BUILTIN, HIGH);
delay(delayMs);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected");
return false;
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("mac address: ");
Serial.println(WiFi.macAddress());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Mask: ");
Serial.println(WiFi.subnetMask());
Serial.print("GW address: ");
Serial.println(WiFi.gatewayIP().toString());
String macStr = WiFi.BSSIDstr();
Serial.print("APMAC: ");
Serial.println(macStr);
return true;
}
/**
* In case of an error, blink with the RED diode. SOS Morse Code
*/
void signalErrorBlick() {
// S morse code
for (int i = 0; i < 3; i++) {
blick(RED_LED, 50);
delay(100);
}
delay(100);
// O morse code
for (int i = 0; i < 3; i++) {
blick(RED_LED, 150);
delay(100);
}
delay(100);
// S morse code
for (int i = 0; i < 3; i++) {
blick(RED_LED, 50);
delay(100);
}
}
void blick(int pin, int onTime) {
digitalWrite(pin, HIGH);
delay(onTime);
digitalWrite(pin, LOW);
}