Allow the weather location to be set by the user

Update the 7_SEG example to show how it's done
pull/62/head
Adam McDaniel 2021-06-17 22:46:21 -06:00
parent 80c4444576
commit 841ef40467
3 changed files with 20 additions and 5 deletions

View File

@ -2,7 +2,11 @@
Watchy7SEG watchy;
void setup(){
void setup() {
// City names with spaces require a +
// temp units must be either metric or imperial
watchy.setWeatherLocation("New+York", "US", "metric");
watchy.init();
}

View File

@ -612,12 +612,19 @@ void Watchy::drawWatchFace(){
display.println(currentTime.Minute);
}
void Watchy::setWeatherLocation(String city, String countryCode, String tempUnit){
currentWeather.city = city;
currentWeather.countryCode = countryCode;
currentWeather.tempUnit = tempUnit;
}
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
String weatherQueryURL = String(OPENWEATHERMAP_URL) + String(CITY_NAME) + String(",") + String(COUNTRY_CODE) + String("&units=") + String(TEMP_UNIT) + String("&appid=") + String(OPENWEATHERMAP_APIKEY);
String weatherQueryURL = String(OPENWEATHERMAP_URL) + String(currentWeather.city) + String(",") + String(currentWeather.countryCode) +
String("&units=") + String(currentWeather.tempUnit) + String("&appid=") + String(OPENWEATHERMAP_APIKEY);
http.begin(weatherQueryURL.c_str());
int httpResponseCode = http.GET();
if(httpResponseCode == 200) {
@ -634,7 +641,7 @@ weatherData Watchy::getWeatherData(){
btStop();
}else{//No WiFi, use RTC Temperature
uint8_t temperature = RTC.temperature() / 4; //celsius
if(strcmp(TEMP_UNIT, "imperial") == 0){
if(currentWeather.tempUnit.equals("imperial")){
temperature = temperature * 9. / 5. + 32.; //fahrenheit
}
currentWeather.temperature = temperature;
@ -963,4 +970,4 @@ void Watchy::updateFWBegin(){
// time_t t = makeTime(tm);
// return t + FUDGE; //add fudge factor to allow for compile time
// }
// }

View File

@ -17,6 +17,9 @@
typedef struct weatherData{
int8_t temperature;
int16_t weatherConditionCode;
String city = CITY_NAME;
String countryCode = COUNTRY_CODE;
String tempUnit = TEMP_UNIT;
}weatherData;
class Watchy {
@ -41,6 +44,7 @@ class Watchy {
void setTime();
void setupWifi();
bool connectWiFi();
void setWeatherLocation(String city, String country, String units);
weatherData getWeatherData();
void updateFWBegin();
@ -61,4 +65,4 @@ extern RTC_DATA_ATTR BMA423 sensor;
extern RTC_DATA_ATTR bool WIFI_CONFIGURED;
extern RTC_DATA_ATTR bool BLE_CONFIGURED;
#endif
#endif