BLE ESP32. Bluetooth. Multiconnect. Connect two mobiles at the same time

Hello friends,

in this topic we saw how we could connect two ESP32s to a mobile phone at the same time.

Now we are going to see how we can connect an ESP32 to several mobiles at the same time. Multiconnect.

We are based on this code from nkolban but simplified and very modified:
github.com/nkolban/ESP32_BLE_Arduino/blob/master/examples/BLE_server_multiconnect/BLE_server_multiconnect.ino

Here this tutorial in Spanish:
http://kio4.com/arduino/160B_Wemos_ESP32_BLE.htm

2 Likes

1.- An ESP32 sends random data to several mobiles at the same time. Notify.

p110i_esp32_ble_notifica_varios.aia (202.2 KB)

  • An ESP32 sends three random numbers in the way 66,57,183 to several mobiles at the same time by notification.

// Juan A. Villalpando
// http://kio4.com/arduino/160B_Wemos_ESP32_BLE.htm

#include <BLEDevice.h>
#include <BLEServer.h>

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      BLEDevice::startAdvertising();
    };
};

void setup() {
  Serial.begin(115200);

  BLEDevice::init("ESP32");
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  BLEService *pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );

  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  BLEDevice::startAdvertising();
}

void loop() {
        String tempe_1 = (String) random(10,100);
        String tempe_2 = (String) random(10,100);
        String humedit = (String) random(100,200);
        String send_data = tempe_1 + "," + tempe_2 + "," + humedit;
        pCharacteristic->setValue(send_data.c_str());
        pCharacteristic->notify();
        int random_delay = random(500,1500);
        delay(random_delay);
}

2 Likes

2.- An ESP32 sends different data to two mobiles at the same time.

p110i_esp32_ble_notifica_caracter.aia (171.0 KB)

  • ESP32 creates:

temp_1,humedit // temp1 with 1 digit
temp_2,humedit // temp2 with 2 digits

If mobile RegisterForStrings CHARACTERISTIC_UUID_1, receive: temp_1,humedit

If mobile RegisterForStrings CHARACTERISTIC_UUID_2, receive: temp_2,humedit

  • The two mobiles can receive the information at the same time.

// Juan A. Villalpando
// http://kio4.com/arduino/160B_Wemos_ESP32_BLE.htm

#include <BLEDevice.h>
#include <BLEServer.h>

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic_1 = NULL;
BLECharacteristic* pCharacteristic_2 = NULL;

#define SERVICE_UUID          "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID_1 "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define CHARACTERISTIC_UUID_2 "beb5483f-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      BLEDevice::startAdvertising();
    };
};

void setup() {
  Serial.begin(115200);

  BLEDevice::init("ESP32");
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  BLEService *pService = pServer->createService(SERVICE_UUID);
  pCharacteristic_1 = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_1,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
  pCharacteristic_2 = pService->createCharacteristic(
                      CHARACTERISTIC_UUID_2,
                      BLECharacteristic::PROPERTY_NOTIFY
                    );

  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  BLEDevice::startAdvertising();
}

void loop() {
        String tempe_1 = (String) random(0,9);    // ONE DIGIT
        String tempe_2 = (String) random(10,90);  // TWO DIGITS
        String humedit = (String) random(100,200);
        // CON CARACTERISTICA 1
        String send_data_ch1 = tempe_1 + "," + humedit;
        pCharacteristic_1->setValue(send_data_ch1.c_str());
        pCharacteristic_1->notify();
        // CON CARACTERISTICA 2
        String send_data_ch2 = tempe_2 + "," + humedit;
        pCharacteristic_2->setValue(send_data_ch2.c_str());
        pCharacteristic_2->notify();
        
        int random_delay = random(500,1500);
        delay(random_delay);
}

2 Likes

3.- Two mobiles send a text to an ESP32 at the same time. ESP32 returns a random number.

p110i_esp32_ble_recibe_caracter.aia (201.8 KB)

  • A mobile sends a text to the ESP32, for example "Mobile color Blue".

  • Another mobile sends another text to ESP32, for example "Mobile color Black".

  • The two mobiles are connected to ESP32 at the same time.

  • ESP32 returns a response, a random number.

  • We can consult the text that arrives at ESP32 through the Serial Monitor.

esp32_41

// Juan A. Villalpando
// http://kio4.com/arduino/160B_Wemos_ESP32_BLE.htm

#include <BLEDevice.h>
#include <BLEServer.h>

BLEServer* pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      BLEDevice::startAdvertising();
    };
};

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();
      String alea = (String) random(1,10000);  // Return a random number.
      pCharacteristic->setValue(alea.c_str()); // Devuelve un número aleatorio

      if (value.length() > 0) {
        String valor = "";
        for (int i = 0; i < value.length(); i++){
          valor = valor + value[i];
        }

        Serial.println(valor); // Presenta valor.
      }
    }
};

void setup() {
  Serial.begin(115200);
  
  BLEDevice::init("ESP32");
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
 
  
  BLEService *pService = pServer->createService(SERVICE_UUID);
   pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );
                                          
  pCharacteristic ->setCallbacks(new MyCallbacks());                                                
  pService->start();

// Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  BLEDevice::startAdvertising();
}

void loop() {
// 
}

3 Likes

4.- Chat with ESP32 as Server.

p110i_esp32_ble_multiconnect_chat.aia (202.1 KB)

  • We are going to build a chat using ESP32 as a Server.

  • A mobile will send a message to ESP32, that message will return to that mobile, and it will also be sent to another mobile that is connected.

  • That is to say, the ESP32 will be the intermediary of the messages, it will receive a message and it will forward it to all the mobiles connected to it.

  • The forwarding is done by notification.

// Juan A. Villalpando
// http://kio4.com/arduino/160B_Wemos_ESP32_BLE.htm

#include <BLEDevice.h>
#include <BLEServer.h>

BLEServer* pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;

BLEServer* pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      BLEDevice::startAdvertising();
    };
};

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();

      if (value.length() > 0) {
        String valor = "";
        for (int i = 0; i < value.length(); i++){
          valor = valor + value[i];
        }
      
        pCharacteristic->setValue(valor.c_str());
        pCharacteristic->notify();
        Serial.println(valor); // Presenta valor.
      }
    }
};

void setup() {
  Serial.begin(115200);
  
  BLEDevice::init("ESP32");
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
 
  
  BLEService *pService = pServer->createService(SERVICE_UUID);
   pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE |
                                         BLECharacteristic::PROPERTY_NOTIFY
                                       );
                                          
  pCharacteristic ->setCallbacks(new MyCallbacks());                                                
  pService->start();

// Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  BLEDevice::startAdvertising();
}

void loop() {
//
}

I think we need to update this for year 2023 and new android version for permissions?

1 Like