-
Notifications
You must be signed in to change notification settings - Fork 2
/
transmit.cpp
456 lines (348 loc) · 12.6 KB
/
transmit.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include "transmit.h"
#include "config.h"
#include "watchdog.h"
#include <SD.h>
#ifdef HEATSEEK_FEATHER_WIFI_WICED
AdafruitHTTP http;
bool wifiConnected = false;
volatile bool response_received = false;
volatile bool transmit_success = false;
#endif
#ifdef HEATSEEK_FEATHER_WIFI_M0
WiFiClient wifiClient;
bool wifiConnected = false;
#endif
#ifdef TRANSMITTER_GSM
#include <avr/dtostrf.h>
HardwareSerial *fonaSerial = &Serial1;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
bool gsmConnected = false;
#endif
#ifdef TRANSMITTER_GSM
bool fona_post(float temperature_f, float humidity, float heat_index, uint32_t current_time) {
fona.HTTP_POST_end();
uint16_t statuscode;
int16_t length;
char url[200];
strcpy(url, CONFIG.data.endpoint_domain);
strcat(url, CONFIG.data.endpoint_path);
char data[200];
char temperature_buffer[10];
char humidity_buffer[10];
char heat_index_buffer[10];
dtostrf(temperature_f, 4, 3, temperature_buffer);
dtostrf(humidity, 4, 3, humidity_buffer);
dtostrf(heat_index, 4, 3, heat_index_buffer);
sprintf(data, "temp=%s&humidity=%s&heat_index=%s&hub=%s&cell=%s&time=%d&sp=%d&cell_version=%s", temperature_buffer, humidity_buffer, heat_index_buffer, CONFIG.data.hub_id, CONFIG.data.cell_id, current_time, CONFIG.data.reading_interval_s, CODE_VERSION);
Serial.print("posting to: "); Serial.println(url);
Serial.print("with data: "); Serial.println(data);
if (!fona.HTTP_POST_start(url, F("application/x-www-form-urlencoded"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
return false;
}
Serial.println("reading status");
if (statuscode != 200) {
Serial.print("status not 200: ");
Serial.println(statuscode);
return false;
}
fona.HTTP_POST_end();
return true;
}
void connect_to_fona() {
Serial.println('starting fona serial');
fonaSerial->begin(4800);
Serial.println('starting fona serial 2');
if (!fona.begin(*fonaSerial)) {
Serial.println("Couldn't find FONA");
while(true); // watchdog will reboot
}
watchdog_feed();
delay(2000);
Serial.println('enabling FONA GPRS');
uint32_t start = millis();
while (!fona.enableGPRS(true)) {
delay(1000);
watchdog_feed();
if (millis() - start > 60000) {
Serial.println("failed to start FONA GPRS after 60 sec");
while (true);
}
}
Serial.println("Enabled FONA GRPS");
gsmConnected = true;
}
bool _transmit(float temperature_f, float humidity, float heat_index, uint32_t current_time) {
if (!CONFIG.data.cell_configured || !CONFIG.data.endpoint_configured) {
Serial.println("cannot send data - not configured");
return false;
}
if (!gsmConnected) connect_to_fona();
watchdog_feed();
int transmit_attempts = 1;
while (!fona_post(temperature_f, humidity, heat_index, current_time)) {
Serial.print("failed to POST, trying again... attempt #");
Serial.println(transmit_attempts);
if (transmit_attempts < 4) {
transmit_attempts++;
watchdog_feed();
delay(500);
} else {
while (true);
}
}
return true;
}
#endif
#ifdef HEATSEEK_FEATHER_WIFI_WICED
void force_wifi_reconnect(void) {
wifiConnected = false;
}
void receive_callback(void) {
http.respParseHeader();
int status_received = http.respStatus();
Serial.printf("transmitted - received status: (%d) \n", status_received);
http.stop();
response_received = true;
transmit_success = (status_received == 200);
}
void connect_to_wifi() {
Serial.print("Please wait while connecting to:");
Serial.print(CONFIG.data.wifi_ssid);
Serial.println("... ");
if (Feather.connect(CONFIG.data.wifi_ssid, CONFIG.data.wifi_pass)) {
Serial.println("Connected!");
wifiConnected = true;
} else {
Serial.printf("Failed! %s (%d) \n", Feather.errstr());
}
Serial.println();
if (!Feather.connected()) { return; }
// Connected: Print network info
Feather.printNetwork();
// Tell the HTTP client to auto print error codes and halt on errors
http.err_actions(true, true);
// Set HTTP client verbose
http.verbose(true);
// Set the callback handlers
http.setReceivedCallback(receive_callback);
}
bool _transmit(float temperature_f, float humidity, float heat_index, uint32_t current_time) {
if (!CONFIG.data.cell_configured || !CONFIG.data.wifi_configured || !CONFIG.data.endpoint_configured) {
Serial.println("cannot send data - not configured");
return false;
}
while (!wifiConnected) { connect_to_wifi(); }
http.connect(CONFIG.data.endpoint_domain, PORT); // Will halt if an error occurs
http.addHeader("User-Agent", USER_AGENT_HEADER);
http.addHeader("Connection", "close");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
char time_buffer[30];
char temperature_buffer[30];
char humidity_buffer[30];
char heat_index_buffer[30];
char reading_interval_buffer[30];
sprintf(time_buffer, "%d", current_time);
sprintf(reading_interval_buffer, "%d", CONFIG.data.reading_interval_s);
sprintf(temperature_buffer, "%.3f", temperature_f);
sprintf(humidity_buffer, "%.3f", humidity);
sprintf(heat_index_buffer, "%.3f", heat_index);
const char* post_data[][2] =
{
{"hub", CONFIG.data.hub_id},
{"cell", CONFIG.data.cell_id},
{"time", time_buffer},
{"temp", temperature_buffer},
{"humidity", humidity_buffer},
{"sp", reading_interval_buffer},
{"heat_index", heat_index_buffer},
{"cell_version", CODE_VERSION},
};
int param_count = 8;
response_received = false;
transmit_success = false;
http.post(CONFIG.data.endpoint_domain, CONFIG.data.endpoint_path, post_data, param_count); // Will halt if an error occurs
while (!response_received || !transmit_success); // Hang if transmit doesn't complete or fails
return true;
}
#endif
#ifdef HEATSEEK_FEATHER_WIFI_M0
void force_wifi_reconnect(void) {
if (wifiConnected) {
wifiConnected = false;
WiFi.end();
}
}
void connect_to_wifi() {
WiFi.setPins(8, 7, 4, 2);
Serial.print("Please wait while connecting to:");
Serial.print(CONFIG.data.wifi_ssid);
Serial.println("... ");
int status = WL_IDLE_STATUS;
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(CONFIG.data.wifi_ssid, CONFIG.data.wifi_pass);
while (status != WL_CONNECTED) {
delay(1000);
Serial.println("Establishing connection...");
}
wifiConnected = true;
Serial.println("Connected to WiFi");
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
watchdog_feed();
}
bool _transmit(float temperature_f, float humidity, float heat_index, uint32_t current_time) {
if (!CONFIG.data.cell_configured || !CONFIG.data.wifi_configured || !CONFIG.data.endpoint_configured) {
Serial.println("cannot send data - not configured");
return false;
}
if (!wifiConnected) { connect_to_wifi(); }
HttpClient client = HttpClient(wifiClient, CONFIG.data.endpoint_domain, 80);
String contentType = "application/x-www-form-urlencoded";
String data = "temp=" + String(temperature_f, 3) + "&humidity=" + String(humidity, 3) + "&heat_index=" + String(heat_index, 3) + "&hub=" + CONFIG.data.hub_id + "&cell=" + CONFIG.data.cell_id + "&time=" + current_time + "&sp=" + CONFIG.data.reading_interval_s + "&cell_version=" + CODE_VERSION;
Serial.print("Posting data: ");
Serial.println(data);
client.post(CONFIG.data.endpoint_path, contentType, data);
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
return statusCode == 200;
}
#endif
typedef struct {
float temperature_f;
float humidity;
float heat_index;
} temp_data_struct;
typedef union {
temp_data_struct data;
uint8_t raw[sizeof(temp_data_struct)];
} temp_data;
// Create a file on the SD card representing a temperature reading
// The file name is the unix timestamp, however, due to FAT naming conventions,
// we have to place a period in it. e.g. 1500985299 -> 1500985.299
// The content of the file is the binary reading data struct.
void queue_transmission(char *filename, temp_data temp, uint32_t current_time) {
char file_path[50];
char timestamp[50];
char timestamp_first_half[50];
char timestamp_last_half[50];
// FAT only allows 8 character names, so we make the last 3 digits of the
// timestamp the file "extension"
sprintf(timestamp, "%d", current_time);
strncpy(timestamp_first_half, timestamp, 7);
timestamp_first_half[7] = '\0';
strncpy(timestamp_last_half, timestamp+7, 3);
timestamp_last_half[3] = '\0';
sprintf(filename, "%s.%s", timestamp_first_half, timestamp_last_half);
sprintf(file_path, "pending/%s", filename);
File temperature_file;
SD.mkdir("pending");
if (temperature_file = SD.open(file_path, FILE_WRITE | O_TRUNC)) {
temperature_file.write(temp.raw, sizeof(temp));
temperature_file.close();
} else {
Serial.println("unable to write temperature");
while(true); // watchdog will reboot
}
}
// Take a filename for a reading, read the temperature data and
// transmit it. Then delete the file when the transfer is successful.
void transmit_queued_temp(char *filename) {
watchdog_feed();
File temperature_file;
temp_data temperature;
char read_time_buffer[100];
char file_path[100];
sprintf(file_path, "pending/%s", filename);
Serial.print("transfering: ");
Serial.println(filename);
strncpy(read_time_buffer, filename, 7);
strncpy(read_time_buffer+7, filename+8, 3);
read_time_buffer[10] = '\0';
uint32_t read_time = strtoul(read_time_buffer, NULL, 0);
if (temperature_file = SD.open(file_path, FILE_READ)) {
int read_size = temperature_file.read(temperature.raw, sizeof(temperature));
if (sizeof(temperature) == read_size) {
bool transmit_success = _transmit(temperature.data.temperature_f, temperature.data.humidity, temperature.data.heat_index, read_time);
temperature_file.close();
delay(100);
if (transmit_success) {
Serial.println("transferred.");
if (SD.remove(file_path)) {
Serial.println("removed.");
} else {
Serial.println("failed to remove file");
}
} else {
Serial.println("failed to transfer");
}
} else {
Serial.print("file incorrect size - expected: ");
Serial.print(sizeof(temperature));
Serial.print(", got: ");
Serial.println(read_size);
}
} else {
Serial.print("failed to open: ");
Serial.println(filename);
}
watchdog_feed();
}
void transmit_queued_temps() {
char filename[100];
File pending_dir = SD.open("pending");
int temps_transfered_count = 0;
while (temps_transfered_count < TRANSMITS_PER_LOOP) {
File entry = pending_dir.openNextFile();
if (!entry) { break; } // No more files
strcpy(filename, entry.name());
entry.close();
transmit_queued_temp(filename);
temps_transfered_count += 1;
}
pending_dir.close();
}
void clear_queued_transmissions() {
File pending_dir = SD.open("pending");
Serial.println("==== Removing queued temperature files");
while (true) {
watchdog_feed();
File entry = pending_dir.openNextFile();
if (!entry) { break; } // No more files
char filename[100];
strcpy(filename, entry.name());
entry.close();
char file_path[100];
sprintf(file_path, "pending/%s", filename);
if (SD.remove(file_path)) {
Serial.println("removed queued temperature file.");
} else {
Serial.println("failed to remove queued temperature file.");
}
}
Serial.println("====");
pending_dir.close();
}
void transmit(float temperature_f, float humidity, float heat_index, uint32_t current_time) {
watchdog_feed();
temp_data temp;
temp.data.temperature_f = temperature_f;
temp.data.humidity = humidity;
temp.data.heat_index = heat_index;
char filename[100];
queue_transmission(filename, temp, current_time);
watchdog_feed();
delay(1000);
Serial.print("created file: ");
Serial.println(filename);
transmit_queued_temp(filename);
transmit_queued_temps();
}