-
Notifications
You must be signed in to change notification settings - Fork 1
/
esp32-e-ink.ino
1649 lines (1497 loc) · 57.6 KB
/
esp32-e-ink.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 <Arduino.h>
/*
ESPink-4.2
----------------------
https://remoteqth.com
___ _ ___ _____ _ _
| _ \___ _ __ ___| |_ ___ / _ \_ _| || | __ ___ _ __
| / -_) ' \/ _ \ _/ -_) (_) || | | __ |_/ _/ _ \ ' \
|_|_\___|_|_|_\___/\__\___|\__\_\|_| |_||_(_)__\___/_|_|_|
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Based on
Display test for LaskaKit ESPink-4.2"
-------- ESPink pinout -------
MOSI/SDI 23
CLK/SCK 18
SS 5 //CS
DC 17
RST 16
BUSY 4
-------------------------------
Email:[email protected]
Web:laskakit.cz
HARDWARE ESP32 Dev Module
IDE 1.8.19
Použití knihovny FS ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/FS
Použití knihovny SD ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/SD
Použití knihovny SPI ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/SPI
Použití knihovny GxEPD2 ve verzi 1.5.2 v adresáři: /home/dan/Arduino/libraries/GxEPD2
Použití knihovny Adafruit_GFX_Library ve verzi 1.11.3 v adresáři: /home/dan/Arduino/libraries/Adafruit_GFX_Library
Použití knihovny Adafruit_BusIO ve verzi 1.14.1 v adresáři: /home/dan/Arduino/libraries/Adafruit_BusIO
Použití knihovny Wire ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/Wire
Použití knihovny WiFi ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/WiFi
Použití knihovny AsyncTCP ve verzi 1.1.1 v adresáři: /home/dan/Arduino/libraries/AsyncTCP
Použití knihovny ESPAsyncWebServer ve verzi 1.2.3 v adresáři: /home/dan/Arduino/libraries/ESPAsyncWebServer
Použití knihovny AsyncElegantOTA ve verzi 2.2.7 v adresáři: /home/dan/Arduino/libraries/AsyncElegantOTA
Použití knihovny Update ve verzi 2.0.0 v adresáři: /home/dan/Arduino/hardware/espressif/esp32/libraries/Update
Použití knihovny PubSubClient ve verzi 2.8 v adresáři: /home/dan/Arduino/libraries/PubSubClient
mosquitto_pub -h 54.38.157.134 -t OK1HRA/0/ROT/Azimuth -m '83'
mosquitto_sub -v -h 54.38.157.134 -t 'OK1HRA/0/ROT/#'
*/
//-------------------------------------------------------------------------------------------------------
#define REV 20240328
// #define USunits // enable American metrological units
#define OTAWEB // enable upload firmware via web
#define MQTT // enable MQTT
#define DISABLE_SD // disable SD card - configure maunaly in setup(void) part of code
// #define APRSFI // enable get from aprs.fi - not work
#include <esp_adc_cal.h>
#include <FS.h>
#include <SD.h>
#include <SPI.h>
#include <GxEPD2_BW.h>
// #define BMPMAP
// Select Display hardware type (see on PCB)
// Display GDEW042T2
GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=5*/ SS, /*DC=*/17, /*RST=*/16, /*BUSY=*/4)); // GDEW042T2 400x300, UC8176 (IL0398)
// Display GDEY042T81
//GxEPD2_BW<GxEPD2_420_GDEY042T81, GxEPD2_420_GDEY042T81::HEIGHT> display(GxEPD2_420_GDEY042T81(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); //GDEY042T81, 400x300, SSD1683 (no inking)
//GxEPD2_3C<GxEPD2_420c_Z21, GxEPD2_420c_Z21::HEIGHT> display(GxEPD2_420c_Z21(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEQ042Z21 400x300, UC8276
#if defined(BMPMAP)
#include "ok.h"
#endif
#if defined(APRSFI)
// #include <HTTPClient.h>
#include <WiFiClientSecure.h>
const char* server = "api.aprs.fi"; // Server URL
WiFiClientSecure client;
// #include <ArduinoJson.h>
String jsonString = "";
#include <jsonlib.h> // https://github.com/wyolum/jsonlib/tree/master/
// ISRG Root X1 root .pem certificate for aprs.fi valid to Mon, 04 Jun 2035 11:04:38 GMT
const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \
"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\n" \
"-----END CERTIFICATE-----\n";
#endif
// source https://oleddisplay.squix.ch/ - must copy via clipboard!
// #include "Open_Sans_Condensed_Light_80.h"
// #include "Open_Sans_Condensed_Bold_20.h"
// #include "Open_Sans_Condensed_Light_16.h"
// https://rop.nl/truetype2gfx/
#include "Logisoso8pt7b.h"
#include "Logisoso10pt7b.h"
#include "Logisoso50pt7b.h"
// display.setFont(&Logisoso250pt7b);
uint16_t colorB = GxEPD_BLACK;
uint16_t colorW = GxEPD_WHITE;
// #define SLEEP // Uncomment so board goes to sleep after printing on display
#define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds
#define TIME_TO_SLEEP 10 // Time ESP32 will go to sleep (in seconds)
const String mainHWdevice[4][2] = {
{"/ROT/", "IP rotator"}, // 0
{"/WX/", "WX station"}, // 1
{"", "aprs.fi"}, // 2
{"topic", "name"}, // 3
};
int mainHWdeviceSelect = -1; //0 = IP rotator, 1 = WX station, 2 = aprs.fi source
String TOPIC = ""; // same as 'location' on IP rotator
String ROT_TOPIC = ""; // mainHWdevice[mainHWdeviceSelect][0]
String WX_TOPIC = ""; // mainHWdevice[mainHWdeviceSelect][0]
byte mqttBroker[4]={0,0,0,0}; // MQTT broker IP address
int MQTT_PORT = 0; // MQTT broker port
IPAddress mqtt_server_ip(mqttBroker[0], mqttBroker[1], mqttBroker[2], mqttBroker[3]); // MQTT broker IP address
String SSID = "";
String PSWD = "";
String APRS_FI_NAME = "";
String APRS_FI_APIKEY = "";
unsigned int eInkRotation = 1; // 1 USB TOP, 3 USB DOWN | 0 default, 1 90°CW, 2 180°CW, 3 90°CCW
int OfflineTimeout = 5; // minutes
bool eInkNegativ = false;
bool eInkNegativTmp = false;
int DesignSkin = 0; // not implemented!
// #define SDTEST_TEXT_PADDING 25
#define SD_CS 27
SPIClass spiSD(HSPI); // Use HSPI for SD card
File myFile;
String ConfigFile="/setup.cfg";
char charConfigFile[11]; // length +1
int microSDlines = 0;
// ROT
int Azimuth = -42;
int AzimuthTmp = -42;
int AzimuthStart = 0;
String Name = "";
int Status = 4;
bool eInkNeedRefresh = false;
bool eInkOfflineDetect = false;
long RxMqttTimer=0;
//WX
float Temperature = 7.3;
float RainToday = 0.0;
float HumidityRel = 0;
float DewPoint = 0;
float Pressure = 0;
int WindDir = 0;
float WindSpeedAvg = 0;
float WindSpeedMaxPeriod = 0;
const int SdCardPresentPin = 33;
bool SdCardPresentStatus = false;
int Az=0;
char buf[21];
#define RAD_TO_DEG 57.295779513082320876798154814105
// ntp
#include "time.h"
const char* ntpServer = "pool.ntp.org";
// const char* ntpServer = "tik.cesnet.cz";
// const char* ntpServer = "time.google.com";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 0;
// 1000 seconds WDT (WatchDogTimer)
#include <esp_task_wdt.h>
#define WDT_TIMEOUT 1000
long WdtTimer=0;
int DebuggingOutput = 1; // 0-off | 1-Serial
#define WIFI
#include <WiFi.h>
// #include <ETH.h>
// int SsidPassSize = (sizeof(SsidPass)/sizeof(char *))/2; //array size
// int SelectSsidPass = -1;
#define wifi_max_try 10 // Number of try
unsigned long WifiTimer = 0;
unsigned long WifiReconnect = 30000;
unsigned int RunApp = 255;
#if defined(OTAWEB)
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
AsyncWebServer OTAserver(80);
#endif
#if defined(MQTT)
#include <PubSubClient.h>
// #include "PubSubClient.h" // lokalni verze s upravou #define MQTT_MAX_PACKET_SIZE 128
WiFiClient espClient;
PubSubClient mqttClient(espClient);
// PubSubClient mqttClient(server, 1883, callback, ethClient);
long lastMqttReconnectAttempt = 0;
boolean MQTT_LOGIN = 0; // enable MQTT broker login
// char MQTT_USER= 'login'; // MQTT broker user login
// char MQTT_PASS= 'passwd'; // MQTT broker password
const int MqttBuferSize = 1000; // 1000
char mqttTX[MqttBuferSize];
char mqttPath[MqttBuferSize];
long MqttStatusTimer[2]{1500,1000};
#endif
//-------------------------------------------------------------------------------------------------------
void setup(void){
#if defined(DISABLE_SD)
//----------- manual config -----------
mainHWdeviceSelect=1; // 0 = IP rotator, 1 = WX station, 2 = aprs.fi (get every 15 minutes) - not works!
SSID="SSID"; // (all) Wifi SSID (max 20 characters)
PSWD="PASSWORD"; // (all) Wifi password (max 20 characters)
eInkRotation=1; // (all) 1 = USB on top, 3 = USB downside (0 default, 1 90°CW, 2 180°CW, 3 90°CCW)
ROT_TOPIC="OK1HRA/0"; // (0) part of MQTT topic CALLSIGN/NR. Must be same as on IP rotator.
WX_TOPIC="OK1HRA-7"; // (1) part of MQTT topic CALLSIGN. Must be same as on WX station.
mqttBroker[0]=54; // (0/1) MQTT broker IP for IP rotator and WX station
mqttBroker[1]=38; // (0/1) Must be same as on main device.
mqttBroker[2]=157; // (0/1)
mqttBroker[3]=134; // (0/1) default 54.38.157.134 (remoteqth.com)
MQTT_PORT=1883; // (0/1) MQTT broker port. Must be same as on main device.
OfflineTimeout=6; // (0/1) minutes
eInkNegativ=1; // (all) 1 = Dark mode (default), 0 = Light mode
DesignSkin=0; // not implemented!
APRS_FI_NAME="OK1HRA-7";// (2) your CALLSIGN on aprs.fi - not works!
APRS_FI_APIKEY="key"; // (2) API key get from https://aprs.fi/account/ - not works!
//----------- manual config end -----------
mqtt_server_ip = mqttBroker;
display.setRotation(eInkRotation); // 1 USB TOP, 3 USB DOWN | 0 default, 1 90°CW, 2 180°CW, 3 90°CCW
if(eInkNegativ==true){
colorB = GxEPD_BLACK;
colorW = GxEPD_WHITE;
eInkNegativTmp=true;
}else{
colorB = GxEPD_WHITE;
colorW = GxEPD_BLACK;
eInkNegativTmp=false;
}
ROT_TOPIC = String(ROT_TOPIC)+String(mainHWdevice[0][0]);
WX_TOPIC = String(WX_TOPIC)+String(mainHWdevice[1][0]);
if(mainHWdeviceSelect==0){
TOPIC = ROT_TOPIC;
}else if(mainHWdeviceSelect==1){
TOPIC = WX_TOPIC;
}
#endif
pinMode(SdCardPresentPin, INPUT);
pinMode(2, OUTPUT); // Set epaper transistor as output
digitalWrite(2, HIGH); // Surn on epaper transistor
delay(100); // Delay so it has time to turn on
display.init();
display.setRotation(eInkRotation); // 1 USB TOP, 3 USB DOWN | 0 default, 1 90°CW, 2 180°CW, 3 90°CCW
Serial.begin(115200);
Serial.println();
Serial.print("e-ink rev ");
Serial.println(REV);
#if !defined(DISABLE_SD)
SdCardPresentStatus=!digitalRead(SdCardPresentPin);
Serial.println("Check microSD ");
if(SdCardPresentStatus != true) {
Serial.print("micro SD card is not inserted");
display.fillScreen(colorW);
display.setTextColor(colorB);
display.setFont(&Logisoso10pt7b);
display.setCursor(30, 120);
display.println("!");
display.setCursor(30, 160);
display.println("micro SD card is not inserted");
display.setCursor(30, 190);
display.println("Please insert card with");
display.setCursor(30, 220);
display.println("setup.cfg config file");
display.display(false);
while(SdCardPresentStatus != true) {
delay(1000);
SdCardPresentStatus=!digitalRead(SdCardPresentPin);
}
}
SDtest();
display.fillScreen(colorB);
display.setTextColor(colorW);
display.setFont(&Logisoso10pt7b);
display.setCursor(70, 150);
display.println("Connecting");
display.setFont(&Logisoso8pt7b);
display.setCursor(90, 190);
display.println("MicroSD import "+String(microSDlines)+" values");
display.fillCircle(80, 190-7, 3, colorW);
display.setCursor(90, 220);
display.println("WiFi "+String(SSID)+"...");
display.fillCircle(80, 220-7, 3, colorW);
display.setFont(&Logisoso8pt7b);
display.setCursor(200, 385);
display.print(REV);
display.display(false);
#endif
// display.fillScreen(colorB);
#if defined(WIFI)
// WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID.c_str(), PSWD.c_str());
Serial.print("Connecting ssid "+String(SSID)+" ");
int count_try = 0;
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
count_try++; // Increase try counter
if ( count_try >= wifi_max_try ) {
Serial.println("\n");
Serial.println("Impossible to establish WiFi connexion");
print_wifi_error();
}
}
Serial.println("");
Serial.print("WIFI connected with IP ");
Serial.println(WiFi.localIP());
Serial.print("WIFI dBm: ");
Serial.println(WiFi.RSSI());
if(mainHWdeviceSelect==2){
display.fillScreen(colorB);
display.setTextColor(colorW);
display.setFont(&Logisoso10pt7b);
display.setCursor(70, 150);
display.println("Connecting");
display.setFont(&Logisoso8pt7b);
display.setCursor(90, 190);
display.println("MicroSD import "+String(microSDlines)+" values");
display.fillCircle(80, 190-7, 3, colorW);
display.setCursor(90, 220);
display.println("WiFi "+String(SSID)+" "+String(WiFi.RSSI())+" dBm");
display.fillCircle(80, 220-7, 3, colorW);
display.setCursor(90, 250);
display.println(WiFi.localIP());
display.fillCircle(80, 250-7, 3, colorW);
display.setCursor(90, 280);
display.fillCircle(80, 280-7, 3, colorW);
display.println(String(mainHWdevice[mainHWdeviceSelect][1])+"/"+String(APRS_FI_NAME)+"...");
display.setFont(&Logisoso8pt7b);
display.setCursor(15, 385);
UtcTime(1).toCharArray(buf, 21);
display.println("UTC "+String(buf));
display.setCursor(200, 385);
display.print(REV);
display.display(false);
}
#if defined(MQTT)
if(mainHWdeviceSelect==0 || mainHWdeviceSelect==1){
if (MQTT_LOGIN == true){
// if (mqttClient.connect("esp32gwClient", MQTT_USER, MQTT_PASS)){
// AfterMQTTconnect();
// }
}else{
if(mainHWdeviceSelect==0 || mainHWdeviceSelect==1 ){
mqttClient.setServer(mqtt_server_ip, MQTT_PORT);
Serial.println("EthEvent-MQTTclient");
mqttClient.setCallback(MqttRx);
Serial.println("EthEvent-MQTTcallback");
lastMqttReconnectAttempt = 0;
char charbuf[50];
WiFi.macAddress().toCharArray(charbuf, 18);
if (mqttClient.connect(charbuf)){
Serial.print("EthEvent-maccharbuf ");
Serial.println(charbuf);
mqttReconnect();
}
}
}
}
#endif
#endif
#if defined(OTAWEB)
OTAserver.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "e-ink rev "+String(REV)+" | PSE QSY to /update");
});
AsyncElegantOTA.begin(&OTAserver); // Start ElegantOTA
OTAserver.begin();
Serial.println("OTAserver start");
#endif
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
#if defined(APRSFI)
jsonString.reserve(900);
#endif
// WDT
esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
esp_task_wdt_add(NULL); //add current thread to WDT watch
WdtTimer=millis();
}
//-------------------------------------------------------------------------------------------------------
void loop(void) {
Watchdog();
Mqtt();
eInkRefresh();
#if defined(OTAWEB)
AsyncElegantOTA.loop();
#endif
}
//-------------------------------------------------------------------------------------------------------
void GetHttps(){
#if defined(APRSFI)
/*
WiFiClientSecure *client = new WiFiClientSecure;
Serial.println("[https] start");
if(client) {
client -> setCACert(rootCACertificate);
{
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;
Serial.print("[https] begin...\n");
if (https.begin(*client, "https://api.aprs.fi/api/get?name="+String(APRS_FI_NAME)+"&what=wx&apikey="+String(APRS_FI_APIKEY)+"&format=json")) {
Serial.print("[https] GET...\n");
// start connection and send HTTP header
int httpCode = https.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[https] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
jsonString = https.getString();
Serial.print("[https] ");
Serial.println(jsonString);
RxMqttTimer=millis();
}
} else {
Serial.printf("[https] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
Serial.printf("[https] Unable to connect\n");
}
// End extra scoping block
}
delete client;
} else {
Serial.println("[https] Unable to create client");
}
*/
#endif
}
//-------------------------------------------------------------------------------------------------------
void eInkRefresh(){
static long eInkRefreshTimer = -5000;
// ROT
if( mainHWdeviceSelect==0 && eInkNeedRefresh==true && millis()-eInkRefreshTimer > 5000 && Azimuth!=-42 && Name != "" ){
display.fillScreen(colorB);
#if defined(BMPMAP)
display.drawBitmap(0, 0, ok, 300, 300, colorW);
#endif
if(Azimuth>=0){
DirectionalRosette(AzimuthShifted(Azimuth), 150, 145, 130);
}
display.setTextColor(colorW);
display.setFont(&Logisoso50pt7b);
/*
char to - (minus) width 39px
char width 33px
char to char width 44px
dot+char width 68px
*/
if(Azimuth>=0){
if(AzimuthShifted(Azimuth)>=100){
display.setCursor(175-44, 360);
}else if(AzimuthShifted(Azimuth)<100 && AzimuthShifted(Azimuth)>=10){
display.setCursor(175, 360);
}else if(AzimuthShifted(Azimuth)<10){
display.setCursor(175+44, 360);
}
display.println(AzimuthShifted(Azimuth));
// display.setCursor(270, 310);
// display.setFont(&Logisoso10pt7b);
// display.println("o");
display.fillCircle(275, 300, 6, colorW);
}else{
display.setCursor(170, 355);
display.println("n/a");
}
int ZZshift=2;
display.setFont(&Logisoso8pt7b);
display.setCursor(15, 285+4*ZZshift);
display.println(String(SSID)+" "+String(WiFi.RSSI())+" dBm");
display.setCursor(15, 310+3*ZZshift);
display.print(WiFi.localIP());
display.setFont(&Logisoso10pt7b);
display.setCursor(15, 335+2*ZZshift);
display.println(Name);
display.setFont(&Logisoso8pt7b);
display.setCursor(15, 360+ZZshift);
display.println(String(TOPIC)+"#");
display.setCursor(15, 385);
UtcTime(1).toCharArray(buf, 21);
display.println("UTC "+String(buf));
if(eInkOfflineDetect==true){
display.setCursor(185, 385);
display.setFont(&Logisoso10pt7b);
display.print("OFF >"+String(OfflineTimeout)+"min");
}else{
display.setCursor(200, 385);
display.print(REV);
}
display.display(false);
eInkNeedRefresh=false;
eInkRefreshTimer=millis();
// WX
}else if( (mainHWdeviceSelect==1 || mainHWdeviceSelect==2) && eInkNeedRefresh==true && millis()-eInkRefreshTimer > 10000 ){
// Serial.println("eInk eInkNegativ "+String(eInkNegativ));
// Serial.println("eInk colorB "+String(colorB));
// Serial.println("eInk colorW "+String(colorW));
display.fillScreen(colorB);
#if defined(USunits)
Temperature = (Temperature*1.8)+32;
DewPoint = (DewPoint*1.8)+32;
RainToday = RainToday/25.4;
WindSpeedMaxPeriod = WindSpeedMaxPeriod*3.281;
#endif
display.setTextColor(colorW);
display.setFont(&Logisoso50pt7b);
int Xshift=0;
if(Temperature<0){
Xshift=-39;
}else{
Xshift=0;
}
if(abs(Temperature)>=10){
display.setCursor(64+Xshift, 85);
}else if(abs(Temperature)<10){
display.setCursor(97+Xshift, 85);
}
String str = String(Temperature);
String subStr = str.substring(0, str.length() - 1);
display.println(String(subStr));
display.setCursor(242, 85);
#if defined(USunits)
display.println("F");
#else
display.println("C");
#endif
display.fillCircle(230, 25, 6, colorW);
display.drawLine(15, 100, 285, 100, 2);
float XX = (285.0-15.0)/100.0*HumidityRel+15.0;
display.fillCircle((int)XX, 100, 3, colorW);
display.setFont(&Logisoso8pt7b);
display.setCursor(15, 125);
display.print("Relative humidity ");
display.setFont(&Logisoso10pt7b);
display.print(String((int)HumidityRel)+"% ");
display.setFont(&Logisoso8pt7b);
display.print("Dew point ");
display.setFont(&Logisoso10pt7b);
display.print(String((int)DewPoint)+" ");
#if defined(USunits)
display.println("F");
#else
display.println("C");
#endif
display.setCursor(15, 150);
display.setFont(&Logisoso8pt7b);
display.print("Pressure ");
display.setFont(&Logisoso10pt7b);
display.print(String((int)Pressure)+" hpa");
Triangle(Pressure, 983.0, 1043.0); // 1013 +-30
display.drawLine(15, 200, 20, 200, 2);
if(RainToday>0){
str = String(RainToday);
subStr = str.substring(0, str.length() - 1);
display.print(" RAIN "+String(subStr)+" ");
#if defined(USunits)
display.println("in");
#else
display.println("mm");
#endif
int ten = (int)RainToday % 10;
if(RainToday>0 && RainToday<1){
display.fillCircle(285-1*(11+1), 170, 3+1, colorW);
}
for (int j=ten; j>0; j--) {
display.fillCircle(285-j*(11+j), 170, 3+j, colorW);
}
int tens = (int)(RainToday/10);
for (int j=tens; j>0; j--) {
display.fillCircle(j*30-5, 170, 13, colorW);
}
}
display.setFont(&Logisoso50pt7b);
if(abs(WindSpeedMaxPeriod)>=10){
display.setCursor(6+4, 265);
}else if(abs(WindSpeedMaxPeriod)<10){
display.setCursor(50+4, 265);
}
if(WindSpeedMaxPeriod>0){
display.println((int)WindSpeedMaxPeriod);
// display.setFont(&Logisoso10pt7b);
display.setFont(&Logisoso8pt7b);
display.setCursor(35, 290);
#if defined(USunits)
display.println("gust ft/s");
#else
display.println("gust m/s");
#endif
}
// int Pressure = 0;
// int WindSpeedAvg = 0;
DirectionalRosette(WindDir, 200, 270, 80);
int ZZshift=2;
// display.setFont(&Logisoso10pt7b);
// display.println(Name);
display.setFont(&Logisoso8pt7b);
display.setCursor(15, 285+4*ZZshift);
display.setCursor(15, 310+3*ZZshift);
display.println(String(SSID)+" "+String(WiFi.RSSI())+" dBm");
display.setCursor(15, 335+2*ZZshift);
display.print(WiFi.localIP());
display.setFont(&Logisoso8pt7b);
display.setCursor(15, 360+ZZshift);
if(mainHWdeviceSelect==1){
display.println(String(TOPIC)+"#");
}else if(mainHWdeviceSelect==2){
display.println(String(mainHWdevice[mainHWdeviceSelect][1])+"/"+String(APRS_FI_NAME));
}
display.setCursor(15, 385);
UtcTime(1).toCharArray(buf, 21);
display.println("UTC "+String(buf));
if(eInkOfflineDetect==true){
display.setCursor(185, 385);
display.setFont(&Logisoso10pt7b);
display.print("OFF >"+String(OfflineTimeout)+"min");
}else{
display.setCursor(200, 385);
display.print(REV);
}
display.display(false);
eInkNeedRefresh=false;
eInkRefreshTimer=millis();
}
}
//------------------------------------------------------------------------------
void Triangle(float VALUE, float MIN, float MAX){
float YY = 400.0-(VALUE - MIN) * (400.0/(MAX-MIN));
display.fillTriangle(0, (int)YY-5, 14, (int)YY, 0, (int)YY+5, colorW);
// Serial.println("Triangle Ypx: "+String(YY));
}
//------------------------------------------------------------------------------
int AzimuthShifted(int DEG){
DEG=DEG+AzimuthStart; // 246 + 390 > 236
if(DEG>359){
DEG=DEG-360;
}
return DEG;
}
//-------------------------------------------------------------------------------------------------------
void WDTimer(){
eInkOfflineDetect = false;
// WDT
esp_task_wdt_reset();
}
//-------------------------------------------------------------------------------------------------------
void Watchdog(){
// // WDT
// if(millis()-WdtTimer > 960000){
// esp_task_wdt_reset();
// WdtTimer=millis();
// Serial.print("WDT reset ");
// Serial.println(UtcTime(1));
// }
#if defined(APRSFI)
static long aprsfiTimer = -900000;
if(millis()-aprsfiTimer > 900000 && mainHWdeviceSelect==2){
aprsfiTimer=millis();
// Serial.println("[json] start");
// Use https://arduinojson.org/v6/assistant to compute the capacity.
// StaticJsonDocument<600> doc;
// {"command":"get","result":"ok","found":1,"what":"wx","entries":[{"name":"OK1HRA-8","time":"1694332396","temp":"20.0","pressure":"1007.5","humidity":"60","wind_direction":"247","wind_speed":"3.1","wind_gust":"3.6","rain_mn":"0.0"}]}
// jsonString = "{\"command\":\"get\",\"result\":\"ok\",\"found\":1,\"what\":\"wx\",\"entries\":[{\"name\":\"OK1HRA-8\",\"time\":\"1693643356\",\"temp\":\"17.8\",\"pressure\":\"1008.2\",\"humidity\":\"81\",\"wind_direction\":\"270\",\"wind_speed\":\"1.3\",\"wind_gust\":\"2.2\",\"rain_mn\":\"0.7\"}]}";
// GetHttps();
// DeserializationError error = deserializeJson(doc, jsonString);
// Test if parsing succeeds.
// if (error) {
// Serial.print("[json] deserializeJson() failed: ");
// Serial.println(error.f_str());
// return;
// }
client.setCACert(rootCACertificate);
Serial.println("\n[https] Starting connection to server...");
if (!client.connect(server, 443))
Serial.println("[https] Connection failed!");
else {
Serial.println("[https] Connected to server!");
// Make a HTTP request:
client.println( "GET /api/get?name="+String(APRS_FI_NAME)+"&what=wx&apikey="+String(APRS_FI_APIKEY)+"&format=json HTTP/1.1" );
client.println("Host: api.aprs.fi");
client.println("Connection: close");
client.println();
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("[https] headers received");
break;
}
}
jsonString = "";
while (client.available()) {
char c = client.read();
jsonString = jsonString + String(c);
// Serial.write(c);
}
int indexStart = jsonString.indexOf("{");
int indexEnd = jsonString.indexOf("]");
jsonString = jsonString.substring(indexStart,indexEnd+2);
Serial.print("[https RX] ");
Serial.println(jsonString);
RxMqttTimer=millis();
client.stop();
Serial.println("[https] stop");
Serial.println("[json] start extract");
String posStr = jsonExtract(jsonString, "entries");
Serial.println("[json] extract temp");
Temperature = jsonExtract(posStr, "temp").toFloat();
Serial.println("[json] extract rain");
RainToday = jsonExtract(posStr, "rain_mn").toFloat();
HumidityRel = jsonExtract(posStr, "humidity").toFloat();
DewPoint = (float)Temperature - (100.0 - constrain(HumidityRel, 0, 100)) / 5.0;
Pressure = jsonExtract(posStr, "pressure").toFloat();
WindDir = jsonExtract(posStr, "wind_direction").toInt();
WindSpeedMaxPeriod = jsonExtract(posStr, "wind_gust").toFloat();
// Temperature = doc["entries"][0]["temp"];
// RainToday = doc["entries"][0]["rain_mn"];
// HumidityRel = doc["entries"][0]["humidity"];
// Pressure = doc["entries"][0]["pressure"];
// WindDir = doc["entries"][0]["wind_direction"];
// WindSpeedMaxPeriod = doc["entries"][0]["wind_gust"];
Serial.println("[json] Temperature: "+String(Temperature));
Serial.println("[json] RainToday: "+String(RainToday));
Serial.println("[json] HumidityRel: "+String(HumidityRel));
Serial.println("[json] DewPoint: "+String(DewPoint));
Serial.println("[json] Pressure: "+String(Pressure));
Serial.println("[json] WindDir: "+String(WindDir));
Serial.println("[json] WindSpeedMaxPeriod: "+String(WindSpeedMaxPeriod));
eInkNeedRefresh=true;
RxMqttTimer=millis();
}
}
#endif
static unsigned long SdCardTimer = millis();
if(millis()-SdCardTimer > 1000){
SdCardPresentStatus=!digitalRead(SdCardPresentPin);
// Serial.println(" microSD "+String(SdCardPresentStatus));
SdCardTimer = millis();
}
static bool eInkOfflineDetectTmp = false;
if( (millis()-RxMqttTimer) > OfflineTimeout*60000 && eInkOfflineDetect == false ){
eInkNeedRefresh=true;
eInkOfflineDetect = true;
eInkOfflineDetectTmp = eInkOfflineDetect;
Serial.print(millis());
Serial.print(" | ");
Serial.print(OfflineTimeout);
Serial.println(" minutes offline timeout");
}
if(eInkOfflineDetect!=eInkOfflineDetectTmp){
eInkNegativ = !eInkNegativ;
eInkOfflineDetectTmp = eInkOfflineDetect;
}
// static unsigned long eInkNegativTimer = millis();
// if(millis()-eInkNegativTimer > 10000 || eInkNeedRefresh==true){
if(eInkNegativ!=eInkNegativTmp){
if(eInkNegativ==true){
colorB = GxEPD_BLACK;
colorW = GxEPD_WHITE;
eInkNegativTmp=true;
}else{
colorB = GxEPD_WHITE;
colorW = GxEPD_BLACK;
eInkNegativTmp=false;
}
// Serial.println("wd eInkNegativ "+String(eInkNegativ));
// Serial.println("wd colorB "+String(colorB));
// Serial.println("wd colorW "+String(colorW));
}
// }
#if defined(WIFI)
unsigned long currentMillis = millis();
// if WiFi is down, try reconnecting every CHECK_WIFI_TIME seconds
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - WifiTimer >=WifiReconnect)) {
Serial.print(millis());
Serial.println(" Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.reconnect();
WifiTimer = currentMillis;
}
#endif
}
//-------------------------------------------------------------------------------------------------------
void DirectionalRosette(int deg, int X, int Y, int R){
int dot1;
int dot2;
if(R>100){
dot1=2;
dot2=5;
display.setFont(&Logisoso10pt7b);
}else{
dot1=1.5;
dot2=3;
display.setFont(&Logisoso8pt7b);
}
if(R>100){
display.setCursor(Xcoordinate(0,X-5,R-10), Ycoordinate(0,Y,R-10));
display.println("N");
display.setCursor(Xcoordinate(90,X+5,R-10), Ycoordinate(90,Y+8,R-10));
display.println("E");
display.setCursor(Xcoordinate(180,X-6,R-10), Ycoordinate(180,Y+13,R-10));
display.println("S");
display.setCursor(Xcoordinate(270,X-16,R-10), Ycoordinate(270,Y+8,R-10));
display.println("W");
}
for (int j=0; j<36; j++) {
if(j % 9 == 0){
if(R<100){
display.fillCircle(Xcoordinate(j*10,X,R), Ycoordinate(j*10,Y,R), dot2, colorW);
}
}else{
display.fillCircle(Xcoordinate(j*10,X,R), Ycoordinate(j*10,Y,R), dot1, colorW);
}
}
if( mainHWdeviceSelect==0 || (mainHWdeviceSelect==1 && WindSpeedMaxPeriod>0) || mainHWdeviceSelect==2){
Arrow(deg,X,Y,R*0.9);
Serial.println("Arrow");
}
}
//-------------------------------------------------------------------------------------------------------
void Arrow(int deg, int X, int Y, int r){
int deg2 = deg+130;
int deg3 = deg+230;
display.fillTriangle(Xcoordinate(deg,X,r), Ycoordinate(deg,Y,r), Xcoordinate(deg2,X,r/2), Ycoordinate(deg2,Y,r/2), Xcoordinate(deg+180,X,0), Ycoordinate(deg+180,Y,0), colorW);
display.fillTriangle(Xcoordinate(deg,X,r), Ycoordinate(deg,Y,r), Xcoordinate(deg3,X,r/2), Ycoordinate(deg3,Y,r/2), Xcoordinate(deg+180,X,0), Ycoordinate(deg+180,Y,0), colorW);
display.fillTriangle(Xcoordinate(deg+180,X,r), Ycoordinate(deg+180,Y,r), Xcoordinate(deg3,X,r/10), Ycoordinate(deg3,Y,r/10), Xcoordinate(deg+180,X,0), Ycoordinate(deg+180,Y,0), colorW);
display.fillTriangle(Xcoordinate(deg+180,X,r), Ycoordinate(deg+180,Y,r), Xcoordinate(deg2,X,r/10), Ycoordinate(deg2,Y,r/10), Xcoordinate(deg+180,X,0), Ycoordinate(deg+180,Y,0), colorW);
}
//-------------------------------------------------------------------------------------------------------
float Xcoordinate(int dir, int Center, int r){
float x = Center + sin(dir/RAD_TO_DEG) * r;
return x;
}
//-------------------------------------------------------------------------------------------------------
float Ycoordinate(int dir, int Center, int r){
float y = Center - cos(dir/RAD_TO_DEG) * r;
return y;
}
//-------------------------------------------------------------------------------------------------------
void print_wifi_error(){
switch(WiFi.status())
{
case WL_IDLE_STATUS : Serial.println("WL_IDLE_STATUS"); break;
case WL_NO_SSID_AVAIL : Serial.println("WL_NO_SSID_AVAIL"); break;
case WL_CONNECT_FAILED : Serial.println("WL_CONNECT_FAILED"); break;
case WL_DISCONNECTED : Serial.println("WL_DISCONNECTED"); break;
default : Serial.printf("No know WiFi error"); break;
}
}
//-------------------------------------------------------------------------------------------------------
void Mqtt(){
#if defined(MQTT)
if (millis()-MqttStatusTimer[0]>MqttStatusTimer[1] && (mainHWdeviceSelect==0 || mainHWdeviceSelect==1)){
if(!mqttClient.connected()){
long now = millis();
if (now - lastMqttReconnectAttempt > 10000) {
lastMqttReconnectAttempt = now;
Serial.print("Attempt to MQTT reconnect | ");
Serial.println(millis()/1000);
Status = 4; // reset
if (mqttReconnect()) {
lastMqttReconnectAttempt = 0;
}
}
}else{
// Client connected
mqttClient.loop();
}
MqttStatusTimer[0]=millis();
}
#endif
}
//-------------------------------------------------------------------------------------------------------
#if defined(MQTT)