-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWAScan.ino
113 lines (89 loc) · 2.81 KB
/
CWAScan.ino
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
#include <BLEDevice.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include "CWAScanConfig.h"
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
// Not sure if WiFiClientSecure checks the validity date of the certificate.
// Setting clock just to be sure...
void setClock() {
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Serial.print(F("Waiting for NTP time sync: "));
time_t nowSecs = time(nullptr);
while (nowSecs < 8 * 3600 * 2) {
delay(500);
Serial.print(F("."));
yield();
nowSecs = time(nullptr);
}
Serial.println();
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
}
void wifiConnect() {
// Connecting to Wifi
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
// wait for WiFi connection
Serial.print("Waiting for WiFi to connect...");
while ((WiFi.status() != WL_CONNECTED)) {
delay(1000);
Serial.print(".");
}
Serial.println(" connected");
}
void setup() {
Serial.begin(115200);
wifiConnect();
setClock();
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
// Filter for CWA service UUID
std::vector<std::string> cwaAddresses;
for (int i = 0; i < foundDevices.getCount(); ++i) {
if (foundDevices.getDevice(i).getServiceUUID().toString().rfind("0000fd6f", 0) == 0) {
cwaAddresses.push_back(foundDevices.getDevice(i).getAddress().toString());
}
}
WiFiClientSecure client;
client.setCACert(ROOT_CA);
HTTPClient https;
if (https.begin(client, REMOTE_URL)) {
https.setAuthorization(BASIC_AUTH_USER, BASIC_AUTH_PASSWORD);
https.addHeader("Content-Type", "application/json");
const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(cwaAddresses.size()) + JSON_OBJECT_SIZE(3);
DynamicJsonDocument doc(capacity);
doc["time"] = time(nullptr);
JsonArray location = doc.createNestedArray("location");
location.add(53.56);
location.add(10.00);
JsonArray data = doc.createNestedArray("data");
for (const std::string& addr : cwaAddresses) {
data.add(addr.c_str());
}
String jsonData;
serializeJson(doc, jsonData);
Serial.println(jsonData);
int resp = https.POST(jsonData.c_str());
Serial.println(resp);
if (resp < 0) {
// Some connection problem, try to reconnect
wifiConnect();
}
https.end();
}
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}