Repository navigation
Expand file tree
/
Copy pathweather.cpp
More file actions
70 lines (57 loc) · 2.12 KB
/
Copy pathweather.cpp
File metadata and controls
70 lines (57 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <LittleFS.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include "weather.h"
extern AppConfig appConfig;
WeatherData fetchWeather() {
WeatherData wd;
if (appConfig.owmKey.isEmpty()) {
wd.error = true;
return wd;
}
WiFiClientSecure client;
client.setInsecure(); // TODO: pin OWM cert fingerprint for production
HTTPClient http;
String url = "https://api.openweathermap.org/data/2.5/weather?q="
+ appConfig.city
+ "&appid=" + appConfig.owmKey
+ "&units=metric";
http.begin(client, url);
http.setTimeout(5000);
http.setConnectTimeout(5000);
int code = http.GET();
if (code != 200) {
http.end();
wd.error = true;
return wd;
}
JsonDocument doc;
deserializeJson(doc, http.getStream());
http.end();
wd.feelsLike = doc["main"]["feels_like"] | 0.0f;
String main = doc["weather"][0]["main"].as<String>();
int cloudCover = doc["clouds"]["all"] | 0;
float rainAmount = 0.0f;
if (doc["rain"].is<JsonObject>()) {
// "1h" takes priority; fall back to "3h" if only the 3-hour bucket is present
if (!doc["rain"]["1h"].isNull()) rainAmount = doc["rain"]["1h"].as<float>();
else if (!doc["rain"]["3h"].isNull()) rainAmount = doc["rain"]["3h"].as<float>();
}
if (main == "Rain" || main == "Drizzle" || main == "Thunderstorm") {
wd.condition = "Rainy";
} else if (main == "Clear") {
wd.condition = "Sunny";
} else if (main == "Clouds" && cloudCover > 60) {
wd.condition = "Cloudy";
} else {
wd.condition = "Mild Cloudy";
}
wd.playUmbrella = (rainAmount > 0.1f || main == "Rain" || main == "Drizzle");
wd.playWarmClothes = (wd.feelsLike <= 16.0f);
if (wd.playUmbrella) wd.suggestion = "Bring an umbrella!";
else if (wd.playWarmClothes) wd.suggestion = "Dress warmly today";
else if (wd.condition == "Sunny") wd.suggestion = "Stay hydrated!";
else wd.suggestion = "Have a good day!";
return wd;
}