46 lines
No EOL
1 KiB
C++
46 lines
No EOL
1 KiB
C++
#include "Weather.h"
|
|
#include <Arduino.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include "openWeatherMapApiKey.h"
|
|
|
|
Weather::Weather(char *lat, char *lon){
|
|
sprintf(_url, WEATHER_URL, lat, lon, OPEN_WEATHER_MAP_API_KEY);
|
|
}
|
|
|
|
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"]);
|
|
} |