-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_weather.h
More file actions
208 lines (181 loc) · 8.49 KB
/
functions_weather.h
File metadata and controls
208 lines (181 loc) · 8.49 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Function to fetch and parse weather data
void setLocation_meteo(String latitude, String longitude){
apiKey_meteo.replace("%LATITUDE%",latitude);
apiKey_meteo.replace("%LONGITUDE%",longitude);
}
void getWeatherData_OWM(String loc_city, String loc_CountryCode, String apiKey) {
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + loc_city + "," + loc_CountryCode + "&appid=" + apiKey + "&units=metric&lang=id";
//String url = "http://api.openweathermap.org/data/2.5/weather?id=" + cityID + "&appid=" + apiKey + "&units=metric&lang=id";
Serial.println(url);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
DynamicJsonDocument doc(2048); // Increased size for more data
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
// Extract all weather data
cityName = doc["name"].as<String>();
weather = doc["weather"][0]["main"].as<String>();
weatherDescription = doc["weather"][0]["description"].as<String>();
weatherDescription.toUpperCase(); // Capitalize description
temperature = String(doc["main"]["temp"].as<float>(), 1);
feelsLike = String(doc["main"]["feels_like"].as<float>(), 1) + " C";
humidity = String(doc["main"]["humidity"].as<int>()) + "%";
windSpeed = String(doc["wind"]["speed"].as<float>(), 1) + " m/s";
Serial.println("Weather Updated: " + cityName + ", " + temperature + "C");
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpResponseCode);
}
http.end();
}
}
void getWeatherData_meteo(String loc_lat, String loc_long) {
String url = "http://api.open-meteo.com/v1/forecast?latitude=" + loc_lat + "&longitude=" + loc_long + "¤t=temperature_2m,relative_humidity_2m,apparent_temperature,is_day,wind_speed_10m,wind_direction_10m,wind_gusts_10m,precipitation,rain,showers,snowfall,weather_code,cloud_cover,pressure_msl,surface_pressure&timezone=auto";
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String payload = http.getString();
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
// Parse weather data from "current"
temperature = String(doc["current"]["temperature_2m"].as<float>());
feelsLike = String(doc["current"]["apparent_temperature"].as<float>());
humidity = String(doc["current"]["relative_humidity_2m"].as<int>());
windSpeed = String(doc["current"]["wind_speed_10m"].as<float>());
//int windDir = doc["current"]["wind_direction_10m"].as<int>();
cloudCoverage = String(doc["current"]["cloud_cover"].as<int>());
airPressure = String(doc["current"]["pressure_msl"].as<float>());
weatherCode = doc["current"]["weather_code"].as<int>();
// Assign to string variables
temperature = String(temperature) + " C";
feelsLike = String(feelsLike) + " C";
humidity = String(humidity) + "%";
windSpeed = String(windSpeed) + " km/h";
//omWindDirection = String(windDir) + "°";
cloudCoverage = String(cloudCoverage) + "%";
airPressure = String(airPressure) + " hPa";
// Map code to description
weather = mapWeatherCode_id(weatherCode);
weatherDescription = weather;
weatherDescription.toUpperCase();
// Debug print
Serial.println("Weather Updated:");
Serial.println("City: " + cityName);
Serial.println("Temperature: " + temperature);
Serial.println("Feels Like: " + feelsLike);
Serial.println("Humidity: " + humidity);
Serial.println("Wind: " + windSpeed);
Serial.println("Cloud Cover: " + cloudCoverage);
Serial.println("Pressure: " + airPressure);
Serial.println("Weather: " + weather);
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpResponseCode);
}
http.end();
}
}
// Map Open-Meteo WMO weather codes to descriptions
String mapWeatherCode_en(int code) {
switch (code) {
case 0: return "Clear sky";
case 1: return "Mainly clear";
case 2: return "Partly cloudy";
case 3: return "Overcast";
case 45: return "Fog";
case 48: return "Depositing rime fog";
case 51: return "Light drizzle";
case 53: return "Moderate drizzle";
case 55: return "Dense drizzle";
case 61: return "Slight rain";
case 63: return "Moderate rain";
case 65: return "Heavy rain";
case 71: return "Slight snow fall";
case 73: return "Moderate snow fall";
case 75: return "Heavy snow fall";
case 80: return "Rain showers";
case 81: return "Heavy rain showers";
case 82: return "Violent rain showers";
case 95: return "Thunderstorm";
case 99: return "Hailstorm";
default: return "Unknown";
}
}
String mapWeatherCode_id(int code) {
switch (code) {
case 0: return "Cerah";
case 1: return "Cerah berawan";
case 2: return "Berawan sebagian";
case 3: return "Berawan";
case 45: return "Kabut";
case 48: return "Kabut rime";
case 51: return "Gerimis ringan";
case 53: return "Gerimis sedang";
case 55: return "Gerimis lebat";
case 61: return "Hujan ringan";
case 63: return "Hujan sedang";
case 65: return "Hujan lebat";
case 71: return "Hujan salju ringan";
case 73: return "Hujan salju sedang";
case 75: return "Hujan salju lebat";
case 80: return "Hujan deras";
case 81: return "Hujan deras lebat";
case 82: return "Hujan deras disertai badai";
case 95: return "Badai petir";
case 99: return "Badai es";
default: return "Tidak diketahui";
}
}
void drawWeatherIcon_OWM(String weatherName){
if (weatherName == "Clouds"){LCD.drawXBitmap(130, 65, icon_clouds, icon_clouds_width, icon_clouds_height, TFT_BLACK, TFT_DARKGREY);}
else if (weatherName == "Clear") {LCD.drawXBitmap(130, 65, icon_sunny, icon_sunny_width, icon_sunny_height, TFT_BLACK, TFT_WHITE);}
else if (weatherName == "Atmosphere") {}
else if (weatherName == "Snow") {}
else if (weatherName == "Rain") {LCD.drawXBitmap(130, 65, icon_rain, icon_rain_width, icon_rain_height, TFT_BLACK, TFT_WHITE);}
else if (weatherName == "Drizzle") {}
else if (weatherName == "Thunderstorm") {LCD.drawXBitmap(130, 65, icon_thunderstorm, icon_thunderstorm_width, icon_thunderstorm_height, TFT_BLACK, TFT_WHITE);}
else {LCD.drawString(weatherName, 180, 120);}
}
void drawWeatherIcon_meteo(int code){
Serial.println("Weather Code: " + code);
// SUNNY
if (code == 2){LCD.drawXBitmap(130, 65, icon_clouds, icon_clouds_width, icon_clouds_height, TFT_BLACK, TFT_DARKGREY);}
else if (code == 3) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
// CLOUDY
else if (code == 45) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
// DRIZZLE
else if (code == 51) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 53) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 55) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
// RAIN
else if (code == 61) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 63) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 65) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
// RAIN SNOW
else if (code == 71) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 73) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 75) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
// HEAVY RAIN
else if (code == 80) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 81) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 82) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
// THUNDERSTORM
else if (code == 95) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else if (code == 99) {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
else {LCD.drawString(mapWeatherCode_id(code), 180, 120);}
}