diff --git a/docs/firmware/apps/OswWeather.md b/docs/firmware/apps/OswWeather.md index 874d358c4..319cb832f 100644 --- a/docs/firmware/apps/OswWeather.md +++ b/docs/firmware/apps/OswWeather.md @@ -1,5 +1,7 @@ -# OSW WEATHER +# OSW WEATHER +![](/assets/apps/OswWeather/h.png) +Author: [@LorenzoSciacca](https://github.com/Lorenzosciacca) ## How to install Add the flag `OSW_FEATURE_WEATHER` to the file `platformio.ini`: ```ini @@ -58,7 +60,8 @@ The / \ and \ / arrows are used to decrease/increase the current selection (da - Humidity: [relative humidity](https://en.wikipedia.org/wiki/Humidity#Relative_humidity) - Pressure: [hPa](https://en.wikipedia.org/wiki/Pascal_(unit)#Multiples_and_submultiples) -### Weather conditions +### Weather conditions + Visit [this link](https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2) for a more detailed description of each weather condition. @@ -164,4 +167,10 @@ Visit [this link](https://openweathermap.org/weather-conditions#Weather-Conditio -
Unknown
\ No newline at end of file + + +## Using the emulator +To use this app while using the OSW emulator, in order to retireve the data, it is necessary to perform the API request using the browser and to save the response in `file_weather.json` in the `/build` folder. + + + diff --git a/include/apps/_experiments/OswAppWeather.h b/include/apps/_experiments/OswAppWeather.h index 995884680..326885b82 100644 --- a/include/apps/_experiments/OswAppWeather.h +++ b/include/apps/_experiments/OswAppWeather.h @@ -60,5 +60,6 @@ class OswAppWeather : public OswApp { tm* tmInit; std::vector dayFirstUpdt{};// n-th entry is the index of first update of the n-th day bool _request(); + void drawPopUp(); }; -#endif \ No newline at end of file +#endif diff --git a/src/apps/_experiments/OswAppWeather.cpp b/src/apps/_experiments/OswAppWeather.cpp index b813a3f3e..8165a6314 100644 --- a/src/apps/_experiments/OswAppWeather.cpp +++ b/src/apps/_experiments/OswAppWeather.cpp @@ -3,16 +3,21 @@ #include "apps/_experiments/OswAppWeatherEncoder.h" #include "services/OswServiceTaskWiFi.h" #include -#include #include #include #include #include #include "fonts/DS_DIGI12pt7b.h" #include "ArduinoJson.h" - +#ifndef OSW_EMULATOR +#include +#endif #define OPENWEATHERMAP_URL "https://api.openweathermap.org/data/2.5/forecast?q=" #define URL_REQ OPENWEATHERMAP_URL OPENWEATHERMAP_CITY "," OPENWEATHERMAP_STATE_CODE "&appid=" OPENWEATHERMAP_APIKEY "&cnt=24" +#ifdef OSW_EMULATOR +#include +#include +#endif /* TODO: multiple location support measurement unit conversion (?) @@ -100,7 +105,6 @@ int WeatherDecoder::_str2wthr(const String& weather) { } - class WeatherParser { public: WeatherParser(); @@ -122,12 +126,12 @@ class WeatherParser { std::vectorrainMed{502, 501};//10 std::vectorrainHigh{503, 504, 511, 520, 521, 522, 531};//11 std::vectorthunderstorm{200, 201, 210, 211, 231, 230};//12 - std::vectorthunderstorHeavy{202, 212, 221, 232};//13 + std::vectorthunderstormHeavy{202, 212, 221, 232};//13 std::vectorsquallTornado{771, 781};//14 //15 ->unknown std::vector>weather_conditions{clearCode, cloudsMin, cloudsMed, cloudsHigh, mist, fog, snowMin, snowMed, snowHigh, rainMin, rainMed, rainHigh, thunderstorm, - thunderstorHeavy, squallTornado }; + thunderstormHeavy, squallTornado }; }; WeatherParser::WeatherParser() {} @@ -159,7 +163,8 @@ String WeatherParser::encodeWeather(DynamicJsonDocument& doc) { update.weather = this->_getWCond(doc["list"][i]["weather"][0]["id"]); res = encoder.setUpdate(update); if (!res) { - return "ERROR_INPUT"; + OSW_LOG_W("ERROR_INPUT" ); + return "ERROR_INPUT" ; } } return encoder.getEncoded(); @@ -176,6 +181,17 @@ int WeatherParser::_getWCond(int weather_code) { return 15; // unknown weather def } +void OswAppWeather::drawPopUp() { + this->hal->gfx()->drawThickLine(50,120,190,120,15,rgb888(255,255,255),true); + this->hal->gfx()->drawThickLine(51,120,189,120,14,rgb888(0,0,0),true); + this->hal->gfx()->setTextCursor(120,120); + this->hal->gfx()->setTextColor(rgb888(255,255,255)); + this->hal->gfx()->setTextCenterAligned(); + this->hal->gfx()->setTextMiddleAligned(); + this->hal->gfx()->print("connecting..."); +} + + void OswAppWeather::drawWeather() { updtTime = initTimestamp + (this->updtSelector * 10800 ); this->printWIcon.drawIcon(this->forecast[this->updtSelector].weather,120,45,1); @@ -252,7 +268,7 @@ void OswAppWeather::printLastUpdate() { this->hal->gfx()->setTextCursor(120, 225); this->hal->gfx()->print(this->initTimeDMY); } - +#ifndef OSW_EMULATOR void OswAppWeather::weatherRequest() { if(!OswServiceAllTasks::wifi.isConnected()) { OswServiceAllTasks::wifi.enableWiFi(); @@ -263,21 +279,22 @@ void OswAppWeather::weatherRequest() { bool OswAppWeather::_request() { HTTPClient http; - OSW_LOG_I(this->url); - http.begin(this->url, OPENWEATHERMAP_CA_CERT); int code = 0; + OSW_LOG_D("Request: "); + OSW_LOG_D(this->url); + http.begin(this->url, OPENWEATHERMAP_CA_CERT); if (OswServiceAllTasks::wifi.isConnected()) { OswHal::getInstance()->disableDisplayBuffer(); this->forecast.clear(); this->dataLoaded=false; - OSW_LOG_I("free heap ", ESP.getFreeHeap()); + OSW_LOG_D("free heap ", ESP.getFreeHeap()); code = http.GET(); } else { return false; } http.end(); OswServiceAllTasks::wifi.disconnectWiFi(); - OSW_LOG_I("code:", code); + OSW_LOG_D("Request returned code: ", code); if (code == 200) { DynamicJsonDocument doc(16432); deserializeJson(doc,http.getStream()); @@ -300,7 +317,7 @@ bool OswAppWeather::_request() { this->requestMode=false; bool res = this->loadData(); if (res) { - OSW_LOG_I("weather updated correctly"); + OSW_LOG_D("weather updated correctly"); this->dataLoaded=true; return true; } else { @@ -309,6 +326,18 @@ bool OswAppWeather::_request() { } } +#else +void OswAppWeather::weatherRequest() { + this->requestMode = true; +} +bool OswAppWeather::_request() { + + this->requestMode=false; + this->dataLoaded=true; + return true; +} +#endif + void OswAppWeather::getDayList(int nUpdates) { time_t timestamp = this->initTimestamp; @@ -379,9 +408,32 @@ void OswAppWeather::printDate() { } bool OswAppWeather::loadData() { - String wstr = this->pref.getString("wtr"); - if (!wstr.equals("")) { - OSW_LOG_I("size of wstr: ", wstr.length()); +#ifdef OSW_EMULATOR + std::ifstream inFile; + inFile.open("file_weather.json"); //open the input file + if(!inFile.is_open()){ + OSW_LOG_E("Emulator Error: Unable to open 'file_weather.json' in the './build' directory"); + } + std::stringstream strStream; + strStream << inFile.rdbuf(); + std::string strW = strStream.str(); + DynamicJsonDocument doc(16432*2);// when in emulator more space is needed + OSW_LOG_I("json file:"); + OSW_LOG_I(strW); + deserializeJson(doc,strW); + WeatherParser pars; + String encoded = pars.encodeWeather(doc); + OSW_LOG_I("encoded"); + OSW_LOG_I(encoded); + int encoded_len = encoded.length(); + char encoded_arr[encoded_len + 1]; + strcpy(encoded_arr, encoded.c_str()); + String wstr = String(encoded_arr); +#else + String wstr = this->pref.getString("wtr"); +#endif + if (wstr!="") { + OSW_LOG_D("size of wstr: ", wstr.length()); if( (wstr.length() % 8) != 0 ) { this->dataLoaded = false; return false; @@ -430,19 +482,21 @@ int OswAppWeather::getPrevDay() { void OswAppWeather::setup() { + OSW_LOG_D("OSW Weather Setup "); this->location1 = OswConfigAllKeys::weatherLocation1.get(); this->state1 = OswConfigAllKeys::weatherState1.get(); this->api_key = OswConfigAllKeys::weatherApiKey.get(); this->url = String(OPENWEATHERMAP_URL); - this->url.concat(this->location1); - this->url.concat(","); - this->url.concat(this->state1); - this->url.concat("&appid="); - this->url.concat(this->api_key); - this->url.concat("&cnt=24"); - pref.begin("wheater-app"); + this->url = this->url + this->location1; + this->url = this->url + ","; + this->url = this->url + this->state1; + this->url = this->url + "&appid="; + this->url = this->url + this->api_key; + this->url = this->url + "&cnt=24"; + pref.begin("wheater-app", false); this->loadData(); this->printWIcon.getHal(this->hal); + OSW_LOG_D("Setup end"); } void OswAppWeather::loop() { @@ -453,19 +507,21 @@ void OswAppWeather::loop() { this->printDate(); this->printLastUpdate(); } + + #ifndef OSW_EMULATOR if(this->requestMode) { if (OswServiceAllTasks::wifi.isConnected()) { this->_request(); - } else {//pop-up - this->hal->gfx()->drawThickLine(50,120,190,120,15,rgb888(255,255,255),true); - this->hal->gfx()->drawThickLine(51,120,189,120,14,rgb888(0,0,0),true); - this->hal->gfx()->setTextCursor(120,120); - this->hal->gfx()->setTextColor(rgb888(255,255,255)); - this->hal->gfx()->setTextCenterAligned(); - this->hal->gfx()->setTextMiddleAligned(); - this->hal->gfx()->print("connecting..."); + } else { + this->drawPopUp(); } } + #else + if(this->requestMode) { + this->_request(); + this->drawPopUp(); + } + #endif if (hal->btnHasGoneDown(BUTTON_2)) { if(this->mainSelector==1) { // next update if(this->updtSelector<23) { @@ -511,4 +567,4 @@ void OswAppWeather::loop() { void OswAppWeather::stop() { } -#endif \ No newline at end of file +#endif diff --git a/src/apps/_experiments/OswAppWeatherEncoder.cpp b/src/apps/_experiments/OswAppWeatherEncoder.cpp index cb491393a..7a58a9b85 100644 --- a/src/apps/_experiments/OswAppWeatherEncoder.cpp +++ b/src/apps/_experiments/OswAppWeatherEncoder.cpp @@ -6,15 +6,23 @@ OswAppWeatherEncoder::OswAppWeatherEncoder() {} bool OswAppWeatherEncoder::setUpdate(OswAppWeather::weather_update_t update) { bool update_ok = true; if(update.temp > 99 || update.temp < -99 ) { - update_ok = false; + OSW_LOG_W("ERROR TEMP"); + OSW_LOG_W(update.temp); + update_ok = false; } if(update.humidity > 100 || update.humidity < 0) { + OSW_LOG_W("ERROR HUMIDITY"); + OSW_LOG_W(update.humidity); update_ok = false; } if(update.pressure < 0 || update.pressure > 2000 ) { + OSW_LOG_W("ERROR PRESSURE"); + OSW_LOG_W(update.pressure); update_ok = false; } if(update.weather < 0 || update.weather > 15) { + OSW_LOG_W(update.weather); + OSW_LOG_W("ERROR WEATHER"); update_ok = false; } if(!update_ok) {