21
21
22
22
// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
23
23
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
24
- #error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
24
+ #error \
25
+ " Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
25
26
#endif
26
27
27
28
// WiFi Connection required
37
38
// Change to your wifi ssid and password
38
39
39
40
#include " OpenWeatherOneCall.h"
40
- #define HOMESSID " "
41
- #define HOMEPW " "
42
-
43
- // Openweather set up information
44
- #define ONECALLKEY " "
41
+ #define SSID " "
42
+ #define PASS " "
43
+
44
+ // Openweather API key
45
+ /* *
46
+ * Note: The OneCall API has moved on to version 3.0,
47
+ * In this sketch we are still using 2.5, which is free.
48
+ * The only requirement is that you need to have an API key older than approx. early 2023.
49
+ * Those API keys are still valid for OneCall 2.5
50
+ *
51
+ * If your key is invalid, you will be notified by the sketch
52
+ *
53
+ */
54
+ char *APIKEY = " " ;
55
+ // Also, declare the function to check if the API key is valid
56
+ bool checkIfAPIKeyIsValid (char *APIKEY);
45
57
46
58
float myLatitude = 45.560001 ; // I got this from Wikipedia
47
59
float myLongitude = 18.675880 ;
@@ -124,15 +136,15 @@ void connectWifi()
124
136
if (ConnectCount++ == 20 )
125
137
{
126
138
Serial.println (" Connect WiFi" );
127
- WiFi.begin (HOMESSID, HOMEPW );
139
+ WiFi.begin (SSID, PASS );
128
140
Serial.print (" Connecting." );
129
141
ConnectCount = 0 ;
130
142
}
131
143
Serial.print (" ." );
132
144
delay (1000 );
133
145
}
134
146
Serial.print (" \n Connected to: " );
135
- Serial.println (HOMESSID );
147
+ Serial.println (SSID );
136
148
Serial.println (" IP address: " );
137
149
Serial.println (WiFi.localIP ());
138
150
Serial.println (" Connected WiFi" );
@@ -148,7 +160,7 @@ void GetCurrentWeather()
148
160
connectWifi ();
149
161
150
162
Serial.println (" Getting weather" );
151
- OWOC.parseWeather (ONECALLKEY , NULL , myLatitude, myLongitude, metric, NULL );
163
+ OWOC.parseWeather (APIKEY , NULL , myLatitude, myLongitude, metric, NULL );
152
164
setTime (OWOC.current .dt );
153
165
t = now ();
154
166
@@ -256,6 +268,26 @@ void setup()
256
268
257
269
connectWifi ();
258
270
271
+ // Check if we have a valid API key:
272
+ Serial.println (" Checking if API key is valid..." );
273
+ if (!checkIfAPIKeyIsValid (APIKEY))
274
+ {
275
+ // If we don't, notify the user
276
+ Serial.println (" API key is invalid!" );
277
+ display.clearDisplay ();
278
+ display.setCursor (0 , 0 );
279
+ display.setTextSize (2 );
280
+ display.println (" Can't get data from OpenWeatherMaps! Check your API key!" );
281
+ display.println (" Only older API keys for OneCall 2.5 work in free tier." );
282
+ display.println (" See the code comments the example for more info." );
283
+ display.display ();
284
+ while (1 )
285
+ {
286
+ delay (100 );
287
+ }
288
+ }
289
+ Serial.println (" API key is valid!" );
290
+
259
291
// Clear display
260
292
t = now ();
261
293
@@ -548,3 +580,75 @@ void drawMoon()
548
580
int currentphase = moonphase * 28 . + .5 ;
549
581
alignText (CENTRE_TOP, moonphasenames[currentphase], MoonCentreX, MoonCentreY + MoonBox + 20 );
550
582
}
583
+
584
+ /* *
585
+ * Make a test API call to see if we have a valid API key
586
+ *
587
+ * Older keys made for OpenWeather 2.5 OneCall API work, while newer ones won't work, due to the service becoming
588
+ * deprecated.
589
+ */
590
+ bool checkIfAPIKeyIsValid (char *APIKEY)
591
+ {
592
+ bool failed = false ;
593
+
594
+ // Create the buffer for holding the API response, make it large enough just in case
595
+ char *data;
596
+ data = (char *)ps_malloc (50000LL );
597
+
598
+ // Http object used to make GET request
599
+ HTTPClient http;
600
+ http.getStream ().setTimeout (10 );
601
+ http.getStream ().flush ();
602
+ http.getStream ().setNoDelay (true );
603
+
604
+ // Combine the base URL and the API key to do a test call
605
+ char *baseURL = " https://api.openweathermap.org/data/2.5/onecall?lat=45.560001&lon=18.675880&units=metric&appid=" ;
606
+ char apiTestURL[200 ];
607
+ sprintf (apiTestURL, " %s%s" , baseURL, APIKEY);
608
+
609
+ // Begin http by passing url to it
610
+ http.begin (apiTestURL);
611
+
612
+ delay (300 );
613
+
614
+ // Download data until it's a verified complete download
615
+ // Actually do request
616
+ int httpCode = http.GET ();
617
+
618
+ if (httpCode == 200 )
619
+ {
620
+ long n = 0 ;
621
+
622
+ long now = millis ();
623
+
624
+ while (millis () - now < 1000 )
625
+ {
626
+ while (http.getStream ().available ())
627
+ {
628
+ data[n++] = http.getStream ().read ();
629
+ now = millis ();
630
+ }
631
+ }
632
+
633
+ data[n++] = 0 ;
634
+
635
+ // If the reply constains this string - it's invalid
636
+ if (strstr (data, " Invalid API key." ) != NULL )
637
+ failed = true ;
638
+ }
639
+ else
640
+ {
641
+ // In case there was another HTTP code, it failed
642
+ Serial.print (" Error! HTTP Code: " );
643
+ Serial.println (httpCode);
644
+ failed = true ;
645
+ }
646
+
647
+ // End http
648
+ http.end ();
649
+
650
+ // Free the memory
651
+ free (data);
652
+
653
+ return !failed;
654
+ }
0 commit comments