-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheink_weather.ino
724 lines (577 loc) · 21.9 KB
/
eink_weather.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
// eink_weather: this project uses an Inkplate 10 display and the
// Pirate Weather API to get a weather forecast for a given latitude
// and longitude and display it.
//
// Copyright (c) 2023 John Graham-Cumming
#include <Inkplate.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <driver/rtc_io.h>
#include <ArduinoJson.h>
#include "params.h"
#include "fonts/gfx/Roboto_Regular_7.h"
#include "fonts/gfx/Roboto_Regular_24.h"
#include "fonts/gfx/Roboto_Bold_12.h"
const GFXfont *fontSmall = &Roboto_Regular7pt8b;
const GFXfont *fontMedium = &Roboto_Bold12pt8b;
const GFXfont *fontLarge = &Roboto_Regular24pt8b;
// The default time when the RTC is not set is January 1, 2066 00:00
#define DEFAULT_EPOCH 3029529605
Inkplate ink(INKPLATE_3BIT);
// This structure and the array are used to blend the historical weather
// forecast with an up to date forecast. That way the hourly data shown
// for 48 hours is a mix of what was predicted at midnight today and what
// is predicted now.
#define ICON_SIZE 64
struct hour_slot {
uint32_t when;
float temperature;
char icon[ICON_SIZE];
};
struct hour_slot hours[48];
#define SECONDS_PER_HOUR (60*60)
#define SECONDS_PER_DAY (24*SECONDS_PER_HOUR)
// setup runs the entire program and then goes to sleep.
void setup() {
ink.begin();
// Default so that on first start up if WiFi doesn't connect then
// will try again in 60 seconds.
uint64_t sleep_time = 60;
if (connectWiFi(wifi_count, wifi_networks, wifi_passwords)) {
if (setRTC()) {
// Figure out which of the update_times[] array is the next minute
// past the hour at which the display should update.
uint32_t now = getRtcNow();
uint32_t next = now + sleep_time;
if (now < DEFAULT_EPOCH) {
uint32_t this_hour = now / SECONDS_PER_HOUR;
this_hour *= SECONDS_PER_HOUR;
for (int h = 0; h < 2; h++) {
for (int i = 0; i < UPDATE_TIMES; i++) {
next = this_hour + h * SECONDS_PER_HOUR + update_times[i] * 60;
if (next > now) {
h = 2;
break;
}
}
}
}
if (ink.sdCardInit() != 0) {
showWeather(now, next);
if (next > now) {
sleep_time = next - now;
}
} else {
fatal("SD card failed to initialize");
}
} else {
fatal("Failed to set RTC from NTP");
}
disconnectWiFi();
} else {
fatal("Failed to connect to WiFi ");
}
deepSleep(sleep_time);
}
// loop contains nothing because the entire sketch will be woken up
// in setup(), do work and then go to sleep again.
void loop() {}
// connectWiFi connects to the passed in WiFi network and returns true
// if successful.
bool connectWiFi(int count, const char **ssids, const char **passes) {
return ink.connectWiFiMulti(count, ssids, passes, 23, true);
}
// disconnectWiFi cleans up the result of connecting via connectWiFi
void disconnectWiFi() {
ink.disconnect();
}
// setRTC sets the RTC via NTP and returns true is successful
bool setRTC() {
// First 0 means no offset from GMT, second 0 means no daylight savings time.
configTime(0, 0, "pool.ntp.org", "time.nist.gov", "time.cloudflare.com");
int tries = 5;
while (tries > 0) {
delay(2000);
tries -= 1;
uint32_t fromntp = time(NULL);
// If time from NTP doesn't look like it's been set then wait
if (fromntp < 1681141968) {
continue;
}
Serial.print("Time from NTP: ");
Serial.println(fromntp);
ink.rtcSetEpoch(fromntp);
return true;
}
return false;
}
// gtcRtcNow returns the current epoch time from the RTC
uint32_t getRtcNow() {
ink.rtcGetRtcData();
return ink.rtcGetEpoch();
}
const char days[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// day converts a Unix epoch to the current day of the week taking into
// account the timezone offset in hours.
const char *day(uint32_t when, float offset) {
when += int(offset * SECONDS_PER_HOUR);
int dow = int(floor(when / SECONDS_PER_DAY) + 4) % 7;
return days[dow];
}
#define HHMM_SIZE 6
// hhmm converts a Unix epoch to hh:mm taking into account the timezone
// offset in hours. out just be at least HHMM_SIZE.
void hhmm(uint32_t when, float offset, char *out) {
when += int(offset * SECONDS_PER_HOUR);
int hh = int(floor(when / SECONDS_PER_HOUR)) % 24;
int mm = int(floor(when / 60)) % 60;
sprintf(out, "%02d:%02d", hh, mm);
}
// prHelper does the actual printing of text. It takes an (x, y) position
// of the text, the text and font. Plus two parameters wm and hm which
// are multipliers to apply to the width and height of the text being
// printed. If the width and height are w and h then this will calculate
// x + wm*w, y + hm*h.
void prHelper(int16_t x, int16_t y, const char *text, const GFXfont *f,
float wm = 0, float hm = 0) {
int16_t x1;
int16_t y1;
uint16_t w;
uint16_t h;
ink.setFont(f);
ink.setTextSize(1);
ink.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
ink.setCursor(x + w * wm, y + h * hm);
ink.print(text);
}
// pr writes text at (x, y) with font f.
void pr(int16_t x, int16_t y, const char *text, const GFXfont *f) {
prHelper(x, y, text, f);
}
// centre centres a piece of text at the x, y position.
void centre(int16_t x, int16_t y, const char *text, const GFXfont *f) {
prHelper(x, y, text, f, -0.5);
}
// right right-justifies text x, y position.
void right(int16_t x, int16_t y, const char *text, const GFXfont *f) {
prHelper(x, y, text, f, -1);
}
// flushRight right-justifies text at the y position with size s.
void flushRight(int16_t y, const char *text, const GFXfont *f) {
prHelper(ink.width() - 20, y, text, f, -1);
}
#define SMALL_IMAGE 43
#define LARGE_IMAGE 128
// centreIcon draws an icon cenetred at the x, y position. If the
// icon name is partly-cloudy-day and the size s is 43 then this
// will load partly-cloudy-day-42.png.
void centreIcon(int16_t x, int16_t y, const char *img, int16_t s) {
char tmp[100];
sprintf(tmp, "%s-%d.png", img, s);
// This assumes that icon are squares
ink.drawImage(tmp, x - s / 2, y - s / 2);
}
// drawRectangle draws a rectangle given the top left corner and
// width and height. We don't use the built in ink.drawRect because
// the lines are thinner than a one pixel line drawn by drawThickLine.
void drawRectangle(int16_t x0, int16_t y0, int16_t w, int16_t h) {
int16_t x1 = x0 + w;
int16_t y1 = y0 + h;
ink.drawThickLine(x0, y0, x1, y0, 0, 1);
ink.drawThickLine(x0, y1, x1, y1, 0, 1);
ink.drawThickLine(x0, y0, x0, y1, 0, 1);
ink.drawThickLine(x1, y0, x1, y1, 0, 1);
}
#define TEMP_SIZE 6
// roundTemp rounds a floating point temperature and write to a string
// that contains the temperature. out must be at least TEMP_SIZE.
void roundTemp(float t, char *out) {
// Temperatures are rounded up if above 0 and down if below 0.
// Example: 1.4C becomes 1C, 1.6C becomes 2C, -0.4C becomes 0C,
// -1.7C becomes -2C.
if (t >= 0) {
t += 0.5;
} else {
t -= 0.5;
}
sprintf(out, "%d", int(t));
}
// CA certificate for the domain being connected to using TLS. Obtained
// using the openssl s_client as follows:
//
// openssl s_client -showcerts -servername api.pirateweather.net \
// -connect api.pirateweather.net:443
//
// The certificate below is the last certificate output by openssl as it
// is the CA certificate of the domain above.
const char *cacert = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIEdTCCA12gAwIBAgIJAKcOSkw0grd/MA0GCSqGSIb3DQEBCwUAMGgxCzAJBgNV\n" \
"BAYTAlVTMSUwIwYDVQQKExxTdGFyZmllbGQgVGVjaG5vbG9naWVzLCBJbmMuMTIw\n" \
"MAYDVQQLEylTdGFyZmllbGQgQ2xhc3MgMiBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0\n" \
"eTAeFw0wOTA5MDIwMDAwMDBaFw0zNDA2MjgxNzM5MTZaMIGYMQswCQYDVQQGEwJV\n" \
"UzEQMA4GA1UECBMHQXJpem9uYTETMBEGA1UEBxMKU2NvdHRzZGFsZTElMCMGA1UE\n" \
"ChMcU3RhcmZpZWxkIFRlY2hub2xvZ2llcywgSW5jLjE7MDkGA1UEAxMyU3RhcmZp\n" \
"ZWxkIFNlcnZpY2VzIFJvb3QgQ2VydGlmaWNhdGUgQXV0aG9yaXR5IC0gRzIwggEi\n" \
"MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDVDDrEKvlO4vW+GZdfjohTsR8/\n" \
"y8+fIBNtKTrID30892t2OGPZNmCom15cAICyL1l/9of5JUOG52kbUpqQ4XHj2C0N\n" \
"Tm/2yEnZtvMaVq4rtnQU68/7JuMauh2WLmo7WJSJR1b/JaCTcFOD2oR0FMNnngRo\n" \
"Ot+OQFodSk7PQ5E751bWAHDLUu57fa4657wx+UX2wmDPE1kCK4DMNEffud6QZW0C\n" \
"zyyRpqbn3oUYSXxmTqM6bam17jQuug0DuDPfR+uxa40l2ZvOgdFFRjKWcIfeAg5J\n" \
"Q4W2bHO7ZOphQazJ1FTfhy/HIrImzJ9ZVGif/L4qL8RVHHVAYBeFAlU5i38FAgMB\n" \
"AAGjgfAwge0wDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAYYwHQYDVR0O\n" \
"BBYEFJxfAN+qAdcwKziIorhtSpzyEZGDMB8GA1UdIwQYMBaAFL9ft9HO3R+G9FtV\n" \
"rNzXEMIOqYjnME8GCCsGAQUFBwEBBEMwQTAcBggrBgEFBQcwAYYQaHR0cDovL28u\n" \
"c3MyLnVzLzAhBggrBgEFBQcwAoYVaHR0cDovL3guc3MyLnVzL3guY2VyMCYGA1Ud\n" \
"HwQfMB0wG6AZoBeGFWh0dHA6Ly9zLnNzMi51cy9yLmNybDARBgNVHSAECjAIMAYG\n" \
"BFUdIAAwDQYJKoZIhvcNAQELBQADggEBACMd44pXyn3pF3lM8R5V/cxTbj5HD9/G\n" \
"VfKyBDbtgB9TxF00KGu+x1X8Z+rLP3+QsjPNG1gQggL4+C/1E2DUBc7xgQjB3ad1\n" \
"l08YuW3e95ORCLp+QCztweq7dp4zBncdDQh/U90bZKuCJ/Fp1U1ervShw3WnWEQt\n" \
"8jxwmKy6abaVd38PMV4s/KCHOkdp8Hlf9BRUpJVeEXgSYCfOn8J3/yNTd126/+pZ\n" \
"59vPr5KW7ySaNRB6nJHGDn2Z9j8Z3/VyVOEVqQdZe4O/Ui5GjLIAZHYcSNPYeehu\n" \
"VsyuLAOQ1xk4meTKCRlb/weWsKh/NEnfVqn3sF/tM+2MR7cwA130A4w=\n" \
"-----END CERTIFICATE-----\n";
// callAPI calls the Pirate Weather API with a set of excluded sections (which
// must not be empty) and an epoch time for when the weather forecast should
// start. Either returns the HTTP body as a String or an empty string for an
// error. If when is zero then gets the current forecast.
String callAPI(char *exclude, uint32_t when) {
WiFiClientSecure tls;
tls.setCACert(cacert);
HTTPClient http;
// The API URL. A number of elements of the response can be filtered
// using exclude to minimize the size of the JSON response.
const char *api_format = \
"https://api.pirateweather.net/forecast/" \
"%s/%s,%s%s" \
"?units=%s" \
"&tz=precise" \
"&exclude=%s";
char api[232];
char whens[32] = "";
if (when != 0) {
sprintf(whens, ",%d", when);
}
sprintf(api, api_format, api_key, lat, lon, whens, units, exclude);
int tries = 5;
// Retry API calls at most five times with two seconds between
// retries.
int code;
while (tries > 0) {
tries -= 1;
if (http.begin(tls, api)) {
code = http.GET();
if (code == HTTP_CODE_OK) {
return http.getString();
}
}
delay(2000);
}
fatal("Pirate Weather API call failed " + http.errorToString(code));
return "";
}
// showWeather gets and displays the weather forecast on screen.
void showWeather(uint32_t update_time, uint32_t next) {
// Step 1.
//
// Since the hourly section of the Pirate Weather API returns the next
// 48 hours and we want today and tomorrow we need to ask for midnight
// today. This is done by retrieving the time now from the RTC. But there's
// a hitch: we want local time so... we first make an API call to the
// Pirate Weather API excluding all sections so we can just extract the UTC
// offset.
uint32_t now = getRtcNow();
// This means that clock hasn't been set
if (now >= 3029529605) {
return;
}
String response = callAPI("currently,minutely,hourly,daily,alerts", 0);
if (response == "") {
return;
}
// This isn't efficient because the return from http.getString() is
// a String which will get duplicated by deserializeJson. Size
// determined using https://arduinojson.org/v6/assistant/#/step1
StaticJsonDocument<1536> jsonTiming;
DeserializationError err = deserializeJson(jsonTiming, response);
if (err) {
fatal("Deserialize JSON failed " + String(err.c_str()));
return;
}
// This will be the delta (in hours) between UTC and the local time. For example,
// in the UK during the summer this will be 1 and to go from UTC to local time
// you must add 1. Note that this is a float because the offset could be 1.5 hours
// or similar (e.g. India is 5.5 hours from UTC).
float offset = jsonTiming["offset"];
const char *tz = jsonTiming["timezone"];
// Figure out the epoch equivalent of midnight local time. First round to the nearest day.
// Since the epoch starts on a midnight boundary this will give us UTC midnight for today.
// The subtract the offset hours to get the UTC time that corresponds to local midnight.
uint32_t seconds_since_midnight = now;
// Do not be tempted to remove these two lines. This is integer division so this is used
// for rounding!
now /= SECONDS_PER_DAY;
now *= SECONDS_PER_DAY;
uint32_t midnight = now - int(offset * SECONDS_PER_HOUR);
seconds_since_midnight -= midnight;
// Step 2.
//
// Now get the historial weather forecast from the calculated local midnight and insert
// it into the hours array.
response = callAPI("currently,daily,minutely,alerts", midnight);
if (response == "") {
return;
}
DynamicJsonDocument doc(32768);
err = deserializeJson(doc, response);
if (err) {
fatal("Deserialize JSON failed " + String(err.c_str()));
return;
}
int i = 0;
JsonObject hourly = doc["hourly"];
for (JsonObject hourly_data_item : hourly["data"].as<JsonArray>()) {
hours[i].when = hourly_data_item["time"];
hours[i].temperature = hourly_data_item["temperature"];
strcpy(hours[i].icon, hourly_data_item["icon"]);
i += 1;
}
// Step 3.
//
// Then get the current forecast and overwrite entries in the hours
// array so we blend the historical forecast and the current one.
response = callAPI("currently,minutely,alerts", 0);
if (response == "") {
return;
}
doc.clear();
err = deserializeJson(doc, response);
if (err) {
fatal("Deserialize JSON failed " + String(err.c_str()));
return;
}
hourly = doc["hourly"];
for (JsonObject hourly_data_item : hourly["data"].as<JsonArray>()) {
for (i = 0; i < 48; i++) {
if (hours[i].when == hourly_data_item["time"]) {
hours[i].temperature = hourly_data_item["temperature"];
strcpy(hours[i].icon, hourly_data_item["icon"]);
}
}
}
// Step 4.
//
// Draw the hourly data on screen and since the last API call included the data
// for the next 7 days draw the daily forecast as well.
int16_t bar_start_x = 50;
int16_t bar_start_y = 200;
// This ensure that the bar_width is a multiple of 24 so that the hours are spaced
// at an integer number of pixels.
int16_t bar_width = ink.width() - 2 * bar_start_x;
int16_t temp_bar_width = bar_width;
bar_width /= 24;
int16_t bar_hour_spacing = bar_width;
bar_width *= 24;
bar_start_x += (temp_bar_width - bar_width) / 2;
int16_t bar_height = 100;
int16_t bar_gap = 100;
drawRectangle(bar_start_x, bar_start_y, bar_width, bar_height);
drawRectangle(bar_start_x, bar_start_y + bar_height + bar_gap, bar_width, bar_height);
// This draws a marker showing the current time relative to the today weather forecast
uint32_t now_offset = ((uint32_t)bar_width * seconds_since_midnight) / SECONDS_PER_DAY;
int16_t now_x = bar_start_x + now_offset;
ink.fillTriangle(now_x - 3, bar_start_y - 7, now_x + 3, bar_start_y - 6, now_x, bar_start_y - 1, 1);
ink.setTextColor(0, 7);
pr(bar_start_x, bar_start_y - 7, "Today", fontMedium);
pr(bar_start_x, bar_start_y + bar_height + bar_gap - 7, "Tomorrow", fontMedium);
int16_t bar_y = bar_start_y;
int16_t bar_x = bar_start_x;
int16_t short_tick = 5;
int16_t long_tick = 10;
int16_t last_x = -1;
char last[128];
for (i = 0; i < 48; i++) {
ink.drawThickLine(bar_x, bar_y + bar_height, bar_x,
bar_y + bar_height + ((i % 2 == 0) ? short_tick : long_tick), 0, 1);
if ((i % 2) == 0) {
char hour[HHMM_SIZE];
hhmm(hours[i].when, offset, &hour[0]);
char temp[TEMP_SIZE];
roundTemp(hours[i].temperature, &temp[0]);
if (i == 0) {
pr(bar_x, bar_y + bar_height + short_tick + 12, hour, fontSmall);
pr(bar_x, bar_y + bar_height + short_tick + 36, temp, fontMedium);
} else {
centre(bar_x, bar_y + bar_height + short_tick + 12, hour, fontSmall);
centre(bar_x, bar_y + bar_height + short_tick + 36, temp, fontMedium);
}
ink.print("\xba");
}
if (last_x == -1) {
strcpy(last, hours[i].icon);
last_x = bar_x;
} else {
if (strcmp(last, hours[i].icon) != 0) {
ink.drawThickLine(bar_x, bar_y, bar_x, bar_y + bar_height, 0, 1);
centreIcon(last_x + (bar_x - last_x) / 2, bar_y + bar_height / 2, last, SMALL_IMAGE);
strcpy(last, hours[i].icon);
last_x = bar_x;
}
}
bar_x += bar_hour_spacing;
if ((i % 24) == 23) {
centreIcon(last_x + (bar_x - last_x) / 2, bar_y + bar_height / 2, last, SMALL_IMAGE);
bar_x = bar_start_x;
bar_y += bar_height + bar_gap;
last_x = -1;
}
}
bar_width /= 2;
bar_width /= 7;
int16_t bar_day_spacing = bar_width;
bar_width *= 7;
bar_x = bar_start_x;
drawRectangle(bar_x, bar_y, bar_width, bar_height);
pr(bar_x, bar_y - 7, "Next 7 Days", fontMedium);
int count = 0;
JsonObject daily = doc["daily"];
for (JsonObject daily_data_item : daily["data"].as<JsonArray>()) {
ink.drawThickLine(bar_x, bar_y, bar_x, bar_y + bar_height, 0, 1);
const char* icon = daily_data_item["icon"];
centreIcon(bar_x + bar_day_spacing / 2, bar_y + bar_height / 2, icon, SMALL_IMAGE);
uint32_t when = daily_data_item["time"];
centre(bar_x + bar_day_spacing / 2, bar_y + bar_height + 22, day(when, offset), fontMedium);
char high[TEMP_SIZE];
roundTemp(daily_data_item["temperatureHigh"], &high[0]);
char low[TEMP_SIZE];
roundTemp(daily_data_item["temperatureLow"], &low[0]);
// The reason the degree symbol is printed separately from the actual temperature is
// that this causes the temperature digits to be centered and looks better. If the
// symbol is included in the centred string it doesn't look as good on screen.
centre(bar_x + bar_day_spacing / 2, bar_y + 21, high, fontMedium);
ink.print("\xba");
centre(bar_x + bar_day_spacing / 2, bar_y + bar_height - 5, low, fontMedium);
ink.print("\xba");
bar_x += bar_day_spacing;
count += 1;
if (count == 7) {
break;
}
}
// Step 5.
//
// Now make another API call to just get the minute-by-minute rain for the current
// hour and draw that on screen.
response = callAPI("daily,hourly,alerts", 0);
if (response == "") {
return;
}
doc.clear();
err = deserializeJson(doc, response);
if (err) {
fatal("Deserialize JSON failed " + String(err.c_str()));
return;
}
int16_t bar_spacing = 50;
int16_t new_width = bar_width - bar_spacing;
new_width /= 60;
new_width *= 60;
bar_x = bar_start_x + bar_width + bar_spacing + (bar_width - new_width - bar_spacing);
bar_width = new_width;
int16_t rain_width = bar_width / 60;
drawRectangle(bar_x, bar_y, bar_width, bar_height);
pr(bar_x, bar_y - 6, "Rain Next 60 Minutes", fontMedium);
float max_rain = 4;
count = 0;
int16_t centre_x;
JsonObject minutely = doc["minutely"];
for (JsonObject minutely_data_item : minutely["data"].as<JsonArray>()) {
float rain = minutely_data_item["precipIntensity"];
if (rain > max_rain) {
rain = max_rain;
}
if (rain > 0) {
ink.drawThickLine(bar_x, bar_y, bar_x, bar_y + (bar_height * (rain / max_rain)), 0, 1);
}
if ((count % 10) == 0) {
int when = minutely_data_item["time"];
char temp[TEMP_SIZE];
hhmm(when, offset, &temp[0]);
if (count == 0) {
pr(bar_x, bar_y + bar_height + short_tick + 11, temp, fontSmall);
} else if (count == 60) {
right(bar_x, bar_y + bar_height + short_tick + 11, temp, fontSmall);
} else {
centre(bar_x, bar_y + bar_height + short_tick + 11, temp, fontSmall);
}
if (count == 30) {
centre_x = bar_x;
}
ink.drawThickLine(bar_x, bar_y + bar_height, bar_x, bar_y + bar_height + short_tick, 0, 1);
}
count += 1;
bar_x += rain_width;
}
// Step 6.
//
// Using data about the current forecast show the title and when the forecast
// was last checked and local observations (weather and temperature).
JsonObject currently = doc["currently"];
const char *icon = currently["icon"];
float c = currently["temperature"];
uint32_t when = currently["time"];
char temp[TEMP_SIZE];
roundTemp(c, &temp[0]);
pr(200, 80, title, fontLarge);
char hm[HHMM_SIZE];
hhmm(update_time, offset, &hm[0]);
char subtitle[80];
sprintf(subtitle, "Forecast checked at %s. It's %s\xba right now.", hm, temp);
pr(200, 150, subtitle, fontLarge);
centreIcon(bar_start_x + 64, 100, icon, LARGE_IMAGE);
// Step 7.
//
// Add the status bar at the bottom
char hmnow[HHMM_SIZE];
hhmm(update_time, offset, &hmnow[0]);
char hmnext[HHMM_SIZE];
hhmm(next, offset, &hmnext[0]);
JsonObject flags = doc["flags"];
const char *version = flags["version"];
char status_bar[255];
sprintf(status_bar, "Updated: %s - Next update: %s - Time zone: %s (%.1f) - Temperature: %d\xba - Battery: %.1fv - API %s",
hmnow, hmnext, tz, offset, ink.readTemperature(), ink.readBattery(), version);
centre(ink.width() / 2, ink.height() - 75, status_bar, fontSmall);
show();
}
// clear clears the display
void clear() {
ink.clearDisplay();
ink.display();
}
// show displays whatever has been written to the display on the
// e-ink screen itself.
void show() {
ink.display();
}
// fatal is used to show a fatal error on screen
void fatal(String s) {
clear();
ink.setTextColor(0, 7);
ink.setTextSize(4);
ink.setCursor(100, 100);
ink.print(s);
show();
}
// deepSleep puts the device into deep sleep mode for sleep_time
// seconds. When it wakes up setup() will be called.
void deepSleep(uint64_t sleep_time) {
// This is needed for Inkplate 10's that use the ESP32 WROVER-E
// in order to reduce power consumption during sleep.
rtc_gpio_isolate(GPIO_NUM_12);
// The following sets the wake up timer to run after the appropriate
// interval (in microseconds) and then goes to sleep.
esp_sleep_enable_timer_wakeup(sleep_time * 1000000);
esp_deep_sleep_start();
}