Skip to content

Commit

Permalink
Updated OpenWeatherStation example
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoric committed Jun 15, 2023
1 parent cf0810d commit 9d42974
Show file tree
Hide file tree
Showing 5 changed files with 555 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#error \
"Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif

// WiFi Connection required
Expand All @@ -37,11 +38,22 @@
// Change to your wifi ssid and password

#include "OpenWeatherOneCall.h"
#define HOMESSID ""
#define HOMEPW ""

// Openweather set up information
#define ONECALLKEY ""
#define SSID ""
#define PASS ""

// Openweather API key
/**
* Note: The OneCall API has moved on to version 3.0,
* In this sketch we are still using 2.5, which is free.
* The only requirement is that you need to have an API key older than approx. early 2023.
* Those API keys are still valid for OneCall 2.5
*
* If your key is invalid, you will be notified by the sketch
*
*/
char *APIKEY = "";
// Also, declare the function to check if the API key is valid
bool checkIfAPIKeyIsValid(char *APIKEY);

float myLatitude = 45.560001; // I got this from Wikipedia
float myLongitude = 18.675880;
Expand Down Expand Up @@ -124,15 +136,15 @@ void connectWifi()
if (ConnectCount++ == 20)
{
Serial.println("Connect WiFi");
WiFi.begin(HOMESSID, HOMEPW);
WiFi.begin(SSID, PASS);
Serial.print("Connecting.");
ConnectCount = 0;
}
Serial.print(".");
delay(1000);
}
Serial.print("\nConnected to: ");
Serial.println(HOMESSID);
Serial.println(SSID);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Connected WiFi");
Expand All @@ -148,7 +160,7 @@ void GetCurrentWeather()
connectWifi();

Serial.println("Getting weather");
OWOC.parseWeather(ONECALLKEY, NULL, myLatitude, myLongitude, metric, NULL);
OWOC.parseWeather(APIKEY, NULL, myLatitude, myLongitude, metric, NULL);
setTime(OWOC.current.dt);
t = now();

Expand Down Expand Up @@ -256,6 +268,26 @@ void setup()

connectWifi();

// Check if we have a valid API key:
Serial.println("Checking if API key is valid...");
if (!checkIfAPIKeyIsValid(APIKEY))
{
// If we don't, notify the user
Serial.println("API key is invalid!");
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.println("Can't get data from OpenWeatherMaps! Check your API key!");
display.println("Only older API keys for OneCall 2.5 work in free tier.");
display.println("See the code comments the example for more info.");
display.display();
while (1)
{
delay(100);
}
}
Serial.println("API key is valid!");

// Clear display
t = now();

Expand Down Expand Up @@ -548,3 +580,75 @@ void drawMoon()
int currentphase = moonphase * 28. + .5;
alignText(CENTRE_TOP, moonphasenames[currentphase], MoonCentreX, MoonCentreY + MoonBox + 20);
}

/**
* Make a test API call to see if we have a valid API key
*
* Older keys made for OpenWeather 2.5 OneCall API work, while newer ones won't work, due to the service becoming
* deprecated.
*/
bool checkIfAPIKeyIsValid(char *APIKEY)
{
bool failed = false;

// Create the buffer for holding the API response, make it large enough just in case
char *data;
data = (char *)ps_malloc(50000LL);

// Http object used to make GET request
HTTPClient http;
http.getStream().setTimeout(10);
http.getStream().flush();
http.getStream().setNoDelay(true);

// Combine the base URL and the API key to do a test call
char *baseURL = "https://api.openweathermap.org/data/2.5/onecall?lat=45.560001&lon=18.675880&units=metric&appid=";
char apiTestURL[200];
sprintf(apiTestURL, "%s%s", baseURL, APIKEY);

// Begin http by passing url to it
http.begin(apiTestURL);

delay(300);

// Download data until it's a verified complete download
// Actually do request
int httpCode = http.GET();

if (httpCode == 200)
{
long n = 0;

long now = millis();

while (millis() - now < 1000)
{
while (http.getStream().available())
{
data[n++] = http.getStream().read();
now = millis();
}
}

data[n++] = 0;

// If the reply constains this string - it's invalid
if (strstr(data, "Invalid API key.") != NULL)
failed = true;
}
else
{
// In case there was another HTTP code, it failed
Serial.print("Error! HTTP Code: ");
Serial.println(httpCode);
failed = true;
}

// End http
http.end();

// Free the memory
free(data);

return !failed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@
#define SSID ""
#define PASS ""

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

float myLatitude = 45.560001; // I got this from Wikipedia
float myLongitude = 18.675880;
Expand Down Expand Up @@ -138,10 +149,8 @@ void GetCurrentWeather()
// Get the Weather Forecast
//=================================

connectWifi();

Serial.println("Getting weather");
OWOC.parseWeather(API_KEY, NULL, myLatitude, myLongitude, metric, NULL);
OWOC.parseWeather(APIKEY, NULL, myLatitude, myLongitude, metric, NULL);
setTime(OWOC.current.dt);
t = now();

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

connectWifi();

// Check if we have a valid API key:
Serial.println("Checking if API key is valid...");
if(!checkIfAPIKeyIsValid(APIKEY))
{
// If we don't, notify the user
Serial.println("API key is invalid!");
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(2);
display.println("Can't get data from OpenWeatherMaps! Check your API key!");
display.println("Only older API keys for OneCall 2.5 work in free tier.");
display.println("See the code comments the example for more info.");
display.display();
while(1)
{
delay(100);
}
}
Serial.println("API key is valid!");

// Set the temp. unit
tempUnit = (metric == 1 ? 'C' : 'F');
}

Expand Down Expand Up @@ -487,3 +519,75 @@ void drawMoon()
display.setCursor(339, 55);
display.print("Moon Phase");
}

/**
* Make a test API call to see if we have a valid API key
*
* Older keys made for OpenWeather 2.5 OneCall API work, while newer ones won't work, due to the service becoming
* deprecated.
*/
bool checkIfAPIKeyIsValid(char *APIKEY)
{
bool failed = false;

// Create the buffer for holding the API response, make it large enough just in case
char * data;
data = (char *)ps_malloc(50000LL);

// Http object used to make GET request
HTTPClient http;
http.getStream().setTimeout(10);
http.getStream().flush();
http.getStream().setNoDelay(true);

// Combine the base URL and the API key to do a test call
char * baseURL = "https://api.openweathermap.org/data/2.5/onecall?lat=45.560001&lon=18.675880&units=metric&appid=";
char apiTestURL[200];
sprintf(apiTestURL, "%s%s", baseURL, APIKEY);

// Begin http by passing url to it
http.begin(apiTestURL);

delay(300);

// Download data until it's a verified complete download
// Actually do request
int httpCode = http.GET();

if (httpCode == 200)
{
long n = 0;

long now = millis();

while (millis() - now < 1000)
{
while (http.getStream().available())
{
data[n++] = http.getStream().read();
now = millis();
}
}

data[n++] = 0;

// If the reply constains this string - it's invalid
if(strstr(data, "Invalid API key.") != NULL) failed = true;
}
else
{
// In case there was another HTTP code, it failed
Serial.print("Error! HTTP Code: ");
Serial.println(httpCode);
failed = true;
}

// End http
http.end();

// Free the memory
free(data);

return !failed;
}

Loading

0 comments on commit 9d42974

Please sign in to comment.