forked from ch570512/Qlockwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenWeather.cpp
More file actions
44 lines (40 loc) · 1.47 KB
/
Copy pathOpenWeather.cpp
File metadata and controls
44 lines (40 loc) · 1.47 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
//******************************************************************************
// OpenWeather.cpp - Get weather data from OpenWeather
//******************************************************************************
#include "OpenWeather.h"
OpenWeather::OpenWeather()
{
}
OpenWeather::~OpenWeather()
{
}
uint8_t OpenWeather::getOutdoorConditions(String location, String apiKey, String langStr)
{
String response;
WiFiClient client;
if (client.connect("api.openweathermap.org", 80))
{
String url = "/data/2.5/weather?q=" + String(location) + "&units=metric&appid=" + String(apiKey) + "&lang=" + langStr;
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: api.openweathermap.org" + "\r\n" + "Connection: close\r\n\r\n");
unsigned long startMillis = millis();
while (client.available() == 0)
{
if (millis() - startMillis > 5000)
{
client.stop();
return 0;
}
}
while (client.available())
response = client.readStringUntil('\r');
response.trim();
response.replace('[', ' ');
response.replace(']', ' ');
JSONVar weatherArray = JSON.parse(response);
description = weatherArray["weather"]["description"];
temperature = (double)weatherArray["main"]["temp"];
humidity = (int)weatherArray["main"]["humidity"];
pressure = (int)weatherArray["main"]["pressure"];
return 1;
}
}