-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBLE_Logger.ino
226 lines (192 loc) · 6.19 KB
/
BLE_Logger.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
const int ledPin = 2;
#include <ArduinoJson.h>
//**********************************************
#include <mySD.h>//SD Card library code
//**********************************************
//VSPI pins
#define MOSI 23
#define MISO 19
#define SCK 18
#define CS 5
File root;
//**********************************************
//DS3231 RTC Stuff
#include <Wire.h>
#include "DS3231.h"
RTClib RTC;
char* ssid_retrieve() {
uint64_t chipid = ESP.getEfuseMac(); // The chip ID is essentially its MAC address(length: 6 bytes).
uint16_t chip = (uint16_t)(chipid >> 32);
static char ssid[23];
snprintf(ssid, 23, "%04X%08X", chip, (uint32_t)chipid);
return ssid;
}
//Beacon List
const int NUMBER_OF_ELEMENTS = 9;
const int MAX_SIZE = 18;
char beacons [NUMBER_OF_ELEMENTS] [MAX_SIZE] = {
{ "d0:fa:94:da:23:64" }, //Beacon #1
{ "d2:67:5e:27:cf:0b" }, //Beacon #2
{ "cc:8c:56:f5:68:29" }, //Beacon #3
{ "ef:e2:d7:a1:5d:ca" }, //Beacon #4
{ "e8:ff:31:f2:fc:19" }, //Beacon #5
{ "d9:00:a5:f0:e4:7d" }, //Beacon #6 (out of commission, damaged)
{ "e4:fd:3c:56:87:99" }, //Beacon #7
{ "c5:20:67:1c:21:b7" }, //Beacon #8
{ "eb:ce:9a:73:50:a0" }, //Beacon #9
};
bool mac_Check(const char test_mac[]) {
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {//Checking to see if the Bluetooth Device is one of ours
if (strcmp(test_mac, beacons[i]) == 0) {
return true; //Match!
}
}
return false; //No match.
}
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 10; //In seconds
int time_between_scans = 60; //In seconds
BLEScan* pBLEScan;
/*
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
*/
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
Wire.begin();
DateTime now = RTC.now();
//char fileName[] = "380EC6C40A24.csv"; //380EC6C40A24.csv
//fileName = String(ssid_retrieve()) + ".csv";
//sprintf(fileName, "%s.csv", ssid_retrieve());
//Serial.println(fileName);
//fileName[17] = '\0';
SD.begin(CS, MOSI, MISO, SCK);
root = SD.open("/");
//creating the custom log name, based on first four chars of hardware id
char fileName[9];
for (uint32_t i = 0; i < 4; i++)
{
fileName[i] = ssid_retrieve()[i];
}
fileName[4] = '\0';
strcat( fileName, ".csv" );
root = SD.open("/");
Serial.print("Logging data to ");
Serial.print(fileName);
if (SD.exists(fileName)) {
Serial.println(" (already exists).");
}
else {
Serial.println(" (doesn't exist yet, creating).");
}
root = SD.open(fileName, FILE_WRITE);
Serial.print("Scanning for ");
Serial.print(scanTime);
Serial.println(" seconds.");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
//pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.println(now.year(), DEC);
//Serial.println(ssid_retrieve());
int deviceCount = foundDevices.getCount();
int beaconCount = 0;
for (uint32_t i = 0; i < deviceCount; i++)
{
BLEAdvertisedDevice device = foundDevices.getDevice(i);
if (mac_Check(device.getAddress().toString().c_str())) //this matches our list of addresses, let's log it
{
//digitalWrite(ledPin, HIGH);
beaconCount++;
//Serial.print(device.getAddress().toString().c_str());
//Serial.println(" matches our list of addresses, logging it");
//char beacon_name[9];
//beacon_name = device.getName();
//strcpy(beacon_name, device.getName());
const size_t capacity = JSON_OBJECT_SIZE(5);
DynamicJsonDocument doc(capacity);
doc["sensor"] = ssid_retrieve();
doc["beacon_name"] = device.getName().c_str();
//doc["beacon_uuid"] = device.getAddress().toString().c_str();
doc["rssi"] = device.getRSSI();
doc["time_seen"] = now.unixtime();
serializeJson(doc, Serial);
Serial.println("");
root.print(ssid_retrieve());
root.print(", ");
root.print(device.getName().c_str());
root.print(", ");
root.print(device.getAddress().toString().c_str());
root.print(", ");
root.print(device.getRSSI());
root.print(", ");
root.print(now.month(), DEC);
root.print('/');
root.print(now.day(), DEC);
root.print('/');
root.print(now.year(), DEC);
root.print(' ');
root.print(now.hour(), DEC);
root.print(':');
if (now.minute() < 10) {
root.print("0");
}
root.print(now.minute(), DEC);
root.print(':');
if (now.second() < 10) {
root.print("0");
}
root.print(now.second(), DEC);
root.println("");
}
}
// test area
root.flush();
/* close the file */
delay(100);
root.close();
delay(100);
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
Serial.print(beaconCount);
Serial.println(" beacons seen during last scan.");
Serial.print("Waiting ");
Serial.print(time_between_scans);
Serial.println(" seconds for restart and next scan.");
digitalWrite(ledPin, LOW);
delay(500);
for (uint32_t i = 0; i < beaconCount; i++)
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
delay(abs((time_between_scans * 1000) - (beaconCount * 1000)));
digitalWrite(ledPin, LOW);
ESP.restart();
}
void loop() {
// put your main code here, to run repeatedly:
//BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
//Serial.print("Devices found: ");
//Serial.println(foundDevices.getCount());
//Serial.println("Scan done!");
}