-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstofradar.cpp
37 lines (30 loc) · 962 Bytes
/
stofradar.cpp
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
#include <stdbool.h>
#include <ArduinoJson.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include "stofradar.h"
static WiFiClient *client;
static HTTPClient http;
void stofradar_begin(WiFiClient &wifiClient, const char *user_agent)
{
client = &wifiClient;
// configure HTTP client: non-chunked mode, disable connection re-use
http.useHTTP10(true);
http.setTimeout(10000);
http.setUserAgent(user_agent);
}
bool stofradar_get(double latitude, double longitude, JsonDocument & json)
{
bool result = false;
char url[128];
sprintf(url, "http://stofradar.nl:9000/air?lat=%.6f&lon=%.6f", latitude, longitude);
if (http.begin(*client, url)) {
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
DeserializationError error = deserializeJson(json, http.getStream());
result = (error == DeserializationError::Ok);
}
http.end();
}
return result;
}