Implement cloud cover
This commit is contained in:
parent
5ffc1b8b0c
commit
e9114275ae
3 changed files with 75 additions and 10 deletions
36
Weather.cpp
36
Weather.cpp
|
@ -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"]);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue