-
Notifications
You must be signed in to change notification settings - Fork 1
/
scale.ino
140 lines (102 loc) · 2.66 KB
/
scale.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
/*
* WIFI SCALE
* Maxime MOREILLON
*
* Board type: Wemos D1 mini
*/
#include "IotKernel.h"
#include <Q2HX711.h>
#include "SSD1306.h"
#include "font.h"
#include "images.h"
#include "scale_config.h"
// Pin mapping
#define DISPLAY_SDA_PIN D5
#define DISPLAY_SCL_PIN D6
#define HX711_DT_PIN D2
#define HX711_SCL_PIN D1
// Weight
#define SAMPLING_PERIOD 100 // [ms]
#define WEIGHT_BUFFER_SIZE 30
#define UPLOAD_MINIMUM_WEIGHT 5 // [kg]
#define UPLOAD_MAXIMUM_WEIGHT_RANGE 0.6 // [kg]
// Display
#define DISPLAY_WIDTH 128
#define DISPLAY_HEIGHT 64
IotKernel iot_kernel("scale","0.2.0");
Q2HX711 hx711(HX711_DT_PIN, HX711_SCL_PIN);
float weight; // [kg]
long last_sample_time;
float weight_buffer[WEIGHT_BUFFER_SIZE];
// Display
SSD1306 display(0x3c, DISPLAY_SDA_PIN, DISPLAY_SCL_PIN);
// States
boolean wifi_connected = false;
boolean weight_above_threshold = false;
boolean uploading = false;
void setup() {
iot_kernel.init();
display_setup();
delay(10);
display_nothing();
}
void loop() {
iot_kernel.loop();
if(WiFi.status() != WL_CONNECTED){
// Wifi disconnected: display connecting
if(wifi_connected){
Serial.println("[Wifi] Disconnected");
wifi_connected = false;
}
display_connecting();
}
else {
// Wifi connected
if(!wifi_connected){
wifi_connected = true;
Serial.println("[Wifi] Connected");
display_connected();
}
// Weight reading loop
if ( millis() - last_sample_time > SAMPLING_PERIOD) {
last_sample_time = millis();
weight = get_weight();
add_to_buffer(weight);
if (weight > UPLOAD_MINIMUM_WEIGHT) {
// Wifi connected, weight above threshold: display weight
if(!weight_above_threshold){
weight_above_threshold = true;
}
if( get_range() > UPLOAD_MAXIMUM_WEIGHT_RANGE) {
// Wifi connected, weight above threshold and unstable
if(!uploading){
display_weight();
}
}
else {
// Wifi connected, weight above threshold and stable: send weight to server
if(!uploading){
uploading = true;
display_uploading();
if(publish_weight()){
display_upload_success();
}
else{
display_upload_fail();
}
}
}
}
else {
// Wifi connected, weight below threshold: display nothing to prevent display burn in
if(weight_above_threshold){
weight_above_threshold = false;
display_nothing();
}
if(uploading){
uploading = false;
}
}
}
}
}