reduce PR to only add ntp

pull/71/head
therealmitchconnors 2021-07-08 10:39:49 -07:00
parent 60f5f53ed2
commit 875229e4bf
3 changed files with 10 additions and 67 deletions

View File

@ -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();
}

View File

@ -41,9 +41,7 @@ class Watchy {
void setTime();
void setupWifi();
bool connectWiFi();
void syncIPLocation();
void syncNTPTime();
void initTimeAndLocation();
weatherData getWeatherData();
void updateFWBegin();

View File

@ -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