-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.cpp
1461 lines (1322 loc) · 62.7 KB
/
weather.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
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
/* Simple firmware for a ESP32 displaying a static image on an EPaper Screen.
*
* Write an image into a header file using a 3...2...1...0 format per pixel,
* for 4 bits color (16 colors - well, greys.) MSB first. At 80 MHz, screen
* clears execute in 1.075 seconds and images are drawn in 1.531 seconds.
*/
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "esp_types.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "sdkconfig.h"
#include <stdio.h>
#include <string.h>
#include "epd_driver.h"
#include "epd_highlevel.h"
// battery
#include <driver/adc.h>
#include "esp_adc_cal.h"
#define BATT_PIN 36
#include "esp_sleep.h"
#include "Arduino.h"
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#include "ArduinoJson.h"
#include <HTTPClient.h>
#include <WiFi.h>
#include "time.h"
#include <SPI.h>
#include "owm_credentials.h"
//#include "common_functions.h"
#include "forecast_record.h"
#include "lang_de.h"
#define SCREEN_WIDTH EPD_WIDTH
#define SCREEN_HEIGHT EPD_HEIGHT
//################ VERSION ##################################################
String version = "1.0 / 4.7in"; // Programme version, see change log at end
//################ VARIABLES ##################################################
enum alignment {LEFT, RIGHT, CENTER};
#define GxEPD_WHITE 0xFF
#define GxEPD_BLACK 0x00
#define GRAY 0x88
#define LRAY 0xBB
#define DRAY 0x44
#define autoscale_on true
#define autoscale_off false
#define barchart_on true
#define barchart_off false
boolean LargeIcon = true, SmallIcon = false;
#define Large 28 // For icon drawing, needs to be odd number for best effect
#define Small 8 // For icon drawing, needs to be odd number for best effect
const byte MaxEvents = 10; // For event reporting, the maximum that can be recorded
String Time_str = "--:--:--";
String Date_str = "-- -- ----";
String EventMessage[MaxEvents]; // strings for Time, Date and Error reporting
const byte EventThreshold = 2; // Change to 1 to view all messages on e-paper screen
int wifi_signal, CurrentHour = 0, CurrentMin = 0, CurrentSec = 0, EventCnt = 0;
//################ PROGRAM VARIABLES and OBJECTS ##########################################
#define max_readings 6
Forecast_record_type WxConditions[1];
Forecast_record_type WxForecast[max_readings];
float pressure_readings[max_readings] = {0};
float temperature_readings[max_readings] = {0};
float humidity_readings[max_readings] = {0};
float rain_readings[max_readings] = {0};
float snow_readings[max_readings] = {0};
long SleepDuration = 60; // Sleep time in minutes, aligned to the nearest minute boundary, so if 30 will always update at 00 or 30 past the hour
int WakeupTime = 6; // Don't wakeup until after 07:00 to save battery power
int SleepTime = 23; // Sleep after (23+1) 00:00 to save battery power
long StartTime = 0, SleepTimer = 0;
long Delta = 12; // correction factor to compensate the ESP32 speed
// Prevents display at xx:59:yy and then xx:00:yy (one minute later) to save power
String LocalIP;
// ambient temperature around device
int temperature = 22;
int vref = 1100;
uint8_t *fb;
//fonts
#include "opensans8.h"
#include "opensans8b.h"
#include "opensans12.h"
#include "opensans12b.h"
#include "opensans16.h"
#include "opensans16b.h"
#include "opensans24.h"
#include "opensans24b.h"
EpdFont currentFont;
bool obtainWeatherData(WiFiClient& client, const String& RequestType);
float mm_to_inches(float value_mm);
float hPa_to_inHg(float value_hPa);
int JulianDate(int d, int m, int y);
float SumOfPrecip(float DataArray[], int readings);
String TitleCase(String text);
double NormalizedMoonPhase(int d, int m, int y);
void Convert_Readings_to_Imperial();
bool DecodeWeather(WiFiClient& json, String Type);
String ConvertUnixTime(int unix_time);
void DisplayWeather();
void DisplayGeneralInfoSection();
void DisplayMainWeatherSection(int x, int y);
void DisplayDisplayWindSection(int x, int y, float angle, float windspeed, int Cradius);
String WindDegToDirection(float winddirection);
void DisplayTemperatureSection(int x, int y, int twidth, int tdepth);
void DisplayForecastTextSection(int x, int y , int fwidth, int fdepth);
void DisplayForecastWeather(int x, int y, int index);
void DisplayPressureSection(int x, int y, int pwidth, int pdepth, float pressure, String slope);
void DisplayAstronomySection(int x, int y);
void DrawMoon(int x, int y, int dd, int mm, int yy, String hemisphere);
String MoonPhase(int d, int m, int y, String hemisphere);
void DisplayForecastSection(int x, int y);
void DisplayConditionsSection(int x, int y, String IconName, bool IconSize);
void arrow(int x, int y, int asize, float aangle, int pwidth, int plength);
void DisplayStatusSection(int x, int y, int rssi);
void DrawRSSI(int x, int y, int rssi);
boolean SetupTime();
boolean UpdateLocalTime() ;
void DrawBattery(int x, int y);
void addcloud(int x, int y, int scale, int linesize);
void addraindrop(int x, int y, int scale);
void addsnow(int x, int y, int scale, bool IconSize);
void addtstorm(int x, int y, int scale);
void addsun(int x, int y, int scale, bool IconSize);
void addfog(int x, int y, int scale, int linesize, bool IconSize);
void Sunny(int x, int y, bool IconSize, String IconName);
void MostlySunny(int x, int y, bool IconSize, String IconName) ;
void MostlyCloudy(int x, int y, bool IconSize, String IconName);
void Cloudy(int x, int y, bool IconSize, String IconName);
void Rain(int x, int y, bool IconSize, String IconName);
void ExpectRain(int x, int y, bool IconSize, String IconName);
void ChanceRain(int x, int y, bool IconSize, String IconName);
void Tstorms(int x, int y, bool IconSize, String IconName);
void Snow(int x, int y, bool IconSize, String IconName);
void Fog(int x, int y, bool IconSize, String IconName);
void Haze(int x, int y, bool IconSize, String IconName);
void CloudCover(int x, int y, int CCover);
void Visibility(int x, int y, String Visi);
void addmoon(int x, int y, int scale, bool IconSize);
void Nodata(int x, int y, bool IconSize, String IconName);
void DrawGraph(int x_pos, int y_pos, int gwidth, int gheight, float Y1Min, float Y1Max, String title, float DataArray[], int readings, boolean auto_scale, boolean barchart_mode);
void drawString(int x, int y, String text, alignment align);
void drawStringMaxWidth(int x, int y, unsigned int text_width, String text, alignment align);
void ReportEvent(String EventMessage[]);
void DisplayPrecipitationSection(int x, int y, int pwidth, int pdepth);
void DisplayTime();
//void VerboseRecordOfResetReason(RESET_REASON reason);
void fillCircle(int x, int y, int r, uint8_t color);
void drawFastHLine(int16_t x0, int16_t y0, int length, uint16_t color);
void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
void drawCircle(int x0, int y0, int r, uint8_t color);
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h,uint16_t color);
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h,uint16_t color);
void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
int16_t x2, int16_t y2, uint16_t color);
void drawPixel(int x, int y, uint8_t color) ;
void setFont(EpdFont const&font);
bool getRandomImage(WiFiClient & client);
void drawImage(WiFiClient & client);
bool decodeImage(WiFiClient& json);
void Delay(uint32_t millis) { vTaskDelay(millis / portTICK_PERIOD_MS); }
uint32_t Millis() { return esp_timer_get_time() / 1000; }
void BeginSleep() {
epd_poweroff();
long SleepTimer = SleepDuration * 60; //Some ESP32 are too fast to maintain accurate time
esp_sleep_enable_timer_wakeup(SleepTimer * 1000000LL);
#ifdef BUILTIN_LED
pinMode(BUILTIN_LED, INPUT); // If it's On, turn it off and some boards use GPIO-5 for SPI-SS, which remains low after screen use
digitalWrite(BUILTIN_LED, HIGH);
#endif
Serial.println("Entering " + String(SleepTimer) + "-secs of sleep time");
Serial.println("Awake for : " + String((millis() - StartTime) / 1000.0, 3) + "-secs");
Serial.println("Starting deep-sleep period...");
epd_deinit();
esp_deep_sleep_start(); // Sleep for e.g. 30 minutes
}
boolean SetupTime() {
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer, "time.nist.gov"); //(gmtOffset_sec, daylightOffset_sec, ntpServer)
setenv("TZ", Timezone, 1); //setenv()adds the "TZ" variable to the environment with a value TimeZone, only used if set to 1, 0 means no change
tzset(); // Set the TZ environment variable
delay(100);
bool TimeStatus = UpdateLocalTime();
return TimeStatus;
}
uint8_t StartWiFi() {
Serial.print("\r\nConnecting to: "); Serial.println(String(ssid));
IPAddress dns(8, 8, 8, 8); // Google DNS
WiFi.disconnect();
WiFi.mode(WIFI_STA); // switch off AP
WiFi.setAutoConnect(true);
WiFi.setAutoReconnect(true);
WiFi.begin(ssid, password);
unsigned long start = millis();
uint8_t connectionStatus;
bool AttemptConnection = true;
while (AttemptConnection) {
connectionStatus = WiFi.status();
if (millis() > start + 15000) { // Wait 15-secs maximum
AttemptConnection = false;
}
if (connectionStatus == WL_CONNECTED || connectionStatus == WL_CONNECT_FAILED) {
AttemptConnection = false;
}
delay(50);
}
if (connectionStatus == WL_CONNECTED) {
wifi_signal = WiFi.RSSI(); // Get Wifi Signal strength now, because the WiFi will be turned off to save power!
Serial.println("WiFi connected at: " + WiFi.localIP().toString());
}
else Serial.println("WiFi connection *** FAILED ***");
return connectionStatus;
}
void StopWiFi() {
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
}
void loop() {
printf("current temperature: %f\n", epd_ambient_temperature());
Delay(300);
Delay(200000);
}
void setup() {
StartTime = millis();
Serial.begin(115200);
if (StartWiFi() == WL_CONNECTED && SetupTime() == true) {
//if ((CurrentHour >= WakeupTime && CurrentHour <= SleepTime)) {
byte Attempts = 1;
bool RxWeather = false, RxForecast = false;
WiFiClient client; // wifi client object
while ((RxWeather == false || RxForecast == false) && Attempts <= 2) { // Try up-to 2 time for Weather and Forecast data
if (RxWeather == false) RxWeather = obtainWeatherData(client, "weather");
if (RxForecast == false) RxForecast = obtainWeatherData(client, "forecast");
Attempts++;
}
if (true || (RxWeather && RxForecast)) { // Only if received both Weather or Forecast proceed
StopWiFi(); // Reduces power consumption
epd_poweron();
volatile uint32_t t1 = Millis();
epd_clear();
volatile uint32_t t2 = Millis();
printf("EPD clear took %dms.\n", t2 - t1);
//epd_poweroff();
//epd_poweron();
//drawImage(client);
DisplayWeather();
//DisplayTime();
t1 = Millis();
int temperature = epd_ambient_temperature();
if (temperature <= 0) temperature = 25;
enum EpdDrawMode mode = (enum EpdDrawMode)(MODE_GC16 | MODE_PACKING_2PPB | PREVIOUSLY_WHITE);
epd_draw_base(epd_full_screen(), fb, epd_full_screen(), mode, temperature, NULL, EPD_BUILTIN_WAVEFORM);
t2 = Millis();
epd_poweroff();
//display.display(false); // Full screen update mode
}
//}
}
BeginSleep();
}
void epd_task(void *pvParameters) {
epd_init(EPD_LUT_1K);
ESP_LOGW("main", "allocating...\n");
fb = (uint8_t *)heap_caps_malloc(EPD_WIDTH * EPD_HEIGHT / 2, MALLOC_CAP_SPIRAM);
memset(fb, 0xFF, EPD_WIDTH * EPD_HEIGHT / 2);
while (1) {
loop();
};
}
extern "C" {
void app_main() {
ESP_LOGW("main", "Hello World!\n");
heap_caps_print_heap_info(MALLOC_CAP_INTERNAL);
heap_caps_print_heap_info(MALLOC_CAP_SPIRAM);
xTaskCreatePinnedToCore(&epd_task, "epd task", 10000, NULL, 2, NULL, 1);
initArduino();
setup();
}
}
void drawImage(WiFiClient & client) {
getRandomImage(client);
}
void Convert_Readings_to_Imperial() {
WxConditions[0].Pressure = hPa_to_inHg(WxConditions[0].Pressure);
WxForecast[1].Rainfall = mm_to_inches(WxForecast[1].Rainfall);
WxForecast[1].Snowfall = mm_to_inches(WxForecast[1].Snowfall);
}
bool DecodeWeather(WiFiClient& json, String Type) {
Serial.print(F("\nCreating object...and "));
// allocate the JsonDocument
DynamicJsonDocument doc(64 * 1024);
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, json);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return false;
}
// convert it to a JsonObject
JsonObject root = doc.as<JsonObject>();
Serial.println(" Decoding " + Type + " data");
if (Type == "weather") {
// All Serial.println statements are for diagnostic purposes and not required, remove if not needed
WxConditions[0].lon = root["coord"]["lon"].as<float>(); Serial.println(" Lon: "+String(WxConditions[0].lon));
WxConditions[0].lat = root["coord"]["lat"].as<float>(); Serial.println(" Lat: "+String(WxConditions[0].lat));
WxConditions[0].Main0 = root["weather"][0]["main"].as<char*>(); Serial.println("Main: "+String(WxConditions[0].Main0));
WxConditions[0].Forecast0 = root["weather"][0]["description"].as<char*>(); Serial.println("For0: "+String(WxConditions[0].Forecast0));
WxConditions[0].Forecast1 = root["weather"][1]["description"].as<char*>(); Serial.println("For1: "+String(WxConditions[0].Forecast1));
WxConditions[0].Forecast2 = root["weather"][2]["description"].as<char*>(); Serial.println("For2: "+String(WxConditions[0].Forecast2));
WxConditions[0].Icon = root["weather"][0]["icon"].as<char*>(); Serial.println("Icon: "+String(WxConditions[0].Icon));
WxConditions[0].Temperature = root["main"]["temp"].as<float>(); Serial.println("Temp: "+String(WxConditions[0].Temperature));
WxConditions[0].Pressure = root["main"]["pressure"].as<float>(); Serial.println("Pres: "+String(WxConditions[0].Pressure));
WxConditions[0].Humidity = root["main"]["humidity"].as<float>(); Serial.println("Humi: "+String(WxConditions[0].Humidity));
WxConditions[0].Low = root["main"]["temp_min"].as<float>(); Serial.println("TLow: "+String(WxConditions[0].Low));
WxConditions[0].High = root["main"]["temp_max"].as<float>(); Serial.println("THig: "+String(WxConditions[0].High));
WxConditions[0].Windspeed = root["wind"]["speed"].as<float>(); Serial.println("WSpd: "+String(WxConditions[0].Windspeed));
WxConditions[0].Winddir = root["wind"]["deg"].as<float>(); Serial.println("WDir: "+String(WxConditions[0].Winddir));
WxConditions[0].Cloudcover = root["clouds"]["all"].as<int>(); Serial.println("CCov: "+String(WxConditions[0].Cloudcover)); // in % of cloud cover
WxConditions[0].Visibility = root["visibility"].as<int>(); Serial.println("Visi: "+String(WxConditions[0].Visibility)); // in metres
WxConditions[0].Rainfall = root["rain"]["1h"].as<float>(); Serial.println("Rain: "+String(WxConditions[0].Rainfall));
WxConditions[0].Snowfall = root["snow"]["1h"].as<float>(); Serial.println("Snow: "+String(WxConditions[0].Snowfall));
WxConditions[0].Country = root["sys"]["country"].as<char*>(); Serial.println("Ctry: "+String(WxConditions[0].Country));
WxConditions[0].Sunrise = root["sys"]["sunrise"].as<int>(); Serial.println("SRis: "+String(WxConditions[0].Sunrise));
WxConditions[0].Sunset = root["sys"]["sunset"].as<int>(); Serial.println("SSet: "+String(WxConditions[0].Sunset));
WxConditions[0].Timezone = root["timezone"].as<int>(); Serial.println("TZon: "+String(WxConditions[0].Timezone)); }
if (Type == "forecast") {
//Serial.println(json);
Serial.print(F("\nReceiving Forecast period - ")); //------------------------------------------------
JsonArray list = root["list"];
for (byte r = 0; r < max_readings; r++) {
Serial.println("\nPeriod-" + String(r) + "--------------");
WxForecast[r].Dt = list[r]["dt"].as<char*>();
WxForecast[r].Temperature = list[r]["main"]["temp"].as<float>(); Serial.println("Temp: "+String(WxForecast[r].Temperature));
WxForecast[r].Low = list[r]["main"]["temp_min"].as<float>(); Serial.println("TLow: "+String(WxForecast[r].Low));
WxForecast[r].High = list[r]["main"]["temp_max"].as<float>(); Serial.println("THig: "+String(WxForecast[r].High));
WxForecast[r].Pressure = list[r]["main"]["pressure"].as<float>(); Serial.println("Pres: "+String(WxForecast[r].Pressure));
WxForecast[r].Humidity = list[r]["main"]["humidity"].as<float>(); Serial.println("Humi: "+String(WxForecast[r].Humidity));
WxForecast[r].Forecast0 = list[r]["weather"][0]["main"].as<char*>(); Serial.println("For0: "+String(WxForecast[r].Forecast0));
WxForecast[r].Forecast1 = list[r]["weather"][1]["main"].as<char*>(); Serial.println("For1: "+String(WxForecast[r].Forecast1));
WxForecast[r].Forecast2 = list[r]["weather"][2]["main"].as<char*>(); Serial.println("For2: "+String(WxForecast[r].Forecast2));
WxForecast[r].Icon = list[r]["weather"][0]["icon"].as<char*>(); Serial.println("Icon: "+String(WxForecast[r].Icon));
WxForecast[r].Description = list[r]["weather"][0]["description"].as<char*>(); Serial.println("Desc: "+String(WxForecast[r].Description));
WxForecast[r].Cloudcover = list[r]["clouds"]["all"].as<int>(); Serial.println("CCov: "+String(WxForecast[r].Cloudcover)); // in % of cloud cover
WxForecast[r].Windspeed = list[r]["wind"]["speed"].as<float>(); Serial.println("WSpd: "+String(WxForecast[r].Windspeed));
WxForecast[r].Winddir = list[r]["wind"]["deg"].as<float>(); Serial.println("WDir: "+String(WxForecast[r].Winddir));
WxForecast[r].Rainfall = list[r]["rain"]["3h"].as<float>(); Serial.println("Rain: "+String(WxForecast[r].Rainfall));
WxForecast[r].Snowfall = list[r]["snow"]["3h"].as<float>(); Serial.println("Snow: "+String(WxForecast[r].Snowfall));
WxForecast[r].Period = list[r]["dt_txt"].as<char*>(); Serial.println("Peri: "+String(WxForecast[r].Period));
}
//------------------------------------------
float pressure_trend = WxForecast[0].Pressure - WxForecast[2].Pressure; // Measure pressure slope between ~now and later
pressure_trend = ((int)(pressure_trend * 10)) / 10.0; // Remove any small variations less than 0.1
WxConditions[0].Trend = "0";
if (pressure_trend > 0) WxConditions[0].Trend = "+";
if (pressure_trend < 0) WxConditions[0].Trend = "-";
if (pressure_trend == 0) WxConditions[0].Trend = "0";
if (Units == "I") Convert_Readings_to_Imperial();
}
return true;
}
//#########################################################################################
String ConvertUnixTime(int unix_time) {
// Returns either '21:12 ' or ' 09:12pm' depending on Units mode
time_t tm = unix_time;
struct tm *now_tm = localtime(&tm);
char output[40];
if (Units == "M") {
strftime(output, sizeof(output), "%H:%M %d/%m/%y", now_tm);
}
else {
strftime(output, sizeof(output), "%I:%M%P %m/%d/%y", now_tm);
}
return output;
}
//#########################################################################################
//WiFiClient client; // wifi client object
bool decodeImage(WiFiClient& json) {
Serial.print(F("\nIMG creating object...and "));
// allocate the JsonDocument
DynamicJsonDocument doc(1200 * 825);
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, json);
// Test if parsing succeeds.
if (error) {
Serial.print(F("IMG deserializeJson() failed: "));
Serial.println(error.c_str());
return false;
}
// convert it to a JsonObject
JsonArray array = doc.as<JsonArray>();
Serial.println("IMG Decoding ...");
uint8_t img_data[EPD_WIDTH * EPD_HEIGHT / 2];
int i = 0;
for(JsonVariant v : array) {
img_data[i] = v.as<uint16_t>();
i++;
}
//img_data = root["data"].as<JsonArray>();
EpdRect area = {
.x = 0,
.y = 0,
.width = 1200,
.height = 825,
};
epd_copy_to_framebuffer(area, (uint8_t *)img_data, fb);
return true;
}
bool getRandomImage(WiFiClient & client) {
client.stop(); // close connection before sending a new request
HTTPClient http;
const char imgServer[] = "bb.org.ua";
String uri = "/get_random_image.php";
http.begin(client, imgServer, 80, uri);
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK) {
if (!decodeImage(http.getStream())) return false;
client.stop();
http.end();
return true;
}
else
{
Serial.printf("connection failed, error: %s", http.errorToString(httpCode).c_str());
client.stop();
http.end();
return false;
}
http.end();
return true;
}
bool obtainWeatherData(WiFiClient & client, const String & RequestType) {
const String units = (Units == "M" ? "metric" : "imperial");
client.stop(); // close connection before sending a new request
HTTPClient http;
String uri = "/data/2.5/" + RequestType + "?q=" + City + "," + Country + "&APPID=" + apikey + "&mode=json&units=" + units + "&lang=" + Language;
if(RequestType != "weather")
{
uri += "&cnt=" + String(max_readings);
}
//http.begin(uri,test_root_ca); //HTTPS example connection
http.begin(client, server, 80, uri);
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK) {
if (!DecodeWeather(http.getStream(), RequestType)) return false;
client.stop();
http.end();
return true;
}
else
{
Serial.printf("connection failed, error: %s", http.errorToString(httpCode).c_str());
client.stop();
http.end();
return false;
}
http.end();
return true;
}
float mm_to_inches(float value_mm)
{
return 0.0393701 * value_mm;
}
float hPa_to_inHg(float value_hPa)
{
return 0.02953 * value_hPa;
}
int JulianDate(int d, int m, int y) {
int mm, yy, k1, k2, k3, j;
yy = y - (int)((12 - m) / 10);
mm = m + 9;
if (mm >= 12) mm = mm - 12;
k1 = (int)(365.25 * (yy + 4712));
k2 = (int)(30.6001 * mm + 0.5);
k3 = (int)((int)((yy / 100) + 49) * 0.75) - 38;
// 'j' for dates in Julian calendar:
j = k1 + k2 + d + 59 + 1;
if (j > 2299160) j = j - k3; // 'j' is the Julian date at 12h UT (Universal Time) For Gregorian calendar:
return j;
}
float SumOfPrecip(float DataArray[], int readings) {
float sum = 0;
for (int i = 0; i <= readings; i++) {
sum += DataArray[i];
}
return sum;
}
String TitleCase(String text){
if (text.length() > 0) {
String temp_text = text.substring(0,1);
temp_text.toUpperCase();
return temp_text + text.substring(1); // Title-case the string
}
else return text;
}
double NormalizedMoonPhase(int d, int m, int y) {
int j = JulianDate(d, m, y);
//Calculate the approximate phase of the moon
double Phase = (j + 4.867) / 29.53059;
return (Phase - (int) Phase);
}
void DisplayWeather() // 4.7" e-paper display is 960x540 resolution
{
DisplayStatusSection(630, 22, wifi_signal); // Wi-Fi signal strength and Battery voltage
DisplayGeneralInfoSection(); // Top line of the display
DisplayDisplayWindSection(800, 210, WxConditions[0].Winddir, WxConditions[0].Windspeed, 130);
//DisplayDisplayWindSection(800, 210, 110, 2, 130);
DisplayAstronomySection(600, 400); // Astronomy section Sun rise/set, Moon phase and Moon icon
DisplayMainWeatherSection(137, 130); // Centre section of display for Location, temperature, Weather report, current Wx Symbol and wind direction
DisplayForecastSection(10, 330); // 3hr forecast boxes
}
void DisplayGeneralInfoSection()
{
setFont(OpenSans8B);
drawString(4, 2, City, LEFT);
// Uncomment the next line if the display of IP- and MAC-Adddress is wanted
//drawString(SCREEN_WIDTH - 150, 20, "IP=" + LocalIP + ", MAC=" + WiFi.macAddress() ,RIGHT);
drawLine(5, 30, SCREEN_WIDTH - 8, 30, 0xAA);
drawString(200, 4, Date_str, LEFT);
drawString(400, 2, TXT_UPDATED + Time_str, LEFT);
}
void DisplayMainWeatherSection(int x, int y) // (x=500, y=190)
{
DisplayConditionsSection(x + 3, y + 50, WxConditions[0].Icon, LargeIcon);
DisplayTemperatureSection(x + 230, y - 30, 180, 170);
DisplayPressureSection(x + 160, y + 70, 180, 170, WxConditions[0].Pressure, WxConditions[0].Trend);
DisplayPrecipitationSection(x + 268, y - 8, 181, 170);
DisplayForecastTextSection(x + 147, y + 100, 180, 170); // DisplayForecastTextSection(x + 147, y + 22, 548, 90);
}
void DisplayDisplayWindSection(int x, int y, float angle, float windspeed, int Cradius)
{
arrow(x, y, Cradius - 22, angle, 18, 33); // Show wind direction on outer circle of width and length
setFont(OpenSans12);
int dxo, dyo, dxi, dyi;
drawCircle(x, y, Cradius, GxEPD_BLACK); // Draw compass circle
drawCircle(x, y, Cradius + 1, GxEPD_BLACK); // Draw compass circle
drawCircle(x, y, Cradius * 0.7, GxEPD_BLACK); // Draw compass inner circle
for (float a = 0; a < 360; a = a + 22.5) {
dxo = Cradius * cos((a - 90) * PI / 180);
dyo = Cradius * sin((a - 90) * PI / 180);
if (a == 45) drawString(dxo + x + 27, dyo + y - 20, TXT_NE, CENTER);
if (a == 135) drawString(dxo + x + 27, dyo + y - 2, TXT_SE, CENTER);
if (a == 225) drawString(dxo + x - 43, dyo + y - 2, TXT_SW, CENTER);
if (a == 315) drawString(dxo + x - 43, dyo + y - 20, TXT_NW, CENTER);
dxi = dxo * 0.9;
dyi = dyo * 0.9;
drawLine(dxo + x, dyo + y, dxi + x, dyi + y, GxEPD_BLACK);
dxo = dxo * 0.7;
dyo = dyo * 0.7;
dxi = dxo * 0.9;
dyi = dyo * 0.9;
drawLine(dxo + x, dyo + y, dxi + x, dyi + y, GxEPD_BLACK);
}
drawString(x - 3, y - Cradius - 30, TXT_N, CENTER);
drawString(x - 5, y + Cradius + 18, TXT_S, CENTER);
drawString(x - Cradius - 27, y - 11, TXT_W, CENTER);
drawString(x + Cradius + 15, y - 11, TXT_E, CENTER);
drawString(x - 12, y - 57, WindDegToDirection(angle), CENTER);
drawString(x + 3, y + 50, String(angle, 0) + "°", CENTER);
setFont(OpenSans24B);
drawString(x + 3, y - 16, String(windspeed, 1), CENTER);
setFont(OpenSans12);
drawString(x -20, y + 22, (Units == "M" ? "m/s" : "mph"), LEFT);
}
String WindDegToDirection(float winddirection) {
if (winddirection >= 348.75 || winddirection < 11.25) return TXT_N;
if (winddirection >= 11.25 && winddirection < 33.75) return TXT_NNE;
if (winddirection >= 33.75 && winddirection < 56.25) return TXT_NE;
if (winddirection >= 56.25 && winddirection < 78.75) return TXT_ENE;
if (winddirection >= 78.75 && winddirection < 101.25) return TXT_E;
if (winddirection >= 101.25 && winddirection < 123.75) return TXT_ESE;
if (winddirection >= 123.75 && winddirection < 146.25) return TXT_SE;
if (winddirection >= 146.25 && winddirection < 168.75) return TXT_SSE;
if (winddirection >= 168.75 && winddirection < 191.25) return TXT_S;
if (winddirection >= 191.25 && winddirection < 213.75) return TXT_SSW;
if (winddirection >= 213.75 && winddirection < 236.25) return TXT_SW;
if (winddirection >= 236.25 && winddirection < 258.75) return TXT_WSW;
if (winddirection >= 258.75 && winddirection < 281.25) return TXT_W;
if (winddirection >= 281.25 && winddirection < 303.75) return TXT_WNW;
if (winddirection >= 303.75 && winddirection < 326.25) return TXT_NW;
if (winddirection >= 326.25 && winddirection < 348.75) return TXT_NNW;
return "?";
}
void DisplayTemperatureSection(int x, int y, int twidth, int tdepth)
{
//setFont(OpenSans24/*24b*/);
setFont(OpenSans24B);
drawString(x, y, String(WxConditions[0].Temperature, 1) + "°C", CENTER); // Show current Temperature
setFont(OpenSans16);
drawString(x, y + 40, String(WxConditions[0].High, 0) + "° | " + String(WxConditions[0].Low, 0) + "°", CENTER); // Show forecast high and Low
}
void DisplayForecastTextSection(int x, int y, int fwidth, int fdepth)
{
String Wx_Description;
setFont(OpenSans16);
if (Language == "DE")
Wx_Description = WxConditions[0].Forecast0;
else {
Wx_Description = WxConditions[0].Main0;
if (WxConditions[0].Forecast0 != "") Wx_Description += " (" + WxConditions[0].Forecast0;
}
if (WxConditions[0].Forecast1 != "") Wx_Description += ", " + WxConditions[0].Forecast1;
if (WxConditions[0].Forecast2 != "") Wx_Description += ", " + WxConditions[0].Forecast2;
if (Wx_Description.indexOf("(") > 0) Wx_Description += ")";
int MsgWidth = 43; // Using proportional fonts, so be aware of making it too wide!
if (Language == "DE") drawStringMaxWidth(x + 30, y + 40, MsgWidth, Wx_Description, LEFT); // Leave German text in original format, 28 character screen width at this font size
else drawStringMaxWidth(x + 30, y + 40, MsgWidth, TitleCase(Wx_Description), LEFT); // 28 character screen width at this font size
}
void DisplayPressureSection(int x, int y, int pwidth, int pdepth, float pressure, String slope)
{
pressure = pressure * 0.750062; //convert to mmhg
setFont(OpenSans12/*24b*/);
drawString(x, y, String(pressure, (Units == "M" ? 0 : 1)) + (Units == "M" ? "mm" : "in"), LEFT);
}
void DisplayForecastWeather(int x, int y, int index) {
int fwidth = 103;
x = x + fwidth * (index - 1);
DisplayConditionsSection(x + fwidth / 2, y + 90, WxForecast[index].Icon, SmallIcon);
setFont(OpenSans8B);
drawString(x + fwidth / 2 - 10, y + 30, String(WxForecast[index].Period.substring(11, 16)), CENTER);
drawString(x + fwidth / 2 + 0, y + 130, String(WxForecast[index].High, 0) + "°/" + String(WxForecast[index].Low, 0) + "°", CENTER);
}
void DisplayPrecipitationSection(int x, int y, int pwidth, int pdepth) {
setFont(OpenSans12);
if (WxForecast[1].Rainfall >= 0.005) { // Ignore small amounts
drawString(x, y + 80, String(WxForecast[1].Rainfall, 2) + (Units == "M" ? "mm" : "in"), LEFT); // Only display rainfall total today if > 0
addraindrop(x + 102, y + 84, 7);
}
if (WxForecast[1].Snowfall >= 0.005) {// Ignore small amounts
drawString(x, y + 110, String(WxForecast[1].Snowfall, 2) + (Units == "M" ? "mm" : "in") + " **", LEFT); // Only display snowfall total today if > 0
}
}
void DisplayAstronomySection(int x, int y) {
setFont(OpenSans12B);
drawString(x + 14, y + 34, ConvertUnixTime(WxConditions[0].Sunrise).substring(0, 5) + " " + TXT_SUNRISE, LEFT);
drawString(x + 14, y + 64, ConvertUnixTime(WxConditions[0].Sunset).substring(0, 5) + " " + TXT_SUNSET, LEFT);
time_t now = time(NULL);
struct tm * now_utc = gmtime(&now);
const int day_utc = now_utc->tm_mday;
const int month_utc = now_utc->tm_mon + 1;
const int year_utc = now_utc->tm_year + 1900;
//drawString(x + 14, y + 94, MoonPhase(day_utc, month_utc, year_utc, Hemisphere), LEFT);
DrawMoon(x + 155, y - 20, day_utc, month_utc, year_utc, Hemisphere);
}
void DrawMoon(int x, int y, int dd, int mm, int yy, String hemisphere) {
const int diameter = 75;
double Phase = NormalizedMoonPhase(dd, mm, yy);
hemisphere.toLowerCase();
if (hemisphere == "south") Phase = 1 - Phase;
// Draw dark part of moon
fillCircle(x + diameter - 1, y + diameter, diameter / 2 + 1, GxEPD_BLACK);
const int number_of_lines = 90;
for (double Ypos = 0; Ypos <= number_of_lines / 2; Ypos++) {
double Xpos = sqrt(number_of_lines / 2 * number_of_lines / 2 - Ypos * Ypos);
// Determine the edges of the lighted part of the moon
double Rpos = 2 * Xpos;
double Xpos1, Xpos2;
if (Phase < 0.5) {
Xpos1 = -Xpos;
Xpos2 = Rpos - 2 * Phase * Rpos - Xpos;
}
else {
Xpos1 = Xpos;
Xpos2 = Xpos - 2 * Phase * Rpos + Rpos;
}
// Draw light part of moon
double pW1x = (Xpos1 + number_of_lines) / number_of_lines * diameter + x;
double pW1y = (number_of_lines - Ypos) / number_of_lines * diameter + y;
double pW2x = (Xpos2 + number_of_lines) / number_of_lines * diameter + x;
double pW2y = (number_of_lines - Ypos) / number_of_lines * diameter + y;
double pW3x = (Xpos1 + number_of_lines) / number_of_lines * diameter + x;
double pW3y = (Ypos + number_of_lines) / number_of_lines * diameter + y;
double pW4x = (Xpos2 + number_of_lines) / number_of_lines * diameter + x;
double pW4y = (Ypos + number_of_lines) / number_of_lines * diameter + y;
drawLine(pW1x, pW1y, pW2x, pW2y, GxEPD_WHITE);
drawLine(pW3x, pW3y, pW4x, pW4y, GxEPD_WHITE);
}
drawCircle(x + diameter - 1, y + diameter, diameter / 2, GxEPD_BLACK);
}
String MoonPhase(int d, int m, int y, String hemisphere) {
int c, e;
double jd;
int b;
if (m < 3) {
y--;
m += 12;
}
++m;
c = 365.25 * y;
e = 30.6 * m;
jd = c + e + d - 694039.09; /* jd is total days elapsed */
jd /= 29.53059; /* divide by the moon cycle (29.53 days) */
b = jd; /* int(jd) -> b, take integer part of jd */
jd -= b; /* subtract integer part to leave fractional part of original jd */
b = jd * 8 + 0.5; /* scale fraction from 0-8 and round by adding 0.5 */
b = b & 7; /* 0 and 8 are the same phase so modulo 8 for 0 */
if (hemisphere == "south") b = 7 - b;
if (b == 0) return TXT_MOON_NEW; // New; 0% illuminated
if (b == 1) return TXT_MOON_WAXING_CRESCENT; // Waxing crescent; 25% illuminated
if (b == 2) return TXT_MOON_FIRST_QUARTER; // First quarter; 50% illuminated
if (b == 3) return TXT_MOON_WAXING_GIBBOUS; // Waxing gibbous; 75% illuminated
if (b == 4) return TXT_MOON_FULL; // Full; 100% illuminated
if (b == 5) return TXT_MOON_WANING_GIBBOUS; // Waning gibbous; 75% illuminated
if (b == 6) return TXT_MOON_THIRD_QUARTER; // Third quarter; 50% illuminated
if (b == 7) return TXT_MOON_WANING_CRESCENT; // Waning crescent; 25% illuminated
return "";
}
/*
void DisplayForecastSection(int x, int y) {
setFont(OpenSans12);
int f = 1;
do {
DisplayForecastWeather(x, y, f);
f++;
} while (f < max_readings);
// Pre-load temporary arrays with with data - because C parses by reference
int r = 1;
do {
if (Units == "I") pressure_readings[r] = WxForecast[r].Pressure * 0.02953; else pressure_readings[r] = WxForecast[r].Pressure;
if (Units == "I") rain_readings[r] = WxForecast[r].Rainfall * 0.0393701; else rain_readings[r] = WxForecast[r].Rainfall;
if (Units == "I") snow_readings[r] = WxForecast[r].Snowfall * 0.0393701; else snow_readings[r] = WxForecast[r].Snowfall;
temperature_readings[r] = WxForecast[r].Temperature;
humidity_readings[r] = WxForecast[r].Humidity;
r++;
} while (r <= max_readings);
int gwidth = 230, gheight = 150;
int gx = (SCREEN_WIDTH - gwidth * 4) / 5 + 7;
int gy = (SCREEN_HEIGHT - gheight - 50);
int gap = gwidth + gx;
//setFont(OpenSans12);
//drawString(SCREEN_WIDTH / 2 - 50, gy - 50, TXT_FORECAST_VALUES, CENTER); // Based on a graph height of 60
//setFont(OpenSans12);
// (x,y,width,height,MinValue, MaxValue, Title, Data Array, AutoScale, ChartMode)
//DrawGraph(gx + 0 * gap, gy, gwidth, gheight, 900, 1050, Units == "M" ? TXT_PRESSURE_HPA : TXT_PRESSURE_IN, pressure_readings, max_readings, autoscale_on, barchart_off);
DrawGraph(gx + 0 * gap, gy, gwidth, gheight, 10, 30, Units == "M" ? TXT_TEMPERATURE_C : TXT_TEMPERATURE_F, temperature_readings, max_readings, autoscale_on, barchart_off);
//DrawGraph(gx + 1 * gap, gy, gwidth, gheight, 0, 100, TXT_HUMIDITY_PERCENT, humidity_readings, max_readings, autoscale_off, barchart_off);
if (SumOfPrecip(rain_readings, max_readings) >= SumOfPrecip(snow_readings, max_readings))
DrawGraph(gx + 1 * gap + 5, gy, gwidth, gheight, 0, 30, Units == "M" ? TXT_RAINFALL_MM : TXT_RAINFALL_IN, rain_readings, max_readings, autoscale_on, barchart_on);
else DrawGraph(gx + 1 * gap + 5, gy, gwidth, gheight, 0, 30, Units == "M" ? TXT_SNOWFALL_MM : TXT_SNOWFALL_IN, snow_readings, max_readings, autoscale_on, barchart_on);
}
*/
void DisplayForecastSection(int x, int y)
{
setFont(OpenSans12/*9*/);
int f = 1;
do {
DisplayForecastWeather(x, y, f);
f++;
} while (f < max_readings);
return;
// Pre-load temporary arrays with with data - because C parses by reference
int r = 1;
do {
if (Units == "I") pressure_readings[r] = WxForecast[r].Pressure * 0.02953; else pressure_readings[r] = WxForecast[r].Pressure;
if (Units == "I") rain_readings[r] = WxForecast[r].Rainfall * 0.0393701; else rain_readings[r] = WxForecast[r].Rainfall;
if (Units == "I") snow_readings[r] = WxForecast[r].Snowfall * 0.0393701; else snow_readings[r] = WxForecast[r].Snowfall;
temperature_readings[r] = WxForecast[r].Temperature;
humidity_readings[r] = WxForecast[r].Humidity;
r++;
} while (r <= max_readings);
int gwidth = 270, gheight = 150;
int gy = (SCREEN_HEIGHT - gheight - 50);
DrawGraph(40, gy, gwidth, gheight, 10, 30, Units == "M" ? TXT_TEMPERATURE_C : TXT_TEMPERATURE_F, temperature_readings, max_readings, autoscale_on, barchart_off);
if (SumOfPrecip(rain_readings, max_readings) >= SumOfPrecip(snow_readings, max_readings)) {
DrawGraph(gwidth + 110, gy, gwidth, gheight, 0, 30, Units == "M" ? TXT_RAINFALL_MM : TXT_RAINFALL_IN, rain_readings, max_readings, autoscale_on, barchart_on);
} else {
DrawGraph(gwidth + 110, gy, gwidth, gheight, 0, 30, Units == "M" ? TXT_SNOWFALL_MM : TXT_SNOWFALL_IN, snow_readings, max_readings, autoscale_on, barchart_on);
}
}
void DisplayConditionsSection(int x, int y, String IconName, bool IconSize) {
Serial.println("Icon name: " + IconName);
if (IconName == "01d" || IconName == "01n") Sunny(x, y, IconSize, IconName);
else if (IconName == "02d" || IconName == "02n") MostlySunny(x, y, IconSize, IconName);
else if (IconName == "03d" || IconName == "03n") Cloudy(x, y, IconSize, IconName);
else if (IconName == "04d" || IconName == "04n") MostlySunny(x, y, IconSize, IconName);
else if (IconName == "09d" || IconName == "09n") ChanceRain(x, y, IconSize, IconName);
else if (IconName == "10d" || IconName == "10n") Rain(x, y, IconSize, IconName);
else if (IconName == "11d" || IconName == "11n") Tstorms(x, y, IconSize, IconName);
else if (IconName == "13d" || IconName == "13n") Snow(x, y, IconSize, IconName);
else if (IconName == "50d") Haze(x, y, IconSize, IconName);
else if (IconName == "50n") Fog(x, y, IconSize, IconName);
else Nodata(x, y, IconSize, IconName);
if (IconSize == LargeIcon) {
setFont(OpenSans16B/*18*/);
drawString(x + 360, y - 74, String(WxConditions[0].Humidity, 0) + "%", CENTER);
if (WxConditions[0].Visibility > 0) Visibility(x - 100, y + 130, String(WxConditions[0].Visibility) + "M");
if (WxConditions[0].Cloudcover > 0) CloudCover(x + 60, y + 130, WxConditions[0].Cloudcover);
}
}
void arrow(int x, int y, int asize, float aangle, int pwidth, int plength) {
float dx = (asize - 10) * cos((aangle - 90) * PI / 180) + x; // calculate X position
float dy = (asize - 10) * sin((aangle - 90) * PI / 180) + y; // calculate Y position
float x1 = 0; float y1 = plength;
float x2 = pwidth / 2; float y2 = pwidth / 2;
float x3 = -pwidth / 2; float y3 = pwidth / 2;
float angle = aangle * PI / 180 - 135;
float xx1 = x1 * cos(angle) - y1 * sin(angle) + dx;
float yy1 = y1 * cos(angle) + x1 * sin(angle) + dy;
float xx2 = x2 * cos(angle) - y2 * sin(angle) + dx;
float yy2 = y2 * cos(angle) + x2 * sin(angle) + dy;
float xx3 = x3 * cos(angle) - y3 * sin(angle) + dx;
float yy3 = y3 * cos(angle) + x3 * sin(angle) + dy;
fillTriangle(xx1, yy1, xx3, yy3, xx2, yy2, GxEPD_BLACK);
}
void DisplayStatusSection(int x, int y, int rssi)
{
setFont(OpenSans8B);
DrawRSSI(x, y + 4, rssi);
DrawBattery(x + 120, y + 4);
}
void DrawRSSI(int x, int y, int rssi)
{
int WIFIsignal = 0;
int xpos = 1;
for (int _rssi = -100; _rssi <= rssi; _rssi = _rssi + 20) {
if (_rssi <= -20) WIFIsignal = 30; // <-20dbm displays 5-bars
if (_rssi <= -40) WIFIsignal = 24; // -40dbm to -21dbm displays 4-bars
if (_rssi <= -60) WIFIsignal = 18; // -60dbm to -41dbm displays 3-bars
if (_rssi <= -80) WIFIsignal = 12; // -80dbm to -61dbm displays 2-bars
if (_rssi <= -100) WIFIsignal = 6; // -100dbm to -81dbm displays 1-bar
fillRect(x + xpos * 8, y - WIFIsignal, 7, WIFIsignal, GxEPD_BLACK);
xpos++;
}
//fillRect(x, y - 1, 5, 1, GxEPD_BLACK);
//drawString(x + 6, y + 10, String(rssi) + "dBm", CENTER);
}
boolean UpdateLocalTime() {
struct tm timeinfo;
char time_output[30], day_output[30], update_time[30];
while (!getLocalTime(&timeinfo, 5000)) { // Wait for 5-sec for time to synchronise
Serial.println("Failed to obtain time");
return false;
}
CurrentHour = timeinfo.tm_hour;
CurrentMin = timeinfo.tm_min;
CurrentSec = timeinfo.tm_sec;
//See http://www.cplusplus.com/reference/ctime/strftime/
Serial.println("***Current time***");
Serial.println(&timeinfo, "%a %b %d %Y %H:%M:%S"); // Displays: Saturday, June 24 2017 14:05:49
if (Units == "M") {
if ((Language == "CZ") || (Language == "DE") || (Language == "NL") || (Language == "PL")) {
sprintf(day_output, "%s, %02u. %s %04u", weekday_D[timeinfo.tm_wday], timeinfo.tm_mday, month_M[timeinfo.tm_mon], (timeinfo.tm_year) + 1900); // day_output >> So., 23. Juni 2019 <<
}
else
{
sprintf(day_output, "%s, %02u %s %04u", weekday_D[timeinfo.tm_wday], timeinfo.tm_mday, month_M[timeinfo.tm_mon], (timeinfo.tm_year) + 1900);
}
strftime(update_time, sizeof(update_time), "%H:%M:%S", &timeinfo); // Creates: '@ 14:05:49' and change from 30 to 8 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
sprintf(time_output, "%s", update_time);
}
else
{
strftime(day_output, sizeof(day_output), "%a %b-%d-%Y", &timeinfo); // Creates 'Sat May-31-2019'
strftime(update_time, sizeof(update_time), "%r", &timeinfo); // Creates: '@ 02:05:49pm'
sprintf(time_output, "%s", update_time);
}
Date_str = day_output;
Time_str = time_output;
return true;
}
void DrawBattery(int x, int y)
{
// When reading the battery voltage, POWER_EN must be turned on
epd_poweron();
delay(50);
Serial.println(epd_ambient_temperature());
uint8_t percentage = 100;
float voltage = ((double_t)analogRead(BATT_PIN) / 4095.0) * 2.0 * 3.3 * (vref / 1000.0);
Serial.println("Voltage = " + String(voltage));
percentage = 2836.9625 * pow(voltage, 4) - 43987.4889 * pow(voltage, 3) + 255233.8134 * pow(voltage, 2) - 656689.7123 * voltage + 632041.7303;
if (voltage >= 4.20) percentage = 100;
if (voltage <= 3.20) percentage = 0; // orig 3.5
drawRect(x + 25, y - 20, 40, 15, GxEPD_BLACK);
fillRect(x + 65, y - 16, 4, 6, GxEPD_BLACK);
fillRect(x + 27, y - 18, 36 * percentage / 100.0, 11, GxEPD_BLACK);
setFont(OpenSans8B/*9*/);
drawString(x + 5, y - 15, String(percentage) + "%", RIGHT);
drawString(x + 110, y - 15, String(voltage, 2) + "v", CENTER);
epd_poweroff();
delay(50);
}
// Symbols are drawn on a relative 10x10grid and 1 scale unit = 1 drawing unit
void addcloud(int x, int y, int scale, int linesize) {
//Draw cloud outer
fillCircle(x - scale * 3, y, scale, GxEPD_BLACK); // Left most circle
fillCircle(x + scale * 3, y, scale, GxEPD_BLACK); // Right most circle
fillCircle(x - scale, y - scale, scale * 1.4, GxEPD_BLACK); // left middle upper circle
fillCircle(x + scale * 1.5, y - scale * 1.3, scale * 1.75, GxEPD_BLACK); // Right middle upper circle
fillRect(x - scale * 3 - 1, y - scale, scale * 6, scale * 2 + 1, GxEPD_BLACK); // Upper and lower lines
//Clear cloud inner
fillCircle(x - scale * 3, y, scale - linesize, GxEPD_WHITE); // Clear left most circle
fillCircle(x + scale * 3, y, scale - linesize, GxEPD_WHITE); // Clear right most circle
fillCircle(x - scale, y - scale, scale * 1.4 - linesize, GxEPD_WHITE); // left middle upper circle
fillCircle(x + scale * 1.5, y - scale * 1.3, scale * 1.75 - linesize, GxEPD_WHITE); // Right middle upper circle
fillRect(x - scale * 3 + 2, y - scale + linesize - 1, scale * 5.9, scale * 2 - linesize * 2 + 2, GxEPD_WHITE); // Upper and lower lines
}
void addraindrop(int x, int y, int scale) {
fillCircle(x, y, scale / 2, GxEPD_BLACK);
fillTriangle(x - scale / 2, y, x, y - scale * 1.2, x + scale / 2, y , GxEPD_BLACK);
x = x + scale * 1.6; y = y + scale / 3;
fillCircle(x, y, scale / 2, GxEPD_BLACK);
fillTriangle(x - scale / 2, y, x, y - scale * 1.2, x + scale / 2, y , GxEPD_BLACK);
}
void addrain(int x, int y, int scale, bool IconSize) {
if (IconSize == SmallIcon) scale *= 1.34;
for (int d = 0; d < 4; d++) {
addraindrop(x + scale * (7.8 - d * 1.95) - scale * 5.2, y + scale * 2.1 - scale / 6, scale / 1.6);