Hello Guys,
I am having an issue with my Nano 33 BLE and hoping you can give me some hand with solving it.
The idea is a something like super simple "keyless access" as cars do.
Arduino scans for devices and if it finds a BLE Tag- you got a green light, system is unlocked.
For that I am doing a scan every 3 seconds.
If I find a key- corresponding flag is set
If key was not found during last 3 scans (no one is perfect)- system goes to lock state
Everything works perfectly until I'm going to some big streets with lots of cars and people around
Arduino then crashes and freezes (I can see it on OLED display, so it's not like just no key)
Here's the sources of scanning code:
#include <ArduinoBLE.h>
String keyUUID = "00ff";
String keyAddress[2];
String keyName = "Tile";
double rescanTime = 3.0; // Seconds
int rescanTries = 0;
int rescanLimit = 3;
bool keyFound = false; // Key found during current scan session
unsigned long lastScan = 0;
bool scanExpired = false;
bool scanActive = false;
bool rescanRequired = false;
double rescanTimeout = 1; // Sec, time between scans
unsigned long rescanStopped = 0;
void securitySetup() {
// begin initialization
keyAddress[0] = "59:9e:c7:22:45:38";
keyAddress[1] = "4c:1f:20:37:b5:0d";
// Lock status LED
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.begin(9600);
//while (!Serial);
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
Serial.println("BLE Central scan");
rescan();
}
void rescanRequest(){
BLE.stopScan();
scanActive = false;
rescanStopped = millis();
lastScan = millis();
}
void rescan(){
//Serial.println("Rescan");
keyFound = false;
scanExpired = false;
BLE.scanForUuid(keyUUID);
//BLE.scanForAddress(keyAddress[0]);
scanActive = true;
lastScan = millis();
}
void rescanManager(){
if(!scanActive){
if(millis() - rescanStopped >= (rescanTimeout * 1000)){
rescan();
}
}
}
void securityLoop() {
// put your main code here, to run repeatedly:
BLEDevice peripheral = BLE.available();
if (peripheral) { // Devices discovered
if (peripheral.hasAdvertisedServiceUuid()) {
for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
if(peripheral.advertisedServiceUuid(i) == keyUUID && peripheral.hasLocalName() && peripheral.localName() == keyName && (peripheral.address() == keyAddress[0] || peripheral.address() == keyAddress[1])){
//Serial.print("Key found in ");
//Serial.print(millis() - lastScan);
//Serial.println("ms");
if(!unlocked){
Serial.println("Key found");
}
rescanTries = 0;
unlocked = true;
keyFound = true;
}
}
}
}
scanExpired = (millis() - lastScan) > (1000 * rescanTime);
if(scanExpired){ // Time for rescan to check if key is still there
if(!keyFound){
rescanTries += 1;
Serial.print("No key, retry, att. ");
Serial.println(rescanTries);
if(rescanTries >= rescanLimit){
unlocked = false;
rescanTries = rescanLimit;
Serial.println("No key, lock");
}
}
rescanRequest();
}
rescanManager();
}
I am thinking maybe about some overflowing that occurs for too much peripherals results.
But that is why I am using scanForUUID, just to filter results and only read useful tags.
Or maybe some devices may crash the scan procedure by some reason.
Has any of you saw something like that?
Would be super cool to solve that issue
Thanks