Implement cloud cover

This commit is contained in:
Robert Marshall 2018-09-22 13:04:05 +01:00
parent 5ffc1b8b0c
commit e9114275ae
3 changed files with 75 additions and 10 deletions

View file

@ -1,4 +1,8 @@
#include "Weather.h"
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <Arduino.h>
#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"]);
}

View file

@ -1,9 +1,25 @@
#define WEATHER_URL "https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s"
#include <Arduino.h>
#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; }
};

View file

@ -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(){