From b84ab91c6b2e1e665755115e186dee7fccaac5a1 Mon Sep 17 00:00:00 2001 From: Sudrien <_@sudrien.net> Date: Fri, 16 Jun 2023 00:17:20 -0400 Subject: [PATCH 1/2] Switch from CityID to Lat an Lon, expose sunrise and sunset --- examples/WatchFaces/7_SEG/settings.h | 8 +++++--- examples/WatchFaces/Basic/settings.h | 8 +++++--- examples/WatchFaces/DOS/settings.h | 8 +++++--- examples/WatchFaces/MacPaint/settings.h | 8 +++++--- examples/WatchFaces/Mario/settings.h | 8 +++++--- examples/WatchFaces/Pokemon/settings.h | 8 +++++--- examples/WatchFaces/StarryHorizon/settings.h | 8 +++++--- examples/WatchFaces/Tetris/settings.h | 8 +++++--- src/Watchy.cpp | 19 ++++++++++++------- src/Watchy.h | 7 +++++-- 10 files changed, 57 insertions(+), 33 deletions(-) diff --git a/examples/WatchFaces/7_SEG/settings.h b/examples/WatchFaces/7_SEG/settings.h index 808a79b..dc46172 100644 --- a/examples/WatchFaces/7_SEG/settings.h +++ b/examples/WatchFaces/7_SEG/settings.h @@ -2,9 +2,10 @@ #define SETTINGS_H //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_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_LANG "en" #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 watchySettings settings{ - .cityID = CITY_ID, + .lat = LAT, + .lon = LON, .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, diff --git a/examples/WatchFaces/Basic/settings.h b/examples/WatchFaces/Basic/settings.h index fa537fa..e295156 100644 --- a/examples/WatchFaces/Basic/settings.h +++ b/examples/WatchFaces/Basic/settings.h @@ -2,9 +2,10 @@ #define SETTINGS_H //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_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_LANG "en" #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 watchySettings settings{ - .cityID = CITY_ID, + .lat = LAT, + .lon = LON, .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, diff --git a/examples/WatchFaces/DOS/settings.h b/examples/WatchFaces/DOS/settings.h index fa537fa..e295156 100644 --- a/examples/WatchFaces/DOS/settings.h +++ b/examples/WatchFaces/DOS/settings.h @@ -2,9 +2,10 @@ #define SETTINGS_H //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_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_LANG "en" #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 watchySettings settings{ - .cityID = CITY_ID, + .lat = LAT, + .lon = LON, .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, diff --git a/examples/WatchFaces/MacPaint/settings.h b/examples/WatchFaces/MacPaint/settings.h index fa537fa..e295156 100644 --- a/examples/WatchFaces/MacPaint/settings.h +++ b/examples/WatchFaces/MacPaint/settings.h @@ -2,9 +2,10 @@ #define SETTINGS_H //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_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_LANG "en" #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 watchySettings settings{ - .cityID = CITY_ID, + .lat = LAT, + .lon = LON, .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, diff --git a/examples/WatchFaces/Mario/settings.h b/examples/WatchFaces/Mario/settings.h index 808a79b..dc46172 100644 --- a/examples/WatchFaces/Mario/settings.h +++ b/examples/WatchFaces/Mario/settings.h @@ -2,9 +2,10 @@ #define SETTINGS_H //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_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_LANG "en" #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 watchySettings settings{ - .cityID = CITY_ID, + .lat = LAT, + .lon = LON, .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, diff --git a/examples/WatchFaces/Pokemon/settings.h b/examples/WatchFaces/Pokemon/settings.h index fa537fa..e295156 100644 --- a/examples/WatchFaces/Pokemon/settings.h +++ b/examples/WatchFaces/Pokemon/settings.h @@ -2,9 +2,10 @@ #define SETTINGS_H //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_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_LANG "en" #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 watchySettings settings{ - .cityID = CITY_ID, + .lat = LAT, + .lon = LON, .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, diff --git a/examples/WatchFaces/StarryHorizon/settings.h b/examples/WatchFaces/StarryHorizon/settings.h index fa537fa..e295156 100644 --- a/examples/WatchFaces/StarryHorizon/settings.h +++ b/examples/WatchFaces/StarryHorizon/settings.h @@ -2,9 +2,10 @@ #define SETTINGS_H //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_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_LANG "en" #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 watchySettings settings{ - .cityID = CITY_ID, + .lat = LAT, + .lon = LON, .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, diff --git a/examples/WatchFaces/Tetris/settings.h b/examples/WatchFaces/Tetris/settings.h index fa537fa..e295156 100644 --- a/examples/WatchFaces/Tetris/settings.h +++ b/examples/WatchFaces/Tetris/settings.h @@ -2,9 +2,10 @@ #define SETTINGS_H //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_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_LANG "en" #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 watchySettings settings{ - .cityID = CITY_ID, + .lat = LAT, + .lon = LON, .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, diff --git a/src/Watchy.cpp b/src/Watchy.cpp index 19212ca..6707184 100644 --- a/src/Watchy.cpp +++ b/src/Watchy.cpp @@ -626,12 +626,12 @@ void Watchy::drawWatchFace() { } weatherData Watchy::getWeatherData() { - return getWeatherData(settings.cityID, settings.weatherUnit, + return getWeatherData(settings.lat, settings.lon, settings.weatherUnit, settings.weatherLang, settings.weatherURL, 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, uint8_t updateInterval) { currentWeather.isMetric = units == String("metric"); @@ -644,9 +644,12 @@ weatherData Watchy::getWeatherData(String cityID, String units, String lang, if (connectWiFi()) { HTTPClient http; // Use Weather API for live data if WiFi is connected http.setConnectTimeout(3000); // 3 second max timeout - String weatherQueryURL = url + cityID + String("&units=") + units + - String("&lang=") + lang + String("&appid=") + - apiKey; + String weatherQueryURL = url; + weatherQueryURL.replace("{lat}", lat); + weatherQueryURL.replace("{lon}", lon); + weatherQueryURL.replace("{units}", units); + weatherQueryURL.replace("{lang}", lang); + weatherQueryURL.replace("{apiKey}", apiKey); http.begin(weatherQueryURL.c_str()); int httpResponseCode = http.GET(); if (httpResponseCode == 200) { @@ -656,9 +659,11 @@ weatherData Watchy::getWeatherData(String cityID, String units, String lang, currentWeather.weatherConditionCode = int(responseObject["weather"][0]["id"]); currentWeather.weatherDescription = - JSONVar::stringify(responseObject["weather"][0]["main"]); + JSONVar::stringify(responseObject["weather"][0]["main"]); 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"]); syncNTP(gmtOffset); } else { diff --git a/src/Watchy.h b/src/Watchy.h index 0e25175..3e72c71 100644 --- a/src/Watchy.h +++ b/src/Watchy.h @@ -23,11 +23,14 @@ typedef struct weatherData { bool isMetric; String weatherDescription; bool external; + tmElements_t sunrise; + tmElements_t sunset; } weatherData; typedef struct watchySettings { // Weather Settings - String cityID; + String lat; + String lon; String weatherAPIKey; String weatherURL; String weatherUnit; @@ -70,7 +73,7 @@ public: void setupWifi(); bool connectWiFi(); 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); void updateFWBegin(); From aa3528371d52563475fc58ae85d5237bcb077d0e Mon Sep 17 00:00:00 2001 From: sqfmi Date: Tue, 2 Apr 2024 14:13:15 -0400 Subject: [PATCH 2/2] getWeatherData to accept cityID or lat lon --- examples/WatchFaces/7_SEG/settings.h | 26 ++++++++++++++----- examples/WatchFaces/Basic/settings.h | 27 +++++++++++++++----- examples/WatchFaces/DOS/settings.h | 27 +++++++++++++++----- examples/WatchFaces/MacPaint/settings.h | 27 +++++++++++++++----- examples/WatchFaces/Mario/settings.h | 26 ++++++++++++++----- examples/WatchFaces/Pokemon/settings.h | 27 +++++++++++++++----- examples/WatchFaces/StarryHorizon/settings.h | 27 +++++++++++++++----- examples/WatchFaces/Tetris/settings.h | 27 +++++++++++++++----- src/Watchy.cpp | 24 +++++++++-------- src/Watchy.h | 5 ++-- 10 files changed, 183 insertions(+), 60 deletions(-) diff --git a/examples/WatchFaces/7_SEG/settings.h b/examples/WatchFaces/7_SEG/settings.h index dc46172..a3a266f 100644 --- a/examples/WatchFaces/7_SEG/settings.h +++ b/examples/WatchFaces/7_SEG/settings.h @@ -2,10 +2,19 @@ #define SETTINGS_H //Weather Settings -#define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/ -#define LON "-97.751968" +#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid + +//You can also use LAT,LON for your location instead of CITY_ID, but not both +//#define LAT "40.7127" //New York City, Looked up on https://www.latlong.net/ +//#define LON "-74.0059" + +#ifdef CITY_ID + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id={cityID}&lang={lang}&units={units}&appid={apiKey}" //open weather api using city ID +#else + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api using lat lon +#endif + #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) -#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_LANG "en" #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes @@ -14,8 +23,13 @@ #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data watchySettings settings{ - .lat = LAT, - .lon = LON, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -26,4 +40,4 @@ watchySettings settings{ .vibrateOClock = true, }; -#endif +#endif \ No newline at end of file diff --git a/examples/WatchFaces/Basic/settings.h b/examples/WatchFaces/Basic/settings.h index e295156..a3a266f 100644 --- a/examples/WatchFaces/Basic/settings.h +++ b/examples/WatchFaces/Basic/settings.h @@ -2,10 +2,19 @@ #define SETTINGS_H //Weather Settings -#define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/ -#define LON "-97.751968" +#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid + +//You can also use LAT,LON for your location instead of CITY_ID, but not both +//#define LAT "40.7127" //New York City, Looked up on https://www.latlong.net/ +//#define LON "-74.0059" + +#ifdef CITY_ID + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id={cityID}&lang={lang}&units={units}&appid={apiKey}" //open weather api using city ID +#else + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api using lat lon +#endif + #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) -#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_LANG "en" #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes @@ -14,8 +23,13 @@ #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data watchySettings settings{ - .lat = LAT, - .lon = LON, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -23,6 +37,7 @@ watchySettings settings{ .weatherUpdateInterval = WEATHER_UPDATE_INTERVAL, .ntpServer = NTP_SERVER, .gmtOffset = GMT_OFFSET_SEC, + .vibrateOClock = true, }; -#endif +#endif \ No newline at end of file diff --git a/examples/WatchFaces/DOS/settings.h b/examples/WatchFaces/DOS/settings.h index e295156..a3a266f 100644 --- a/examples/WatchFaces/DOS/settings.h +++ b/examples/WatchFaces/DOS/settings.h @@ -2,10 +2,19 @@ #define SETTINGS_H //Weather Settings -#define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/ -#define LON "-97.751968" +#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid + +//You can also use LAT,LON for your location instead of CITY_ID, but not both +//#define LAT "40.7127" //New York City, Looked up on https://www.latlong.net/ +//#define LON "-74.0059" + +#ifdef CITY_ID + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id={cityID}&lang={lang}&units={units}&appid={apiKey}" //open weather api using city ID +#else + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api using lat lon +#endif + #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) -#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_LANG "en" #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes @@ -14,8 +23,13 @@ #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data watchySettings settings{ - .lat = LAT, - .lon = LON, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -23,6 +37,7 @@ watchySettings settings{ .weatherUpdateInterval = WEATHER_UPDATE_INTERVAL, .ntpServer = NTP_SERVER, .gmtOffset = GMT_OFFSET_SEC, + .vibrateOClock = true, }; -#endif +#endif \ No newline at end of file diff --git a/examples/WatchFaces/MacPaint/settings.h b/examples/WatchFaces/MacPaint/settings.h index e295156..a3a266f 100644 --- a/examples/WatchFaces/MacPaint/settings.h +++ b/examples/WatchFaces/MacPaint/settings.h @@ -2,10 +2,19 @@ #define SETTINGS_H //Weather Settings -#define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/ -#define LON "-97.751968" +#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid + +//You can also use LAT,LON for your location instead of CITY_ID, but not both +//#define LAT "40.7127" //New York City, Looked up on https://www.latlong.net/ +//#define LON "-74.0059" + +#ifdef CITY_ID + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id={cityID}&lang={lang}&units={units}&appid={apiKey}" //open weather api using city ID +#else + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api using lat lon +#endif + #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) -#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_LANG "en" #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes @@ -14,8 +23,13 @@ #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data watchySettings settings{ - .lat = LAT, - .lon = LON, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -23,6 +37,7 @@ watchySettings settings{ .weatherUpdateInterval = WEATHER_UPDATE_INTERVAL, .ntpServer = NTP_SERVER, .gmtOffset = GMT_OFFSET_SEC, + .vibrateOClock = true, }; -#endif +#endif \ No newline at end of file diff --git a/examples/WatchFaces/Mario/settings.h b/examples/WatchFaces/Mario/settings.h index dc46172..a3a266f 100644 --- a/examples/WatchFaces/Mario/settings.h +++ b/examples/WatchFaces/Mario/settings.h @@ -2,10 +2,19 @@ #define SETTINGS_H //Weather Settings -#define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/ -#define LON "-97.751968" +#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid + +//You can also use LAT,LON for your location instead of CITY_ID, but not both +//#define LAT "40.7127" //New York City, Looked up on https://www.latlong.net/ +//#define LON "-74.0059" + +#ifdef CITY_ID + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id={cityID}&lang={lang}&units={units}&appid={apiKey}" //open weather api using city ID +#else + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api using lat lon +#endif + #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) -#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_LANG "en" #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes @@ -14,8 +23,13 @@ #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data watchySettings settings{ - .lat = LAT, - .lon = LON, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -26,4 +40,4 @@ watchySettings settings{ .vibrateOClock = true, }; -#endif +#endif \ No newline at end of file diff --git a/examples/WatchFaces/Pokemon/settings.h b/examples/WatchFaces/Pokemon/settings.h index e295156..a3a266f 100644 --- a/examples/WatchFaces/Pokemon/settings.h +++ b/examples/WatchFaces/Pokemon/settings.h @@ -2,10 +2,19 @@ #define SETTINGS_H //Weather Settings -#define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/ -#define LON "-97.751968" +#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid + +//You can also use LAT,LON for your location instead of CITY_ID, but not both +//#define LAT "40.7127" //New York City, Looked up on https://www.latlong.net/ +//#define LON "-74.0059" + +#ifdef CITY_ID + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id={cityID}&lang={lang}&units={units}&appid={apiKey}" //open weather api using city ID +#else + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api using lat lon +#endif + #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) -#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_LANG "en" #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes @@ -14,8 +23,13 @@ #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data watchySettings settings{ - .lat = LAT, - .lon = LON, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -23,6 +37,7 @@ watchySettings settings{ .weatherUpdateInterval = WEATHER_UPDATE_INTERVAL, .ntpServer = NTP_SERVER, .gmtOffset = GMT_OFFSET_SEC, + .vibrateOClock = true, }; -#endif +#endif \ No newline at end of file diff --git a/examples/WatchFaces/StarryHorizon/settings.h b/examples/WatchFaces/StarryHorizon/settings.h index e295156..a3a266f 100644 --- a/examples/WatchFaces/StarryHorizon/settings.h +++ b/examples/WatchFaces/StarryHorizon/settings.h @@ -2,10 +2,19 @@ #define SETTINGS_H //Weather Settings -#define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/ -#define LON "-97.751968" +#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid + +//You can also use LAT,LON for your location instead of CITY_ID, but not both +//#define LAT "40.7127" //New York City, Looked up on https://www.latlong.net/ +//#define LON "-74.0059" + +#ifdef CITY_ID + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id={cityID}&lang={lang}&units={units}&appid={apiKey}" //open weather api using city ID +#else + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api using lat lon +#endif + #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) -#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_LANG "en" #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes @@ -14,8 +23,13 @@ #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data watchySettings settings{ - .lat = LAT, - .lon = LON, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -23,6 +37,7 @@ watchySettings settings{ .weatherUpdateInterval = WEATHER_UPDATE_INTERVAL, .ntpServer = NTP_SERVER, .gmtOffset = GMT_OFFSET_SEC, + .vibrateOClock = true, }; -#endif +#endif \ No newline at end of file diff --git a/examples/WatchFaces/Tetris/settings.h b/examples/WatchFaces/Tetris/settings.h index e295156..a3a266f 100644 --- a/examples/WatchFaces/Tetris/settings.h +++ b/examples/WatchFaces/Tetris/settings.h @@ -2,10 +2,19 @@ #define SETTINGS_H //Weather Settings -#define LAT "45.793411" // New York City, Looked up on https://www.latlong.net/ -#define LON "-97.751968" +#define CITY_ID "5128581" //New York City https://openweathermap.org/current#cityid + +//You can also use LAT,LON for your location instead of CITY_ID, but not both +//#define LAT "40.7127" //New York City, Looked up on https://www.latlong.net/ +//#define LON "-74.0059" + +#ifdef CITY_ID + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?id={cityID}&lang={lang}&units={units}&appid={apiKey}" //open weather api using city ID +#else + #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&lang={lang}&units={units}&appid={apiKey}" //open weather api using lat lon +#endif + #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) -#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_LANG "en" #define WEATHER_UPDATE_INTERVAL 30 //must be greater than 5, measured in minutes @@ -14,8 +23,13 @@ #define GMT_OFFSET_SEC 3600 * -5 //New York is UTC -5 EST, -4 EDT, will be overwritten by weather data watchySettings settings{ - .lat = LAT, - .lon = LON, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -23,6 +37,7 @@ watchySettings settings{ .weatherUpdateInterval = WEATHER_UPDATE_INTERVAL, .ntpServer = NTP_SERVER, .gmtOffset = GMT_OFFSET_SEC, + .vibrateOClock = true, }; -#endif +#endif \ No newline at end of file diff --git a/src/Watchy.cpp b/src/Watchy.cpp index 6707184..06f9815 100644 --- a/src/Watchy.cpp +++ b/src/Watchy.cpp @@ -626,12 +626,12 @@ void Watchy::drawWatchFace() { } weatherData Watchy::getWeatherData() { - return getWeatherData(settings.lat, settings.lon, settings.weatherUnit, - settings.weatherLang, settings.weatherURL, - settings.weatherAPIKey, settings.weatherUpdateInterval); + return _getWeatherData(settings.cityID, settings.lat, settings.lon, + settings.weatherUnit, settings.weatherLang, settings.weatherURL, + settings.weatherAPIKey, settings.weatherUpdateInterval); } -weatherData Watchy::getWeatherData(String lat, String lon, String units, String lang, +weatherData Watchy::_getWeatherData(String cityID, String lat, String lon, String units, String lang, String url, String apiKey, uint8_t updateInterval) { currentWeather.isMetric = units == String("metric"); @@ -645,8 +645,12 @@ weatherData Watchy::getWeatherData(String lat, String lon, String units, String HTTPClient http; // Use Weather API for live data if WiFi is connected http.setConnectTimeout(3000); // 3 second max timeout String weatherQueryURL = url; - weatherQueryURL.replace("{lat}", lat); - weatherQueryURL.replace("{lon}", lon); + if(cityID != ""){ + weatherQueryURL.replace("{cityID}", cityID); + }else{ + weatherQueryURL.replace("{lat}", lat); + weatherQueryURL.replace("{lon}", lon); + } weatherQueryURL.replace("{units}", units); weatherQueryURL.replace("{lang}", lang); weatherQueryURL.replace("{apiKey}", apiKey); @@ -659,10 +663,10 @@ weatherData Watchy::getWeatherData(String lat, String lon, String units, String currentWeather.weatherConditionCode = int(responseObject["weather"][0]["id"]); currentWeather.weatherDescription = - JSONVar::stringify(responseObject["weather"][0]["main"]); - currentWeather.external = true; - breakTime((time_t)(int)responseObject["sys"]["sunrise"], currentWeather.sunrise); - breakTime((time_t)(int)responseObject["sys"]["sunset"], currentWeather.sunset); + JSONVar::stringify(responseObject["weather"][0]["main"]); + currentWeather.external = true; + 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"]); syncNTP(gmtOffset); diff --git a/src/Watchy.h b/src/Watchy.h index 3e72c71..fd32a90 100644 --- a/src/Watchy.h +++ b/src/Watchy.h @@ -29,6 +29,7 @@ typedef struct weatherData { typedef struct watchySettings { // Weather Settings + String cityID; String lat; String lon; String weatherAPIKey; @@ -73,8 +74,6 @@ public: void setupWifi(); bool connectWiFi(); weatherData getWeatherData(); - weatherData getWeatherData(String lat, String lon, String units, String lang, - String url, String apiKey, uint8_t updateInterval); void updateFWBegin(); void showWatchFace(bool partialRefresh); @@ -88,6 +87,8 @@ private: uint16_t len); static uint16_t _writeRegister(uint8_t address, uint8_t reg, uint8_t *data, uint16_t len); + weatherData _getWeatherData(String cityID, String lat, String lon, String units, String lang, + String url, String apiKey, uint8_t updateInterval); }; extern RTC_DATA_ATTR int guiState;