diff --git a/examples/WatchFaces/7_SEG/settings.h b/examples/WatchFaces/7_SEG/settings.h index 808a79b..a3a266f 100644 --- a/examples/WatchFaces/7_SEG/settings.h +++ b/examples/WatchFaces/7_SEG/settings.h @@ -3,8 +3,18 @@ //Weather Settings #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?id=" //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 +23,13 @@ #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, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -24,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 fa537fa..a3a266f 100644 --- a/examples/WatchFaces/Basic/settings.h +++ b/examples/WatchFaces/Basic/settings.h @@ -3,8 +3,18 @@ //Weather Settings #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?id=" //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 +23,13 @@ #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, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -21,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 fa537fa..a3a266f 100644 --- a/examples/WatchFaces/DOS/settings.h +++ b/examples/WatchFaces/DOS/settings.h @@ -3,8 +3,18 @@ //Weather Settings #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?id=" //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 +23,13 @@ #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, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -21,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 fa537fa..a3a266f 100644 --- a/examples/WatchFaces/MacPaint/settings.h +++ b/examples/WatchFaces/MacPaint/settings.h @@ -3,8 +3,18 @@ //Weather Settings #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?id=" //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 +23,13 @@ #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, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -21,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 808a79b..a3a266f 100644 --- a/examples/WatchFaces/Mario/settings.h +++ b/examples/WatchFaces/Mario/settings.h @@ -3,8 +3,18 @@ //Weather Settings #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?id=" //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 +23,13 @@ #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, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -24,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 fa537fa..a3a266f 100644 --- a/examples/WatchFaces/Pokemon/settings.h +++ b/examples/WatchFaces/Pokemon/settings.h @@ -3,8 +3,18 @@ //Weather Settings #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?id=" //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 +23,13 @@ #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, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -21,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 fa537fa..a3a266f 100644 --- a/examples/WatchFaces/StarryHorizon/settings.h +++ b/examples/WatchFaces/StarryHorizon/settings.h @@ -3,8 +3,18 @@ //Weather Settings #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?id=" //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 +23,13 @@ #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, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -21,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 fa537fa..a3a266f 100644 --- a/examples/WatchFaces/Tetris/settings.h +++ b/examples/WatchFaces/Tetris/settings.h @@ -3,8 +3,18 @@ //Weather Settings #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?id=" //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 +23,13 @@ #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, + #ifdef CITY_ID + .cityID = CITY_ID, + #else + .cityID = "", + .lat = LAT, + .lon = LON, + #endif .weatherAPIKey = OPENWEATHERMAP_APIKEY, .weatherURL = OPENWEATHERMAP_URL, .weatherUnit = TEMP_UNIT, @@ -21,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 9655cba..7523acf 100644 --- a/src/Watchy.cpp +++ b/src/Watchy.cpp @@ -625,12 +625,12 @@ void Watchy::drawWatchFace() { } weatherData Watchy::getWeatherData() { - return getWeatherData(settings.cityID, 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 cityID, 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"); @@ -643,9 +643,16 @@ 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; + 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); http.begin(weatherQueryURL.c_str()); int httpResponseCode = http.GET(); if (httpResponseCode == 200) { @@ -655,9 +662,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"]); - currentWeather.external = true; - // sync NTP during weather API call and use timezone of city + 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); } else { diff --git a/src/Watchy.h b/src/Watchy.h index 2fb0164..15465a6 100644 --- a/src/Watchy.h +++ b/src/Watchy.h @@ -23,11 +23,15 @@ 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; @@ -69,8 +73,6 @@ public: void setupWifi(); bool connectWiFi(); weatherData getWeatherData(); - weatherData getWeatherData(String cityID, String units, String lang, - String url, String apiKey, uint8_t updateInterval); void updateFWBegin(); void showWatchFace(bool partialRefresh); @@ -84,6 +86,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;