// keyless
// DATA BLE ASUN
// 1. Amazfit T-Rex, Address: c3:39:ed:41:a4:a6
// 2. Mi Smart Band 4, Address: cd:0b:a8:85:32:1f
// 3. Mi Band 3, Address: eb:b0:13:96:32:16
// 4. B01_M4, Address: 02:fc:da:d7:dc:97
// BOARD: FireBeetle.esp32
// Flash : 80mhz
// upload : 921600
// progrmmer : avr ISP
// elco 1uf/10uf/25uf
// + ke EN & - ke GND
// tidak perlu tekan apapun
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#define kontak 2 // PIN D2
String knownBLEAddresses[] = {"c3:39:ed:41:a4:a6"}; //masukan address itag kalian AMAZFIT T RTEX
int device_found;
int scanTime = 1; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
boolean dapat = false;
for (int i = 0; i < (sizeof(knownBLEAddresses) / sizeof(knownBLEAddresses[0])); i++) {
if (strcmp(advertisedDevice.getAddress().toString().c_str(), knownBLEAddresses[i].c_str()) == 0) {
dapat = true;
break; // Tambahkan break untuk efisiensi
}
}
if (dapat == true) {
Serial.println("asun86");
Serial.println(advertisedDevice.getRSSI());
if (advertisedDevice.getRSSI() > -50) { // rssi = -60(dekat) || rssi = -90(jauh)
device_found = true;
} else {
device_found = false;
}
advertisedDevice.getScan()->stop();
delay(100);
}
// Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str()); //hilangkan (//) untuk mencari address itag kalian
}
};
void setup() {
Serial.begin(115200); //Enable UART on ESP32
Serial.println("Scanning..."); // Print Scanning
pinMode(kontak, OUTPUT); //make BUILTIN_LED pin as output
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //Init Callback Function
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100); // set Scan interval
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// PERBAIKAN: Gunakan pointer BLEScanResults*
BLEScanResults* foundDevices = pBLEScan->start(scanTime, false);
if (device_found == true) {
digitalWrite(kontak, HIGH);
delay(10000);
} else {
digitalWrite(kontak, LOW);
}
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(100);
}
Mi Smart Band 4, Address: cd:0b:a8:85:32:1f
Amazfit T-Rex, Address: c3:39:ed:41:a4:a6
iTAG 1, Address: 53:23:7b:ac:93:a5
iTAG 2, Address: ff:ff:11:50:e9:9a
iTAG 3, Address: ff:ff:11:51:07:e2