Switch from CityID to Lat an Lon, expose sunrise and sunset

pull/233/head
Sudrien 2023-06-16 00:17:20 -04:00
parent 342eb48a49
commit b84ab91c6b
10 changed files with 57 additions and 33 deletions

View File

@ -2,9 +2,10 @@
#define SETTINGS_H #define SETTINGS_H
//Weather Settings //Weather Settings
#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid #define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/
#define LON "-97.751968"
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id=" //open weather api #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api
#define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit #define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit
#define TEMP_LANG "en" #define TEMP_LANG "en"
#define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes
@ -13,7 +14,8 @@
#define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data
watchySettings settings{ watchySettings settings{
.cityID = CITY_ID, .lat = LAT,
.lon = LON,
.weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherAPIKey = OPENWEATHERMAP_APIKEY,
.weatherURL = OPENWEATHERMAP_URL, .weatherURL = OPENWEATHERMAP_URL,
.weatherUnit = TEMP_UNIT, .weatherUnit = TEMP_UNIT,

View File

@ -2,9 +2,10 @@
#define SETTINGS_H #define SETTINGS_H
//Weather Settings //Weather Settings
#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid #define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/
#define LON "-97.751968"
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id=" //open weather api #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api
#define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit #define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit
#define TEMP_LANG "en" #define TEMP_LANG "en"
#define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes
@ -13,7 +14,8 @@
#define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data
watchySettings settings{ watchySettings settings{
.cityID = CITY_ID, .lat = LAT,
.lon = LON,
.weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherAPIKey = OPENWEATHERMAP_APIKEY,
.weatherURL = OPENWEATHERMAP_URL, .weatherURL = OPENWEATHERMAP_URL,
.weatherUnit = TEMP_UNIT, .weatherUnit = TEMP_UNIT,

View File

@ -2,9 +2,10 @@
#define SETTINGS_H #define SETTINGS_H
//Weather Settings //Weather Settings
#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid #define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/
#define LON "-97.751968"
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id=" //open weather api #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api
#define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit #define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit
#define TEMP_LANG "en" #define TEMP_LANG "en"
#define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes
@ -13,7 +14,8 @@
#define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data
watchySettings settings{ watchySettings settings{
.cityID = CITY_ID, .lat = LAT,
.lon = LON,
.weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherAPIKey = OPENWEATHERMAP_APIKEY,
.weatherURL = OPENWEATHERMAP_URL, .weatherURL = OPENWEATHERMAP_URL,
.weatherUnit = TEMP_UNIT, .weatherUnit = TEMP_UNIT,

View File

@ -2,9 +2,10 @@
#define SETTINGS_H #define SETTINGS_H
//Weather Settings //Weather Settings
#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid #define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/
#define LON "-97.751968"
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id=" //open weather api #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api
#define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit #define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit
#define TEMP_LANG "en" #define TEMP_LANG "en"
#define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes
@ -13,7 +14,8 @@
#define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data
watchySettings settings{ watchySettings settings{
.cityID = CITY_ID, .lat = LAT,
.lon = LON,
.weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherAPIKey = OPENWEATHERMAP_APIKEY,
.weatherURL = OPENWEATHERMAP_URL, .weatherURL = OPENWEATHERMAP_URL,
.weatherUnit = TEMP_UNIT, .weatherUnit = TEMP_UNIT,

View File

@ -2,9 +2,10 @@
#define SETTINGS_H #define SETTINGS_H
//Weather Settings //Weather Settings
#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid #define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/
#define LON "-97.751968"
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id=" //open weather api #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api
#define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit #define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit
#define TEMP_LANG "en" #define TEMP_LANG "en"
#define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes
@ -13,7 +14,8 @@
#define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data
watchySettings settings{ watchySettings settings{
.cityID = CITY_ID, .lat = LAT,
.lon = LON,
.weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherAPIKey = OPENWEATHERMAP_APIKEY,
.weatherURL = OPENWEATHERMAP_URL, .weatherURL = OPENWEATHERMAP_URL,
.weatherUnit = TEMP_UNIT, .weatherUnit = TEMP_UNIT,

View File

@ -2,9 +2,10 @@
#define SETTINGS_H #define SETTINGS_H
//Weather Settings //Weather Settings
#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid #define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/
#define LON "-97.751968"
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id=" //open weather api #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api
#define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit #define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit
#define TEMP_LANG "en" #define TEMP_LANG "en"
#define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes
@ -13,7 +14,8 @@
#define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data
watchySettings settings{ watchySettings settings{
.cityID = CITY_ID, .lat = LAT,
.lon = LON,
.weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherAPIKey = OPENWEATHERMAP_APIKEY,
.weatherURL = OPENWEATHERMAP_URL, .weatherURL = OPENWEATHERMAP_URL,
.weatherUnit = TEMP_UNIT, .weatherUnit = TEMP_UNIT,

View File

@ -2,9 +2,10 @@
#define SETTINGS_H #define SETTINGS_H
//Weather Settings //Weather Settings
#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid #define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/
#define LON "-97.751968"
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id=" //open weather api #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api
#define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit #define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit
#define TEMP_LANG "en" #define TEMP_LANG "en"
#define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes
@ -13,7 +14,8 @@
#define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data
watchySettings settings{ watchySettings settings{
.cityID = CITY_ID, .lat = LAT,
.lon = LON,
.weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherAPIKey = OPENWEATHERMAP_APIKEY,
.weatherURL = OPENWEATHERMAP_URL, .weatherURL = OPENWEATHERMAP_URL,
.weatherUnit = TEMP_UNIT, .weatherUnit = TEMP_UNIT,

View File

@ -2,9 +2,10 @@
#define SETTINGS_H #define SETTINGS_H
//Weather Settings //Weather Settings
#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid #define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/
#define LON "-97.751968"
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id=" //open weather api #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api
#define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit #define TEMP_UNIT "metric" //metric = Celsius , imperial = Fahrenheit
#define TEMP_LANG "en" #define TEMP_LANG "en"
#define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes
@ -13,7 +14,8 @@
#define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data
watchySettings settings{ watchySettings settings{
.cityID = CITY_ID, .lat = LAT,
.lon = LON,
.weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherAPIKey = OPENWEATHERMAP_APIKEY,
.weatherURL = OPENWEATHERMAP_URL, .weatherURL = OPENWEATHERMAP_URL,
.weatherUnit = TEMP_UNIT, .weatherUnit = TEMP_UNIT,

View File

@ -626,12 +626,12 @@ void Watchy::drawWatchFace() {
} }
weatherData Watchy::getWeatherData() { weatherData Watchy::getWeatherData() {
return getWeatherData(settings.cityID, settings.weatherUnit, return getWeatherData(settings.lat, settings.lon, settings.weatherUnit,
settings.weatherLang, settings.weatherURL, settings.weatherLang, settings.weatherURL,
settings.weatherAPIKey, settings.weatherUpdateInterval); settings.weatherAPIKey, settings.weatherUpdateInterval);
} }
weatherData Watchy::getWeatherData(String cityID, String units, String lang, weatherData Watchy::getWeatherData(String lat, String lon, String units, String lang,
String url, String apiKey, String url, String apiKey,
uint8_t updateInterval) { uint8_t updateInterval) {
currentWeather.isMetric = units == String("metric"); currentWeather.isMetric = units == String("metric");
@ -644,9 +644,12 @@ weatherData Watchy::getWeatherData(String cityID, String units, String lang,
if (connectWiFi()) { if (connectWiFi()) {
HTTPClient http; // Use Weather API for live data if WiFi is connected HTTPClient http; // Use Weather API for live data if WiFi is connected
http.setConnectTimeout(3000); // 3 second max timeout http.setConnectTimeout(3000); // 3 second max timeout
String weatherQueryURL = url + cityID + String("&units=") + units + String weatherQueryURL = url;
String("&lang=") + lang + String("&appid=") + weatherQueryURL.replace("{lat}", lat);
apiKey; weatherQueryURL.replace("{lon}", lon);
weatherQueryURL.replace("{units}", units);
weatherQueryURL.replace("{lang}", lang);
weatherQueryURL.replace("{apiKey}", apiKey);
http.begin(weatherQueryURL.c_str()); http.begin(weatherQueryURL.c_str());
int httpResponseCode = http.GET(); int httpResponseCode = http.GET();
if (httpResponseCode == 200) { if (httpResponseCode == 200) {
@ -656,9 +659,11 @@ weatherData Watchy::getWeatherData(String cityID, String units, String lang,
currentWeather.weatherConditionCode = currentWeather.weatherConditionCode =
int(responseObject["weather"][0]["id"]); int(responseObject["weather"][0]["id"]);
currentWeather.weatherDescription = currentWeather.weatherDescription =
JSONVar::stringify(responseObject["weather"][0]["main"]); JSONVar::stringify(responseObject["weather"][0]["main"]);
currentWeather.external = true; currentWeather.external = true;
// sync NTP during weather API call and use timezone of city breakTime((time_t)(int)responseObject["sys"]["sunrise"], currentWeather.sunrise);
breakTime((time_t)(int)responseObject["sys"]["sunset"], currentWeather.sunset);
// sync NTP during weather API call and use timezone of lat & lon
gmtOffset = int(responseObject["timezone"]); gmtOffset = int(responseObject["timezone"]);
syncNTP(gmtOffset); syncNTP(gmtOffset);
} else { } else {

View File

@ -23,11 +23,14 @@ typedef struct weatherData {
bool isMetric; bool isMetric;
String weatherDescription; String weatherDescription;
bool external; bool external;
tmElements_t sunrise;
tmElements_t sunset;
} weatherData; } weatherData;
typedef struct watchySettings { typedef struct watchySettings {
// Weather Settings // Weather Settings
String cityID; String lat;
String lon;
String weatherAPIKey; String weatherAPIKey;
String weatherURL; String weatherURL;
String weatherUnit; String weatherUnit;
@ -70,7 +73,7 @@ public:
void setupWifi(); void setupWifi();
bool connectWiFi(); bool connectWiFi();
weatherData getWeatherData(); weatherData getWeatherData();
weatherData getWeatherData(String cityID, String units, String lang, weatherData getWeatherData(String lat, String lon, String units, String lang,
String url, String apiKey, uint8_t updateInterval); String url, String apiKey, uint8_t updateInterval);
void updateFWBegin(); void updateFWBegin();