-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAZOpenWeather.ino
More file actions
2432 lines (2150 loc) · 90.5 KB
/
Copy pathRAZOpenWeather.ino
File metadata and controls
2432 lines (2150 loc) · 90.5 KB
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
// *************************************************************************************
// V4.1 04-05-25 Added conditions to MQTT
// V3.2 17-10-24 MQTT fixes for symbols
// V3.1 31-05-24 Auto refresh bug
// V2.8 05-05-24 First OTA Update
// V2.5 04-05-24 OTA & RDKOTA Library - Releaseversion
// V2.3 19-03-24 Version number on screen
// V2.2.2 12-03-24 OpenWeather library compatible with OneCall2.5 and OneCall3.0
// Library included in github
// Some small warnings removed
// Blue moon removed
// V2.2.1 26-12-23 Calibrate touch en scherm draaibaar.
// V2.2.0 26-11-23 Setting wel/niet meesturen symbool in MQTT.
// Refresh pages bij opstarten.
// MQTT connect 3x controleren
// V2.1.1 16-11-23 Geselecteerde locatie werd niet opgeslagen, display aangepast van 22 naar 14 ivm nieuwe printen
// V2.1.0 11-06-23 Settings and reboot prohibited from external
// V2.0.9 08-06-23 - Velden voor lat en kon verlengd van 15 naar 25 chars
// - Reset ingebouwd als bij het opstarten op het scherm wordt gedrukt.
// - Een time-out op de MQTT connectie. Als die na 5 seconden faalt, zet ik de MQTT connectie uit.
// - Foutjes op de settings webpage bij city3, 4 en 5.
// V2.0.8 06-06-23 Settings page fits on phone
// Auto refresh after selecting other location in browser
// Forecast from today
// V2.0.7 05-06-23 Show IP address
// V2.0.6 21-05-23 Vertical layout of webpage repaired for phone
// V2.0.5 18-05-23 Eliminated the usage of external images, use them from SPIFFS
// V2.0.4 11-05-23 Bug with useWapp and moontext
// V2.0.3 11-05-23 Int. tempsensor automatic enabled and placed on website.
// V2.0.2 10-05-23 Tel. nr. longer and adjustments from Henny.
// V2.0.1 09-05-23 Select location online
// V2.0.0 01-05-23 Websettings
// V1.8.6 13-01-23 Webpage added
// V1.8.5 05-12-22 Local temperature added
// V1.8.4 01-12-22 Possibility to send weather report via Whatsapp added.
// V1.8.3 15-11-22 Add multiple splashscreen en variabele pageDelay welke bepaalt hoelang een sub scherm getoond wordt
// V1.8.2b 15-11-22 Override en gebruik van de messagebox voorde locationlist,
// V1.8.2a 15-11-22 PA2HGJ changes
// line 779: add messageBox() function
// line 225: changed tft.drawString to messageBox
// line 264: log connected WiFi network
// line 270: show connected SSID in messageBox
// line 288: changed tft.drawString to messageBox
// line 440: use AXTLS on ESP8266 BearSSL on ESP32
// line 622: changed function calcWindDirection() to calcWindAngle()
// line 602: use calcWindAngle() to get correct icon name
// line 612: Display winddir from winddir[] array in AllSettings.h
// line 887 and 916 use calcWindAngle() for correct logging and MQTT MQTT_MESSAGES
// line 1021: changed handlePage1() function
// - add waiting message
// - first download data then draw screen
//
// V1.8.2 07-11-22 Belgium MUF table (page2 in 2 versions)
// V1.8.1 03-11-22 More renovation, WiFi reset
// V1.8 27-10-2022 Code renovation by PA2RDK
// V1.7 18-04-2022 Muf informatie toegevoegd
// V1.6 Automatic login strongest available WiFi. Automatic login last used location.
// V1.5 Regel378 en 500
// V1.4 22-08-2020 Location-list added
// Added personal startup screen in line 226. Upload whatever picture in portrait format to Picasa,
// select picture, export in 320 format to datamap "TFT eSPI Open weather",execute ESP8266 Data Upload,
// update line 226 for image stored in Data map
// Line 224: corrected location splah screen
// deleted clear instructions line 228- 246
// Changed line 264, draw string into "Get weather data...."
// V1.3 22-08-2020 Change windspeed from m/s to Baufort.
// V1.2 21-07-2020 All_Settings.h moonPhase[8] changed to moonPhase[13]
// V1.1 07-07-2020 Led display swithed on for ESP8266 E12
//
// In this sketch we use the sketch of Peter DD6USB to obtain and display the propagation data.
// To better integrate it into Bodmer's weather station sketch, changes have been made to both original sketches by Robert PA2RDK.
// Thank you all very much for the excellent work.
//
// Example from OpenWeather library: https://github.com/Bodmer/OpenWeather
// Adapted by Bodmer to use the TFT_eSPI library: https://github.com/Bodmer/TFT_eSPI
// *************************************************************************************
#define AA_FONT_SMALL "fonts/NotoSansBold15" // 15 point sans serif bold
#define AA_FONT_LARGE "fonts/NotoSansBold36" // 36 point sans serif bold
#define B_DD6USB 0x0006 // 0, 0, 4 my preferred background color !!!
#define HAMQSL_HOST "hamqsl.com" // source page in www
#define IONIAP_HOST "www.ionosonde.iap-kborn.de" // another source page in www
#define IONBE_HOST "ionosphere.meteo.be" // another source page in www
#define DEG2RAD 0.0174532925 // Degrees to Radians conversion factor
#define INCANLGE 2 // Minimum segment subtended angle and plotting angle increment (in degrees)
#define offsetEEPROM 32
#define EEPROM_SIZE 4096
#define TIMEZONE euCET
#define OTAHOST "https://www.rjdekok.nl/Updates/RAZOpenWeather"
#define VERSION "v4.1"
//#define isCYD //CYD Display (Cheap Yellow Display)
//#define CYDLovyan //ESP32-2432S022 with Lovyan driver
//#define SmallCYD //ESP32-2432S022
#define HasTouch
/***************************************************************************************
Change this in User_Setup_Select.h in the TFT_eSPI library
if no CYD: #include <Setup1_ILI9341.h> // Setup file configured for my ILI9341 2.8 inch
if CYD: #include <Setup1_ILI9341CYDRAZ.h> // Setup file configured for my ILI9341 2.8 inch on CYD
if SmallCYD:#include <Setup1_ST7789Parallel.h> // Setup file configured for ESP32-2432S022
***************************************************************************************/
/***************************************************************************************
** Load the libraries and settings
***************************************************************************************/
#include <Arduino.h>
#include <SPI.h>
#ifndef CYDLovyan
#include <TFT_eSPI.h> // https://github.com/Bodmer/TFT_eSPI
#else
#include <LovyanGFX.hpp>
#include <FS.h>
#endif
#include <OneWire.h> // Local temp.
#include <DallasTemperature.h> // Local temp.
#ifdef isCYD
#include <XPT2046_Touchscreen.h>
#endif
// Additional functions
#ifndef CYDLovyan
#include "GfxUi.h" // Attached to this sketch
#endif
#include "SPIFFS.h" // Attached to this sketch
// Multi Wifi added by PA3HK
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include <WiFi.h>
#include <WifiMulti.h>
#include <HTTPClient.h>
WiFiMulti wifiMulti;
#include <EEPROM.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>
#include <UrlEncode.h>
#include <ESPAsyncWebServer.h>
#include <RDKOTA.h>
// Json streaming parser: (do not use IDE library manager version)
#include <JSON_Decoder.h> // https://github.com/Bodmer/JSON_Decoder
#include <OpenWeather.h> // Latest here: https://github.com/Bodmer/OpenWeather
#include "NTP_Time.h" // Attached to this sketch
/***************************************************************************************
** Display definition for CYDLovyan
***************************************************************************************/
#ifdef CYDLovyan
class LGFX : public lgfx::LGFX_Device //Door Robert!
{
lgfx::Panel_ST7789 _panel_instance; // ST7789UI
lgfx::Bus_Parallel8 _bus_instance; // MCU8080 8B
public:
LGFX(void)
{
{
auto cfg = _bus_instance.config();
cfg.freq_write = 25000000;
cfg.pin_wr = 4;
cfg.pin_rd = 2;
cfg.pin_rs = 16;
cfg.pin_d0 = 15;
cfg.pin_d1 = 13;
cfg.pin_d2 = 12;
cfg.pin_d3 = 14;
cfg.pin_d4 = 27;
cfg.pin_d5 = 25;
cfg.pin_d6 = 33;
cfg.pin_d7 = 32;
_bus_instance.config(cfg);
_panel_instance.setBus(&_bus_instance);
}
{
auto cfg = _panel_instance.config();
cfg.pin_cs = 17;
cfg.pin_rst = -1;
cfg.pin_busy = -1;
cfg.panel_width = 240;
cfg.panel_height = 320;
cfg.offset_x = 0;
cfg.offset_y = 0;
cfg.offset_rotation = 0;
// cfg.dummy_read_pixel = 8;
// cfg.dummy_read_bits = 1;
cfg.readable = false;
cfg.invert = false;
cfg.rgb_order = false;
cfg.dlen_16bit = false;
cfg.bus_shared = true;
_panel_instance.config(cfg);
}
setPanel(&_panel_instance);
}
};
#include <LGFX_TFT_eSPI.hpp>
#endif
/***************************************************************************************
** Locations
***************************************************************************************/
typedef struct { // Location data
uint16_t Xlocos; // Xoffset
uint16_t Xlocsr; // X size rectang
uint16_t Xlocnr; // X next rectang
uint16_t Ylocos; // Yoffset
uint16_t Ylocsr; // Y size rectang
uint16_t Ylocnr; // Y next rectang
} WeatherLocation;
uint16_t Xfloc = 5;
uint16_t Yfloc = 10;
WeatherLocation weatherLocation[] = {
{Xfloc, 230, 0, Yfloc, 30 , 0},
{Xfloc, 230, 0, Yfloc, 30 , 30},
{Xfloc, 230, 0, Yfloc, 30 , 60},
{Xfloc, 230, 0, Yfloc, 30 , 90},
{Xfloc, 230, 0, Yfloc, 30 , 120},
{Xfloc, 230, 0, Yfloc, 30 , 150},
{Xfloc, 230, 0, Yfloc, 30 , 180},
{Xfloc, 230, 0, Yfloc, 30 , 210},
{Xfloc, 230, 0, Yfloc, 30 , 240},
{Xfloc, 230, 0, Yfloc, 30 , 270} //10 Max.
};
/***************************************************************************************
** Define the globals and class instances
***************************************************************************************/
int8_t actualPage; // which page I am ?
uint16_t maxWebBytes = 1700; // max. bytes to read from content
String solarBuf; // to hold the compressed content
String contentStrings[10]; // holds the IAP data
String tot; // Total string for WHATSAPP
long lastRefresh = millis(); // Last refresh in millis()
bool forceRefresh = true; // Refresh after touch or internet access
long lastWHATSAPPRefresh = -1; // Last WHATSAPPrefresh in millis()
long lastLocTempRefresh = -1; // last LocTempRefresh in millis;
int EEPROM_ADDRESS = 0; // EEProm address
int maxPage = 5; // Nr. of available pages
String lastIAP = "";
long startTime = millis();
bool apMode = false;
bool printConfig = false;
int MQTTFailCounter = 0;
bool doTouch = false;
typedef struct {
byte chkDigit;
char wifiSSID[25];
char wifiPass[25];
char openWeatherAPI[35];
bool useMQTT;
char mqttBroker[25];
char mqttUser[25];
char mqttPass[25];
char mqttSubject[25];
bool mqttTXUnits;
int mqttPort;
bool useWapp;
char wappPhone[15];
char wappAPI[35];
int wappInterval;
bool serialMessages;
bool hasLocalTempSensor;
bool formatSpiffs;
int updateInterval;
int pageDelay;
int actualWeatherStation;
char city1[25];
char latitude1[25];
char longitude1[25];
char city2[25];
char latitude2[25];
char longitude2[25];
char city3[25];
char latitude3[25];
char longitude3[25];
char city4[25];
char latitude4[25];
char longitude4[25];
bool isDebug;
bool reverseRotation;
uint16_t calData0;
uint16_t calData1;
uint16_t calData2;
uint16_t calData3;
uint16_t calData4;
} Settings;
typedef struct { // Location name data
const char *name;
String latitude;
String longitude;
} WeatherStation;
typedef struct { // WiFi Access
const char *SSID;
const char *PASSWORD;
} wlanSSID;
// check All_Settings.h for adapting to your needs
// #include "RDK_Settings.h"
#include "All_Settings.h"
const int nrOffLocations = (sizeof weatherStation / sizeof (WeatherStation)) - 1;
#ifndef CYDLovyan
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#else
TFT_eSPI tft;
#endif
#ifdef isCYD
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
#endif
#ifndef CYDLovyan
GfxUi ui = GfxUi(&tft); // Jpeg and bmpDraw functions TODO: pull outside of a class
#endif
WiFiClientSecure httpsNet;
WiFiClient httpNet;
RDKOTA rdkOTA(OTAHOST);
#include <MQTT.h>
WiFiClient mqttNet;
MQTTClient client;
OW_Weather ow; // Weather forecast library instance
OW_current *current; // Pointer to structs that temporarily holds weather data
OW_hourly *hourly; // Not used
OW_daily *daily;
#define ONE_WIRE_BUS_PIN 13
//Swith LED at display
#ifdef isCYD
#define Display_Led 21
#define displayon 1
#elif defined(CYDLovyan)
#define Display_Led 0
#define displayon 1
#elif defined(SmallCYD)
#define Display_Led 0
#define displayon 1
#else
#define Display_Led 14
#define displayon 0
#endif
// #define Display_Led 22 //Oude print zonder connector en transistor voor LED
// #define displayon 1
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);
DeviceAddress Probe01 = { 0x28, 0xFF, 0x00, 0x91, 0x6B, 0x18, 0x01, 0x86 }; // Temp. sensor
AsyncWebServer server(80);
#include "webpages.h"
/***************************************************************************************
** Setup
***************************************************************************************/
void setup() {
#ifdef SmallCYD
pinMode(32, INPUT);
pinMode(33, INPUT);
#endif
Serial.begin(115200);
pinMode(Display_Led, OUTPUT);
digitalWrite(Display_Led, displayon);
EEPROM.begin(EEPROM_SIZE);
#ifdef isCYD
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
touchscreen.setRotation(2);
#endif
tft.begin();
uint16_t touchX = 0, touchY = 0;
bool pressed=false;
#ifdef isCYD
pressed =touchscreen.tirqTouched() && touchscreen.touched();
if (pressed){
TS_Point p = touchscreen.getPoint();
touchX = map(p.x, 170, 3850, 1,240);
touchY = map(p.y, 310, 3900, 1,320);
printTouchToSerial(touchX, touchY, p.z);
}
#else
#ifdef HasTouch
pressed = tft.getTouch(&touchX, &touchY);
#endif
#endif
if (pressed || !LoadConfig()){
if (settings.isDebug) Serial.println(F("Writing defaults"));
messageBox("Reset to default", TFT_WHITE, TFT_NAVY);
SaveConfig();
delay(2000);
}
LoadConfig();
tft.setRotation(settings.reverseRotation?2:0);
uint16_t calData[5] = {settings.calData0, settings.calData1, settings.calData2, settings.calData3, settings.calData4};
#ifdef HasTouch
tft.setTouch(calData);
#endif
LoadWeatherLocations();
settings.hasLocalTempSensor = (GetLocalTemp()>-50);
Serial.printf("Local temp enabled:%s",settings.hasLocalTempSensor?"yes":"no");
unsigned long timeout = millis();
bool timedOut = false;
locationList(); // Print locations
Serial.println("Show loclist");
bool showLocList = true;
while (showLocList) {
bool pressed = false;
#ifdef isCYD
pressed =touchscreen.tirqTouched() && touchscreen.touched();
if (pressed){
TS_Point p = touchscreen.getPoint();
touchX = map(p.x, 170, 3850, 1,240);
touchY = map(p.y, 310, 3900, 1,320);
printTouchToSerial(touchX, touchY, p.z);
}
#else
#ifdef HasTouch
pressed = tft.getTouch(&touchX, &touchY);
#endif
#endif
timedOut = (millis() - timeout) > 10000;
Serial.printf("Wait for touch or TimeOut %02d \r\n",millis() - timeout);
if (pressed or timedOut) {
showLocList = useLocation(timedOut, touchX, touchY);
}
}
Serial.println("Let's continu");
tft.fillScreen(TFT_BLACK);
SPIFFS.begin();
//listFiles();
// Enable if you want to erase SPIFFS, this takes some time!
// then disable and reload sketch to avoid reformatting on every boot!
if (settings.formatSpiffs){
tft.setTextDatum(BC_DATUM); // Bottom Centre datum
tft.drawString("Formatting SPIFFS, so wait!", 120, 195); SPIFFS.format();
}
if (SPIFFS.exists(splashFile)) {
#ifndef CYDLovyan
ui.drawJpeg(splashFile, 0, 0);
#else
//tft.drawJpeg(SPIFFS, splashFile, 0, 0)
auto spFile = SPIFFS.open(splashFile);
tft.drawJpg(&spFile, 0, 0);
#endif
delay(2000);
}
messageBox(VERSION, TFT_WHITE, TFT_NAVY, 5, 215, 230, 24);
delay(1000);
bool font_missing = false;
if (SPIFFS.exists("/fonts/NotoSansBold15.vlw") == false) font_missing = true;
if (SPIFFS.exists("/fonts/NotoSansBold36.vlw") == false) font_missing = true;
if (font_missing) {
Serial.println("\r\nFont missing in SPIFFS, did you upload it?");
//while(1) yield();
}
// add Wi-Fi networks from All_Settings.h
int maxNetworks = (sizeof(wifiNetworks) / sizeof(wlanSSID));
for (int i = 0; i < maxNetworks; i++ )
wifiMulti.addAP(wifiNetworks[i].SSID, wifiNetworks[i].PASSWORD);
wifiMulti.addAP(settings.wifiSSID,settings.wifiPass);
messageBox("Verbinden met WiFi", TFT_WHITE, TFT_NAVY);
if (Connect2WiFi()){
Serial.print("Connected to: ");
Serial.println(WiFi.SSID());
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
if (rdkOTA.checkForUpdate(VERSION)){
if (questionBox("Installeer update", TFT_WHITE, TFT_NAVY, 5, 240, 230, 48)){
messageBox("Installing update", TFT_YELLOW, TFT_NAVY, 5, 240, 230, 48);
rdkOTA.installUpdate();
}
}
String SSID = WiFi.SSID();
char ssid[SSID.length() + 1];
SSID.toCharArray(ssid, SSID.length() + 1);
char ipNo[16];
sprintf(ipNo, "%d.%d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
messageBox(ssid, TFT_GREEN, TFT_NAVY);
messageBox(ipNo, TFT_GREEN, TFT_NAVY, 5, 265, 230, 24);
} else {
messageBox("Connect to RAZWeather", TFT_GREEN, TFT_NAVY);
WiFi.mode(WIFI_AP);
WiFi.softAP("RAZWeather", NULL);
apMode = true;
}
delay(3000);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
if (apMode)
request->send_P(200, "text/html", settings_html, processor);
else
request->send_P(200, "text/html", index_html, processor);
});
server.on("/settings", HTTP_GET, [] (AsyncWebServerRequest *request) {
if (request->client()->remoteIP()[0] == 192 || request->client()->remoteIP()[0] == 10 || request->client()->remoteIP()[0] == 172)
request->send_P(200, "text/html", settings_html, processor);
else
request->send_P(200, "text/html", warning_html, processor);
});
server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/css", css_html);
});
server.on("/plaatje", HTTP_GET, [](AsyncWebServerRequest *request){
//"/43b4.png"
if (request->hasParam("image")) request->getParam("image")->value();
request->send(SPIFFS, request->getParam("image")->value(), "image/png");
});
server.on("/reboot", HTTP_GET, [] (AsyncWebServerRequest *request) {
if (request->client()->remoteIP()[0] == 192 || request->client()->remoteIP()[0] == 10 || request->client()->remoteIP()[0] == 172){
request->send(200, "text/plain", "Rebooting");
ESP.restart();
}
else
request->send_P(200, "text/html", warning_html, processor);
});
server.on("/calibrate", HTTP_GET, [] (AsyncWebServerRequest *request) {
if (request->client()->remoteIP()[0] == 192 || request->client()->remoteIP()[0] == 10 || request->client()->remoteIP()[0] == 172){
request->send_P(200, "text/html", index_html, processor);
doTouch = true;
}
else
request->send_P(200, "text/html", warning_html, processor);
});
server.on("/store", HTTP_GET, [] (AsyncWebServerRequest *request) {
if (request->client()->remoteIP()[0] == 192 || request->client()->remoteIP()[0] == 10 || request->client()->remoteIP()[0] == 172){
SaveSettings(request);
SaveConfig();
LoadWeatherLocations();
printConfig=true;
request->send_P(200, "text/html", index_html, processor);
}
else
request->send_P(200, "text/html", warning_html, processor);
});
server.on("/golocation", HTTP_GET, [] (AsyncWebServerRequest *request) {
if (request->hasParam("location")) settings.actualWeatherStation = request->getParam("location")->value().toInt();
actualPage = 0;
SaveConfig();
printConfig=true;
request->send_P(200, "text/html", refresh_html, processor);
});
server.begin();
Serial.println("HTTP server started");
if (settings.useMQTT){
Serial.println("Start MQTT");
client.begin(settings.mqttBroker, settings.mqttPort, mqttNet);
Serial.println("Started MQTT");
}
messageBox("Wacht op gegevens", TFT_WHITE, TFT_NAVY);
//Fetch the time
udp.begin(localPort);
syncTime();
//tft.unloadFont();
ow.partialDataSet(true); // Collect a subset of the data available
httpsNet.setInsecure();
actualPage = 0;
// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Probe01, 10);
// Create the structures that hold the retrieved weather
current = new OW_current;
daily = new OW_daily;
hourly = new OW_hourly;
}
void LoadWeatherLocations(){
WeatherStation weatherLocation;
weatherLocation.name = settings.city1;
weatherLocation.latitude = settings.latitude1;
weatherLocation.longitude = settings.longitude1;
weatherStation[2] = weatherLocation;
weatherLocation.name = settings.city2;
weatherLocation.latitude = settings.latitude2;
weatherLocation.longitude = settings.longitude2;
weatherStation[3] = weatherLocation;
weatherLocation.name = settings.city3;
weatherLocation.latitude = settings.latitude3;
weatherLocation.longitude = settings.longitude3;
weatherStation[4] = weatherLocation;
weatherLocation.name = settings.city4;
weatherLocation.latitude = settings.latitude4;
weatherLocation.longitude = settings.longitude4;
weatherStation[5] = weatherLocation;
}
bool Connect2WiFi(){
startTime = millis();
if (settings.isDebug) Serial.print("Connect to Multi WiFi");
while (wifiMulti.run() != WL_CONNECTED && millis()-startTime<30000){
// esp_task_wdt_reset();
delay(1000);
if (settings.isDebug) Serial.print(".");
}
if (settings.isDebug) Serial.println();
return (WiFi.status() == WL_CONNECTED);
}
/***************************************************************************************
** Loop
***************************************************************************************/
void loop() {
if (doTouch){
doTouch = false;
TouchCalibrate();
forceRefresh = true;
}
uint16_t touchX = 0, touchY = 0;
bool pressed = false;
#ifdef isCYD
pressed =touchscreen.tirqTouched() && touchscreen.touched();
if (pressed){
TS_Point p = touchscreen.getPoint();
touchX = map(p.x, 170, 3850, 1,240);
touchY = map(p.y, 310, 3900, 1,320);
printTouchToSerial(touchX, touchY, p.z);
}
#else
#ifdef HasTouch
pressed = tft.getTouch(&touchX, &touchY);
#endif
#endif
if (pressed) {
delay(200);
if ((touchX > 180) and (touchY > 270)) {
#ifdef isCYD
messageBox("Herstarten...", TFT_WHITE, TFT_NAVY);
delay(5000);
ESP.restart();
#else
ESP.restart();
#endif
}
if ((touchX < 90) and (touchY > 270)) {
actualPage == maxPage ? actualPage = 0 : actualPage++;
forceRefresh = true;
}
pressed = false;
}
if (printConfig){
PrintConfig();
forceRefresh = true;
printConfig=false;
}
if (forceRefresh || (millis() - lastRefresh > 1000UL * settings.updateInterval)) {
lastRefresh = millis();
Serial.println("Refresh page");
if (!forceRefresh) {
if (actualPage<maxPage){
for (int x = actualPage + 1; x <= maxPage; x++) {
handlePages(x);
delay(settings.pageDelay * 1000);
}
}
for (int x = 0; x <= actualPage; x++) {
handlePages(x);
delay(settings.pageDelay * 1000);
}
} else {
forceRefresh = false;
handlePages(actualPage);
}
lastRefresh = millis();
}
if (minute() != lastMinute) {
Serial.println("Refresh minute");
syncTime();
if (actualPage == 0) drawTime();
lastMinute = minute();
}
if (settings.useWapp){
if (lastWHATSAPPRefresh == -1 || (millis() - lastWHATSAPPRefresh > 1000UL * settings.wappInterval)) {
sendMessagetoWHATSAPP(tot);
lastWHATSAPPRefresh = millis();
}
}
}
/***************************************************************************************/
void handlePages(int pageNr) {
Serial.print("Handle page:");
Serial.println(pageNr);
//if (pageNr == 0) WiFi.mode(WIFI_AP_STA);
while (wifiMulti.run() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
if (httpsNet.connected()) httpsNet.stop();
if (httpNet.connected()) httpNet.stop();
switch (pageNr) // good for handling more pages (maybe in future)
{
case 0 :
{
handlePage0(); // Weather
break;
}
case 1 :
{
handlePage1(); // Band usage
break;
}
case 2 :
{
handlePage2(0); // Muf DE
break;
}
case 3 :
{
handlePage2(1); // Muf BE
break;
}
case 4 :
{
handlePage3(); // Overview 1
break;
}
case 5 :
{
handlePage4(); // Overview 2
break;
}
}
}
/***************************************************************************************
** Fetch the weather data and update screen
** Update the Internet based information and update screen
***************************************************************************************/
void handlePage0() {
tft.fillScreen(TFT_BLACK);
updateWeather();
syncTime();
drawTime();
lastMinute = minute();
}
/***************************************************************************************/
void updateWeather() {
#ifndef CYDLovyan
tft.loadFont(AA_FONT_SMALL);
#else
tft.setFont(&fonts::Font2); // 6x8, heel klein
#endif
drawProgress(50, "Updating...");
Serial.printf("Actual weather %d = from lat:%s and lon:%s\r\n",settings.actualWeatherStation, weatherStation[settings.actualWeatherStation].latitude, weatherStation[settings.actualWeatherStation].longitude);
bool parsed = ow.getForecast(current, hourly, daily, settings.openWeatherAPI, weatherStation[settings.actualWeatherStation].latitude, weatherStation[settings.actualWeatherStation].longitude, "metric", "nl");
tft.fillScreen(TFT_BLACK);
printWeather(); // For debug, turn on output with #define SERIAL_MESSAGES
if (lastRefresh == -1) {
drawProgress(100, "Done...");
delay(2000);
tft.fillScreen(TFT_BLACK);
}
if (parsed) {
drawCurrentWeather();
drawForecast();
drawAstronomy();
tft.unloadFont();
// Update the temperature here so we don't need to keep
// loading and unloading font which takes time
#ifndef CYDLovyan
tft.loadFont(AA_FONT_LARGE);
#else
tft.setFont(&fonts::Font4); // 6x8, heel klein
#endif
tft.setTextDatum(TR_DATUM);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
// Font ASCII code 0xB0 is a degree symbol, but o used instead in small font
tft.setTextPadding(tft.textWidth(" -88")); // Max width of values
String weatherText = "";
weatherText = String(current->temp, 0); // Make it integer temperature
tft.drawString(weatherText, 215, 95); // + "°" symbol is big... use o in small font
} else {
Serial.println("Failed to get weather");
}
if (settings.hasLocalTempSensor) PrintLocalTemp();
tft.unloadFont();
}
/***************************************************************************************
** Update progress bar
***************************************************************************************/
void drawProgress(uint8_t percentage, String text) {
tft.setTextDatum(BC_DATUM);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
tft.setTextPadding(240);
tft.drawString(text, 120, 260);
#ifndef CYDLovyan
ui.drawProgressBar(10, 269, 240 - 20, 15, percentage, TFT_WHITE, TFT_BLUE);
#endif
tft.setTextPadding(0);
}
/***************************************************************************************
** Draw the clock digits
***************************************************************************************/
void drawTime() {
#ifndef CYDLovyan
tft.loadFont(AA_FONT_LARGE);
#else
tft.setFont(&fonts::Font4); // 6x8, heel klein
#endif
// Convert UTC to local time, returns zone code in tz1_Code, e.g "GMT"
time_t local_time = TIMEZONE.toLocal(now(), &tz1_Code);
String timeNow = "";
if (hour(local_time) < 10) timeNow += "0";
timeNow += hour(local_time);
timeNow += ":";
if (minute(local_time) < 10) timeNow += "0";
timeNow += minute(local_time);
tft.setTextDatum(BC_DATUM);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextPadding(tft.textWidth(" 44:44 ")); // String width + margin
tft.drawString(timeNow, 120, 53);
drawSeparator(51);
tft.setTextPadding(0);
tft.unloadFont();
}
/***************************************************************************************
** Draw the current weather
***************************************************************************************/
void drawCurrentWeather() {
String date = "Updated: " + strDate(current->dt);
tft.setTextDatum(BC_DATUM);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
tft.setTextPadding(240);
tft.drawString(weatherStation[settings.actualWeatherStation].name, 120, 16);
String weatherIcon = "";
String currentSummary = current->main;
currentSummary.toLowerCase();
weatherIcon = getMeteoconIcon(current->id, true);
String file = "/icon/" + weatherIcon + ".bmp";
#ifndef CYDLovyan
ui.drawBmp(file, 0, 53);
#else
auto spFile = SPIFFS.open(file);
tft.drawBmp(&spFile, 0, 53);
#endif
String weatherText = current->main;
tft.setTextDatum(BR_DATUM);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
int splitPoint = 0;
int xpos = 235;
splitPoint = splitIndex(weatherText);
tft.setTextPadding(xpos - 100); // xpos - icon width
if (splitPoint) tft.drawString(weatherText.substring(0, splitPoint), xpos, 69);
else tft.drawString(" ", xpos, 69);
tft.drawString(weatherText.substring(splitPoint), xpos, 86);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.setTextDatum(TR_DATUM);
tft.setTextPadding(0);
tft.drawString("oC", 237, 95);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
weatherText = windspeedconv(current->wind_speed);
weatherText += " Bft";
tft.setTextDatum(TC_DATUM);
tft.setTextPadding(tft.textWidth("88 Bft")); // Max string length?
tft.drawString(weatherText, 124, 138);
weatherText = String(current->pressure, 0);
weatherText += " hPa";
tft.setTextDatum(TR_DATUM);
tft.setTextPadding(tft.textWidth(" 8888hPa")); // Max string length?
tft.drawString(weatherText, 230, 138);
int windAngle = calcWindAngle(current->wind_deg);
String wind[] = {"N", "NE", "E", "SE", "S", "SW", "W", "NW" };
file = "/wind/" + wind[windAngle] + ".bmp";
#ifndef CYDLovyan
ui.drawBmp(file, 101, 86);
#else
spFile = SPIFFS.open(file);
tft.drawBmp(&spFile, 101, 86);
#endif
drawSeparator(153);
tft.setTextColor(TFT_ORANGE, TFT_BLACK);
tft.setTextDatum(TC_DATUM);
tft.setTextPadding(tft.textWidth("88")); // Max string length?
tft.drawString(winddir[windAngle], 126, 69);
tft.setTextDatum(TL_DATUM); // Reset datum to normal
tft.setTextPadding(0); // Reset padding width to none
}
/***************************************************************************************
** Calculate degrees to winddirection
***************************************************************************************/
int calcWindAngle(uint16_t windDeg) {
int windAngle = (windDeg + 22.5) / 45;
if (windAngle > 7) windAngle = 0;
return windAngle;
}
/***************************************************************************************
** Draw the 4 forecast columns
***************************************************************************************/
// draws the three forecast columns
void drawForecast() {
int8_t dayIndex = 0;
drawForecastDetail( 8, 171, dayIndex++);
drawForecastDetail( 66, 171, dayIndex++); // was 95