mirror of https://github.com/sqfmi/Watchy.git
reduce PR to only add ntp
parent
60f5f53ed2
commit
875229e4bf
|
@ -12,9 +12,6 @@ RTC_DATA_ATTR weatherData currentWeather;
|
||||||
RTC_DATA_ATTR int weatherIntervalCounter = WEATHER_UPDATE_INTERVAL;
|
RTC_DATA_ATTR int weatherIntervalCounter = WEATHER_UPDATE_INTERVAL;
|
||||||
|
|
||||||
bool ntpSet = false;
|
bool ntpSet = false;
|
||||||
String countryCode;
|
|
||||||
String city;
|
|
||||||
int offset;
|
|
||||||
|
|
||||||
String getValue(String data, char separator, int index)
|
String getValue(String data, char separator, int index)
|
||||||
{
|
{
|
||||||
|
@ -75,7 +72,6 @@ void Watchy::init(String datetime){
|
||||||
#ifndef ESP_RTC
|
#ifndef ESP_RTC
|
||||||
_rtcConfig(datetime);
|
_rtcConfig(datetime);
|
||||||
#endif
|
#endif
|
||||||
initTimeAndLocation();
|
|
||||||
_bmaConfig();
|
_bmaConfig();
|
||||||
showWatchFace(false); //full update on reset
|
showWatchFace(false); //full update on reset
|
||||||
break;
|
break;
|
||||||
|
@ -618,40 +614,8 @@ void Watchy::drawWatchFace(){
|
||||||
display.println(currentTime.Minute);
|
display.println(currentTime.Minute);
|
||||||
}
|
}
|
||||||
|
|
||||||
// must call connectWifi() before calling this function
|
// must call connectWifi() and configTime() 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
|
|
||||||
void Watchy::syncNTPTime(){
|
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;
|
struct tm timeinfo;
|
||||||
if(!getLocalTime(&timeinfo)){
|
if(!getLocalTime(&timeinfo)){
|
||||||
Serial.println("Failed to obtain time");
|
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(){
|
weatherData Watchy::getWeatherData(){
|
||||||
if(weatherIntervalCounter >= WEATHER_UPDATE_INTERVAL){ //only update if WEATHER_UPDATE_INTERVAL has elapsed i.e. 30 minutes
|
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
|
if(connectWiFi()){//Use Weather API for live data if WiFi is connected
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
http.setConnectTimeout(3000);//3 second max timeout
|
http.setConnectTimeout(3000);//3 second max timeout
|
||||||
|
|
||||||
syncIPLocation();
|
String weatherQueryURL = String(OPENWEATHERMAP_URL) + String(CITY_NAME) + String(",") + String(COUNTRY_CODE) + String("&units=") + String(TEMP_UNIT) + String("&appid=") + String(OPENWEATHERMAP_APIKEY);
|
||||||
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);
|
|
||||||
http.begin(weatherQueryURL.c_str());
|
http.begin(weatherQueryURL.c_str());
|
||||||
int httpResponseCode = http.GET();
|
int httpResponseCode = http.GET();
|
||||||
if(httpResponseCode == 200) {
|
if(httpResponseCode == 200) {
|
||||||
String payload = http.getString();
|
String payload = http.getString();
|
||||||
JSONVar responseObject = JSON.parse(payload);
|
JSONVar responseObject = JSON.parse(payload);
|
||||||
currentWeather.temperature = int(responseObject["main"]["temp"]);
|
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{
|
}else{
|
||||||
//http error
|
//http error
|
||||||
}
|
}
|
||||||
|
@ -875,7 +822,6 @@ void Watchy::setupWifi(){
|
||||||
display.setTextColor(GxEPD_WHITE);
|
display.setTextColor(GxEPD_WHITE);
|
||||||
display.println("Connected to");
|
display.println("Connected to");
|
||||||
display.println(WiFi.SSID());
|
display.println(WiFi.SSID());
|
||||||
initTimeAndLocation();
|
|
||||||
display.display(false);//full refresh
|
display.display(false);//full refresh
|
||||||
display.hibernate();
|
display.hibernate();
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,9 +41,7 @@ class Watchy {
|
||||||
void setTime();
|
void setTime();
|
||||||
void setupWifi();
|
void setupWifi();
|
||||||
bool connectWiFi();
|
bool connectWiFi();
|
||||||
void syncIPLocation();
|
|
||||||
void syncNTPTime();
|
void syncNTPTime();
|
||||||
void initTimeAndLocation();
|
|
||||||
weatherData getWeatherData();
|
weatherData getWeatherData();
|
||||||
void updateFWBegin();
|
void updateFWBegin();
|
||||||
|
|
||||||
|
|
|
@ -25,13 +25,12 @@
|
||||||
#define DISPLAY_WIDTH 200
|
#define DISPLAY_WIDTH 200
|
||||||
#define DISPLAY_HEIGHT 200
|
#define DISPLAY_HEIGHT 200
|
||||||
//weather api
|
//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 COUNTRY_CODE "US"
|
||||||
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
|
#define OPENWEATHERMAP_APIKEY "f058fe1cad2afe8e2ddc5d063a64cecb" //use your own API key :)
|
||||||
#define OPENWEATHERMAP_URL "http://api.openweathermap.org/data/2.5/weather?q="
|
#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 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
|
#define WEATHER_UPDATE_INTERVAL 30 //minutes
|
||||||
//wifi
|
//wifi
|
||||||
#define WIFI_AP_TIMEOUT 60
|
#define WIFI_AP_TIMEOUT 60
|
||||||
|
|
Loading…
Reference in New Issue