Not able to run arduinoWebSockets while doing BLE scan
I am trying to run the ArduinoWebSockets library on an ESP32. The example code runs well and I am able to establish a connection to my websocket server. However, I would like to run a scan for nearby Bluetooth (BLE) devices and send the list of device names, MAC address, and RSSI using ArduinoWebSockets library. This combination of websockets and BLE seems to not work.
I have seen that the ESP32 is technically capable of switching between Bluetooth and WiFi without issues. In fact, there is another author who wrote a solution just to scan BLE devices and send them via MQTT. However, I really want to use ArduinoWebSockets library because I have a websocket server which is already on a production server.
When I try doing BLEDevice::init, the webSocket object just decides not to connect. I can't seem to get the ESP32 to perform a scan while maintaining the webSocket connection. Something freezes and it does not connect. Both the BLE scan and the ArduinoWebSockets library works individually, but not in the same code, the way I intended. Please help, its quite urgent.
Thank you in advance!
Here is the code I tried:
#include <WiFi.h>
#include <BLEDevice.h>
#include <BLEScan.h>
#include <BLEUtils.h>
// ....other includes for Arduino Websockets and WiFi Manager....
// Create a BLE scanner
BLEScan* pBLEScan;
// ...webSocketEvent callback goes here...
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
WiFiMulti.addAP("SSID", "passpasspass");
//WiFi.disconnect();
while(WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
// server address, port and URL
webSocket.begin("example.com", 3000, "/");
// event handler
webSocket.onEvent(webSocketEvent);
// use HTTP Basic Authorization this is optional remove if not needed
webSocket.setAuthorization("user", "Password");
// try ever 5000 again if connection has failed
webSocket.setReconnectInterval(5000);
// Initialize BLE
BLEDevice::init("ESP32");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
}
void loop() {
// Start a BLE scan
BLEScanResults foundDevices = pBLEScan->start(10);
Serial.println("Scan complete");
// Wait for the next scan cycle
webSocket.loop();
delay(100);
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
// Print the device name, MAC address, and RSSI
Serial.print("Device name: ");
Serial.println(advertisedDevice.getName().c_str());
Serial.print("MAC address: ");
Serial.println(advertisedDevice.getAddress().toString().c_str());
Serial.print("RSSI: ");
Serial.println(advertisedDevice.getRSSI());
}
};
have you tried pinning the scanning to core 1?
What I ended up doing is changed the BLE library to NimBLE. After that, everything started working normally on the ESP32. Again, thanks for your wonderful websocket library!