-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathESP32_BLE_Scanner.ino
More file actions
138 lines (118 loc) · 3.82 KB
/
ESP32_BLE_Scanner.ino
File metadata and controls
138 lines (118 loc) · 3.82 KB
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
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleScan.cpp
*/
#include <Arduino.h>
#include <sstream>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
//*************************************************
// Defines
//*************************************************
#define LED_PIN 21 // Pin for LED to indicate beacon presence
#define MIN_RSSI -80 // Minimum RSSI [db] needed to perform address comparison
#define SCAN_TIME 10 // seconds
#define LOOP_TIME 5 // seconds
#define NO_DEVICE_TIMER 3*60 // seconds
// comment the follow line to disable serial message
#define SERIAL_PRINT
// comment this out to switch off the device listing using callback;
// Seems this has memory leak
//#define DEVICE_LIST_OUTPUT
//*************************************************
// Global Variables
//*************************************************
//Device Address List. Include a devices' address
String addrList[] = {"90:dd:5d:a2:a2:80", "18:4f:32:46:42:2a", "00:9e:c8:ae:d8:c1", "25:db:53:34:04:ce", " 0c:d1:5b:d9:c3:e2"};
long noDevicesMillis = 0, cycleCnt = 0;
bool present = false;
bool scanFinished = true;
//*************************************************
// BLEAdvertisedDevice Callback
//*************************************************
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
#ifdef SERIAL_PRINT
Serial.printf("Advertised Device: %s, RSSI: %d \n", advertisedDevice.getAddress().toString().c_str(), advertisedDevice.getRSSI());
#endif
}
};
//*************************************************
// Setup function. Runs once
//*************************************************
void setup()
{
pinMode(LED_PIN, OUTPUT); //GPIO output for LED
#ifdef SERIAL_PRINT
Serial.begin(115200);
Serial.println("ESP32 BLE Scanner");
#endif
}
//*************************************************
// Loop function. Runs forever
//*************************************************
void loop()
{
if(scanFinished)
{
scanFinished = false;
performScan();
}
if(!present)
{
if(millis() > noDevicesMillis + NO_DEVICE_TIMER * 1000)
{
digitalWrite(LED_PIN, LOW);
}
}
else
{
digitalWrite(LED_PIN, HIGH);
noDevicesMillis = millis();
}
}
//*************************************************
// BLE devices scan. Here ve decide if a davice belongs to list
//*************************************************
void performScan()
{
cycleCnt++;
BLEDevice::init("");
BLEScan *pBLEScan = BLEDevice::getScan(); //create new scan
#ifdef DEVICE_LIST_OUTPUT
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
#endif
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(0x50);
pBLEScan->setWindow(0x30);
#ifdef SERIAL_PRINT
Serial.printf("Start BLE scan for %d seconds...\n", SCAN_TIME);
#endif
BLEScanResults foundDevices = pBLEScan->start(SCAN_TIME);
int count = foundDevices.getCount();
present = false;
for (int i = 0; i < count; i++)
{
BLEAdvertisedDevice d = foundDevices.getDevice(i);
String currDevAddr = d.getAddress().toString().c_str();
for(int j = 0; j < sizeof(addrList)/sizeof(addrList[0]); j++)
{
if(d.getRSSI() >= MIN_RSSI && addrList[j].equalsIgnoreCase(currDevAddr))
{
#ifdef SERIAL_PRINT
Serial.printf("Found device #%d with RSSI: %d \n", j, d.getRSSI());
#endif
present = true;
break;
}
}
}
#ifdef SERIAL_PRINT
Serial.println("Scan done!");
Serial.printf("Cycle counter: %d, Free heap: %d \n", cycleCnt, ESP.getFreeHeap());
#endif
scanFinished = true;
}