-
Notifications
You must be signed in to change notification settings - Fork 0
/
sniffer-api.ino
81 lines (66 loc) · 2.21 KB
/
sniffer-api.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
// Smell MAc address from wifi area and post a json to a server.
// Show a fake ssid.
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <vector>
const char* apSsid = "Free Wifi";
const char* apPassword = ""; //no password
const char* clientSsid = "************";
const char* clientPassword = "******************";
HTTPClient http;
WiFiEventHandler probeRequestPrintHandler;
String macToString(const unsigned char* mac) {
char buf[20];
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(buf);
}
std::vector<WiFiEventSoftAPModeProbeRequestReceived> myList;
void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
myList.push_back(evt);
}
void setup() {
Serial.begin(115200);
Serial.println("Hello, is now set up!");
// Don't save WiFi configuration in flash - optional
WiFi.persistent(false);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(apSsid, apPassword);
WiFi.begin(clientSsid, clientPassword);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("");
probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
}
void loop() {
delay(3000);
String json = "";
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonArray& probes = root.createNestedArray("probes");
for(WiFiEventSoftAPModeProbeRequestReceived w : myList){
JsonObject& probe = probes.createNestedObject();
probe["mac"] = macToString(w.mac);
probe["rssi"] = w.rssi;
probe["cluster_id"] = 1;
probe["read_at"] = '2019-03-03 22:22:22';
}
myList.clear();
root.printTo(json);
Serial.println("json:" + json);
if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
http.begin("http://nodes.ezequielfernandez.com/api/nodes");
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(json);
Serial.println(httpCode);
String payload = http.getString();
Serial.println(payload); //Print request response payload
http.end();
}else{
Serial.print("Error in Wifi connection");
}
}