diff --git a/Weather.cpp b/Weather.cpp index ffe8871..98a681b 100644 --- a/Weather.cpp +++ b/Weather.cpp @@ -1,4 +1,8 @@ #include "Weather.h" +#include +#include +#include + #include #include "openWeatherMapApiKey.h" @@ -7,6 +11,36 @@ Weather::Weather(char *lat, char *lon){ sprintf(_url, WEATHER_URL, lat, lon, OPEN_WEATHER_MAP_API_KEY); } -void Weather::update(){ +String Weather::getWeatherJson() { + HTTPClient http; + http.begin(_url); + int httpCode = http.GET(); + String result = ""; + if (httpCode > 0) + result = http.getString(); + http.end(); + return result; +} + +WeatherCondition Weather::parseCondition(int weatherId){ + if (weatherId > 200 && weatherId < 300) + return WeatherCondition::Thunder; + if (weatherId > 300 && weatherId < 700) + return WeatherCondition::Rain; + if (weatherId < 800) + return WeatherCondition::Other; + return WeatherCondition::Clear; +} + +void Weather::update(){ + String json = getWeatherJson(); + StaticJsonBuffer<2048> jsonBuffer; + JsonObject &root = jsonBuffer.parseObject(json); + + if (!root.success()) + return; + + _cloudCover = root["clouds"]["all"]; + _condition = parseCondition(root["weather"][0]["id"]); } \ No newline at end of file diff --git a/Weather.h b/Weather.h index 723a6e6..23ef4ad 100644 --- a/Weather.h +++ b/Weather.h @@ -1,9 +1,25 @@ -#define WEATHER_URL "https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s" +#include + +#define WEATHER_URL "http://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s" + +enum WeatherCondition +{ + Clear, + Rain, + Thunder, + Other +}; class Weather{ - char _url[100]; + char _url[120]; + int _cloudCover; + WeatherCondition _condition; + String getWeatherJson(); + WeatherCondition parseCondition(int weatherId); -public: + public: Weather(char *lat, char *lon); void update(); + int getCloudCover() { return _cloudCover; } + WeatherCondition getCondition() { return _condition; } }; \ No newline at end of file diff --git a/monitor.ino b/monitor.ino index fc870ec..1ecfb47 100644 --- a/monitor.ino +++ b/monitor.ino @@ -34,7 +34,7 @@ NaturalLight _naturalLight = NaturalLight(LATITUDE, LONGITUDE, TIMEZONE_OFFSET); Weather _weather = Weather(LATITUDE, LONGITUDE); CRGB _leds[LED_COUNT]; - +byte targetBrightness = 255; void waitForWiFi() { if (WiFi.status() == WL_CONNECTED) @@ -98,16 +98,31 @@ float getHeatIndex() { return HEAT_INDEX_MAX * indexMultiplier; } +void setTargetBrightness(){ + float multiplier = (100 - (float)_weather.getCloudCover()) / 100.0; + if (_weather.getCondition() == WeatherCondition::Other) + multiplier /= 2.0; + + float brightness = 255.0 * multiplier; + FastLED.setBrightness(brightness); +} + void doLighting() { - EVERY_N_SECONDS(1) { + EVERY_N_SECONDS(10800){ + _naturalLight.update(); + } + + EVERY_N_SECONDS(10){ + _weather.update(); + setTargetBrightness(); + } + + EVERY_N_SECONDS(1) + { CRGB colour = ColorFromPalette(HeatColors_p, getHeatIndex()); fill_solid(_leds, LED_COUNT, colour); FastLED.show(); } - - EVERY_N_SECONDS(10800){ - _naturalLight.update(); - } } void publishReadings(){