2121
2222// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
2323#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."
2526#endif
2627
2728// WiFi Connection required
3738// Change to your wifi ssid and password
3839
3940#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);
4557
4658float myLatitude = 45.560001 ; // I got this from Wikipedia
4759float myLongitude = 18.675880 ;
@@ -124,15 +136,15 @@ void connectWifi()
124136 if (ConnectCount++ == 20 )
125137 {
126138 Serial.println (" Connect WiFi" );
127- WiFi.begin (HOMESSID, HOMEPW );
139+ WiFi.begin (SSID, PASS );
128140 Serial.print (" Connecting." );
129141 ConnectCount = 0 ;
130142 }
131143 Serial.print (" ." );
132144 delay (1000 );
133145 }
134146 Serial.print (" \n Connected to: " );
135- Serial.println (HOMESSID );
147+ Serial.println (SSID );
136148 Serial.println (" IP address: " );
137149 Serial.println (WiFi.localIP ());
138150 Serial.println (" Connected WiFi" );
@@ -148,7 +160,7 @@ void GetCurrentWeather()
148160 connectWifi ();
149161
150162 Serial.println (" Getting weather" );
151- OWOC.parseWeather (ONECALLKEY , NULL , myLatitude, myLongitude, metric, NULL );
163+ OWOC.parseWeather (APIKEY , NULL , myLatitude, myLongitude, metric, NULL );
152164 setTime (OWOC.current .dt );
153165 t = now ();
154166
@@ -256,6 +268,26 @@ void setup()
256268
257269 connectWifi ();
258270
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+
259291 // Clear display
260292 t = now ();
261293
@@ -548,3 +580,75 @@ void drawMoon()
548580 int currentphase = moonphase * 28 . + .5 ;
549581 alignText (CENTRE_TOP, moonphasenames[currentphase], MoonCentreX, MoonCentreY + MoonBox + 20 );
550582}
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