-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.cpp
424 lines (359 loc) · 12.4 KB
/
config.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <Arduino.h>
#include <DHT.h>
#include "config.h"
#include <SD.h>
#include "watchdog.h"
#include "rtc.h"
#include "transmit.h"
#ifdef HEATSEEK_FEATHER_WIFI_WICED
char const* get_encryption_str(int32_t enc_type);
#endif
CONFIG_union CONFIG;
static DHT dht(DHT_DATA, DHT22);
void write_config() {
File config_file;
if (config_file = SD.open("config.bin", FILE_WRITE | O_TRUNC)) {
config_file.write(CONFIG.raw, sizeof(CONFIG));
config_file.close();
} else {
Serial.println("unable to update config");
while(true); // watchdog will reboot
}
}
bool read_config() {
File config_file;
bool success = false;
if (config_file = SD.open("config.bin", FILE_READ)) {
int read_size = config_file.read(CONFIG.raw, sizeof(CONFIG));
if (sizeof(CONFIG) == read_size) {
Serial.print("Version from file: ");
Serial.print(CONFIG.data.version);
Serial.print("; expected version: ");
Serial.println(CONFIG_VERSION);
if (CONFIG.data.version == CONFIG_VERSION) {
Serial.println("config loaded");
success = true;
} else {
Serial.println("incorrect config version");
}
} else {
Serial.print("config incorrect size - expected: ");
Serial.print(sizeof(CONFIG));
Serial.print(", got: ");
Serial.println(read_size);
}
config_file.close();
} else {
Serial.println("unable to read config");
}
return success;
}
void set_default_config() {
CONFIG.data.version = CONFIG_VERSION;
CONFIG.data.reading_interval_s = 5 * 60;
CONFIG.data.cell_configured = 0;
CONFIG.data.wifi_configured = 0;
CONFIG.data.temperature_offset_f = 0.0;
strcpy(CONFIG.data.hub_id, "featherhub");
strcpy(CONFIG.data.endpoint_domain, "relay.heatseek.org");
strcpy(CONFIG.data.endpoint_path, "/temperatures");
CONFIG.data.endpoint_configured = 1;
}
int read_input_until_newline(char *message, char *buffer) {
int i = 0;
bool reached_newline = false;
while (true) {
Serial.println(message);
while (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
// Serial.println("done setting");
reached_newline = true;
break;
} else {
buffer[i++] = c;
}
}
if (reached_newline) break;
watchdog_feed();
delay(2000);
}
return i;
}
void print_menu() {
Serial.println("-------------------------------------");
Serial.println("[?] Print this menu");
Serial.println("[t] Set RTC");
Serial.println("[r] Set reading interval");
Serial.println("[q] Clear reading transmission queue");
Serial.println("[v] Calibrate temperature sensor");
#ifdef TRANSMITTER_WIFI
Serial.println("[w] Setup wifi");
#endif
#ifdef HEATSEEK_FEATHER_WIFI_WICED
Serial.println("[a] List nearby access points");
#endif
Serial.println("[i] Setup Cell ID");
Serial.println("[e] Setup API Endpoint");
Serial.println("[p] Print config");
Serial.println("[d] Reset config");
Serial.println("[s] Exit config");
}
void print_config_info() {
Serial.println("-------------------------------------");
Serial.println("Current config:");
#ifdef TRANSMITTER_WIFI
if (CONFIG.data.wifi_configured) {
Serial.print("wifi ssid: ");
Serial.print(CONFIG.data.wifi_ssid);
Serial.print(", wifi pass: ");
Serial.print(CONFIG.data.wifi_pass);
} else {
Serial.print("Wifi not configured");
}
Serial.println();
#endif
if (CONFIG.data.cell_configured) {
Serial.print("hub id: ");
Serial.print(CONFIG.data.hub_id);
Serial.print(", cell id: ");
Serial.print(CONFIG.data.cell_id);
} else {
Serial.print("cell id not configured");
}
Serial.println();
if (CONFIG.data.endpoint_configured) {
Serial.print("endpoint: ");
Serial.print(CONFIG.data.endpoint_domain);
Serial.print(CONFIG.data.endpoint_path);
} else {
Serial.print("endpoint not configured");
}
Serial.println();
Serial.print("reading_interval (seconds): ");
Serial.println(CONFIG.data.reading_interval_s);
}
void enter_configuration() {
print_menu();
while(true) {
char command = Serial.read();
while(Serial.available()) { Serial.read(); }
switch (command) {
case '?': {
print_menu();
break;
}
case 't': {
rtc_set();
print_menu();
break;
}
case 'q': {
clear_queued_transmissions();
print_menu();
break;
}
case 'r': {
char buffer[200];
int length;
length = read_input_until_newline("Enter Reading interval in seconds", buffer);
buffer[length] = '\0';
CONFIG.data.reading_interval_s = strtol(buffer, NULL, 0);
write_config();
update_last_reading_time(0); // take initial reading after configuration
clear_queued_transmissions(); // clear transmission queue
Serial.println("Reading interval Configured");
print_config_info();
print_menu();
break;
}
#ifdef TRANSMITTER_WIFI
case 'w': {
char buffer[200];
int length;
length = read_input_until_newline("Enter WiFi SSID", buffer);
buffer[length] = '\0';
strcpy(CONFIG.data.wifi_ssid, buffer);
length = read_input_until_newline("Enter WiFi password", buffer);
buffer[length] = '\0';
strcpy(CONFIG.data.wifi_pass, buffer);
CONFIG.data.wifi_configured = 1;
write_config();
force_wifi_reconnect();
Serial.println("Wifi Configured");
print_config_info();
print_menu();
break;
}
#endif
#ifdef HEATSEEK_FEATHER_WIFI_WICED
case 'a': {
wl_ap_info_t ap_list[20];
int networkCount = 0;
networkCount = Feather.scanNetworks(ap_list, 20);
Serial.println("=========");
Serial.print("Found "); Serial.print(networkCount); Serial.println(" Networks");
for (int i = 0; i < networkCount; i++) {
Serial.println("=========");
wl_ap_info_t ap = ap_list[i];
Serial.print("SSID: "); Serial.println(ap.ssid);
Serial.print("RSSI: "); Serial.println(ap.rssi);
Serial.print("max data rate: "); Serial.println(ap.max_data_rate);
Serial.print("network type: "); Serial.println(ap.network_type);
Serial.print("security: "); Serial.print(ap.security); Serial.print(" - "); Serial.println(get_encryption_str(ap.security));
Serial.print("channel: "); Serial.println(ap.channel);
Serial.print("band_2_4ghz: "); Serial.println(ap.band_2_4ghz);
}
break;
}
#endif
case 'i': {
char buffer[200];
int length;
// hub is now always set to 'featherhub'
// length = read_input_until_newline("Enter HUB ID", buffer);
// buffer[length] = '\0';
// strcpy(CONFIG.data.hub_id, buffer);
length = read_input_until_newline("Enter CELL ID", buffer);
buffer[length] = '\0';
strcpy(CONFIG.data.cell_id, buffer);
CONFIG.data.cell_configured = 1;
write_config();
Serial.println("Cell ID Configured");
print_config_info();
print_menu();
break;
}
case 'e': {
char buffer[200];
int length;
length = read_input_until_newline("Enter domain of API endpoint (Example: 'heatseek.org')", buffer);
buffer[length] = '\0';
strcpy(CONFIG.data.endpoint_domain, buffer);
length = read_input_until_newline("Enter path of API endpoint (Example: '/readings/create')", buffer);
buffer[length] = '\0';
strcpy(CONFIG.data.endpoint_path, buffer);
CONFIG.data.endpoint_configured = 1;
write_config();
Serial.println("API Endpoint configured");
print_config_info();
print_menu();
break;
}
case 'd': {
Serial.println("reseting config");
set_default_config();
write_config();
print_config_info();
print_menu();
break;
}
case 'p': {
Serial.println("print config info");
print_config_info();
break;
}
case 's': {
Serial.println("exiting config");
return;
}
case 'v': {
char buffer[200];
int length;
float current_temp;
int readings_taken = 0;
float temperature_f;
float average_temperature_f = 0.0;
length = read_input_until_newline("Enter current temperature, in fahrenheit, with one decimal place. For example: '41.0'. PLEASE ENSURE THAT SENSOR IS ON FOR APPROX. 5 MINUTES BEFORE CALIBRATING!", buffer);
buffer[length] = '\0';
current_temp = strtof(buffer, NULL);
while (readings_taken < 5) {
watchdog_feed();
Serial.println("Calibrating, please wait...");
temperature_f = dht.readTemperature(true);
if (!isnan(temperature_f)) {
if (average_temperature_f == 0.0) {
average_temperature_f = temperature_f;
} else {
average_temperature_f = (average_temperature_f + temperature_f) / 2;
}
} else {
Serial.println("Failed to read temperature sensor; reboot device and try again.");
while(true);
}
watchdog_feed();
readings_taken++;
delay(2000);
}
watchdog_feed();
float temperature_offset = current_temp - average_temperature_f;
CONFIG.data.temperature_offset_f = temperature_offset;
write_config();
Serial.print("Temperature offset set to: ");
Serial.println(temperature_offset);
print_config_info();
print_menu();
break;
}
}
watchdog_feed();
delay(50);
}
}
uint32_t get_last_reading_time() {
File reading_time_file;
uint8_t data[4];
if (reading_time_file = SD.open("time.bin", FILE_READ)) {
reading_time_file.read(data, sizeof(data));
reading_time_file.close();
} else {
Serial.println("unable to read last reading time");
return 0;
}
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
}
void update_last_reading_time(uint32_t timestamp) {
uint8_t data[4];
data[0] = (timestamp & 0x000000ff);
data[1] = (timestamp & 0x0000ff00) >> 8;
data[2] = (timestamp & 0x00ff0000) >> 16;
data[3] = (timestamp & 0xff000000) >> 24;
File reading_time_file;
if (reading_time_file = SD.open("time.bin", FILE_WRITE | O_TRUNC)) {
reading_time_file.write(data, sizeof(data));
reading_time_file.close();
} else {
Serial.println("unable to update last reading time");
while(true); // watchdog will reboot
}
Serial.println("updated last reading time");
}
#ifdef HEATSEEK_FEATHER_WIFI_WICED
char const* get_encryption_str(int32_t enc_type)
{
// read the encryption type and print out the name:
switch (enc_type)
{
case ENC_TYPE_AUTO: return "ENC_TYPE_AUTO";
case ENC_TYPE_OPEN: return "ENC_TYPE_OPEN";
case ENC_TYPE_WEP: return "ENC_TYPE_WEP";
case ENC_TYPE_WEP_SHARED: return "ENC_TYPE_WEP_SHARED";
case ENC_TYPE_WPA_TKIP: return "ENC_TYPE_WPA_TKIP";
case ENC_TYPE_WPA_AES: return "ENC_TYPE_WPA_AES";
case ENC_TYPE_WPA_MIXED: return "ENC_TYPE_WPA_MIXED";
case ENC_TYPE_WPA2_AES: return "ENC_TYPE_WPA2_AES";
case ENC_TYPE_WPA2_TKIP: return "ENC_TYPE_WPA2_TKIP";
case ENC_TYPE_WPA2_MIXED: return "ENC_TYPE_WPA2_MIXED";
case ENC_TYPE_WPA_TKIP_ENT: return "ENC_TYPE_WPA_TKIP_ENT";
case ENC_TYPE_WPA_AES_ENT: return "ENC_TYPE_WPA_AES_ENT";
case ENC_TYPE_WPA_MIXED_ENT: return "ENC_TYPE_WPA_MIXED_ENT";
case ENC_TYPE_WPA2_TKIP_ENT: return "ENC_TYPE_WPA2_TKIP_ENT";
case ENC_TYPE_WPA2_AES_ENT: return "ENC_TYPE_WPA2_AES_ENT";
case ENC_TYPE_WPA2_MIXED_ENT: return "ENC_TYPE_WPA2_MIXED_ENT";
case ENC_TYPE_WPS_OPEN: return "ENC_TYPE_WPS_OPEN";
case ENC_TYPE_WPS_SECURE: return "ENC_TYPE_WPS_SECURE";
case ENC_TYPE_IBSS_OPEN: return "ENC_TYPE_IBSS_OPEN";
default: return "UNKNOWN";
}
}
#endif