BLE between two ESP32

wuzipu
Posts: 6
Joined: Wed Oct 31, 2018 6:40 pm

BLE between two ESP32

Postby wuzipu » Thu Nov 01, 2018 10:17 am

Hello everybody,

I have been wondering if it was possible to use BLE to make 2 ESP32
communicate with each other. I have already tried uploading the Arduino
IDE BLE_server example to the first ESP32 and the BLE_client example to
the second Arduino. Also I have watched Kolban's videos about this topic
and I think I understand the code.

The problem is the BLE_client doesn't really do what it actually should
do. It is able to find the ESP32 running BLE_server but it just doesn't
connect to it and I don't know why and what I can do.

So the question is: "Is it even possible to make two ESP32 communicate
with each other over BLE?
And if not is it possible to make them communicate over classic
bluetooth?

Btw wifi is not an option since it is for outdoor use.

Since this project is needed for my final school work I would really
appreciate your help.

Thank you!
Attachments
Code_Client.txt
(4.84 KiB) Downloaded 2661 times
Code_Server.txt
(1.36 KiB) Downloaded 2240 times

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE between two ESP32

Postby chegewara » Thu Nov 01, 2018 12:14 pm

Short answer, yes it is possible.

Long answer, you are missing one important thing. BLE_client will connect to BLE_server device if and only when BLE_server will advertise serviceUUID and advertised serviceUUID is the same BLE_client is looking for:
https://github.com/nkolban/ESP32_BLE_Ar ... nt.ino#L78

The thing is that BLE_server example is not advertising any serviceUUID:
https://github.com/nkolban/ESP32_BLE_Ar ... no#L31-L32

There is missing one line of code:

Code: Select all

pAdvertising->addServiceUUID(SERVICE_UUID);

wuzipu
Posts: 6
Joined: Wed Oct 31, 2018 6:40 pm

Re: BLE between two ESP32

Postby wuzipu » Thu Nov 01, 2018 1:24 pm

chegewara wrote:Short answer, yes it is possible.

Long answer, you are missing one important thing. BLE_client will connect to BLE_server device if and only when BLE_server will advertise serviceUUID and advertised serviceUUID is the same BLE_client is looking for:
https://github.com/nkolban/ESP32_BLE_Ar ... nt.ino#L78

The thing is that BLE_server example is not advertising any serviceUUID:
https://github.com/nkolban/ESP32_BLE_Ar ... no#L31-L32

There is missing one line of code:

Code: Select all

pAdvertising->addServiceUUID(SERVICE_UUID);
Thanks for your fast answer.
I have added the missing line but nothing has changed so far. Did I add it in the correct line?

Code: Select all

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

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

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

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("MyESP32");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello World says Neil");
  pService->start();
  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->start();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

wuzipu
Posts: 6
Joined: Wed Oct 31, 2018 6:40 pm

Re: BLE between two ESP32

Postby wuzipu » Thu Nov 01, 2018 1:42 pm

Alright I got the problem solved.
The name of the server device "MyESP32" was too long.
As i read on this topic https://github.com/nkolban/esp32-snippets/issues/269 the name needs to be 5 characters or less so I changed it to "MyESP" and it is working now.

I thank you so much for helping me chegewara

Younes
Posts: 2
Joined: Sat Dec 05, 2020 12:50 am

Re: BLE between two ESP32

Postby Younes » Sat Dec 05, 2020 1:02 am

wuzipu wrote:
Thu Nov 01, 2018 1:24 pm
chegewara wrote:Short answer, yes it is possible.

Long answer, you are missing one important thing. BLE_client will connect to BLE_server device if and only when BLE_server will advertise serviceUUID and advertised serviceUUID is the same BLE_client is looking for:
https://github.com/nkolban/ESP32_BLE_Ar ... nt.ino#L78

The thing is that BLE_server example is not advertising any serviceUUID:
https://github.com/nkolban/ESP32_BLE_Ar ... no#L31-L32

There is missing one line of code:

Code: Select all

pAdvertising->addServiceUUID(SERVICE_UUID);
Thanks for your fast answer.
I have added the missing line but nothing has changed so far. Did I add it in the correct line?

Code: Select all

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

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

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

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("MyESP32");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello World says Neil");
  pService->start();
  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->start();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

Who is online

Users browsing this forum: PepeTheGreat and 60 guests