Skip to content

Commit 9d42974

Browse files
committed
Updated OpenWeatherStation example
1 parent cf0810d commit 9d42974

File tree

5 files changed

+555
-41
lines changed

5 files changed

+555
-41
lines changed

examples/Inkplate10/Projects/Inkplate10_OpenWeather_Station/Inkplate10_OpenWeather_Station.ino

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
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
@@ -37,11 +38,22 @@
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

4658
float myLatitude = 45.560001; // I got this from Wikipedia
4759
float 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("\nConnected 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+
}

examples/Inkplate4/Projects/Inkplate4_OpenWeather_Station/Inkplate4_OpenWeather_Station.ino

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,19 @@
4444
#define SSID ""
4545
#define PASS ""
4646

47-
// Openweather api key
48-
#define API_KEY ""
47+
// Openweather API key
48+
/**
49+
* Note: The OneCall API has moved on to version 3.0,
50+
* In this sketch we are still using 2.5, which is free.
51+
* The only requirement is that you need to have an API key older than approx. early 2023.
52+
* Those API keys are still valid for OneCall 2.5
53+
*
54+
* If your key is invalid, you will be notified by the sketch
55+
*
56+
*/
57+
char * APIKEY = "";
58+
// Also, declare the function to check if the API key is valid
59+
bool checkIfAPIKeyIsValid(char * APIKEY);
4960

5061
float myLatitude = 45.560001; // I got this from Wikipedia
5162
float myLongitude = 18.675880;
@@ -138,10 +149,8 @@ void GetCurrentWeather()
138149
// Get the Weather Forecast
139150
//=================================
140151

141-
connectWifi();
142-
143152
Serial.println("Getting weather");
144-
OWOC.parseWeather(API_KEY, NULL, myLatitude, myLongitude, metric, NULL);
153+
OWOC.parseWeather(APIKEY, NULL, myLatitude, myLongitude, metric, NULL);
145154
setTime(OWOC.current.dt);
146155
t = now();
147156

@@ -195,6 +204,29 @@ void setup()
195204
// Init Inkplate library (you should call this function ONLY ONCE)
196205
display.begin();
197206

207+
connectWifi();
208+
209+
// Check if we have a valid API key:
210+
Serial.println("Checking if API key is valid...");
211+
if(!checkIfAPIKeyIsValid(APIKEY))
212+
{
213+
// If we don't, notify the user
214+
Serial.println("API key is invalid!");
215+
display.clearDisplay();
216+
display.setCursor(0,0);
217+
display.setTextSize(2);
218+
display.println("Can't get data from OpenWeatherMaps! Check your API key!");
219+
display.println("Only older API keys for OneCall 2.5 work in free tier.");
220+
display.println("See the code comments the example for more info.");
221+
display.display();
222+
while(1)
223+
{
224+
delay(100);
225+
}
226+
}
227+
Serial.println("API key is valid!");
228+
229+
// Set the temp. unit
198230
tempUnit = (metric == 1 ? 'C' : 'F');
199231
}
200232

@@ -487,3 +519,75 @@ void drawMoon()
487519
display.setCursor(339, 55);
488520
display.print("Moon Phase");
489521
}
522+
523+
/**
524+
* Make a test API call to see if we have a valid API key
525+
*
526+
* Older keys made for OpenWeather 2.5 OneCall API work, while newer ones won't work, due to the service becoming
527+
* deprecated.
528+
*/
529+
bool checkIfAPIKeyIsValid(char *APIKEY)
530+
{
531+
bool failed = false;
532+
533+
// Create the buffer for holding the API response, make it large enough just in case
534+
char * data;
535+
data = (char *)ps_malloc(50000LL);
536+
537+
// Http object used to make GET request
538+
HTTPClient http;
539+
http.getStream().setTimeout(10);
540+
http.getStream().flush();
541+
http.getStream().setNoDelay(true);
542+
543+
// Combine the base URL and the API key to do a test call
544+
char * baseURL = "https://api.openweathermap.org/data/2.5/onecall?lat=45.560001&lon=18.675880&units=metric&appid=";
545+
char apiTestURL[200];
546+
sprintf(apiTestURL, "%s%s", baseURL, APIKEY);
547+
548+
// Begin http by passing url to it
549+
http.begin(apiTestURL);
550+
551+
delay(300);
552+
553+
// Download data until it's a verified complete download
554+
// Actually do request
555+
int httpCode = http.GET();
556+
557+
if (httpCode == 200)
558+
{
559+
long n = 0;
560+
561+
long now = millis();
562+
563+
while (millis() - now < 1000)
564+
{
565+
while (http.getStream().available())
566+
{
567+
data[n++] = http.getStream().read();
568+
now = millis();
569+
}
570+
}
571+
572+
data[n++] = 0;
573+
574+
// If the reply constains this string - it's invalid
575+
if(strstr(data, "Invalid API key.") != NULL) failed = true;
576+
}
577+
else
578+
{
579+
// In case there was another HTTP code, it failed
580+
Serial.print("Error! HTTP Code: ");
581+
Serial.println(httpCode);
582+
failed = true;
583+
}
584+
585+
// End http
586+
http.end();
587+
588+
// Free the memory
589+
free(data);
590+
591+
return !failed;
592+
}
593+

0 commit comments

Comments
 (0)