-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeolocate.cpp
67 lines (57 loc) · 1.87 KB
/
geolocate.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
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
#include <stdbool.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include "geolocate.h"
bool geolocate(WiFiClient &wifiClient, float &latitude, float &longitude, float &accuracy)
{
// scan for networks
printf("Scanning...");
int n = WiFi.scanNetworks();
printf("%d APs found...", n);
// create JSON request
DynamicJsonDocument doc(4096);
doc["considerIp"] = "true";
JsonArray aps = doc.createNestedArray("wifiAccessPoints");
int num = 0;
for (int i = 0; i < n; i++) {
// skip hidden and "nomap" APs
if (WiFi.isHidden(i) || WiFi.SSID(i).endsWith("_nomap")) {
continue;
}
JsonObject ap = aps.createNestedObject();
ap["macAddress"] = WiFi.BSSIDstr(i);
ap["signalStrength"] = WiFi.RSSI(i);
if (++num == 20) {
// limit scan to some arbitrary number to avoid alloc fails later
break;
}
}
printf("%d APs used\n", num);
String json;
serializeJson(doc, json);
// send JSON with POST
HTTPClient httpClient;
httpClient.begin(wifiClient, "stofradar.nl", 9000, "/v1/geolocate");
httpClient.addHeader("Content-Type", "application/json");
int res = httpClient.POST(json);
bool result = (res == HTTP_CODE_OK);
String response = result ? httpClient.getString() : httpClient.errorToString(res);
httpClient.end();
if (res != HTTP_CODE_OK) {
printf("HTTP code %d\n", res);
return false;
}
// parse response
printf("%s\n", response.c_str());
doc.clear();
if (deserializeJson(doc, response) != DeserializationError::Ok) {
printf("Failed to deserialize JSON!\n");
return false;
}
JsonObject location = doc["location"];
latitude = location["lat"];
longitude = location["lng"];
accuracy = doc["accuracy"];
return true;
}