From 875229e4bf801a8463f6de797c62c835d9dfc866 Mon Sep 17 00:00:00 2001 From: therealmitchconnors Date: Thu, 8 Jul 2021 10:39:49 -0700 Subject: [PATCH] reduce PR to only add ntp --- src/Watchy.cpp | 70 ++++++-------------------------------------------- src/Watchy.h | 2 -- src/config.h | 5 ++-- 3 files changed, 10 insertions(+), 67 deletions(-) diff --git a/src/Watchy.cpp b/src/Watchy.cpp index 2cca6cb..f787b67 100644 --- a/src/Watchy.cpp +++ b/src/Watchy.cpp @@ -12,9 +12,6 @@ RTC_DATA_ATTR weatherData currentWeather; RTC_DATA_ATTR int weatherIntervalCounter = WEATHER_UPDATE_INTERVAL; bool ntpSet = false; -String countryCode; -String city; -int offset; String getValue(String data, char separator, int index) { @@ -75,7 +72,6 @@ void Watchy::init(String datetime){ #ifndef ESP_RTC _rtcConfig(datetime); #endif - initTimeAndLocation(); _bmaConfig(); showWatchFace(false); //full update on reset break; @@ -618,40 +614,8 @@ void Watchy::drawWatchFace(){ display.println(currentTime.Minute); } -// must call connectWifi() before calling this function -void Watchy::syncIPLocation(){ - HTTPClient http; - http.setConnectTimeout(3000);//3 second max timeout - - // Get Time offset, city, countrycode from ip-api.com - http.begin(String(IP_API_URL)); - int ipRespnseCode = http.GET(); - if(ipRespnseCode != 200) { - Serial.println("Failed to obtain ip-api"); - return; - } - String payload = http.getString(); - JSONVar responseObject = JSON.parse(payload); - countryCode = (const char*) responseObject["countryCode"]; - city = (const char*) responseObject["city"]; - offset = int(responseObject["offset"]); - // url encode city - city.replace(" ", "+"); -} - -// must call connectWifi() before calling this function +// must call connectWifi() and configTime() before calling this function void Watchy::syncNTPTime(){ - if (city.length() < 1) { - syncIPLocation(); - if (city.length() < 1) { - // if we don't have our city name, we don't know how to offset time... - return; - } - } - if (!ntpSet) { - configTime(offset, 0, NTP_SERVER); - ntpSet = true; - } struct tm timeinfo; if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); @@ -672,42 +636,25 @@ void Watchy::syncNTPTime(){ } } -void Watchy::initTimeAndLocation(){ - if(connectWiFi()){ - syncNTPTime(); - WiFi.mode(WIFI_OFF); - btStop(); - } -} - weatherData Watchy::getWeatherData(){ 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 - syncIPLocation(); - syncNTPTime(); - - // if city is set by ip address, use it. else fall back to compile time choice from config.h. - String chosenCity; - String chosenCountryCode; - if (city.length() > 0) { - chosenCity = city; - chosenCountryCode = countryCode; - } else { - chosenCity = CITY_NAME; - chosenCountryCode = COUNTRY_CODE; - } - - String weatherQueryURL = String(OPENWEATHERMAP_URL) + String(chosenCity) + String(",") + String(chosenCountryCode) + String("&units=") + String(TEMP_UNIT) + String("&appid=") + String(OPENWEATHERMAP_APIKEY); + 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"]); + currentWeather.weatherConditionCode = int(responseObject["weather"][0]["id"]); + if (!ntpSet) { + configTime(int(responseObject["timezone"]), 0, NTP_SERVER); + ntpSet = true; + } + syncNTPTime(); }else{ //http error } @@ -875,7 +822,6 @@ void Watchy::setupWifi(){ display.setTextColor(GxEPD_WHITE); display.println("Connected to"); display.println(WiFi.SSID()); - initTimeAndLocation(); display.display(false);//full refresh display.hibernate(); } diff --git a/src/Watchy.h b/src/Watchy.h index e66cd42..58538ab 100644 --- a/src/Watchy.h +++ b/src/Watchy.h @@ -41,9 +41,7 @@ class Watchy { void setTime(); void setupWifi(); bool connectWiFi(); - void syncIPLocation(); void syncNTPTime(); - void initTimeAndLocation(); weatherData getWeatherData(); void updateFWBegin(); diff --git a/src/config.h b/src/config.h index 339d311..38c6147 100644 --- a/src/config.h +++ b/src/config.h @@ -25,13 +25,12 @@ #define DISPLAY_WIDTH 200 #define DISPLAY_HEIGHT 200 //weather api -#define CITY_NAME "SEATTLE" //if your city name has a space, replace with '+' +#define CITY_NAME "NEW+YORK" //if your city name has a space, replace with '+' #define COUNTRY_CODE "US" #define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :) #define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?q=" -#define IP_API_URL "http://ip-api.com/json/?fields=33554450" #define NTP_SERVER "pool.ntp.org" -#define TEMP_UNIT "imperial" //use "imperial" for Fahrenheit" +#define TEMP_UNIT "metric" //use "imperial" for Fahrenheit" #define WEATHER_UPDATE_INTERVAL 30 //minutes //wifi #define WIFI_AP_TIMEOUT 60