arduinoWebSockets icon indicating copy to clipboard operation
arduinoWebSockets copied to clipboard

Not able to run arduinoWebSockets while doing BLE scan

Open dominicklee opened this issue 3 years ago • 2 comments

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

dominicklee avatar Feb 08 '23 12:02 dominicklee

have you tried pinning the scanning to core 1?

kakopappa avatar Feb 15 '23 08:02 kakopappa

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!

dominicklee avatar Feb 15 '23 08:02 dominicklee