-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDesktopDashboard.ino
1157 lines (985 loc) · 36.9 KB
/
DesktopDashboard.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
//
// created in Visual Studio 2017 with Visual Micro add-in: vMicro - New Arduino Project
// https://www.visualstudio.com/downloads/
//
// http://www.visualmicro.com/
//
// View - Solution Explorer; Right-click on Solution in ; select "add to sourcce control"
// View - Team Explorer; Publish to GitHub (Get Started...)
//
// copy / paste Adafruit graphictest.ino (see Arduino File-Examples...)
// if Arduino environment properly installed, simply vMicro-Build to automaticlly resolve references (no need to manually vMicro-Add Library...)
//
// Default vMicro project location is C:\Users\<username>\Documents\Arduino\<ProjectName>
//
// Target display is like the Adafruit ILI9341 http://www.adafruit.com/products/1651
//
//***************************************************
#include "GlobalDefine.h"
#include "hardwareHelper.h"
#include "DashboardClient.h"
#include "htmlHelper.h"
String DasboardDataFile = DASHBOARD_DEFAULT_DATA; // set a default, but based on mac address we might determine a user-specific value
// htmlHelper myHtmlHelper;
//#include <vector>
//int test()
//{
// std::vector<char> fcharacters;
//
//}
// include "ili9341test.h"
// include "settings.h"
// include "debughandler.h"
// include "wifiConnectHelper.h" // no longer used, see htmlHelper
// include "DashboardListener.h" // this is our implementation of a JSON listener used by JsonStreamingParser
// include "/workspace/FastSeedTFTv2//FastTftILI9341.h" // needs avr/pgmspace - what to do for ESP8266?
#ifdef _MSC_VER
#pragma region BOARD_DETECT
#endif
#undef FOUND_BOARD
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include "FS.h" // ESP8266 file system wrapper
#define FOUND_BOARD ESP8266
#endif
#ifdef ARDUINO_ARCH_ESP32
#include <HTTPClient.h>
#include <WiFi.h>
#include "FS.h" // ESP8266 file system wrapper
#include "SPIFFS.h" // ESP32 requires SPIFFS.h
#define FOUND_BOARD ESP32
#endif
#ifndef FOUND_BOARD
#pragma message(Reminder "Error Target hardware not defined !")
#endif // ! FOUND_BOARD
#ifdef _MSC_VER
#pragma endregion
#endif
#include "SPI.h"
#include "Adafruit_GFX.h" // setup via Arduino IDE; Sketch - Include Library - Manage Libraries; Adafruit GFX Library 1.1.5
#include "Adafruit_ILI9341.h" // setup via Arduino IDE; Sketch - Include Library - Manage Libraries; Adafruit ILI9341
#include "FreeSansBold24pt7b.h" // Adafruit_ILI9341.h is needed; copy to project directory from Adafruit-GFX-Library\Fonts; show all files. right-click "include in project"
#include "JsonStreamingParser.h" // this library is already included as local library, but may need to be copied manually from https://github.com/squix78/json-streaming-parser
#include "JsonListener.h"
#include "sslHelper.h"
#include "WiFiHelper.h"
#include "htmlHelper.h" // htmlHelper files copied to this project from https://github.com/gojimmypi/VisitorWiFi-ESP8266
// TODO - delete display stuff once move to tftHelper
//#define TFT_DC 2
//#define TFT_CS 15
//
//#define Touch_CS 4
//#define Touch_IRQ 5
//#define SD_CS 9
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#include "ImageViewer.h"
#include "tftHelper.h" // tft screen printing
unsigned long timeoutDashboardDataRefresh = 0;
unsigned long timerRefresh = 0;
const int DATA_REFRESH_INTERVAL = 20000; // how frequently to fetch HTML data
bool fetchWaiting = false;
DashboardClient listener;
//String myMacAddress;
JsonStreamingParser parser; // note the parser can only be used once! (TODO - consider implementing some sort of re-init)
WIFI_CLIENT_CLASS client;
#ifdef USE_TLS_SSL
const int httpPort = 443;
#else
const int httpPort = 80;
#endif
//#ifdef USE_TLS_SSL
//void fetchDashboardData(WIFI_CLIENT_CLASS * client, JsonStreamingParser * parser) {
// Serial.println("Using WiFiClientSecure!");
//#else
//void fetchDashboardData(WiFiClient * client, JsonStreamingParser * parser) {
//#endif
// parser->reset();
// boolean isBody = false;
// char c;
// String msg = "";
// int size = 0;
// client->setNoDelay(false);
//#ifdef JSON_DEBUG
// int sizePreview = 0; // number of chars of JSON preview, as we don't usually need to see the whole file
// Serial.print("Showing first ");
// Serial.println(sizePreview);
// Serial.println(" bytes of JSON Data:");
//#endif // JSON_DEBUG
//
// //while (client->connected()) {
// while ((size = client->available()) > 0) {
// c = client->read();
// if (!isBody) {
// msg += c;
// }
//#ifdef JSON_DEBUG
// sizePreview--;
// if (sizePreview >= 0) {
// Serial.print(c); // we read JSON data one char at a time.... but only display the first [sizePreview] bytes
// }
//#endif // JSON_DEBUG
//
// if (c == '{' || c == '[') {
// isBody = true;
// }
// if (isBody) {
// parser->parse(c);
// yield();
// }
// }
// //}
//
//#ifdef JSON_DEBUG
// Serial.println("Done parsing data!\r\n\r\n");
// Serial.println("Message");
// Serial.println(msg);
//
//#endif // JSON_DEBUG
//
//
//#ifdef ARDUINO_ARCH_ESP8266
// client->stopAll(); // flush client (only ESP8266 seems to have implemented stopAll)
//#endif
//
//#ifdef ARDUINO_ARCH_ESP32
// client->stop(); // flush client (the ESP32 does not seem to have implemented stopAll)
//#endif
//
// parser->setListener(NULL); // cleanup the parser
//
//}
//*******************************************************************************************************************************************
// UpdateDashboard (deprecated)
//*******************************************************************************************************************************************
//void UpdateDashboard() {
// tftScreenClear();
// tftPrintlnCentered("Refreshing...");
//
// //tft.setRotation(3);
// //tft.setFont(&FreeSansBold24pt7b); // load our custom 24pt font
//
// //tftScreenClear();
// //tft.setCursor(0, 36);
// //tft.setTextColor(ILI9341_WHITE); // tft.setTextSize(1);
// //tft.println("Refreshing...");
//
// parser.setListener(&listener); // init our JSON listener
// HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
//
////#ifdef USE_TLS_SSL
//// WiFiClientSecure client;
//// const int httpPort = 443;
//// Serial.println("Using WiFiClientSecure!");
////#else
//// WiFiClient client;
//// const int httpPort = 80;
////#endif
// Serial.println("UpdateDashboard for file =" + DasboardDataFile);
// String httpPayload2 = String("GET ") + DASHBOARD_PATH + DasboardDataFile + " HTTP/1.1\r\n" +
// "Host: " + DASHBOARD_HOST + "\r\n" +
// String(WIFI_USER_AGENT) + "\r\n" + // see myPrivbateSettings, typical value: "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; ASTE; rv:11.0) like Gecko\r\n";
// "Connection: Close\r\n\r\n";
//
// Serial.println("JSON fetch httpPayload2:");
// Serial.println(DEBUG_SEPARATOR);
// Serial.println(httpPayload2);
// Serial.println(DEBUG_SEPARATOR);
//
// String httpPayload = htmlBasicHeaderText("GET", DASHBOARD_HOST, DASHBOARD_PATH + DasboardDataFile);
//#ifdef JSON_DEBUG
// Serial.println();
// Serial.println("JSON fetch httpPayload:");
// Serial.println(DEBUG_SEPARATOR);
// Serial.println(httpPayload);
// Serial.println(DEBUG_SEPARATOR);
//#endif
//
//
// if (!client.connect(DASHBOARD_HOST, httpPort)) {
// Serial.println("connection failed");
// Serial.println("");
// }
//
// if (client.verify(DASHBOARD_HOST_THUMBPRINT, DASHBOARD_HOST)) {
// Serial.println("TLS certificate matches");
// }
// else {
// Serial.println("TLS certificate doesn't match");
// }
//
// client.print(httpPayload);
// Serial.println("request sent"); // delay needed here?
//
// Serial.println("Reading Headers...");
// Serial.println(DEBUG_SEPARATOR);
//
// String line = "";
// unsigned long timeout = millis();
//
// timeout = millis();
// while (client.connected()) {
// while (client.available() == 0) {
// yield();
// if (millis() - timeout > 10000) {
// Serial.println(">>> Client Timeout !");
// client.stop();
// return;
// }
// }
// if (client.available()) {
// yield();
// timeout = millis();
// line = client.readStringUntil('\n');
// Serial.println(line);
// if (line == "\r") {
// Serial.println("headers received!");
// Serial.println(DEBUG_SEPARATOR);
// break;
// }
// }
// //else {
// // Serial.print("No data available; status = ");
// // Serial.println(client.status());
// // Serial.println(DEBUG_SEPARATOR);
// // Serial.println("waiting 60 seconds...");
// // delay(60000);
// // return;
// //}
// yield();
// }
//
//
// int retryCounter = 0;
// // see http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/client-examples.html
//
// Serial.print("\r\n\r\nProceeding! client.status = ");
//
//#ifdef ARDUINO_ARCH_ESP8266
// Serial.print(client.status()); // (only ESP8266 seems to have implemented (client.status)
//#endif
//
//#ifdef ARDUINO_ARCH_ESP32
// Serial.print("client.status() not available for ESP32"); // flush client (the ESP32 does not seem to have implemented client.status)
//#endif
//
// fetchDashboardData(&client, &parser);
//
// tft.setRotation(3);
// tft.fillScreen(ILI9341_BLACK);
// unsigned long start = micros();
// tft.setFont(&FreeSansBold24pt7b); // load our custom 24pt font
//
//
//
//#ifdef ARDUINO_ARCH_ESP8266
// client.stopAll(); // flush client (only ESP8266 seems to have implemented stopAll)
//#endif
//
//#ifdef ARDUINO_ARCH_ESP32
// client.stop(); // flush client (the ESP32 does not seem to have implemented stopAll)
//#endif
//}
// see https://www.visualmicro.com/page/User-Guide.aspx?doc=Arduino-gdb-In-Brief.html#noopt
#if _VM_DEBUG
#pragma GCC optimize ("O0")
#endif
//*******************************************************************************************************************************************
// fetchDashboardDataChar
//*******************************************************************************************************************************************
bool fetchingData = false;
boolean isBody = false;
String msg = "";
int size = 0;
//*******************************************************************************************************************************************
void fetchDashboardDataChar(WIFI_CLIENT_CLASS * client, JsonStreamingParser * parser) {
//*******************************************************************************************************************************************
char c;
client->setNoDelay(false);
#ifdef JSON_DEBUG
int sizePreview = 0; // number of chars of JSON preview, as we don't usually need to see the whole file
#endif // JSON_DEBUG
//while (client->connected()) {
if ((size = client->available()) > 0) {
c = client->read();
//if (!isBody) {
// msg += c;
//}
#ifdef JSON_DEBUG
sizePreview--;
if (sizePreview >= 0) {
Serial.print(c); // we read JSON data one char at a time.... but only display the first [sizePreview] bytes
}
#endif // JSON_DEBUG
if (c == '{' || c == '[') {
isBody = true;
}
if (isBody) {
parser->parse(c);
yield();
}
}
else {
if (fetchingData) {
Serial.println("End of Fetch Data! Cleaning up....");
#ifdef ARDUINO_ARCH_ESP8266
client->stopAll(); // flush client (only ESP8266 seems to have implemented stopAll)
#endif
#ifdef ARDUINO_ARCH_ESP32
client->stop(); // flush client (the ESP32 does not seem to have implemented stopAll)
#endif
parser->setListener(NULL); // cleanup the parser
fetchingData = false;
isBody = false;
msg = "";
size = 0;
}
}
}
bool readyDashboardDataRefresh() {
#ifdef TIMER_DEBUG
if ((millis() - timerRefresh) > 1000) {
// Serial.print("Ready in: ");
Serial.print((int)((DATA_REFRESH_INTERVAL - (millis() - timeoutDashboardDataRefresh)) / 1000));
Serial.print(".");
timerRefresh = millis();
}
#endif
return ((millis() - timeoutDashboardDataRefresh) > DATA_REFRESH_INTERVAL);
}
void testParser() {
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.print("&parser =");
Serial.println((long)&parser);
JsonStreamingParser newParser;
Serial.print("&newParser =");
Serial.println((long)&newParser);
parser = newParser;
Serial.println("Assigned newParser");
Serial.print("&parser =");
Serial.println((long)&parser);
parser.setListener(&listener); // init our JSON listener
HEAP_DEBUG_PRINT("Heap=");
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
}
struct tcp_pcb;
extern struct tcp_pcb* tcp_tw_pcbs;
extern "C" void tcp_abort(struct tcp_pcb* pcb);
void tcpCleanup()
{
while (tcp_tw_pcbs != NULL)
{
tcp_abort(tcp_tw_pcbs);
}
}
void GetDashboardData() {
SET_HEAP_MESSAGE("GetDashboardData ");
if (!fetchingData && !fetchWaiting && !readyDashboardDataRefresh()) {
// HEAP_DEBUG_PRINT("Prestop "); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
#ifdef ARDUINO_ARCH_ESP8266
client.stopAll(); // flush client (only ESP8266 seems to have implemented stopAll)
#endif
#ifdef ARDUINO_ARCH_ESP32
client.stop(); // flush client (the ESP32 does not seem to have implemented stopAll)
#endif
yield();
//
// HEAP_DEBUG_PRINT("Stop "); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
// solution #1 assign new client object
tcpCleanup();
// client == NULL;
//WIFI_CLIENT_CLASS newClient;
//client = newClient;
//client.setInsecure(); // TODO fix this. Needed for BearSSL
// HEAP_DEBUG_PRINT("Reassign "); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
// solution #2 call tcpCleanup
// tcpCleanup();
}
if (!fetchingData && !fetchWaiting && readyDashboardDataRefresh()){
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.print("&parser =");
Serial.println((long)&parser);
JsonStreamingParser newParser;
Serial.print("&newParser =");
Serial.println((long)&newParser);
parser = newParser;
Serial.println("Assigned newParser");
Serial.print("&parser =");
Serial.println((long)&parser);
parser.setListener(&listener); // init our JSON listener
HEAP_DEBUG_PRINT("Heap=");
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
//String httpPayload2 = String("GET ") + DASHBOARD_PATH + DasboardDataFile + " HTTP/1.1\r\n" +
// "Host: " + DASHBOARD_HOST + "\r\n" +
// String(WIFI_USER_AGENT) + "\r\n" + // see myPrivbateSettings, typical value: "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; ASTE; rv:11.0) like Gecko\r\n";
// "Connection: Keep-Alive\r\n\r\n";
//Serial.println("JSON fetch httpPayload2:");
//Serial.println(DEBUG_SEPARATOR);
//Serial.println(httpPayload2);
//Serial.println(DEBUG_SEPARATOR);
String httpPayload = htmlBasicHeaderText("GET", DASHBOARD_HOST, DASHBOARD_PATH + DasboardDataFile);
#ifdef JSON_DEBUG
Serial.println();
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.println("JSON fetch httpPayload:");
Serial.println(DEBUG_SEPARATOR);
Serial.println(httpPayload);
Serial.println(DEBUG_SEPARATOR);
#endif
Serial.println("calling client.connect...");
if (client == NULL) {
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.println("NOT creating new client...");
//WIFI_CLIENT_CLASS newClient;
//client = newClient;
#ifdef ARDUINO_ARCH_ESP8266
client.setInsecure(); // TODO fix this. Needed for BearSSL
#endif
#ifdef ARDUINO_ARCH_ESP32
//client.setInsecure(); // not implemented in ESP32
#endif
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
}
delay(1000);
Serial.println("client.connect");
if (!client.connect(DASHBOARD_HOST, httpPort)) {
Serial.println("connection failed; need to implement wait clear"); // TODO
Serial.println("");
fetchWaiting = true; // TODO does this even do anything once set?
return;
}
#ifdef ARDUINO_ARCH_ESP8266
if (client.verify(DASHBOARD_HOST_THUMBPRINT, DASHBOARD_HOST)) {
Serial.println("GetDashboardData TLS certificate matches");
}
else {
Serial.println("GetDashboardData TLS certificate doesn't match");
}
#endif
#ifdef ARDUINO_ARCH_ESP32
// not implemented in ESP32
#endif
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
client.print(httpPayload);
Serial.println("request sent"); // delay needed here?
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.println("Reading Headers...");
Serial.println(DEBUG_SEPARATOR);
String line = "";
unsigned long timeout = millis();
timeout = millis();
while (client.connected()) {
//HEAP_DEBUG_PRINT("1"); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
while (client.available() == 0) {
yield();
// HEAP_DEBUG_PRINT("2"); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
if (millis() - timeout > 10000) {
Serial.println(">>> GetDashboardData Client Timeout !");
client.stop();
HEAP_DEBUG_PRINT("3"); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.println("RE-Connecting to WiFi...");
wifiConnect(50);
HEAP_DEBUG_PRINT("4"); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
return;
}
}
//HEAP_DEBUG_PRINT("5"); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
if (client.available()) {
yield();
//HEAP_DEBUG_PRINT("6"); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
timeout = millis();
//HEAP_DEBUG_PRINT("7"); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
line = client.readStringUntil('\n');
//HEAP_DEBUG_PRINT("8"); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.println(line);
if (line == "\r") {
Serial.println("headers received!");
Serial.print(sizeof(line));
Serial.println(" bytes");
Serial.println(DEBUG_SEPARATOR);
break;
}
}
//else {
// Serial.print("No data available; status = ");
// Serial.println(client.status());
// Serial.println(DEBUG_SEPARATOR);
// Serial.println("waiting 60 seconds...");
// delay(60000);
// return;
//}
yield();
fetchingData = true;
timeoutDashboardDataRefresh = millis();
}
// int retryCounter = 0;
// see http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/client-examples.html
Serial.print("\r\n\r\nProceeding! client.status = ");
#ifdef ARDUINO_ARCH_ESP8266
Serial.print(client.status()); // (only ESP8266 seems to have implemented (client.status)
#endif
#ifdef ARDUINO_ARCH_ESP32
Serial.print("client.status() not available for ESP32"); // flush client (the ESP32 does not seem to have implemented client.status)
#endif
}
else {
fetchDashboardDataChar(&client, &parser);
}
}
//*******************************************************************************************************************************************
// return baseURL like http://myDashboardHost.com/theDashboardApplicationPath/
//*******************************************************************************************************************************************
String baseURL() {
return String(httpText) + String(DASHBOARD_HOST) + String(DASHBOARD_APP);
}
//*******************************************************************************************************************************************
// show images (setRotation = 2)
//*******************************************************************************************************************************************
void showDasbboardImages() {
// Server_Payroll_Hours.png
// Server_Payroll_Hours.bmp
tftScreenClear();
tft.setFont(); // reset to default small font when drawing images so that any long error message is readable.
tft.setRotation(2);
tft.setCursor(1, 1);
String thisImage = baseURL() + "imageConvert2BMP.aspx?targetImageName=server_payroll_hours.bmp&newImageSizeX=320";
String oldLink = "http://gojimmypi-test-imageconvert2bmp.azurewebsites.net/default.aspx?targetHttpImage=http://healthagency.slocounty.ca.gov/azm/images/server_payroll_hours.bmp&newImageSizeX=320";
Serial.print("Drawing: ");
Serial.print(thisImage);
bmpDrawFromUrlStream(&tft, thisImage, 0, 0);
imageViewDelay();
}
//*******************************************************************************************************************************************
// startup image
//*******************************************************************************************************************************************
void showStartupImage() {
// startup image
Serial.print("Build string...");
tft.setRotation(2);
String imageURL = "http://" + String(DASHBOARD_HOST) + String(DASHBOARD_APP) + "imageConvert2BMP.aspx?targetImageName=logo.png&newImageSizeX=320";
Serial.println(imageURL);
bmpDrawFromUrlStream(&tft, imageURL, 0, 0);
Serial.print("Done with logo");
}
//*******************************************************************************************************************************************
// wifiConnect
//
// WiFi.begin with repeated attempts with TFT screen and optional serial progress indication
//
//*******************************************************************************************************************************************
//int wifiConnect(int maxAttempts = 50) {
// int countAttempt = 0;
// WiFi.mode(WIFI_STA);
// WiFi.begin(WIFI_SSID, WIFI_PWD);
//
// myMacAddress = WiFi.macAddress(); // this returns 6 hex bytes, delimited by colons
// screenMessage("MAC Address", myMacAddress.substring(0, 9), myMacAddress.substring(9, 18)); // 01:34:67:90:12:45
//
//#ifdef WIFI_DEBUG
// Serial.print("Connecting to ");
// Serial.print(WIFI_SSID);
//#endif
// while (WiFi.status() != WL_CONNECTED) { // try to connect wifi for 6 sec then reset
//
// // this tft code is not actualy DOING anything yet
// tft.setTextColor(ILI9341_BLUE);
// tft.setCursor(15, 195);
// delay(250);
// tft.setTextColor(ILI9341_RED);
// tft.setCursor(15, 195);
//
//#ifdef WIFI_DEBUG
// Serial.print(".");
//#endif
// delay(250);
// countAttempt++;
// if (countAttempt > maxAttempts) {
// countAttempt = 0;
//#ifdef WIFI_DEBUG
// Serial.println("WiFi Disconnect... ");
//#endif
// WiFi.disconnect();
// delay(5000);
//#ifdef WIFI_DEBUG
// Serial.println("WiFi Retrying. ");
//#endif
// WiFi.mode(WIFI_STA);
// WiFi.begin(WIFI_SSID, WIFI_PWD);
// }
// }
// delay(5000);
// myMacAddress.replace(":", "");
// myMacAddress.replace("-", ""); // probably not used, but just in case they MAC address starts returning other well known delimiters such as dash
// myMacAddress.replace(" ", ""); // or perhaps even a space
//
// Serial.println("MAC Address=" + myMacAddress);
//}
//*******************************************************************************************************************************************
//
// TODO - based on MAC address, determine data file name
//*******************************************************************************************************************************************
void GetDashboardDataFile() {
// the files are expected to be static JSON, pre-generated by process at server (we don't want to wait on generation of files for display!)
DasboardDataFile = String(DASHBOARD_KEY) + wifiMacAddress() + ".json";
delay(2000);
HEAP_DEBUG_PRINT("Heap (before htmlExists) "); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
HTTP_DEBUG_PRINT("Checking for DasboardDataFile = "); HTTP_DEBUG_PRINTLN(DasboardDataFile);
if (!htmlExists(DasboardDataFile)) {
Serial.println(DasboardDataFile + " not found, using default");
DasboardDataFile = "2134.json";
}
HEAP_DEBUG_PRINT("Heap (after htmlExists) "); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
screenMessage("Using file", "ID: " + DasboardDataFile);
// TODO - if the file does not exist, then use default.
}
#ifdef _MSC_VER
#pragma region setup
#endif
//*******************************************************************************************************************************************
int setupSPIFFS() {
//*******************************************************************************************************************************************
int okSPIFFS = 0;
HEAP_DEBUG_PRINT("Heap (before SPIFFS.begin) "); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
SPIFFS.begin();
// SPIFFS.format();
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
File f = SPIFFS.open("/myFile.txt", "w");
if (!f) {
SPIFFS_DEBUG_PRINTLN("SPIFSS File open failed; Formatting...");
SPIFFS.format();
}
else {
SPIFFS_DEBUG_PRINTLN("file open SUCCESS.");
}
f.close();
f = SPIFFS.open("/myFile.txt", "w");
if (!f) {
SPIFFS_DEBUG_PRINTLN("SPIFSS File open failed.");
okSPIFFS = 1;
}
else {
SPIFFS_DEBUG_PRINTLN("SPIFSS File open SUCCESS. Writing...");
f.println("Hello World!");
}
f.close();
#ifdef ARDUINO_ARCH_ESP8266
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
SPIFFS_DEBUG_PRINT(dir.fileName());
File f = dir.openFile("r");
SPIFFS_DEBUG_PRINTLN(f.size());
}
#endif
#ifdef ARDUINO_ARCH_ESP32
// see https://github.com/espressif/arduino-esp32/blob/master/libraries/SPIFFS/examples/SPIFFS_Test/SPIFFS_Test.ino
File root = SPIFFS.open("/");
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
SPIFFS_DEBUG_PRINT(" DIR : ");
SPIFFS_DEBUG_PRINTLN(file.name());
//if (levels) {
// listDir(fs, file.name(), levels - 1);
//}
}
else {
SPIFFS_DEBUG_PRINT(" FILE: ");
SPIFFS_DEBUG_PRINT(file.name());
SPIFFS_DEBUG_PRINT("\tSIZE: ");
SPIFFS_DEBUG_PRINTLN(file.size());
}
file = root.openNextFile();
}
#endif
String fmsg;
f = SPIFFS.open("/myFile.txt", "r");
if (!f) {
SPIFFS_DEBUG_PRINTLN("file open failed 2");
}
else {
SPIFFS_DEBUG_PRINTLN("file open SUCCESS 2");
okSPIFFS = 2;
fmsg = f.readString();
}
SPIFFS_DEBUG_PRINT("SPIFFS test read msg=");
SPIFFS_DEBUG_PRINTLN(fmsg);
f.close();
SPIFFS_DEBUG_PRINTLN("File test done!");
SPIFFS.end();
HEAP_DEBUG_PRINT("Heap (after SPIFFS.end)="); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
return okSPIFFS;
}
//*******************************************************************************************************************************************
// setupDisplay
//*******************************************************************************************************************************************
int setupDisplay() {
Serial.println("ILI9341 Test!");
tft.begin();
tft.setFont(&FreeSansBold24pt7b); // load our custom 24pt font
tftScreenDiagnostics();
delay(20);
uint8_t tx = tft.readcommand8(ILI9341_RDMODE);
tx = tft.readcommand8(ILI9341_RDSELFDIAG);
delay(20);
Serial.print("Self Diagnostic: 0x"); Serial.println(tx, HEX);
delay(20);
tft.setCursor(1, 1);
tftScreenDiagnostics();
screenMessage("Startup...");
return 0;
}
//*******************************************************************************************************************************************
// setupWiFi
//*******************************************************************************************************************************************
int setupWiFi() {
SET_HEAP_MESSAGE("setupWiFi Heap = ");
HEAP_DEBUG_PRINTLN("Heap (setupWiFi begin)");
#ifdef ARDUINO_ARCH_ESP8266
client.setInsecure(); // TODO fix this. Needed for BearSSL
#endif
#ifdef ARDUINO_ARCH_ESP32
//client.setInsecure(); // not implemented in ESP32
#endif
HEAP_DEBUG_PRINTLN("htmlSetClient(&client)...");
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
htmlSetClient(&client);
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
WIFI_DEBUG_PRINTLN("calling wifiConnect...");
wifiConnect(50);
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
screenMessage("Connected to", WIFI_SSID);
WIFI_DEBUG_PRINTLN("WiFi connected. My IP address:");
WIFI_DEBUG_PRINTLN("");
WIFI_DEBUG_PRINTLN(WiFi.localIP());
if (confirmedInternetConnectivity(DASHBOARD_HOST) == 0) {
Serial.println("Successfully connected!");
}
// testSSL();
HEAP_DEBUG_PRINT("Heap (setupWiFi end); "); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
return 0;
}
#ifdef _MSC_VER
#pragma endregion
#endif
#ifdef _MSC_VER
#pragma region OldStuff
#endif
//char json[] = "{\"a\":3, \"b\":{\"c\":\"d\"}}";
//for (int i = 0; i < sizeof(json); i++) {
// parser.parse(json[i]);
//}
//String urlLogo = String("GET http://") +
// String(DASHBOARD_HOST) +
// String(DASHBOARD_APP) +
// "/imageConvert2BMP.aspx?targetImageName=logo.png&newImageSizeX=320";
//delay(20);
//tft.setRotation(2);
//tft.drawRect(0, 0, 240, 320, 0x00FF);
//delay(20);
//String htmlString = String("GET http://") + String(DASHBOARD_HOST) + "/" + " HTTP/1.1\r\n" +
// "Host: " + String(DASHBOARD_HOST) + "\r\n" +
// "Content-Encoding: identity" + "\r\n" +
// "Connection: Keep-Alive\r\n\r\n";
//htmlSend(DASHBOARD_HOST, 80, htmlString); // test to confirm internet operational
//showStartupImage();
//
//bmpDraw(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/image/24bit.bmp");
//delay(2000);
// Hello World!
#ifdef _MSC_VER
#pragma endregion
#endif
//*******************************************************************************************************************************************
// setup()
//*******************************************************************************************************************************************
void setup() {
delay(500);
Serial.begin(19200);
SET_HEAP_MESSAGE( "mySetup; heap = ");
HEAP_DEBUG_PRINTLN("info for (begin)");
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
Serial.println(DEBUG_SEPARATOR);
HEAP_DEBUG_PRINTLN("");
HEAP_DEBUG_PRINTLN("Heap (Setup begin)=");
HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
setupDisplay();
checkFlash();
setupSPIFFS();
setupWiFi();
timeoutDashboardDataRefresh = millis();
GetDashboardDataFile();
// UpdateDashboard(); // we do this in the loop only
Serial.println(F("Setup Done!"));
HEAP_DEBUG_PRINT("Heap (setup complete)="); HEAP_DEBUG_PRINTLN(DEFAULT_DEBUG_MESSAGE);
// #pragma message(Reminder "Fix this problem!")
Serial.println(("a" == "b") ? (getHeapMsg() + (String)ESP.getFreeHeap()) : "msg") ;
}
//*******************************************************************************************************************************************
//
//*******************************************************************************************************************************************
void imageTest() {
tft.setRotation(2);
tft.setCursor(0, 0);
bmpDrawFromUrlStream(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=IMG_20161109_133054198.jpg&newImageSizeY=240&newImageSizeX=320", 50, 50);
delay(2000);
tftScreenClear();
bmpDrawFromUrlStream(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=buspirate.png&newImageSizeX=320");
delay(2000);
tftScreenClear();
bmpDrawFromUrlStream(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=nasa1.jpg&newImageSizeX=320");
delay(2000);
tftScreenClear();
bmpDrawFromUrlStream(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=nasa2.jpg&newImageSizeX=320");
delay(2000);
tftScreenClear();
bmpDrawFromUrlStream(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=nasa3.png&newImageSizeX=320");
delay(2000);
tftScreenClear();
bmpDrawFromUrlStream(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=IMG_20161109_133054198.jpg&newImageSizeY=240&newImageSizeX=320");
delay(2000);
tftScreenClear();
bmpDrawFromUrlStream(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=image.png&newImageSizeY=55");
delay(2000);
tftScreenClear();
bmpDraw(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=image.png&newImageSizeY=60");
delay(2000);
tftScreenClear();
bmpDraw(&tft, "http://gojimmypi-dev-imageconvert2bmp.azurewebsites.net/default.aspx?targetImageName=image.png&newImageSizeY=70");
delay(2000);
tftScreenClear();