From 55cb672cee6fa4ed3f14f92e804609e45b722dd7 Mon Sep 17 00:00:00 2001 From: SQFMI Date: Thu, 1 Apr 2021 15:24:32 -0400 Subject: [PATCH] update weather interval --- examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp | 1 + library.json | 2 +- library.properties | 2 +- src/Watchy.cpp | 60 ++++++++++++---------- src/Watchy.h | 1 + src/config.h | 1 + 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp b/examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp index b863bed..7ce9207 100644 --- a/examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp +++ b/examples/WatchFaces/7_SEG/Watchy_7_SEG.cpp @@ -95,6 +95,7 @@ void Watchy7SEG::drawBattery(){ void Watchy7SEG::drawWeather(){ weatherData currentWeather = getWeatherData(); + int8_t temperature = currentWeather.temperature; int16_t weatherConditionCode = currentWeather.weatherConditionCode; diff --git a/library.json b/library.json index 11603ea..45c257b 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Watchy", - "version": "1.2.3", + "version": "1.2.5", "description": "Watchy - An Open Source E-Paper Watch by SQFMI", "authors": [ { diff --git a/library.properties b/library.properties index 7a86759..35dacef 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Watchy -version=1.2.3 +version=1.2.5 author=SQFMI maintainer=SQFMI sentence=Watchy - An Open Source E-Paper Watch by SQFMI diff --git a/src/Watchy.cpp b/src/Watchy.cpp index b97efaf..f3cf22e 100644 --- a/src/Watchy.cpp +++ b/src/Watchy.cpp @@ -8,6 +8,8 @@ RTC_DATA_ATTR int menuIndex; RTC_DATA_ATTR BMA423 sensor; RTC_DATA_ATTR bool WIFI_CONFIGURED; RTC_DATA_ATTR bool BLE_CONFIGURED; +RTC_DATA_ATTR weatherData currentWeather; +RTC_DATA_ATTR int weatherIntervalCounter = WEATHER_UPDATE_INTERVAL; String getValue(String data, char separator, int index) { @@ -484,35 +486,37 @@ void Watchy::drawWatchFace(){ } weatherData Watchy::getWeatherData(){ - - weatherData currentWeather; - - if(connectWiFi()){//Use Weather API for live data if WiFi is connected - HTTPClient http; - http.setConnectTimeout(3000);//3 second max timeout - String weatherQueryURL = String(OPENWEATHERMAP_URL) + String(CITY_NAME) + String(",") + String(COUNTRY_CODE) + String("&units=") + String(TEMP_UNIT) + String("&appid=") + String(OPENWEATHERMAP_APIKEY); - http.begin(weatherQueryURL.c_str()); - int httpResponseCode = http.GET(); - if(httpResponseCode == 200) { - String payload = http.getString(); - JSONVar responseObject = JSON.parse(payload); - currentWeather.temperature = int(responseObject["main"]["temp"]); - currentWeather.weatherConditionCode = int(responseObject["weather"][0]["id"]); - }else{ - //http error + if(weatherIntervalCounter >= WEATHER_UPDATE_INTERVAL){ //only update if WEATHER_UPDATE_INTERVAL has elapsed i.e. 30 minutes + if(connectWiFi()){//Use Weather API for live data if WiFi is connected + HTTPClient http; + http.setConnectTimeout(3000);//3 second max timeout + String weatherQueryURL = String(OPENWEATHERMAP_URL) + String(CITY_NAME) + String(",") + String(COUNTRY_CODE) + String("&units=") + String(TEMP_UNIT) + String("&appid=") + String(OPENWEATHERMAP_APIKEY); + http.begin(weatherQueryURL.c_str()); + int httpResponseCode = http.GET(); + if(httpResponseCode == 200) { + String payload = http.getString(); + JSONVar responseObject = JSON.parse(payload); + currentWeather.temperature = int(responseObject["main"]["temp"]); + currentWeather.weatherConditionCode = int(responseObject["weather"][0]["id"]); + }else{ + //http error + } + http.end(); + //turn off radios + WiFi.mode(WIFI_OFF); + btStop(); + }else{//No WiFi, use RTC Temperature + uint8_t temperature = RTC.temperature() / 4; //celsius + if(strcmp(TEMP_UNIT, "imperial") == 0){ + temperature = temperature * 9. / 5. + 32.; //fahrenheit + } + currentWeather.temperature = temperature; + currentWeather.weatherConditionCode = 800; } - http.end(); - //turn off radios - WiFi.mode(WIFI_OFF); - btStop(); - }else{//No WiFi, use RTC Temperature - uint8_t temperature = RTC.temperature() / 4; //celsius - if(strcmp(TEMP_UNIT, "imperial") == 0){ - temperature = temperature * 9. / 5. + 32.; //fahrenheit - } - currentWeather.temperature = temperature; - currentWeather.weatherConditionCode = 800; - } + weatherIntervalCounter = 0; + }else{ + weatherIntervalCounter++; + } return currentWeather; } diff --git a/src/Watchy.h b/src/Watchy.h index 76ac7ab..887d58e 100644 --- a/src/Watchy.h +++ b/src/Watchy.h @@ -33,6 +33,7 @@ class Watchy { void handleButtonPress(); void showMenu(byte menuIndex, bool partialRefresh); + void showFastMenu(byte menuIndex); void showBattery(); void showBuzz(); void showAccelerometer(); diff --git a/src/config.h b/src/config.h index 95ee488..28112d2 100644 --- a/src/config.h +++ b/src/config.h @@ -30,6 +30,7 @@ #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?q=" #define TEMP_UNIT "metric" //use "imperial" for Fahrenheit" +#define WEATHER_UPDATE_INTERVAL 30 //minutes //wifi #define WIFI_AP_TIMEOUT 60 #define WIFI_AP_SSID "Watchy AP"