-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWildFireV4Demo.ino
1341 lines (1195 loc) · 42.7 KB
/
WildFireV4Demo.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ESP8266_AT_Client.h>
#include <util/crc16.h>
#include <avr/eeprom.h>
// Begin Software Reset Implementation
#include <avr/wdt.h>
#define soft_restart() \
do \
{ \
wdt_enable(WDTO_15MS); \
for(;;) \
{ \
} \
} while(0)
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
void wdt_init(void){
MCUSR = 0;
wdt_disable();
return;
}
// End Software Reset Implementation
#define IDLE_TIMEOUT_MS 10000 // Amount of time to wait (in milliseconds) with no data
// received before closing the connection. If you know the server
// you're accessing is quick to respond, you can reduce this value.
#define EDGE_TYPE_ALWAYS (0) // publishes periodically regardless of its value
#define EDGE_TYPE_RISING (1) // publishes whenever value crosses threshold low to high
#define EDGE_TYPE_FALLING (2) // publishes whenever value crosses threshold high to low
#define EDGE_TYPE_ABOVE (3) // publishes periodically whenever value is above threshold
#define EDGE_TYPE_BELOW (4) // publishes periodically whenever value is below threshold
#define EDGE_TYPE_BOTH (5) // publishes whenever value crosses threshold, either high to low, or low to high
typedef struct {
char NETWORK_SSID[32];
char NETWORK_PASSWORD[32];
char PUBLIC_URL[256];
char PUBLIC_KEY[256];
char PRIVATE_KEY[256];
char DELETE_KEY[256];
char ANALOG_ALIAS[8][64];
uint8_t ANALOG_ENABLE[8];
uint32_t POST_INTERVAL_SECONDS;
uint16_t THRESHOLD[8];
uint16_t PREVIOUS_VALUE[8];
uint16_t CURRENT_VALUE[8];
uint8_t EDGE_TYPE[8];
} configuration_t;
configuration_t configuration;
uint16_t CONFIGURATION_CHECKSUM = 0;
#define CONFIGURATION_EEPROM_BASE_ADDRESS (256)
#define CONFIGURATION_CHECKSUM_ADDRESS (4096 - 2) // last two bytes of EEPROM
// the config mode state machine's return values
#define CONFIG_MODE_NOTHING_SPECIAL (0)
#define CONFIG_MODE_GOT_INIT (1)
#define CONFIG_MODE_GOT_EXIT (2)
int esp8266_enable_pin = 23; // Arduino digital the pin that is used to reset/enable the ESP8266 module
Stream * at_command_interface = &Serial1; // Serial1 is the 'stream' the AT command interface is on
ESP8266_AT_Client esp(esp8266_enable_pin, at_command_interface); // instantiate the client object
#define ESP8266_INPUT_BUFFER_SIZE (1500)
uint8_t input_buffer[ESP8266_INPUT_BUFFER_SIZE] = {0}; // sketch must instantiate a buffer to hold incoming data
// 1500 bytes is way overkill for MQTT, but if you have it, may as well
// make space for a whole TCP packet
void processResponseData(uint8_t * data, uint32_t data_length);
void userConfigurationProcess(void);
uint8_t configModeStateMachine(char b, boolean reset_buffers);
char scratch[2048] = {0};
void get(char * hostname, uint16_t port, char * url_path, void (*responseBodyProcessor)(uint8_t *, uint32_t));
uint16_t computeConfigCrc(){
uint16_t computed_crc = 0;
uint8_t * ptr = (uint8_t *) (&configuration);
for(uint16_t ii = 0; ii < sizeof(configuration); ii++){
computed_crc = _crc16_update(computed_crc, *ptr++);
}
return computed_crc;
}
void commitConfiguration(void){
eeprom_write_block((const void *) &configuration, (void *) CONFIGURATION_EEPROM_BASE_ADDRESS, sizeof(configuration));
eeprom_write_word((uint16_t *) CONFIGURATION_CHECKSUM_ADDRESS, computeConfigCrc());
}
void commitConfigurationPartial(uint8_t * field_ptr, uint16_t field_size_bytes){
uint16_t offset_bytes = ((uint16_t) field_ptr) - ((uint16_t) (&configuration));
eeprom_write_block((const void *) field_ptr, (void *) ((uint8_t *)(CONFIGURATION_EEPROM_BASE_ADDRESS + offset_bytes)), field_size_bytes);
eeprom_write_word((uint16_t *) CONFIGURATION_CHECKSUM_ADDRESS, computeConfigCrc());
}
unsigned long previousMillis = 0; // will store last time data was posted
long interval = 1000; // interval at which to post data
char HOSTNAME[256] = "";
char URL_PATH_FORMAT_STRING[1024] = "";
char URL_PATH[2048] = "";
char HOSTPORT[10] = "80";
uint16_t hport = 80;
boolean configurationValid(){
uint16_t computed_crc = computeConfigCrc();
if(computed_crc == CONFIGURATION_CHECKSUM){
return true;
}
return false;
}
void setup(void){
Serial.begin(115200); // debug console
Serial1.begin(115200); // AT command interface
// load the configuration, initialize if necessary
eeprom_read_block((void *) &configuration, (const void *) CONFIGURATION_EEPROM_BASE_ADDRESS, sizeof(configuration));
CONFIGURATION_CHECKSUM = eeprom_read_word((const uint16_t *) CONFIGURATION_CHECKSUM_ADDRESS);
if(!configurationValid()){ // we are dealing with virgin EEPROM
// initialize it to all zeros
Serial.println("Initializing Configuration for the first time");
reset("config");
eeprom_read_block((void *) &configuration, (const void *) CONFIGURATION_EEPROM_BASE_ADDRESS, sizeof(configuration));
}
printConfig();
userConfigurationProcess();
// convert interval to milliseconds
interval = configuration.POST_INTERVAL_SECONDS * 1000;
// extract the HOSTNAME from the Public URL
// everything between "http://" and "/"
// if find colon, look for different http port
char * http_slash_slash = strstr(configuration.PUBLIC_URL, "http://");
char * ptr = http_slash_slash + strlen("http://");
if(http_slash_slash != NULL && strlen(ptr) > 8){
for(uint16_t ii = 0; ii < 255; ii++){
if((*ptr) == '/'){
URL_PATH_FORMAT_STRING[0] = '/';
break;
}
else{
HOSTNAME[ii] = *ptr;
}
ptr++;
}
}
ptr = strstr(HOSTNAME,":");
if (ptr != NULL){
*ptr = '\0';
ptr++;
strncpy(HOSTPORT, ptr, 10);
}
Serial.print("Hostname: ");
Serial.println(HOSTNAME);
Serial.print("Hostport: ");
Serial.println(HOSTPORT);
hport = atoi(HOSTPORT);
// build the URL_PATH_FORMAT_STRING
if(strlen(URL_PATH_FORMAT_STRING) == 1){
// add input
strcat(URL_PATH_FORMAT_STRING, "input");
// add "/[publicKey]?private_key=[privateKey]
strcat(URL_PATH_FORMAT_STRING, "/");
strcat(URL_PATH_FORMAT_STRING, configuration.PUBLIC_KEY);
strcat(URL_PATH_FORMAT_STRING, "?private_key=");
strcat(URL_PATH_FORMAT_STRING, configuration.PRIVATE_KEY);
// for each enabled variable add "&[alias]=%d"
for(uint8_t ii = 0; ii < 8; ii++){
if(configuration.ANALOG_ENABLE[ii]){
strcat(URL_PATH_FORMAT_STRING, "&");
strcat(URL_PATH_FORMAT_STRING, &(configuration.ANALOG_ALIAS[ii][0]));
strcat(URL_PATH_FORMAT_STRING, "=%d");
}
}
}
esp.setInputBuffer(input_buffer, ESP8266_INPUT_BUFFER_SIZE); // connect the input buffer up
esp.reset(); // reset the module
// Serial.print("Set Mode to Station...");
esp.setNetworkMode(1);
// Serial.println("OK");
Serial.println();
displayRSSI();
Serial.print("Connecting to Network...");
if(esp.connectToNetwork(configuration.NETWORK_SSID, configuration.NETWORK_PASSWORD, 60000, NULL)){
Serial.println("OK.");
}
else{
Serial.println("Failed.");
}
}
void displayRSSI(void){
static ap_scan_result_t res = {0};
boolean found_ssid = false;
uint8_t target_network_secMode = 0;
uint8_t num_results_found = 0;
char * ssid = &(configuration.NETWORK_SSID[0]);
Serial.println(F("Scanning for networks..."));
boolean foundSSID = esp.scanForAccessPoint(ssid, &res, &num_results_found);
Serial.print(F("Network Scan found "));
Serial.print(num_results_found);
Serial.println(F(" networks"));
if(foundSSID){
Serial.print(F("Found Access Point \""));
Serial.print(ssid);
Serial.print(F("\", "));
int8_t rssi_dbm = res.rssi;
Serial.print(F("RSSI = "));
Serial.print(res.rssi);
Serial.print(F(" dBm, "));
Serial.print(rssi_to_bars(rssi_dbm));
Serial.print(F("/5 bars"));
}
else{
Serial.print(F("Access Point \""));
Serial.print(ssid);
Serial.println(F("\" not found."));
}
Serial.println();
}
uint8_t rssi_to_bars(int8_t rssi_dbm){
uint8_t num_bars = 0;
if (rssi_dbm < -87){
num_bars = 0;
}
else if (rssi_dbm < -82){
num_bars = 1;
}
else if (rssi_dbm < -77){
num_bars = 2;
}
else if (rssi_dbm < -72){
num_bars = 3;
}
else if (rssi_dbm < -67){
num_bars = 4;
}
else{
num_bars = 5;
}
return num_bars;
}
void sampleAndbuildUrlPathString(){
uint16_t field[8] = {0};
uint8_t num_channels_enabled = 0;
memset(URL_PATH, 0, sizeof(URL_PATH));
for(uint8_t ii = 0; ii < 8; ii++){
if(configuration.ANALOG_ENABLE[ii]){
uint16_t value = analogRead(ii);
field[num_channels_enabled++] = value;
configuration.PREVIOUS_VALUE[ii] = configuration.CURRENT_VALUE[ii];
configuration.CURRENT_VALUE[ii] = value;
}
}
switch(num_channels_enabled){
case 1: sprintf(URL_PATH, URL_PATH_FORMAT_STRING, field[0]); break;
case 2: sprintf(URL_PATH, URL_PATH_FORMAT_STRING, field[0], field[1]); break;
case 3: sprintf(URL_PATH, URL_PATH_FORMAT_STRING, field[0], field[1], field[2]); break;
case 4: sprintf(URL_PATH, URL_PATH_FORMAT_STRING, field[0], field[1], field[2], field[3]); break;
case 5: sprintf(URL_PATH, URL_PATH_FORMAT_STRING, field[0], field[1], field[2], field[3], field[4]); break;
case 6: sprintf(URL_PATH, URL_PATH_FORMAT_STRING, field[0], field[1], field[2], field[3], field[4], field[5]); break;
case 7: sprintf(URL_PATH, URL_PATH_FORMAT_STRING, field[0], field[1], field[2], field[3], field[4], field[5], field[6]); break;
case 8: sprintf(URL_PATH, URL_PATH_FORMAT_STRING, field[0], field[1], field[2], field[3], field[4], field[5], field[6], field[7]); break;
}
}
void loop(void){
unsigned long currentMillis = millis();
if(!esp.connectedToNetwork()){
soft_restart();
}
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sampleAndbuildUrlPathString();
if(shouldPublishData()){
get(HOSTNAME, hport, URL_PATH, processResponseData);
}
}
}
boolean shouldPublishData(){
static boolean first = true;
// the following loop implicitly implements an OR type union of conditional functionality across channel configurations
for(uint8_t ii = 0; ii < 8; ii++){
if(configuration.ANALOG_ENABLE[ii]){
//ignore edges on first opportunity
if(!first){
// define a RISING EDGE as if *any* channel is set to 'enabled', and 'rising' and current > previous, and current > threshold, and previous < threshold
boolean is_rising_edge = (configuration.CURRENT_VALUE[ii] > configuration.PREVIOUS_VALUE[ii])
&& (configuration.CURRENT_VALUE[ii] > configuration.THRESHOLD[ii])
&& (configuration.PREVIOUS_VALUE[ii] < configuration.THRESHOLD[ii]) ? true : false;
// define a FALLING EDGE as if *any* channel is set to 'enabled' and 'falling', and current < previous, and current < threshold, and previous > threshold
boolean is_falling_edge = (configuration.CURRENT_VALUE[ii] < configuration.PREVIOUS_VALUE[ii])
&& (configuration.CURRENT_VALUE[ii] < configuration.THRESHOLD[ii])
&& (configuration.PREVIOUS_VALUE[ii] > configuration.THRESHOLD[ii]) ? true : false;
if((configuration.EDGE_TYPE[ii] == EDGE_TYPE_RISING) && is_rising_edge){
return true;
}
if((configuration.EDGE_TYPE[ii] == EDGE_TYPE_FALLING) && is_falling_edge){
return true;
}
if((configuration.EDGE_TYPE[ii] == EDGE_TYPE_BOTH) && (is_rising_edge || is_falling_edge)){
return true;
}
}
else{
first = false;
}
// if *any* channel is set to 'enabled' and 'always' we should publish
if(configuration.EDGE_TYPE[ii] == EDGE_TYPE_ALWAYS){
return true;
}
// if *any* channel is set to 'enabled', and 'above', and current > threshold we should publish
if((configuration.EDGE_TYPE[ii] == EDGE_TYPE_ABOVE)
&& (configuration.CURRENT_VALUE[ii] > configuration.THRESHOLD[ii])){
return true;
}
// if *any* channel is set to 'enabled', and 'below', and current < threshold we should publish
if((configuration.EDGE_TYPE[ii] == EDGE_TYPE_BELOW)
&& (configuration.CURRENT_VALUE[ii] < configuration.THRESHOLD[ii])){
return true;
}
}
}
return false; // if nothing returned true we should not publish
}
uint16_t download_body_crc16_checksum = 0;
uint32_t download_body_bytes_received = 0;
boolean download_past_header = false;
uint32_t download_content_length = 0;
void processHeader(char * key, char * value){
if(strstr(key, "HTTP") != NULL){
Serial.print(millis());
Serial.print("::");
Serial.println(key);
//Serial.print("\"");
//Serial.print(key);
//Serial.print("\" => \"");
//Serial.print(value);
//Serial.println("\"");
}
if(strcmp(key, "Content-Length") == 0){
download_content_length = strtoul(value, NULL, 10);
// Serial.print("Content-Length = ");
// Serial.println(download_content_length);
}
}
uint32_t processHeader(uint8_t * data, uint32_t data_length){
uint32_t start_index = 0;
static uint8_t header_guard_index = 0;
static boolean past_first_line = false;
static char key[64] = {0};
static char value[64] = {0};
static uint8_t key_or_value = 0;
static uint8_t keyval_index = 0;
if(!download_past_header){
for(uint32_t ii = 0; ii < data_length; ii++){
switch(header_guard_index){
case 0:
if(data[ii] == '\r') header_guard_index++;
else if(data[ii] == ':'){
key_or_value = 1;
keyval_index = 0;
}
else if(past_first_line){
if(keyval_index < 63){
if(!((keyval_index == 0) && (data[ii] == ' '))){ // strip leading spaces
if(key_or_value == 0) key[keyval_index++] = data[ii];
else value[keyval_index++] = data[ii];
}
}
else{
// warning the key string doesn't fit in 64 characters
}
}
break;
case 1:
if(data[ii] == '\n'){
header_guard_index++;
if(past_first_line){
processHeader((char *) key, (char *) value);
}
past_first_line = true;
key_or_value = 0;
keyval_index = 0;
memset(key, 0, 64);
memset(value, 0, 64);
}
else header_guard_index = 0;
break;
case 2:
if(data[ii] == '\r') header_guard_index++;
else{
key[keyval_index++] = data[ii];
header_guard_index = 0;
}
break;
case 3:
if(data[ii] == '\n') header_guard_index++;
else header_guard_index = 0;
break;
case 4:
download_past_header = true;
start_index = ii;
header_guard_index = 0;
break;
}
}
}
return start_index;
}
void get(char * hostname, uint16_t port, char * url_path, void (*responseBodyProcessor)(uint8_t *, uint32_t)){
unsigned long total_bytes_read = 0;
uint8_t mybuffer[64] = {0};
// re-initialize the globals
download_body_crc16_checksum = 0;
download_body_bytes_received = 0;
download_past_header = false;
download_content_length = 0;
/* Try connecting to the website.
Note: HTTP/1.1 protocol is used to keep the server from closing the connection before all data is read.
*/
esp.connect(hostname, port);
if (esp.connected()) {
memset(scratch, 0, 2048);
snprintf(scratch, 2047, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n\r\n", url_path, hostname);
esp.print(scratch);
} else {
Serial.println(F("Error: Failed to publish to datastream"));
return;
}
//Serial.println(F("Info: -------------------------------------"));
/* Read data until either the connection is closed, or the idle timeout is reached. */
unsigned long lastRead = millis();
unsigned long num_bytes_read = 0;
unsigned long start_time = millis();
uint32_t loop_counter = 0;
while (esp.connected(false) && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
while (esp.available()) {
//char c = esp.read();
num_bytes_read = esp.read(mybuffer, 64);
total_bytes_read += num_bytes_read;
loop_counter++;
if((loop_counter % 4096) == 0){
Serial.print(".");
}
if(responseBodyProcessor != 0){
responseBodyProcessor(mybuffer, num_bytes_read); // signal end of stream
}
lastRead = millis();
}
}
esp.stop();
// Serial.println();
// Serial.println("Debug: Response Processed");
// Serial.print("Total Bytes: ");
// Serial.println(total_bytes_read);
// Serial.print("File Size: ");
// Serial.println(download_body_bytes_received);
// Serial.print("Checksum: ");
// Serial.println(download_body_crc16_checksum);
// Serial.print("Duration: ");
// Serial.println(millis() - start_time);
}
void processResponseData(uint8_t * data, uint32_t data_length){
uint32_t start_index = processHeader(data, data_length);
if(download_past_header){
download_body_bytes_received += data_length - start_index;
for(uint32_t ii = start_index; ii < data_length; ii++){
download_body_crc16_checksum = _crc16_update(download_body_crc16_checksum, data[ii]);
}
}
}
const char * command_mode_init_string = "cfg";
void userConfigurationProcess(void){
uint32_t timeout = 15000;
Serial.print("Enter '");
Serial.print(command_mode_init_string);
Serial.print("' for CONFIG mode.");
Serial.println();
Serial.print("Enter 'help' for a list of available commands.");
Serial.println();
Serial.print("Enter 'help <command>' for detailed help on any commands.");
Serial.println();
while(timeout > 0){
if(Serial.available()){
if (CONFIG_MODE_GOT_INIT == configModeStateMachine(Serial.read(), false)) {
prompt();
break;
}
}
delay(1);
if((timeout % 1000) == 0){
Serial.print(timeout / 1000);
Serial.print("...");
}
timeout--;
}
if(timeout > 0){ // user interupted the countdown
timeout = 5*60000; // 5 minutes
// stay in this loop until we see the exit token
while(timeout > 0){
if(Serial.available()){
if (CONFIG_MODE_GOT_EXIT == configModeStateMachine(Serial.read(), false)) {
break;
}
}
delay(1);
timeout--;
}
}
}
// this state machine receives bytes and
// returns true if the function is in config mode
char * commands[] = {
"ssid ",
"pwd ",
"password ",
"puburl ",
"pubkey ",
"prikey ",
"delkey ",
"alias ",
"enable ",
"disable ",
"interval ",
"always ",
"rising ",
"falling ",
"both ",
"above ",
"below ",
"reset ",
NULL
};
void (*command_functions[])(char * arg) = {
set_ssid,
set_network_password,
set_network_password,
set_public_url,
set_public_key,
set_private_key,
set_delete_key,
alias,
enable,
disable,
set_interval,
set_always,
set_rising,
set_falling,
set_both,
set_above,
set_below,
reset,
NULL
};
void lowercase(char * str) {
uint16_t len = strlen(str);
if (len < 0xFFFF) {
for (uint16_t ii = 0; ii < len; ii++) {
str[ii] = tolower(str[ii]);
}
}
}
void get_help_indent(void){
Serial.print(F(" "));
}
uint8_t configModeStateMachine(char b, boolean reset_buffers) {
static boolean received_init_code = false;
const uint8_t buf_max_write_idx = 126; // [127] must always have a null-terminator
static char buf[128] = {0}; // buffer to hold commands / data
static uint8_t buf_idx = 0; // current number of bytes in buf
boolean line_terminated = false;
char * first_arg = 0;
uint8_t ret = CONFIG_MODE_NOTHING_SPECIAL;
if (reset_buffers) {
buf_idx = 0;
}
// if you are at the last write-able location in the buffer
// the only legal characters to accept are a backspace, a newline, or a carriage return
// reject anything else implicitly
if((buf_idx == buf_max_write_idx) && (b != 0x7F) && (b != 0x0D) && (b != 0x0A)){
Serial.println(F("Warn: Input buffer full and cannot accept new characters. Press enter to clear buffers."));
}
// the following logic rejects all non-printable characters besides 0D, 0A, and 7F
else if (b == 0x7F) { // backspace key is special
if (buf_idx > 0) {
buf_idx--;
buf[buf_idx] = '\0';
Serial.print(b); // echo the character
}
}
else if (b == 0x0D || b == 0x0A) { // carriage return or new line is also special
buf[buf_idx] = '\0'; // force terminator do not advance write pointer
line_terminated = true;
Serial.println(); // echo the character
}
else if ((buf_idx <= buf_max_write_idx) && isprint(b)) {
// otherwise if there's space and the character is 'printable' add it to the buffer
// silently drop all other non-printable characters
buf[buf_idx++] = b;
buf[buf_idx] = '\0';
Serial.print(b); // echo the character
}
char lower_buf[128] = {0};
if (line_terminated) {
strncpy(lower_buf, buf, 127);
lowercase(lower_buf);
}
// process the data currently stored in the buffer
if (received_init_code && line_terminated) {
// with the exeption of the command "exit"
// commands are always of the form <command> <argument>
// they are minimally parsed here and delegated to
// callback functions that take the argument as a string
// Serial.print("buf = ");
// Serial.println(buf);
if (strncmp(command_mode_init_string, lower_buf, 3) == 0) {
ret = CONFIG_MODE_GOT_INIT;
}
if (strncmp("exit", lower_buf, 4) == 0) {
Serial.println(F("Exiting CONFIG mode..."));
ret = CONFIG_MODE_GOT_EXIT;
}
else {
// the string must have one, and only one, space in it
uint8_t num_spaces = 0;
char * p;
for (p = buf; *p != '\0'; p++) { // all lines are terminated by '\r' above
if (*p == ' ') {
num_spaces++;
}
if ((num_spaces == 1) && (*p == ' ')) {
// if this is the first space encountered, null the original string here
// in order to mark the first argument string
*p = '\0';
}
else if ((num_spaces > 0) && (first_arg == 0) && (*p != ' ')) {
// if we are beyond the first space,
// and have not encountered the beginning of the first argument
// and this character is not a space, it is by definition
// the beginning of the first argument, so mark it as such
first_arg = p;
}
}
// deal with commands that can legitimately have no arguments first
if (strncmp("help", lower_buf, 4) == 0) {
help_menu(first_arg);
}
else if (first_arg != 0) {
//Serial.print(F("Received Command: \""));
//Serial.print(buf);
//Serial.print(F("\" with Argument: \""));
//Serial.print(first_arg);
//Serial.print(F("\""));
//Serial.println();
// command with argument was received, determine if it's valid
// and if so, call the appropriate command processing function
boolean command_found = false;
for (uint8_t ii = 0; commands[ii] != 0; ii++) {
if (strncmp(commands[ii], lower_buf, strlen(buf)) == 0) {
command_functions[ii](first_arg);
command_found = true;
break;
}
}
if (!command_found) {
Serial.print(F("Error: Unknown command \""));
Serial.print(buf);
Serial.println(F("\""));
}
}
else if (strlen(buf) > 0) {
Serial.print(F("Error: Argument expected for command \""));
Serial.print(buf);
Serial.println(F("\", but none was received"));
}
}
}
else if (line_terminated) {
// before we receive the init code, the only things
// we are looking for are an exact match to the strings
// "AQE\r" or "aqe\r"
if (strncmp(command_mode_init_string, lower_buf, 3) == 0) {
received_init_code = true;
ret = CONFIG_MODE_GOT_INIT;
}
else if (strlen(buf) > 0) {
Serial.print(F("Error: Expecting Config Mode Unlock Code ('"));
Serial.print(command_mode_init_string);
Serial.print(F("'), but received '"));
Serial.print(buf);
Serial.println(F("'"));
}
}
// clean up the buffer if you got a line termination
if (line_terminated) {
if (ret == CONFIG_MODE_NOTHING_SPECIAL) {
prompt();
}
buf[0] = '\0';
buf_idx = 0;
}
return ret;
}
void prompt(void) {
Serial.print(F("CFG>: "));
}
void help_menu(char * arg) {
const uint8_t commands_per_line = 3;
const uint8_t first_dynamic_command_index = 2;
lowercase(arg);
if (arg == 0) {
// list the commands that are legal
Serial.print(F("help \texit \t"));
for (uint8_t ii = 0, jj = first_dynamic_command_index; commands[ii] != 0; ii++, jj++) {
if ((jj % commands_per_line) == 0) {
Serial.println();
}
//Serial.print(jj + 1);
//Serial.print(". ");
Serial.print(commands[ii]);
Serial.print('\t');
}
Serial.println();
}
else {
// we have an argument, so the user is asking for some specific usage instructions
// as they pertain to this command
if (strncmp("help", arg, 4) == 0) {
Serial.println(F("help <param>"));
Serial.println(F(" <param> is any legal command keyword"));
Serial.println(F(" result: usage instructions are printed"));
Serial.println(F(" for the command named <arg>"));
}
else if (strncmp("exit", arg, 4) == 0){
Serial.println(F("exit"));
get_help_indent(); Serial.println(F("exits CONFIG mode and begins OPERATIONAL mode."));
}
else if (strncmp("ssid", arg, 4) == 0){
Serial.println(F("ssid <string>"));
get_help_indent(); Serial.println(F("<string> is the SSID of the network the device should connect to."));
}
else if ((strncmp("pwd", arg, 3) == 0) || (strncmp("password", arg, 8) == 0)){
Serial.print(arg);
Serial.println(F(" <string>"));
get_help_indent(); Serial.println(F("<string> is the network password for "));
get_help_indent(); Serial.println(F("the SSID that the device should connect to."));
}
else if (strncmp("puburl", arg, 6) == 0){
Serial.println(F("puburl <string>"));
get_help_indent(); Serial.println(F("<string> is the Public URL provided by data.sparkfun.com"));
get_help_indent(); Serial.println(F(" e.g. do *not* copy the link location. Use the text on the page (http://...)"));
}
else if (strncmp("pubkey", arg, 6) == 0){
Serial.println(F("pubkey <string>"));
get_help_indent(); Serial.println(F("<string> is the Public Key provided by data.sparkfun.com"));
}
else if (strncmp("prikey", arg, 6) == 0){
Serial.println(F("prikey <string>"));
get_help_indent(); Serial.println(F("<string> is the Private Key provided by data.sparkfun.com"));
}
else if (strncmp("delkey", arg, 6) == 0){
Serial.println(F("delkey <string>"));
get_help_indent(); Serial.println(F("<string> is the Delete Key provided by data.sparkfun.com"));
}
else if (strncmp("alias", arg, 5) == 0){
Serial.println(F("alias <number> <string>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("<number> is the associated Field Name on data.sparkfun.com"));
}
else if (strncmp("enable", arg, 6) == 0){
Serial.println(F("enable <number>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("Enabled fields will be published"));
}
else if (strncmp("disable", arg, 7) == 0){
Serial.println(F("disable <number>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("Disabled fields will not be published"));
}
else if (strncmp("interval", arg, 8) == 0){
Serial.println(F("interval <number>"));
get_help_indent(); Serial.println(F("<number> number of seconds between posting to data.sparkfun.com"));
}
else if (strncmp("always", arg, 6) == 0){
Serial.println(F("always <number>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("Note: Publishes on global interval regardless of its value"));
}
else if (strncmp("rising", arg, 6) == 0){
Serial.println(F("rising <number> <threshold>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("<threshold> analog to digital converter threshold value 0..1023"));
get_help_indent(); Serial.println(F("Note: Publish happens once, when value crosses threshold from below to above"));
}
else if (strncmp("falling", arg, 7) == 0){
Serial.println(F("falling <number> <threshold>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("<threshold> analog to digital converter threshold value 0..1023"));
get_help_indent(); Serial.println(F("Note: Publish happens once, when value crosses threshold from above to below"));
}
else if (strncmp("both", arg, 4) == 0){
Serial.println(F("both <number> <threshold>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("<threshold> analog to digital converter threshold value 0..1023"));
get_help_indent(); Serial.println(F("Note: Publish happens when value crosses threshold from above to below, or from below to above"));
}
else if (strncmp("above", arg, 5) == 0){
Serial.println(F("above <number> <threshold>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("<threshold> analog to digital converter threshold value 0..1023"));
get_help_indent(); Serial.println(F("Note: Publishes on global interval as long as value is above threshold"));
}
else if (strncmp("below", arg, 5) == 0){
Serial.println(F("below <number> <threshold>"));
get_help_indent(); Serial.println(F("<number> is the WildFire Analog Input number 0..7"));
get_help_indent(); Serial.println(F("<threshold> analog to digital converter threshold value 0..1023"));
get_help_indent(); Serial.println(F("Note: Publishes on global interval as long as value is below threshold"));
}
else if (strncmp("reset", arg, 5) == 0){
Serial.println(F("reset config"));
get_help_indent(); Serial.println(F("Clears the configuration data"));
}
}
}
void set_ssid(char * arg) {
// we've reserved 32-bytes of EEPROM for an SSID
// so the argument's length must be <= 31
uint16_t len = strlen(arg);
if (len < 32) {
uint8_t * ptr = (uint8_t *) (&(configuration.NETWORK_SSID[0]));
memset(ptr, 0, 32);
strncpy((char *) ptr, arg, len);
commitConfigurationPartial(ptr, 32);
}
else {
Serial.println(F("Error: SSID must be less than 32 characters in length"));
}
}
void set_network_password(char * arg) {
// we've reserved 32-bytes of EEPROM for a network password
// so the argument's length must be <= 31
uint16_t len = strlen(arg);
if (len < 32) {
uint8_t * ptr = (uint8_t *) (&(configuration.NETWORK_PASSWORD[0]));
memset(ptr, 0, 32);
strncpy((char *)ptr, arg, len);
commitConfigurationPartial(ptr, 32);
}
else {
Serial.println(F("Error: Network password must be less than 32 characters in length"));
}
}
void set_public_url(char * arg) {
trim_string(arg);
uint16_t len = strlen(arg);
if (len < 256) {
uint8_t * ptr = (uint8_t *) (&(configuration.PUBLIC_URL[0]));
memset(ptr, 0, 256);
strncpy((char *)ptr, arg, len);
commitConfigurationPartial(ptr, 256);
}
else {
Serial.println(F("Error: Public URL must be less than 256 characters in length"));
}
}
void set_public_key(char * arg) {
trim_string(arg);
uint16_t len = strlen(arg);
if (len < 256) {
uint8_t * ptr = (uint8_t *) (&(configuration.PUBLIC_KEY[0]));
memset(ptr, 0, 256);
strncpy((char *)ptr, arg, len);
commitConfigurationPartial(ptr, 256);
}
else {
Serial.println(F("Error: Public Key must be less than 256 characters in length"));
}
}
void set_private_key(char * arg) {
trim_string(arg);
uint16_t len = strlen(arg);
if (len < 256) {
uint8_t * ptr = (uint8_t *) (&(configuration.PRIVATE_KEY[0]));
memset(ptr, 0, 256);
strncpy((char *)ptr, arg, len);
commitConfigurationPartial(ptr, 256);
}
else {
Serial.println(F("Error: Private Key must be less than 256 characters in length"));
}
}
void set_delete_key(char * arg) {
trim_string(arg);
uint16_t len = strlen(arg);
if (len < 256) {
uint8_t * ptr = (uint8_t *) (&(configuration.DELETE_KEY[0]));
memset(ptr, 0, 256);
strncpy((char *)ptr, arg, len);
commitConfigurationPartial(ptr, 256);
}
else {
Serial.println(F("Error: Delete key must be less than 256 characters in length"));
}
}