-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_reciever.cpp
64 lines (53 loc) · 1.39 KB
/
ESP32_reciever.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
#include <BLEAdvertisedDevice.h>
#include <BLEDevice.h>
#include <BLEScan.h>
#include <BLEAddress.h>
#include <string>
using namespace std;
const int PIN2 = 23;
const int PIN = 2;
const int COMM = 22;
const int CUTOFF = -55;
const int BUZZ = -65;
string want = "unset";
void setup() {
pinMode(PIN, OUTPUT);
pinMode(PIN2, OUTPUT);
pinMode(COMM, OUTPUT);
BLEDevice::init("");
Serial.begin(115200);
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(1);
int best = -1e5;
for (int i = 0; i < results.getCount(); i++) {
BLEAdvertisedDevice device = results.getDevice(i);
int rssi = device.getRSSI();
if (rssi > best) {
best = rssi;
want = device.getAddress().toString();
}
}
}
void loop() {
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(1);
int best = -1e5;
for (int i = 0; i < results.getCount(); i++) {
BLEAdvertisedDevice device = results.getDevice(i);
BLEAddress address = device.getAddress();
int rssi = device.getRSSI();
if (want == address.toString()) {
best = rssi;
}
}
digitalWrite(PIN, best > CUTOFF ? HIGH : LOW);
if(best > CUTOFF){
digitalWrite(COMM, HIGH);
} else{
digitalWrite(COMM, LOW);
}
digitalWrite(PIN2, ((CUTOFF>best) && (best > BUZZ)) ? HIGH : LOW);
Serial.println(best);
}