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);
}
}