-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherFlags.ino
956 lines (733 loc) · 26.2 KB
/
WeatherFlags.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
#include <ESP32Servo.h>
#include <ArduinoJson.h>
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <HTTPClient.h>
#include <TimeLib.h>
#include "OWM_key.h"
#include "WiFi_cred.h"
// How many slots does the forecast return to us? From the docs.
int N_forecasts = 48;
int N_yesterday = 24;
// The open weather map API call
String OWM_url = "http://api.openweathermap.org/data/2.5/onecall?lat=53.383&lon=-1.4659&exclude=daily,alerts,minutely&appid=";
String OWM_past_url = "http://api.openweathermap.org/data/2.5/onecall/timemachine?lat=53.383&lon=-1.4659&dt=%d&appid=%s";
// WiFi and connectivity stuff
int status = WL_IDLE_STATUS;
WiFiClient client;
WiFiUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets, for the NTP time sync
// Time getter stuff
const int timeZone = 0;
static const char ntpServerName[] = "time.nist.gov";
time_t getNtpTime();
void sendNTPpacket(IPAddress &address);
int daylight_savings = 0;
// This stores the weather info. We can loop over it later.
String json_buffer;
DynamicJsonDocument forecast_doc(26000);
DynamicJsonDocument todays_history_doc(12288);
DynamicJsonDocument yesterdays_history_doc(12288);
JsonArray forecast;
JsonArray todays_history;
JsonArray yesterdays_history;
// Servo stuff
int lowangle = 0;
int highangle = 180;
Servo servo_firepit;
int pin_firepit = 12;
Servo servo_umbrella;
int pin_umbrella = 14;
Servo servo_picnic;
int pin_picnic = 15;
Servo servo_hike;
int pin_hike = 13;
Servo servo_storm;
int pin_storm = 2;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
// while (!Serial);
Serial.println("\n\nBegun");
// Add the API key to the OWM url tail
OWM_url = OWM_url + API_KEY;
// Set up servos
Serial.print("Setting all servos to lowangle, or ");
Serial.println(lowangle);
servo_firepit.attach(pin_firepit);
servo_firepit.write(lowangle);
servo_umbrella.attach(pin_umbrella);
servo_umbrella.write(lowangle);
servo_picnic.attach(pin_picnic);
servo_picnic.write(lowangle);
servo_hike.attach(pin_hike);
servo_hike.write(lowangle);
servo_storm.attach(pin_storm);
servo_storm.write(lowangle);
// Demonstrate the servos are working correctly
set_flag(servo_firepit, true);
set_flag(servo_umbrella, true);
set_flag(servo_picnic, true);
set_flag(servo_hike, true);
set_flag(servo_storm, true);
set_flag(servo_firepit, false);
set_flag(servo_umbrella, false);
set_flag(servo_picnic, false);
set_flag(servo_hike, false);
set_flag(servo_storm, false);
connect_wifi();
setSyncProvider(getNtpTime);
setSyncInterval(300);
}
void loop() {
connect_wifi();
getWeather();
getPastWeather();
tmElements_t tm;
breakTime(now(), tm);
Serial.print("Current day is ");
Serial.println(tm.Day);
// And then process all my flags.
bool condition1 = evalFirepit();
Serial.print("The firepit flag is ");
if (condition1) {Serial.println("Raised");} else {Serial.println("Lowered");}
Serial.println("Setting firepit angle");
set_flag(servo_firepit, condition1);
bool condition2 = evalUmbrella();
Serial.print("The umbrella flag is ");
if (condition2) {Serial.println("Raised");} else {Serial.println("Lowered");}
Serial.println("Setting umbrella angle");
set_flag(servo_umbrella, condition2);
bool condition3 = evalPicnic();
Serial.print("The picnic flag is ");
if (condition3) {Serial.println("Raised");} else {Serial.println("Lowered");}
Serial.println("Setting picnic angle");
set_flag(servo_picnic, condition3);
bool condition4 = evalStorm();
Serial.print("The storm flag is ");
if (condition4) {Serial.println("Raised");} else {Serial.println("Lowered");}
Serial.println("Setting storm angle");
set_flag(servo_storm, condition4);
bool condition5 = evalHike();
Serial.print("The hike flag is ");
if (condition5) {Serial.println("Raised");} else {Serial.println("Lowered");}
Serial.println("Setting hike angle");
set_flag(servo_hike, condition5);
delay(600000);
}
void connect_wifi() {
// attempt to connect to Wifi network, if I'm not already.
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
if (status == WL_CONNECTED) {
Serial.println("Connected to wifi");
} else {
//wait, then retry
delay(10000);
}
}
}
void set_flag(Servo &flag_servo, bool target_position) {
// Get the angle I want to be at
int target_angle;
Serial.print("target_position is ");
Serial.println(target_position);
if (target_position) {
target_angle = highangle;
} else {
target_angle = lowangle;
}
// Retrieve the servos current position
int current_angle = flag_servo.read();
// Move by 1 degree every 20ms
if (current_angle < target_angle) {
Serial.println("Raising flag");
while (current_angle < target_angle) {
current_angle++;
flag_servo.write(current_angle);
delay(20);
}
} else {
Serial.println("Lowering flag");
while (current_angle > target_angle) {
current_angle--;
flag_servo.write(current_angle);
delay(20);
}
}
Serial.println("");
}
bool evalFirepit() {
// above 10 degrees from 5pm - 12pm, with no precipitation.
Serial.println("\n\nChecking for firepit flag");
bool is_OK = true;
bool any_valid_times = false;
bool is_valid_time;
// I just need this temporary variable
int dt;
// Only examine data in this time range
int currDay = day();
int lowtime = 10 - daylight_savings;
int hightime = 23 - daylight_savings;
Serial.print("lowtime: ");
Serial.println(lowtime);
Serial.print("hightime: ");
Serial.println(hightime);
Serial.print("Current day is ");
Serial.println(currDay);
// Temperature must be HIGHER than this. Units are kelvin.
int minTemp = 273 + 10;
Serial.print("Temperature threshold is ");
Serial.println(minTemp);
Serial.println("\n");
int clear_skies = 700;
// Check the future
for (int i=0; i<N_forecasts; i++) {
JsonObject this_forecast = forecast[i];
JsonObject thisWeather = this_forecast["weather"][0];
dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "Looking at the forecast for the %d day of the month, hour %d", tm.Day, tm.Hour);
Serial.println(buf);
// Check that we're in the correct window
is_valid_time = ((tm.Hour > lowtime) and (tm.Hour < hightime) and (tm.Day == currDay));
Serial.print("Is this a valid time? -> ");
Serial.println(is_valid_time);
if (is_valid_time) {
// Check if the temperature is high enough to be comfy
int forecastTemp = this_forecast["feels_like"];
int temp_OK = (forecastTemp > minTemp);
Serial.print("For timestamp ");
Serial.print(dt);
Serial.println(" is the weather good?");
Serial.print("Temperature feels like ");
Serial.println(forecastTemp);
Serial.print("Is this ok? ");
if (temp_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
int weatherCode = thisWeather["id"];
bool weather_OK = (weatherCode > clear_skies);
Serial.print("The weather code is ");
Serial.println(weatherCode);
Serial.print("Is this ok? ");
if (weather_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
is_OK = (temp_OK and weather_OK);
Serial.println("---------------------------------------");
}
}
Serial.println("\n\nNow checking history of today...\n\n");
// Check the past for decent weather
for (int i=0; i<N_yesterday; i++) {
JsonObject this_forecast = todays_history[i];
JsonObject thisWeather = this_forecast["weather"][0];
dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "Looking at the forecast for day %d, hour %d", tm.Day, tm.Hour);
Serial.println(buf);
// Check that we're in the correct window
is_valid_time = ((tm.Hour > lowtime) and (tm.Hour < hightime) and (tm.Day == currDay));
Serial.print("Is this a valid time? ");
Serial.print(" -> ");
Serial.println(is_valid_time);
if (is_valid_time) {
// Check if the temperature is high enough to be comfy
int forecastTemp = this_forecast["feels_like"];
int temp_OK = (forecastTemp > minTemp);
Serial.print("For timestamp ");
Serial.print(dt);
Serial.println(" is the weather good?");
Serial.print("Temperature feels like ");
Serial.println(forecastTemp);
Serial.print("Is this ok? ");
if (temp_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
int weatherCode = thisWeather["id"];
bool weather_OK = (weatherCode > clear_skies);
Serial.print("The weather code is ");
Serial.println(weatherCode);
Serial.print("Is this ok? ");
if (weather_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
is_OK = (temp_OK and weather_OK);
Serial.println("---------------------------------------");
}
}
return true;
}
bool evalUmbrella() {
// if there's rain between 7am and 11am, or between 3pm and 7pm.
Serial.println("\n\nChecking for umbrella flag");
bool is_OK = true;
bool any_valid_times = false;
bool is_valid_time;
// I just need this temporary variable
int dt;
// Only examine data in this time range
int currDay = day();
int lowtime1 = 7 - daylight_savings;
int hightime1 = 11 - daylight_savings;
int lowtime2 = 15 - daylight_savings;
int hightime2 = 19 - daylight_savings;
Serial.print("lowtime1: ");
Serial.println(lowtime1);
Serial.print("hightime1: ");
Serial.println(hightime1);
Serial.print("lowtime2: ");
Serial.println(lowtime2);
Serial.print("hightime2: ");
Serial.println(hightime2);
Serial.print("Current day is ");
Serial.println(currDay);
int clear_skies = 600;
// Check the future
for (int i=0; i<N_forecasts; i++) {
JsonObject this_forecast = forecast[i];
JsonObject thisWeather = this_forecast["weather"][0];
dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "Looking at the forecast for the %d day of the month, hour %d", tm.Day, tm.Hour);
Serial.println(buf);
// Check that we're in the correct window
is_valid_time = (
// (tm.Day == currDay) and
(tm.Hour > lowtime1) and (tm.Hour < hightime1) or
(tm.Hour > lowtime2) and (tm.Hour < hightime2)
);
Serial.print("Is this a valid time? -> ");
Serial.println(is_valid_time);
if (is_valid_time) {
int weatherCode = thisWeather["id"];
bool weather_OK = (weatherCode < clear_skies);
Serial.print("The weather code is ");
Serial.println(weatherCode);
Serial.print("Is this raining? ");
if (weather_OK) {
Serial.println("YES");
return true;
} else {
Serial.println("NO");
}
}
Serial.println("---------------------------------------");
}
return false;
}
bool evalPicnic() {
// Sunny on weekdays 4pm-6pm, or weekends all day (10am - 6pm
Serial.println("\n\nChecking for picnic flag");
bool is_OK = true;
bool any_valid_times = false;
bool is_valid_time;
// I just need this temporary variable
int dt;
// Only examine data in this time range
int curr_weekday = weekday();
int curr_day = day();
// Set the time limits
int lowtime;
int hightime;
if ((curr_weekday == 1) or (curr_weekday == 7)) {
// It's a weekend! Check for sum all day (10am - 6pm)
Serial.println("It's a weekend!");
lowtime = 12 - daylight_savings;
hightime = 18 - daylight_savings;
} else {
// It's a weekday
Serial.println("It's a weekday!");
lowtime = 16 - daylight_savings;
hightime = 20 - daylight_savings;
}
// Temperature must be HIGHER than this. Units are kelvin.
int minTemp = 273 + 18;
int clear_skies = 700;
Serial.print("Temperature threshold is ");
Serial.println(minTemp);
Serial.println("\n");
// Check the future
for (int i=0; i<N_forecasts; i++) {
JsonObject this_forecast = forecast[i];
JsonObject this_weather = this_forecast["weather"][0];
dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "Looking at the forecast for the %d day of the month, hour %d", tm.Day, tm.Hour);
Serial.println(buf);
// Check that we're in the correct window
is_valid_time = ((tm.Hour >= lowtime) and (tm.Hour <= hightime) and (tm.Day == curr_day));
if (is_valid_time) {
// Check if the temperature is high enough to be comfy
int forecastTemp = this_forecast["feels_like"];
int temp_OK = (forecastTemp > minTemp);
Serial.print("For timestamp ");
Serial.print(dt);
Serial.println(" is the weather good?");
Serial.print("Temperature feels like ");
Serial.println(forecastTemp);
Serial.print("Is this ok? ");
if (temp_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
int weather_code = this_weather["id"];
bool weather_OK = (weather_code >= clear_skies);
Serial.print("The weather code is ");
Serial.println(weather_code);
Serial.print("Is this ok? ");
if (weather_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
is_OK = (temp_OK and weather_OK);
Serial.println("---------------------------------------");
}
}
Serial.println("\n\nNow checking history of today...\n\n");
// Check the past for decent weather
for (int i=0; i<N_yesterday; i++) {
JsonObject this_forecast = todays_history[i];
JsonObject this_weather = this_forecast["weather"][0];
dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "Looking at the forecast for day %d, hour %d", tm.Day, tm.Hour);
Serial.println(buf);
// Check that we're in the correct window
is_valid_time = ((tm.Hour >= lowtime) and (tm.Hour <= hightime) and (tm.Day == curr_day));
if (is_valid_time) {
// Check if the temperature is high enough to be comfy
int forecastTemp = this_forecast["feels_like"];
int temp_OK = (forecastTemp > minTemp);
Serial.print("For timestamp ");
Serial.print(dt);
Serial.println(" is the weather good?");
Serial.print("Temperature feels like ");
Serial.println(forecastTemp);
Serial.print("Is this ok? ");
if (temp_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
int weather_code = this_weather["id"];
bool weather_OK = (weather_code >= 700);
Serial.print("The weather code is ");
Serial.println(weather_code);
Serial.print("Is this ok? ");
if (weather_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
is_OK = (temp_OK and weather_OK);
Serial.println("---------------------------------------");
}
}
return true;
}
bool evalStorm() {
// Check if there's gonna be a thunderstorm today, at any time in the future
// Weather codes less than this are thunderstorms
int storm_code = 300;
for (int i=0; i<N_forecasts; i++) {
JsonObject this_forecast = forecast[i];
JsonObject this_weather = this_forecast["weather"][0];
int weather_code = this_weather["id"];
// Some reporting
int dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "The weather code is %d at day %d and hour %d", weather_code, tm.Day, tm.Hour);
Serial.println(buf);
if (weather_code < storm_code) {
return true;
}
}
return false;
}
bool evalHike() {
// above 10 degrees from 5pm - 12pm, with no precipitation.
Serial.println("\n\nChecking for hiking flag");
bool is_OK = true;
bool any_valid_times = false;
bool is_valid_time;
// I just need this temporary variable
int dt;
// Only examine data in this time range
int currDay = day();
int lowtime = 10 - daylight_savings;
int hightime = 20 - daylight_savings;
Serial.print("lowtime: ");
Serial.println(lowtime);
Serial.print("hightime: ");
Serial.println(hightime);
Serial.print("Current day is ");
Serial.println(currDay);
int clear_skies = 600;
// Check the future
for (int i=0; i<N_forecasts; i++) {
JsonObject this_forecast = forecast[i];
JsonObject thisWeather = this_forecast["weather"][0];
dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "Looking at the forecast for the %d day of the month, hour %d", tm.Day, tm.Hour);
Serial.println(buf);
// Check that we're in the correct window
is_valid_time = ((tm.Hour > lowtime) and (tm.Hour < hightime) and (tm.Day == currDay));
Serial.print("Is this a valid time? -> ");
Serial.println(is_valid_time);
if (is_valid_time) {
// Check for rain
int weatherCode = thisWeather["id"];
bool weather_OK = (weatherCode > clear_skies);
Serial.print("The weather code is ");
Serial.println(weatherCode);
Serial.print("Is this ok? ");
if (weather_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
is_OK = weather_OK;
Serial.println("---------------------------------------");
}
}
Serial.println("\n\nNow checking history of today...\n\n");
// Check the past for decent weather
for (int i=0; i<N_yesterday; i++) {
JsonObject this_forecast = todays_history[i];
JsonObject thisWeather = this_forecast["weather"][0];
dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "Looking at the forecast for day %d, hour %d", tm.Day, tm.Hour);
Serial.println(buf);
// Check that we're in the correct window
is_valid_time = ((tm.Hour > lowtime) and (tm.Hour < hightime) and (tm.Day <= currDay));
Serial.print("Is this a valid time? ");
Serial.print(" -> ");
Serial.println(is_valid_time);
if (is_valid_time) {
int weatherCode = thisWeather["id"];
bool weather_OK = (weatherCode > clear_skies);
Serial.print("The weather code is ");
Serial.println(weatherCode);
Serial.print("Is this ok? ");
if (weather_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
is_OK = weather_OK;
Serial.println("---------------------------------------");
}
}
// Check the past for decent weather
for (int i=0; i<N_yesterday; i++) {
JsonObject this_forecast = yesterdays_history[i];
JsonObject thisWeather = this_forecast["weather"][0];
dt = this_forecast["dt"];
tmElements_t tm;
breakTime(dt, tm);
char buf[100];
sprintf(buf, "Looking at the forecast for day %d, hour %d", tm.Day, tm.Hour);
Serial.println(buf);
// Check that we're in the correct window
is_valid_time = ((tm.Hour > lowtime) and (tm.Hour < hightime) and (tm.Day <= currDay));
Serial.print("Is this a valid time? ");
Serial.print(" -> ");
Serial.println(is_valid_time);
if (is_valid_time) {
int weatherCode = thisWeather["id"];
bool weather_OK = (weatherCode > clear_skies);
Serial.print("The weather code is ");
Serial.println(weatherCode);
Serial.print("Is this ok? ");
if (weather_OK) {
Serial.println("YES");
} else {
Serial.println("NO");
return false;
}
is_OK = weather_OK;
Serial.println("---------------------------------------");
}
}
return true;
}
void getWeather() {
// Gets the weather forecast from OWM. Saves the hourly forecast to a global variable
// https://openweathermap.org/api/one-call-api
Serial.println("\nConnecting to OWM server...");
json_buffer = httpGETRequest(OWM_url.c_str());
DeserializationError error = deserializeJson(forecast_doc, json_buffer);
forecast = forecast_doc["hourly"];
int dt = forecast[0]["dt"];
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
} else {
Serial.println("Successfully got the weather!\n");
}
}
void getPastWeather() {
// Get the weather history from OWM. Get the hourly forecast starting 24 hours ago
Serial.println("\nGetting today's weather history from OWM server...");
// Rewind 24 hours and get the 48 hour forecast
int target_time = now();
char url_buffer[250];
sprintf(url_buffer, OWM_past_url.c_str(), target_time, API_KEY);
Serial.println("Getting past weather from url:");
Serial.println(url_buffer);
json_buffer = httpGETRequest(url_buffer);
DeserializationError error = deserializeJson(todays_history_doc, json_buffer);
todays_history = todays_history_doc["hourly"];
// OWM conveniently provides a daylight savings offset. Bonus, makes this code portable to other locations.
int timezone_offset = todays_history_doc["timezone_offset"];
Serial.print("Timeszone fooset is ");
Serial.println(timezone_offset);
daylight_savings = timezone_offset / 3600;
char buf[100];
sprintf(buf, "I will add %d hours to all my time constraints", daylight_savings);
Serial.println(buf);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
} else {
Serial.println("Successfully got the weather!\n");
}
Serial.println("\nGetting yesterday's weather history OWM server...");
// Rewind 24 hours and get the 24 hour forecast
target_time = now() - 1*24*60*60;
sprintf(url_buffer, OWM_past_url.c_str(), target_time, API_KEY);
Serial.println("Getting past weather from url:");
Serial.println(url_buffer);
json_buffer = httpGETRequest(url_buffer);
error = deserializeJson(yesterdays_history_doc, json_buffer);
yesterdays_history = yesterdays_history_doc["hourly"];
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
} else {
Serial.println("Successfully got the weather!\n");
}
}
String httpGETRequest(const char* serverName) {
//makes a GET request (obviously), and returns the payload response.
// If an error is hit, reports the error.
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int 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;
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
IPAddress ntpServerIP; // NTP server's ip address
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
// get a random server from the pool
WiFi.hostByName(ntpServerName, ntpServerIP);
Serial.print(ntpServerName);
Serial.print(": ");
Serial.println(ntpServerIP);
sendNTPpacket(ntpServerIP);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}