Hi,
I am new to this forum and to Arduino programming. I am working on a project that detects human presence and sends the result by broadcasting a 1 second burst in iBeacon format. I broadcast two different UUIDs depending on presence status (one for present and one for absent)
First, I need to say I understand this is not a normal way of using an iBeacon, but I have no other choice. The receiver I am communicating with only accepts the iBeacon format.
The problem that I am facing is that I can get the Arduino to broadcast only one time. When I stop the beacon, change the UUID and restart it, the program crashes and I need to double-click the reset button to recover. I tried multiple ways and some allowed me to turn the iBeacon on and off a couple of times (less than 5 times at best). The attached test case allows me to send a 1 second burst on the first attempt and then crashes on the next one. Here is the execution summary:
- Boot, get the "Enter a Key - 'a' for Absent or 'p' for Present to send presence broadcasting:" prompt on the serial monitor
- Hit the ‘a’ Key then the system Prints “Absent” on the monitor and then broadcasts for 1 second, using the “absent” UUID
- hit the ‘p’ key : then the system Prints “Present” on the monitor, but nothing else happens (no broadcast) and the board hangs (disconnects from the serial monitor).
If I comment out the calls to the “changeBeaconUUID” code, of course the program works well forever, printing “Absent” every time I hit ‘a’ and printing “Present” every time I hit ‘p’
Has anyone worked with the beaconNano library and successfully changed the UUID on the fly multiple times? I could not find any documented bug or bug fix about this library. There was another thread similar to mine, but it was closed a while ago:
https://forum.arduino.cc/t/arduino-nano-33-ble-as-ibeacon/623266
Thanks in advance for any help
Here's my test case:
#include <ArduinoBLE.h>
#include <BeaconNano.h>
#define oneSecDelay 1000
#define presentUUID "5e3490c7946841a199dd6897cd538d2a"
#define absentUUID "468a81cc1b944cdeb5dd0e3408ed87b8"
void changeBeaconUUID(char* uuidVal)
{
BeaconNano bn;
bn.setUuid(uuidVal);
bn.setMajor(1);
bn.setMinor(2);
bn.setTx(197);
bn.startBeacon();
delay(oneSecDelay);
bn.stopBeacon();
delay(oneSecDelay);
}
void setup()
{
delay(oneSecDelay);
Serial.begin(115200);
delay(oneSecDelay);
Serial.println("Enter a Key - 'a' for Absent or 'p' for Present to send presence broadcasting:");
delay(oneSecDelay);
}
void loop()
{
int keyRead;
do
{
keyRead = Serial.read();
}
while(keyRead != 'p' && keyRead != 'a');
if (keyRead == 'p')
{
Serial.println("Présent");
delay(oneSecDelay);
changeBeaconUUID(presentUUID);
}
if (keyRead == 'a')
{
Serial.println("Absent");
delay(oneSecDelay);
changeBeaconUUID(absentUUID);
}
}