No longer receives connections after a while

My BLE application simply listens for a specific device to appear, and operates a solenoid. The transmitting device (a battery-less button) is generally dormant, and simply identifies itself when the button is pressed. The code is below (here I simply flash the LED when the button is pressed). The problem I have is that the code works fine for 24 hours or more, but at some point becomes unresponsive (pressing the button doesn't make the LED light). Recycling the power (9V to VIN) restores normal function for a day or so, after which it stops again. I've found the BLE documentation a bit light on detail: am I doing something obviously wrong, for example inadvertently creating some kind of space-leak? If so, it would be really helpful to understand what's going on, and see how it should be coded. Alternatively, are there any sensible questions I can ask of BLE which might help diagnose where the problem is occurring? Thanks.
Here's the code:
#include <ArduinoBLE.h>

static String bellAddress = "60:c0:bf:2a:9e:60";

void setup() {
pinMode(LED_BUILTIN, OUTPUT);

if (!BLE.begin()) {
flashLED();
}

}

void loop() {
static boolean scanning = false;
BLEDevice bell;

if (!scanning && !(BLE.scanForAddress(bellAddress, false))) return;

scanning = true;
bell = BLE.available();

if (bell) {
operate();
BLE.stopScan();
scanning = false;
}

}

void operate()
/*
* Operate the solenoid, and also flash the led for local confirmation
*/
{
digitalWrite(LED_BUILTIN, HIGH);
delay(300);
digitalWrite(LED_BUILTIN, LOW);
}

void flashLED()
{
while(1) {
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
}
}

I have a slightly different situation.
In my case I scan continually for a specific BLE address and every now and again I connect and pickup some data.
This has been working reliably for many days now.
The project is BLE Room Temperature controlled Heater

So for your use can you just scan continually and if you see your special address operate() then go back to scanning, perhaps after a short delay to let the BLE device shut down.

Hi! Thanks for the reply. As there's a load of new libraries and protocols in your code, it could take a me a while to swap over. I'd still be interested in knowing if I'm doing something wrong, or maybe if no one else has reported it, there's something wrong with my board. Thanks again for the alternative though.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.