-
Notifications
You must be signed in to change notification settings - Fork 1
/
pooltemp.ino
259 lines (228 loc) · 6.37 KB
/
pooltemp.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
254
255
256
257
258
259
#include <WiFi101.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include "secrets.h"
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
const char* server = "rbyers-pooltemp.appspot.com";
bool rssibug = false;
bool wifireset = false;
// Update data every 2 minutes
const int updateIntervalSec = 2 * 60;
//const int updateIntervalSec = 10;
// Don't display data older than 15 minutes
const int maxStaleSec = 15 * 60;
const int gmtOffset = -5;
void log(String msg, bool newline = true) {
unsigned long time = millis() / 1000;
unsigned int sec = time % 60;
time /= 60;
unsigned int min = time % 60;
time /= 60;
Serial.print(time);
Serial.print(":");
if (min < 10)
Serial.print("0");
Serial.print(min);
Serial.print(":");
if (sec < 10)
Serial.print("0");
Serial.print(sec);
Serial.print(": ");
Serial.print(msg);
if (newline)
Serial.println();
}
void showMessage(const char* msg) {
alpha4.clear();
const char* p = msg;
for (int i = 0; i < 4 && *p; i++, p++) {
bool dot = false;
if (*(p+1) == '.') {
dot = true;
}
alpha4.writeDigitAscii(i, *p, dot || (i==0 && rssibug) || (i==1 && wifireset));
if (dot)
p++;
}
alpha4.writeDisplay();
}
void setup() {
alpha4.begin(0x70);
alpha4.clear();
alpha4.writeDisplay();
showMessage("BOOT");
Serial.begin(9600);
//Configure pins for Adafruit ATWINC1500 Feather
WiFi.setPins(8,7,4,2);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
log("No WiFi found");
showMessage("NOWS");
// don't continue:
while (true);
}
// Limits receives to the 100ms beacon - should be fine for our purposes.
// TODO: Re-enable once WiFi seems reliable
//WiFi.maxLowPowerMode();
// Give the serial port a chance to connect so we don't miss messages.
// But don't block for long in case there's none connected.
delay(2000);
log("Starting up");
}
String readLine(WiFiClient client) {
String s;
while (client.available() || client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n')
break;
if (c == '\r')
continue;
s.concat(c);
}
}
return s;
}
unsigned long lastUpdate = 0;
unsigned long lastAttempt = 0;
unsigned long lastTime = 0;
bool haveTemps() {
return (lastUpdate && millis() - lastUpdate < maxStaleSec * 1000);
}
String WiFiStatus(int status) {
switch(status) {
case WL_IDLE_STATUS:
return "WL_IDLE_STATUS";
case WL_NO_SSID_AVAIL:
return "WL_NO_SSID_AVAIL";
case WL_CONNECTED:
return "WL_CONNECTED";
case WL_CONNECT_FAILED:
return "WL_CONNECT_FAILED";
case WL_CONNECTION_LOST:
return "WL_CONNECTION_LOST";
case WL_DISCONNECTED:
return "WL_DISCONNECTED";
default:
return String(status);
}
}
void loop() {
unsigned long time = millis();
if (lastAttempt > time || lastUpdate > time) {
// Overflow protection (~50 days)
lastAttempt = 1;
lastUpdate = 1;
lastTime = 1;
}
if (lastAttempt && time - lastAttempt < updateIntervalSec * 1000)
return;
lastAttempt = time;
// Attempt to work around issue with silent WiFi disconnect
// https://github.com/arduino-libraries/WiFi101/issues/86
if (WiFi.status() == WL_CONNECTED && WiFi.RSSI() == 0) {
showMessage("RSSI");
rssibug = true;
WiFi.disconnect();
}
// Attempt to connect to WiFi network
int status;
while(status = WiFi.status() != WL_CONNECTED) {
if (!haveTemps())
showMessage("WiFi");
log("WiFi Status: " + WiFiStatus(status));
log(String("WiFi RSSI: ") + WiFi.RSSI());
log(String("Trying to connect to SSID: ") + SECRET_SSID);
status = WiFi.begin(SECRET_SSID, SECRET_PASS);
if (status == WL_CONNECTED) {
log(String("Connected to WiFi, RSSI:") + WiFi.RSSI());
} else {
log("WiFi connect failed: " + WiFiStatus(status));
if (!haveTemps())
showMessage("WiFL");
delay(2000);
}
}
WiFiClient client;
log(String("WiFi RSSI: ") + WiFi.RSSI());
// Every 10 minutes query the time via NTP to set display brightness
if (!lastTime || time - lastTime > 10 * 60 * 1000) {
if (!haveTemps())
showMessage("NTP");
unsigned long ntp = WiFi.getTime();
log(String("Got time: ") + ntp);
if (ntp > 0) {
int hour = (ntp / 3600 + gmtOffset) % 24;
bool night = hour >= 21 || hour < 7;
int brightness = night ? 3 : 7; // 0-15
log(String("Hour: ") + hour + " Brightness: " + brightness);
alpha4.setBrightness(brightness);
lastTime = time;
}
}
log(String("Attempting to connect to ") + server);
if (!haveTemps())
showMessage("HTTP");
if (!client.connect(server, 80)) {
log("Connection failed");
if (!haveTemps()) {
showMessage("FAIL");
WiFi.disconnect();
wifireset = true;
}
//client.stop();
} else {
// Make the HTTP request
client.println("GET /display HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println("User-Agent: RByers Arduino pooltemp");
client.println();
log("Request sent");
bool doneHeaders = false;
bool doneStatus = false;
while(client.connected()) {
String s = readLine(client);
//Serial.print("Read line: ");
//Serial.println(s);
if (!doneStatus) {
doneStatus = true;
int i = s.indexOf(" ");
if (i == -1) {
log("Invalid HTTP response: " + s);
if (!haveTemps())
showMessage("HTER");
break;
}
String status = s.substring(i+1, i+4);
if (status != "200") {
log("HTTP Failed: " + s);
if (!haveTemps())
showMessage("HERR");
break;
}
}
if (doneHeaders) {
// First line of the body - just show it
if (haveTemps() && s == "OFFLINE") {
log("Got offline");
} else {
log("Updating display: " + s);
showMessage(s.c_str());
lastUpdate = time;
}
break;
}
if (!doneHeaders && s == "") {
doneHeaders = true;
}
}
client.stop();
}
// The WiFi chip appears to draw ~100mA even in max power saving mode
// (at least according to my cheap USB power monitor). Just disconnect
// between update intervals to save power.
// WiFi.disconnect();
}