-
Notifications
You must be signed in to change notification settings - Fork 0
/
epdWeatherClockV1.ino
1416 lines (1270 loc) · 51.5 KB
/
epdWeatherClockV1.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
// Weather icon credits: https://github.com/SeBassTian23/ESP32-WeatherDisplay/tree/master
// base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
// enable or disable GxEPD2_GFX base class
#define ENABLE_GxEPD2_GFX 0
#include <GxEPD2_3C.h>
#include <Fonts/FreeMonoBold9pt7b.h>
#include <U8g2_for_Adafruit_GFX.h>
#include <Wire.h> // Used to establish serial communication on the I2C bus
#include <SparkFun_TMP117.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include "RTClib.h"
#include "image.h" //for sleep icon
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include <BH1750.h>
#include <TimeLib.h>
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include <Preferences.h>
Preferences pref;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>Wi-Fi Manager</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
font-family: Arial, Helvetica, sans-serif;
display: inline-block;
text-align: center;
}
h1 {
font-size: 1.8rem;
color: white;
}
p {
font-size: 1.4rem;
}
.topnav {
overflow: hidden;
background-color: #0A1128;
}
body {
margin: 0;
}
.content {
padding: 5%;
}
.card-grid {
max-width: 800px;
margin: 0 auto;
display: grid;
grid-gap: 2rem;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}
.card {
background-color: white;
box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5);
}
.card-title {
font-size: 1.2rem;
font-weight: bold;
color: #034078
}
input[type=submit] {
border: none;
color: #FEFCFB;
background-color: #034078;
padding: 15px 15px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
width: 100px;
margin-right: 10px;
border-radius: 4px;
transition-duration: 0.4s;
}
input[type=submit]:hover {
background-color: #1282A2;
}
input[type=text], input[type=number], select {
width: 50%;
padding: 12px 20px;
margin: 18px;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
label {
font-size: 1.2rem;
}
.value{
font-size: 1.2rem;
color: #1282A2;
}
.state {
font-size: 1.2rem;
color: #1282A2;
}
button {
border: none;
color: #FEFCFB;
padding: 15px 32px;
text-align: center;
font-size: 16px;
width: 100px;
border-radius: 4px;
transition-duration: 0.4s;
}
.button-on {
background-color: #034078;
}
.button-on:hover {
background-color: #1282A2;
}
.button-off {
background-color: #858585;
}
.button-off:hover {
background-color: #252524;
}
</style>
</head>
<body>
<div class="topnav">
<h1>Weather Station Wi-Fi Manager</h1>
</div>
<div class="content">
<div class="card-grid">
<div class="card">
<form action="/" method="POST">
<p>
<label for="ssid">SSID</label>
<input type="text" id ="ssid" name="ssid"><br>
<label for="pass">Password</label>
<input type="text" id ="pass" name="pass"><br>
<input type ="submit" value ="Submit">
</p>
</form>
</div>
</div>
</div>
</body>
</html>
)rawliteral";
// Search for parameter in HTTP POST request
const char* PARAM_INPUT_1 = "ssid";
const char* PARAM_INPUT_2 = "pass";
//your wifi name and password
String ssid;
String password;
// openWeatherMap Api Key from your profile in account section
String openWeatherMapApiKey="";
// Replace with your lat and lon
String lat = "22.5895515";
String lon = "88.2876455";
RTC_DS3231 rtc; //Initalize rtc
TMP117 sensor; // Initalize temperature sensor
Adafruit_BME680 bme; // Initalize environmental sensor
BH1750 lightMeter(0x23); //Initalize light sensor
GxEPD2_3C<GxEPD2_420c_Z21, GxEPD2_420c_Z21::HEIGHT> display(GxEPD2_420c_Z21(/*CS=5*/ SS, /*DC=*/3, /*RST=*/4, /*BUSY=*/5)); //400x300, UC8276
U8G2_FOR_ADAFRUIT_GFX u8g2Fonts;
//#define SEALEVELPRESSURE_HPA (1013.25)
#define BATPIN A0 //battery voltage divider connection pin (1M Ohm with 104 Capacitor)
#define DEBUG_PIN 21
#define BATTERY_LEVEL_SAMPLING 32
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
int TIME_TO_SLEEP = 900;
//battery related settings
#define battChangeThreshold 0.15
#define battUpperLim 4.19
#define battHigh 4.2
#define battLow 2.9
int nightFlag = 0; //preserves data in rtc memory from deep sleep loss
float battLevel;
bool DEBUG_MODE = false, BATTERY_CRITICAL = false;
String jsonBuffer;
//for storing highest temp and lowest temp of the day
float hTemp, lTemp;
char daysOfTheWeek[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
char monthName[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int httpResponseCode;
//takes samples based on BATTERY_LEVEL_SAMPLING, averages them and returns actual battery voltage
float batteryLevel() {
uint32_t Vbatt = 0;
for (int i = 0; i < BATTERY_LEVEL_SAMPLING; i++) {
Vbatt = Vbatt + analogReadMilliVolts(BATPIN); // ADC with correction
}
float Vbattf = 2 * Vbatt / BATTERY_LEVEL_SAMPLING / 1000.0; // attenuation ratio 1/2, mV --> V
//Serial.println(Vbattf);
return (Vbattf);
}
//forward declaration
void tempPrint(byte offset = 0);
void setup() {
Serial.begin(115200);
Serial.println("Setup");
pinMode(BATPIN, INPUT);
pinMode(DEBUG_PIN, INPUT);
/*if (digitalRead(DEBUG_PIN) == 1)
DEBUG_MODE = true;*/
Wire.begin();
Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode)
display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
u8g2Fonts.begin(display); // connect u8g2 procedures to Adafruit GFX
pref.begin("database", false);
//preferences.end();
BATTERY_CRITICAL = pref.isKey("battCrit");
if (!BATTERY_CRITICAL)
pref.putBool("battCrit", "");
BATTERY_CRITICAL = pref.getBool("battCrit", false);
bool tempBATTERY_CRITICAL = BATTERY_CRITICAL;
bool checkFlag = pref.isKey("nightFlag");
if (!checkFlag) { //create key:value pair
pref.putBool("nightFlag", "");
}
nightFlag = pref.getBool("nightFlag", false);
if (!BATTERY_CRITICAL) {
bool wifiConfigExist = pref.isKey("ssid");
if (!wifiConfigExist) { //create key:value pairs
pref.putString("ssid", "");
pref.putString("password", "");
}
ssid = pref.getString("ssid", "");
password = pref.getString("password", "");
if (ssid == "" || password == "") {
Serial.println("No values saved for ssid or password");
// Connect to Wi-Fi network with SSID and password
Serial.println("Setting AP (Access Point)");
// NULL sets an open Access Point
WiFi.softAP("WCLOCK-WIFI-MANAGER", "79756622761");
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
debugPrinter("Connect to 'WCLOCK-WIFI-MANAGER' \nfrom your phone or computer (Wifi).\nUse password 79756622761.\nThen go to " + IP.toString() + "\nfrom your browser.");
// Web Server Root URL
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
request->send(200, "text/html", index_html);
});
server.on("/", HTTP_POST, [](AsyncWebServerRequest* request) {
int params = request->params();
for (int i = 0; i < params; i++) {
const AsyncWebParameter* p = request->getParam(i);
if (p->isPost()) {
// HTTP POST ssid value
if (p->name() == PARAM_INPUT_1) {
ssid = p->value();
Serial.print("SSID set to: ");
Serial.println(ssid);
pref.putString("ssid", ssid);
}
// HTTP POST pass value
if (p->name() == PARAM_INPUT_2) {
password = p->value();
Serial.print("Password set to: ");
Serial.println(password);
pref.putString("password", password);
}
//Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(200, "text/html", "<h2>Done. Weather Station will now restart</h2>");
delay(3000);
ESP.restart();
});
server.begin();
while (true)
;
}
}
if (lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE)) {
Serial.println(F("BH1750 Advanced begin"));
} else {
Serial.println(F("Error initialising BH1750"));
errMsg("Error BH1750");
while (1)
; // Runs forever
}
float lux = 0;
while (!lightMeter.measurementReady(true)) {
yield();
}
lux = lightMeter.readLightLevel();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
errMsg("Error RTC");
while (1)
; // Runs forever
}
Serial.println("RTC Ready");
DateTime now = rtc.now();
if ((now.hour() == 0) && (now.minute() >= 0 && now.minute() < 15)) { //reset high low at midnight
pref.putFloat("hTemp", 0.0);
pref.putFloat("lTemp", 60.0);
}
if (lux != 0 || DEBUG_MODE == true) {
if (sensor.begin() == true) // Function to check if the sensor will correctly self-identify with the proper Device ID/Address
{
Serial.println("Lux Begin");
} else {
Serial.println("Device failed to setup- Freezing code.");
errMsg("Error TMP117");
while (1)
; // Runs forever
}
if (!bme.begin()) {
Serial.println(F("Could not find a valid BME680 sensor, check wiring!"));
errMsg("Error BME680");
while (1)
; // Runs forever
}
Serial.println("BME Ready");
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_2X);
bme.setHumidityOversampling(BME680_OS_16X);
bme.setPressureOversampling(BME680_OS_16X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_7);
bme.setGasHeater(0, 0); // 0*C for 0 ms
if (!BATTERY_CRITICAL) {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed");
break;
}
bool apiConfigExist = pref.isKey("api");
if (!apiConfigExist) //create key:value pairs
pref.putString("api", openWeatherMapApiKey);
openWeatherMapApiKey = pref.getString("api", "");
} else {
//wifioff cpu speed reduced
WiFi.disconnect(true); // Disconnect from the network
WiFi.mode(WIFI_OFF); // Switch WiFi off
setCpuFrequencyMhz(40);
}
hTemp = pref.getFloat("hTemp", -1.0);
lTemp = pref.getFloat("lTemp", -1.0);
battLevel = pref.getFloat("battLevel", -1.0);
if (hTemp == -1.0 || lTemp == -1.0 || battLevel == -1.0) {
Serial.println("No values saved for hTemp, lTemp or battLevel");
pref.putFloat("hTemp", 0.0);
pref.putFloat("lTemp", 60.0);
pref.putFloat("battLevel", 4.2);
}
}
float hTempHold = hTemp, lTempHold = lTemp, tempBattLevel = battLevel;
bool tempNightFlag = nightFlag;
Serial.println("Setup done");
if (DEBUG_MODE) {
errMsg("DEBUG MODE");
} else {
if (lux == 0) {
TIME_TO_SLEEP = 300; //5 min wake period while darkness sleeping
if (nightFlag == 0) { //prevents unnecessary redrawing of same thing
nightFlag = 1;
display.setRotation(0);
display.setFullWindow();
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
display.drawInvertedBitmap(0, 0, nightMode, 400, 300, GxEPD_BLACK);
} while (display.nextPage());
}
display.hibernate();
display.powerOff();
} else {
nightFlag = 0;
display.setRotation(0);
display.setFullWindow();
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE);
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Time And Weather");
tempPrint();
openWeatherPrint();
Serial.println("Time And Weather Done");
} else {
Serial.println("Time");
display.drawBitmap(270, 0, wifiOff, 12, 12, GxEPD_BLACK);
tempPrint(40);
Serial.println("Time Done");
}
} while (display.nextPage());
display.hibernate();
display.powerOff();
}
Serial.println("Data Write");
if (lux != 0) {
if (hTempHold != hTemp)
pref.putFloat("hTemp", hTemp);
if (lTempHold != lTemp)
pref.putFloat("lTemp", lTemp);
if (tempBattLevel != battLevel)
pref.putFloat("battLevel", battLevel);
if (tempBATTERY_CRITICAL != BATTERY_CRITICAL)
pref.putBool("battCrit", BATTERY_CRITICAL);
}
if (tempNightFlag != nightFlag)
pref.putBool("nightFlag", nightFlag);
Serial.println("Data Write Done");
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP / 60) + " Mins");
//Go to sleep now
Serial.println("Going to sleep now");
Serial.flush();
delay(100);
esp_deep_sleep_start();
}
}
void loop() {}
//Handles the httpresponse and returns the data as json payload
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
/*tempPrint function prints internal sensor based readings. If offset is provided then it shifts in y axis.
Provide offset when wifi is not connected and online weather data cannot be printed.*/
void tempPrint(byte offset) {
uint16_t bg = GxEPD_WHITE;
uint16_t fg = GxEPD_BLACK;
u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
u8g2Fonts.setFontDirection(0); // left to right (this is default)
u8g2Fonts.setForegroundColor(fg); // apply Adafruit GFX color
u8g2Fonts.setBackgroundColor(bg); // apply Adafruit GFX color
// select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
float tempC;
if (sensor.dataReady() == true) // Function to make sure that there is data ready to be printed, only prints temperature values when data is ready
{
tempC = sensor.readTempC();
Serial.println(); // Create a white space for easier viewing
Serial.print("Temperature in Celsius: ");
Serial.println(tempC);
if (tempC > hTemp)
hTemp = tempC;
if (tempC < lTemp)
lTemp = tempC;
}
float newBattLevel = batteryLevel();
if (newBattLevel < battLevel) //to maintain steady decrease in battery level
battLevel = newBattLevel;
if (((newBattLevel - battLevel) >= battChangeThreshold) || newBattLevel > battUpperLim) //to update the battery level in case of charging
battLevel = newBattLevel;
u8g2Fonts.setFont(u8g2_font_luRS08_tf);
u8g2Fonts.setCursor(28, 11);
u8g2Fonts.print(battLevel, 2);
u8g2Fonts.print("V");
int percent = ((battLevel - battLow) / (battHigh - battLow)) * 100; //range is battHigh - 100% and battLow - 0%
BATTERY_CRITICAL = false;
if (percent < 1) {
BATTERY_CRITICAL = true;
percent = 0; //for battry icon
} else if (percent > 100)
percent = 100;
u8g2Fonts.setCursor(63, 11);
if (!BATTERY_CRITICAL) {
u8g2Fonts.print(percent, 1);
u8g2Fonts.print("%");
} else
u8g2Fonts.print("BATTERY CRITICAL, WIFI TURNED OFF");
iconBattery(percent);
DateTime now = rtc.now();
u8g2Fonts.setFont(u8g2_font_luRS08_tf);
u8g2Fonts.setCursor(295, 11);
u8g2Fonts.print("Last Update: ");
u8g2Fonts.print(now.hour() < 10 ? "0" + String(now.hour()) : now.hour());
u8g2Fonts.print(now.minute() < 10 ? ":0" + String(now.minute()) : ":" + String(now.minute()));
u8g2Fonts.setFont(u8g2_font_logisoso20_tf);
u8g2Fonts.setCursor(10, 75 + offset);
u8g2Fonts.print(now.day() < 10 ? "0" + String(now.day()) : now.day());
u8g2Fonts.print(", ");
u8g2Fonts.print(monthName[now.month() - 1]);
u8g2Fonts.setCursor(10, 105 + offset);
u8g2Fonts.print(daysOfTheWeek[now.dayOfTheWeek()]);
u8g2Fonts.setFont(u8g2_font_inb19_mf);
u8g2Fonts.setCursor(320, 60 + offset); // start writing at this position
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_logisoso58_tf);
u8g2Fonts.setCursor(150, 110 + offset); // start writing at this position
u8g2Fonts.print(String(tempC));
u8g2Fonts.setFont(u8g2_font_logisoso58_tf);
u8g2Fonts.setCursor(330, 110 + offset);
u8g2Fonts.print(String("C"));
if (!BATTERY_CRITICAL) {
display.drawLine(0, 121 + offset, 400, 121 + offset, GxEPD_RED);
display.drawLine(0, 122 + offset, 400, 122 + offset, GxEPD_RED);
display.drawLine(0, 154 + offset, 400, 154 + offset, GxEPD_RED);
display.drawLine(0, 155 + offset, 400, 155 + offset, GxEPD_RED);
} else {
display.drawLine(0, 121 + offset, 400, 121 + offset, GxEPD_BLACK);
display.drawLine(0, 122 + offset, 400, 122 + offset, GxEPD_BLACK);
display.drawLine(0, 154 + offset, 400, 154 + offset, GxEPD_BLACK);
display.drawLine(0, 155 + offset, 400, 155 + offset, GxEPD_BLACK);
}
unsigned long endTime = bme.beginReading();
if (endTime == 0) {
Serial.println(F("Failed to begin reading :("));
return;
}
if (!bme.endReading()) {
Serial.println(F("Failed to complete reading :("));
return;
}
u8g2Fonts.setFont(u8g2_font_logisoso20_tf);
u8g2Fonts.setCursor(2, 150 + offset); // start writing at this position
u8g2Fonts.print(bme.humidity);
//u8g2Fonts.setCursor(67, 200);
u8g2Fonts.print(String("%"));
u8g2Fonts.setCursor(264, 150 + offset); // start writing at this position
u8g2Fonts.print(bme.pressure / 100.0);
//u8g2Fonts.setCursor(186, 200);
u8g2Fonts.print(String("hPa"));
u8g2Fonts.setFont(u8g2_font_logisoso16_tf);
u8g2Fonts.setCursor(85, 148 + offset); // start writing at this position
u8g2Fonts.print("H:");
//u8g2Fonts.setCursor(30, 250); // start writing at this position
u8g2Fonts.print(hTemp);
u8g2Fonts.setFont(u8g2_font_fub11_tf);
u8g2Fonts.setCursor(148, 138 + offset); // start writing at this position
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_logisoso16_tf);
u8g2Fonts.setCursor(158, 148 + offset); // start writing at this position
u8g2Fonts.print("C");
u8g2Fonts.setCursor(180, 148 + offset); // start writing at this position
u8g2Fonts.print("L:");
u8g2Fonts.setFont(u8g2_font_logisoso16_tf);
//u8g2Fonts.setCursor(30, 280); // start writing at this position
u8g2Fonts.print(lTemp);
u8g2Fonts.setFont(u8g2_font_fub11_tf);
u8g2Fonts.setCursor(242, 138 + offset); // start writing at this position
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_logisoso16_tf);
u8g2Fonts.setCursor(252, 148 + offset); // start writing at this position
u8g2Fonts.print("C");
}
//works only if wifi is connected. Prints data from openweather api.
void openWeatherPrint() {
String serverPath = "http://api.openweathermap.org/data/3.0/onecall?lat=" + lat + "&lon=" + lon + "&exclude=hourly,minutely&units=metric&appid=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
if (httpResponseCode == -1 || httpResponseCode == -11)
ESP.restart();
Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
ESP.restart();
return;
}
/*Serial.print("JSON object = ");
Serial.println(myObject);
Serial.print("Temperature: ");
Serial.println(myObject["current"]["temp"]);
Serial.print("Pressure: ");
Serial.println(myObject["current"]["pressure"]);
Serial.print("Humidity: ");
Serial.println(myObject["current"]["humidity"]);*/
if (myObject["current"]["temp"] == null) {
networkInfo();
} else {
wifiStatus();
u8g2Fonts.setFont(u8g2_font_helvB10_tf);
u8g2Fonts.setCursor(29, 170);
u8g2Fonts.print("OUTDOOR");
u8g2Fonts.setFont(u8g2_font_fub20_tf); //u8g2_font_fub30_tf
uint16_t width;
width = u8g2Fonts.getUTF8Width(JSON.stringify(myObject["current"]["temp"]).c_str());
u8g2Fonts.setCursor(20, 200); // start writing at this position
u8g2Fonts.print(myObject["current"]["temp"]);
u8g2Fonts.setCursor(30 + width, 200);
u8g2Fonts.print("C");
u8g2Fonts.setFont(u8g2_font_fub11_tf);
u8g2Fonts.setCursor(22 + width, 185); // start writing at this position
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_fur11_tf); //u8g2_font_fur14_tf
width = u8g2Fonts.getUTF8Width(("Real Feel:" + JSON.stringify(myObject["current"]["feels_like"])).c_str());
u8g2Fonts.setCursor(5, 220); // start writing at this position
u8g2Fonts.print("Real Feel:");
u8g2Fonts.setCursor(75, 220);
u8g2Fonts.print(myObject["current"]["feels_like"]);
u8g2Fonts.setCursor(width + 16, 220);
u8g2Fonts.print(String("C"));
u8g2Fonts.setFont(u8g2_font_baby_tf); //u8g2_font_robot_de_niro_tf
u8g2Fonts.setCursor(13 + width, 211); // start writing at this position
u8g2Fonts.print("o");
u8g2Fonts.setFont(u8g2_font_fur14_tf);
u8g2Fonts.setCursor(5, 245); // start writing at this position
u8g2Fonts.print(myObject["current"]["humidity"]);
u8g2Fonts.print(String("%"));
u8g2Fonts.setCursor(5, 270); // start writing at this position
u8g2Fonts.print(myObject["current"]["pressure"]);
u8g2Fonts.print(String("hPa"));
u8g2Fonts.setFont(u8g2_font_helvB10_tf);
u8g2Fonts.setCursor(5, 294); // start writing at this position
u8g2Fonts.print("UVI: ");
u8g2Fonts.print(myObject["current"]["uvi"]);
u8g2Fonts.setFont(u8g2_font_fur11_tf);
double uv = double(myObject["current"]["uvi"]);
if (uv < 2)
u8g2Fonts.print(" Low");
else if (uv < 5)
u8g2Fonts.print(" Medium");
else if (uv <= 7)
u8g2Fonts.print(" High");
else if (uv > 7)
u8g2Fonts.print(" Danger");
display.drawLine(136, 155, 136, 299, GxEPD_RED);
display.drawLine(137, 155, 137, 299, GxEPD_RED);
//Sunset sunrise print
time_t t = strtoll(JSON.stringify(myObject["current"]["sunrise"]).c_str(), nullptr, 10);
setTime(t);
adjustTime(19800);
iconSunRise(152, 170, true);
u8g2Fonts.setCursor(166, 175); // start writing at this position
u8g2Fonts.print("0");
u8g2Fonts.print(hour());
u8g2Fonts.print(":");
u8g2Fonts.print(minute() < 10 ? "0" + String(minute()) : minute());
t = strtoll(JSON.stringify(myObject["current"]["sunset"]).c_str(), nullptr, 10);
setTime(t);
adjustTime(19800);
iconSunRise(267, 170, false);
u8g2Fonts.setCursor(281, 175);
u8g2Fonts.print(hour());
u8g2Fonts.print(":");
u8g2Fonts.print(minute() < 10 ? "0" + String(minute()) : minute());
display.drawLine(320, 155, 320, 299, GxEPD_RED);
display.drawLine(321, 155, 321, 299, GxEPD_RED);
display.drawLine(320, 230, 400, 230, GxEPD_RED);
display.drawLine(320, 231, 400, 231, GxEPD_RED);
iconMoonPhase(360, 260, 20, double(myObject["daily"][0]["moon_phase"]));
u8g2Fonts.setFont(u8g2_font_luRS08_tf);
u8g2Fonts.setCursor(330, 297);
u8g2Fonts.print("Moon Phase");
String s = JSON.stringify(myObject["current"]["weather"][0]["icon"]);
int lastIndex = s.length() - 1;
s.remove(lastIndex);
s.remove(0, 1);
if (s == "01d") { //Clear Day
iconSun(361, 189, 15);
//iconSleet(x,y,r);//iconHail(x,y,r);//same
//iconWind(x,y,r);
//iconTornado(x,y,r);
} else if (s == "01n") //Clear Night
iconMoon(361, 189, 15);
else if (s == "02d") //few clouds
iconCloudyDay(330, 160, 60);
else if (s == "02n")
iconCloudyNight(330, 160, 60);
else if (s == "03d") //scattered clouds
iconCloud(361, 189, 15);
else if (s == "03n")
iconCloud(361, 189, 15);
else if (s == "04d") //broken clouds (two clouds)
iconCloudy(330, 160, 60);
else if (s == "04n")
iconCloudy(330, 160, 60);
else if (s == "09d") //shower rain
iconSleet(330, 160, 60);
else if (s == "09n")
iconSleet(330, 160, 60);
else if (s == "10d") //snow
iconRain(330, 160, 60);
else if (s == "10n")
iconRain(330, 160, 60);
else if (s == "11d") //thunderstorm
iconThunderstorm(330, 160, 60);
else if (s == "11n")
iconThunderstorm(330, 160, 60);
else if (s == "13d") //snow
iconSnow(330, 160, 60);
else if (s == "13n")
iconSnow(330, 160, 60);
else if (s == "50d") //mist
iconFog(330, 160, 60);
else if (s == "50n")
iconFog(330, 160, 60);
u8g2Fonts.setFont(u8g2_font_luRS08_tf); //u8g2_font_fur11_tf
s = JSON.stringify(myObject["current"]["weather"][0]["main"]);
lastIndex = s.length() - 1;
s.remove(lastIndex);
s.remove(0, 1);
u8g2Fonts.setCursor(330, 227);
u8g2Fonts.print(s);
//u8g2Fonts.setCursor(186, 200);
s = JSON.stringify(myObject["alerts"][0]["event"]);
lastIndex = s.length() - 1;
s.remove(lastIndex);
s.remove(0, 1);
if (s != "ul") {
int16_t tbx, tby;
uint16_t tbw, tbh;
display.getTextBounds("Alerts: " + s, 0, 0, &tbx, &tby, &tbw, &tbh);
// center the bounding box by transposition of the origin:
uint16_t x = ((display.width() - tbw) / 2) - tbx;
u8g2Fonts.setCursor(x, 25); // start writing at this position
u8g2Fonts.print("Alerts: ");
u8g2Fonts.print(s);
}
}
}
//Separate the icons in future update to separate file
void iconCloud(uint16_t x, uint16_t y, uint16_t r) {
// top circle
display.fillCircle(x, y, r, GxEPD_BLACK);
// left circle
display.fillCircle(x - r * 0.85, y + r * 0.8, r * 0.85, GxEPD_BLACK);
// right circle
display.fillCircle(x + r * 1.1, y + r * 0.8, r * 0.85, GxEPD_BLACK);
// rectangle
display.fillRect(x - r * 0.85, y + r * 0.8, (x + r * 1.1) - (x - r * 0.85), r * 0.9, GxEPD_BLACK);
// top circle
float offset = 0.8;
display.fillCircle(x, y, r * offset, GxEPD_WHITE);
// left circle
display.fillCircle(x - r * 0.85, y + r * 0.8, r * 0.85 * offset, GxEPD_WHITE);
// right circle
display.fillCircle(x + r * 1.1, y + r * 0.8, r * 0.85 * offset, GxEPD_WHITE);
// rectangle
display.fillRect(x - r * 0.85, y + r * 0.7, (x + r * 1.1) - (x - r * 0.85), r * offset, GxEPD_WHITE);
}
void iconSun(uint16_t x, uint16_t y, uint16_t r) {
display.drawLine(x - r * 1.75, y, x + r * 1.75, y, GxEPD_BLACK);
display.drawLine(x, y - r * 1.75, x, y + r * 1.75, GxEPD_BLACK);
display.drawLine(x - r * 1.25, y - r * 1.25, x + r * 1.25, y + r * 1.25, GxEPD_BLACK);
display.drawLine(x - r * 1.25, y + r * 1.25, x + r * 1.25, y - r * 1.25, GxEPD_BLACK);
display.fillCircle(x, y, r * 1.2, GxEPD_WHITE);
display.fillCircle(x, y, r, GxEPD_BLACK);
float offset = 0.9;
display.fillCircle(x, y, r * offset, GxEPD_RED);
}
void iconMoon(uint16_t x, uint16_t y, uint16_t r) {
float offset = 0.9;
display.fillCircle(x, y, r, GxEPD_BLACK);
display.fillCircle(x, y, r * offset, GxEPD_RED);
display.fillCircle(x + r, y - r, r, GxEPD_BLACK);
display.fillCircle(x + r, y - r, r * offset, GxEPD_WHITE);
display.fillRect(x, y - r * 2, r * 2.5, r, GxEPD_WHITE);
display.fillRect(x + r + 1, y - r, r * 1.5, r * 1.5, GxEPD_WHITE);
}
void iconClearDay(uint16_t x, uint16_t y, uint16_t s) {
iconSun(x + s / 2, y + s / 2, s / 5);
}
void iconClearNight(uint16_t x, uint16_t y, uint16_t s) {
iconMoon(x + s / 2, y + s / 2, s / 5);
}
void iconRain(uint16_t x, uint16_t y, uint16_t s) {
iconCloud(x + s / 2.2, y + s / 2.5, s / 5);
display.fillRect(x + s * 0.275, y + s * 0.6, s / 2.5, s / 5, GxEPD_WHITE);
float offset = 0.8;
for (int i = 0; i <= s * 0.1; i++) {
display.fillCircle(x + s * 0.4 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s * 0.6 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
}
for (int i = 0; i <= s * 0.16; i++) {
display.fillCircle(x + s * 0.5 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
}
}
void iconSleet(uint16_t x, uint16_t y, uint16_t s) {
iconCloud(x + s / 2.2, y + s / 2.5, s / 5);
display.fillRect(x + s * 0.275, y + s * 0.6, s / 2.5, s / 5, GxEPD_WHITE);
float offset = 0.8;
for (int i = 0; i <= s * 0.1; i++) {
if (i < 1 || i > s * 0.1 * 0.5) {
display.fillCircle(x + s * 0.4 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s * 0.6 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
}
}
for (int i = 0; i <= s * 0.16; i++) {
if (i < s * 0.16 * 0.5 || i > s * 0.16 * 0.8)
display.fillCircle(x + s * 0.5 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
}
}
void iconSnow(uint16_t x, uint16_t y, uint16_t s) {
iconCloud(x + s / 2.2, y + s / 2.5, s / 5);
display.fillRect(x + s * 0.275, y + s * 0.6, s / 2.5, s / 5, GxEPD_WHITE);
float offset = 0.8;
display.fillCircle(x + s / 2.75, y + s * 0.7, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s / 1.75, y + s * 0.7, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s / 2.75, y + s * 0.8, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s / 1.75, y + s * 0.8, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s / 2.15, y + s * 0.65, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s / 2.15, y + s * 0.75, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s / 2.15, y + s * 0.85, s * 0.02, GxEPD_BLACK);
}
void iconWind(uint16_t x, uint16_t y, uint16_t s) {
float offset = 0.8;
for (int i = 0; i <= s * 0.7; i++) {
if (i < s * 0.6)
display.fillCircle(x + s * 0.15 + i, y + s * 0.4, s * 0.02, GxEPD_BLACK);
if (i < s * 0.5)
display.fillCircle(x + s * 0.1 + i, y + s * 0.5, s * 0.02, GxEPD_BLACK);
if (i < s * 0.2)
display.fillCircle(x + s * 0.7 + i, y + s * 0.5, s * 0.02, GxEPD_BLACK);
if (i < s * 0.6)
display.fillCircle(x + s * 0.2 + i, y + s * 0.6, s * 0.02, GxEPD_BLACK);
}
}
void iconFog(uint16_t x, uint16_t y, uint16_t s) {
iconCloud(x + s / 2.2, y + s / 2.5, s / 5);
display.fillRect(x + s * 0.1, y + s * 0.55, s * 0.75, s / 5, GxEPD_WHITE);
float offset = 0.8;
for (int i = 0; i <= s * 0.7; i++) {
display.fillCircle(x + s * 0.1 + i, y + s * 0.6, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s * 0.2 + i, y + s * 0.7, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s * 0.15 + i, y + s * 0.8, s * 0.02, GxEPD_BLACK);
}
}
void iconCloudy(uint16_t x, uint16_t y, uint16_t s) {
iconCloud(x + (s / 4) * 3, y + s / 4, s / 10);
iconCloud(x + s / 2.1, y + s / 2.2, s / 5);
}
void iconCloudyDay(uint16_t x, uint16_t y, uint16_t s) {
iconSun(x + (s / 3) * 2, y + s / 2.5, s / 6);
iconCloud(x + s / 2.2, y + s / 2.2, s / 5);
}
void iconCloudyNight(uint16_t x, uint16_t y, uint16_t s) {
iconMoon(x + (s / 3) * 2, y + s / 3, s / 6);
iconCloud(x + s / 2.2, y + s / 2.2, s / 5);
}
void iconHail(uint16_t x, uint16_t y, uint16_t s) {
iconCloud(x + s / 2.2, y + s / 2.5, s / 5);
display.fillRect(x + s * 0.275, y + s * 0.6, s / 2.5, s / 5, GxEPD_WHITE);
float offset = 0.8;
for (int i = 0; i <= s * 0.1; i++) {
if (i < s * 0.1 * 0.5 || i == s * 0.1) {
display.fillCircle(x + s * 0.4 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
display.fillCircle(x + s * 0.6 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
}
}
for (int i = 0; i <= s * 0.16; i++) {
if (i < s * 0.16 * 0.7 || i == s * 0.16)
display.fillCircle(x + s * 0.5 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
}
}
void iconThunderstorm(uint16_t x, uint16_t y, uint16_t s) {
iconCloud(x + s / 2.2, y + s / 2.5, s / 5);
display.fillRect(x + s * 0.275, y + s * 0.6, s / 2.5, s / 5, GxEPD_WHITE);
float offset = 0.8;
for (int i = 0; i <= s * 0.1; i++) {
display.fillCircle(x + s * 0.6 - i * 0.5, y + s * 0.65 + i, s * 0.02, GxEPD_BLACK);
}