Ive been using BLE across several particle platforms ( P2, Boron, Tracker and Monitor One) with great success scanning for and relaying data to my back end.
However the Muron seems to have a issue with BLE.
This code for example, causes a crash/fault after running breifly but runs perfectly on Tracker and Monitor One platforms.
I have disabled the wifi to prevent wifi and BLE fighting over the same antenna.
#include "Particle.h"
SYSTEM_MODE(AUTOMATIC);
SYSTEM_THREAD(ENABLED);
SerialLogHandler logHandler(LOG_LEVEL_TRACE);
void scanForDevices();
void discoverServicesAndCharacteristics(BlePeerDevice &peerDevice);
const std::chrono::milliseconds scanInterval = 30s;
unsigned long scanLastMillis = 0;
void setup()
{
WiFi.off();
Cellular.prefer();
BLE.on(); // Turn on BLE
BLE.setScanTimeout(5000); // Set scan timeout to 5 seconds
}
void loop()
{
if (millis() - scanLastMillis >= scanInterval.count()) {
scanLastMillis = millis();
Log.info("Starting BLE scan...");
scanForDevices(); // Scan for BLE devices
}
}
// Function to scan for nearby BLE devices
void scanForDevices()
{
BleScanResult scanResults[20]; // Array to hold scan results
int foundDevices = BLE.scan(scanResults, 20); // Scan for up to 20 devices
if (foundDevices > 0)
{
Log.info("Found %d devices", foundDevices);
for (int i = 0; i < foundDevices; i++)
{
BleAddress address = scanResults[i].address();
Log.info("Device %d: %s", i + 1, address.toString().c_str());
// Try connecting to the device
BlePeerDevice peerDevice = BLE.connect(scanResults[i].address());
if (peerDevice.connected())
{
Log.info("Connected to device %d", i + 1);
// Discover all services and characteristics
discoverServicesAndCharacteristics(peerDevice);
// Disconnect after discovering the services and characteristics
peerDevice.disconnect();
Log.info("Disconnected from device %d", i + 1);
}
else
{
Log.error("Failed to connect to device %d", i + 1);
}
}
}
else
{
Log.error("No BLE devices found");
}
}
// Function to discover services and characteristics of a connected BLE device
void discoverServicesAndCharacteristics(BlePeerDevice &peerDevice)
{
// Loop through all discovered services
for (BleService &service : peerDevice.services())
{
BleUuid serviceUUID = service.UUID();
Log.info("Service UUID: %s", serviceUUID.toString().c_str());
Vector<BleCharacteristic> characteristics = peerDevice.discoverAllCharacteristics();
// Loop through all characteristics in this service
for (BleCharacteristic &characteristic : characteristics)
{
BleUuid characteristicUUID = characteristic.UUID();
Log.info(" Characteristic UUID: %s", characteristicUUID.toString().c_str());
}
}
}
Have you isolated which part is causing the SOS? For example comment out the whole if (foundDeviuces > 0) block, and so on to narrow down where the issue may be? Or maybe start from the bottom with commenting out the call to discoverServicesAndCharacteristics first and see if the code runs.
Do you get any log messages before the SOS?
Does this code work on the P2? The Boron, Tracker, and Monitor One have a completely different MCU with a different BLE stack. The P2 and M-SoM/Muon have very similar Realtel RTL872x MCUs so it would be a little unusual for it to SOS on the M-SoM and not the P2/Photon 2.
Rick, Still resetting afer a few minutes.
Dispite setting while in SEMI_AUTOMATIC:
WiFi.prefer(false);
WiFi.off();
log shows Wifi turning back on at the beginning of every BLE scan
Wait - the timestamp did not reset to 0 so the system did not reset. Is the problem that the scans stop working after the end of that log? Those messages might be from automatic connection management. Does the status LED change when that happens?
Wait - the timestamp did not reset to 0 so the system did not reset. Is the problem that the scans stop working after the end of that log? Those messages might be from automatic connection management. Does the status LED change when that happens?
Oh, I see, the reset occurs after the end of that log. But something is happening in the rest of the system right before the SOS, which explains why it happens on the M-SoM and not the P2. It might be related to an interaction between automatic connection management and BLE.
Ive set :
SYSTEM_MODE(MANUAL);
Cellular.prefer(false);
WiFi.prefer(false);
in an attempt to disable other radios but "[hal] INFO: WiFi on" continues to pop up and eventually it crashes