I would like to configure BLE serial communication using xiao esp32c3/s3.

Hello.
First of all, please know that I have no idea about coding.
I wanted to wirelessly configure some serial communication equipment, so I bought Arduino and hm-10 for the first time, and I succeeded with it to some extent.
And I bought xiao esp32c3/s3 to miniaturize this.
By the way, I found that the BLE code of this is completely different from the combination of Arduino + hm-10.
Using IDE’s BLE_UART example, sending text from a smartphone to a serial monitor is fine, but conversely, I cannot send text from a serial monitor to a smartphone.
It works well without any problems when communicating by connecting the equipment and esp32c3 by wire like the code below without using BLE.

#include <HardwareSerial.h>

HardwareSerial MySerial1(1);

void setup() {

Serial.begin(300);

MySerial1.begin(115200, SERIAL_8N1, 9, 10);

}

void loop() {

if(Serial.available()){

MySerial1.write(Serial.read());

}

if(MySerial1.available()){

Serial.write(MySerial1.read());

}

}

I would like to use BLE to create a code that enables two-way communication with my smartphone.

Hi there,
So I would recommend to look through the Examples that come up with a G search and on here with the Magnifier up in the Top right next to menu. There are many.
this is one such Generic example

Here is an example of two-way serial communication with a BLE device using an Arduino:
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX, TX

void setup() {
  BTSerial.begin(9600);  // The HC-05 default speed is 9600
  Serial.begin(9600);  // Display the data to the serial monitor
}

void loop() {
  // Check if there is any data available from the Bluetooth module
  if (BTSerial.available()) {
    // Read the data from the Bluetooth module
    char c = BTSerial.read();
    // Display the data to the serial monitor
    Serial.print(c);
  }

  // Check if there is any data available from the serial monitor
  if (Serial.available()) {
    // Read the data from the serial monitor
    char c = Serial.read();
    // Send the data to the Bluetooth module
    BTSerial.print(c);
  }
} 
This code will create a software serial port on pins 10 and 11, which will be used to communicate with the BLE device. The baud rate is set to 9600, which is the default baud rate for the HC-05 Bluetooth module.
In the loop function, the code first checks if there is any data available from the Bluetooth module. If there is, the code reads the data and displays it to the serial monitor. Next, the code checks if there is any data available from the serial monitor. If there is, the code reads the data and sends it to the Bluetooth module.
This code can be used to send and receive data between an Arduino and a BLE device. For example, you could use this code to control a robot or other device with your smartphone.

The module’s are better cable replacements, no software involved, and it treats them like a terminal.
What kind of distance are you wanting to reach?
I used this tutorial long ago for the first time and everything worked Great!

look it over , and you don’t need a PHD :nerd_face:in programming to make this stuff work.
HTH
GL :slight_smile: PJ :v:

To establish bidirectional communication between your ESP32C3 and your smartphone using BLE, you can try this code:

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

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

BLEServer* pServer = NULL;
BLECharacteristic* pTxCharacteristic;
bool deviceConnected = false;
uint8_t txValue = 0;

class MyServerCallbacks : public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

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

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

  BLEService *pService = pServer->createService(SERVICE_UUID);
  pTxCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID_TX,
                                         BLECharacteristic::PROPERTY_NOTIFY
                                       );
  pTxCharacteristic->addDescriptor(new BLE2902());

  BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(
                                          CHARACTERISTIC_UUID_RX,
                                          BLECharacteristic::PROPERTY_WRITE
                                        );

  pRxCharacteristic->setCallbacks(new BLECharacteristicCallbacks() {
      void onWrite(BLECharacteristic *pCharacteristic) {
        std::string rxValue = pCharacteristic->getValue();
        if (rxValue.length() > 0) {
          Serial.println("Received Value: " + rxValue);
        }
      }
  });

  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

void loop() {
  if (deviceConnected) {
    pTxCharacteristic->setValue(&txValue, 1);
    pTxCharacteristic->notify();
    txValue++;
    delay(10);
  }
}

Hi there,
SO a swing and a miss, do you know this code you offer doesn’t compile?

conversely the example I posted , does.
IMO, Probably not a good idea to offer advice code to try without ,mentioning the source of it and weather :index_pointing_at_the_viewer: YOU know it works, otherwise it only further confuses new users.
HTH
GL :slight_smile: PJ

What I want is not to be bypassed between software or hardware serial ports.
I wanted to use ESP32c3’s own BLE to naturally communicate with my smartphone and equipment.
I was able to send and receive text perfectly with the code below.
However, it did not work properly on equipment powered by serial communication.
For your information, UUID should only use SERVICE and CHARACTERISTIC.

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

HardwareSerial MySerial1(1);
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t txValue = 0;

#define SERVICE_UUID “0000ffe0-0000-1000-8000-00805f9b34fb”
#define CHARACTERISTIC_UUID “0000ffe1-0000-1000-8000-00805f9b34fb”
#define MAX_CHUNK_SIZE 20

class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};

void onDisconnect(BLEServer* pServer) {
    deviceConnected = false; 
}

};

class MyCharacteristicCallback : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
for (int i = 0; i < value.length(); i++) {
MySerial1.write(value[i]);
}
}
}
};

void setup() {
MySerial1.begin(115200, SERIAL_8N1, 9, 10);

BLEDevice::init("BLEtool");
BLEServer *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 MyCharacteristicCallback());
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic->setValue("Hello World"); 
pService->start();


BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();

}

void loop() {
if (deviceConnected) {
uint8_t dataBuffer[MAX_CHUNK_SIZE];
size_t dataSize = 0;

    while (MySerial1.available() && dataSize < MAX_CHUNK_SIZE) {
        dataBuffer[dataSize++] = MySerial1.read();
    }
    
    if (dataSize > 0) {
        pCharacteristic->setValue(dataBuffer, dataSize);
        pCharacteristic->notify();
    }
}

}

Hi there,
You need to put the code into the </> tags so it looks correct and is easy to read.
How is it PINS 9 & 10? (this WON’T work, BTW)
can you detail the connection required drawing…

DEVICE<-----> SERIAL1<–ESP32–>----(TERMINAL) : This works" ?

DEVICE<-----> SERIAL1<–ESP32–>****BLE ****(mobile) : This is what you want ?

is this correct? do you have a Picture of this setup?
HTH
GL :slight_smile: PJ

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

HardwareSerial MySerial1(1);
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t txValue = 0;

#define SERVICE_UUID “0000ffe0-0000-1000-8000-00805f9b34fb”
#define CHARACTERISTIC_UUID “0000ffe1-0000-1000-8000-00805f9b34fb”
#define MAX_CHUNK_SIZE 20

class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};

void onDisconnect(BLEServer* pServer) {
    deviceConnected = false; 
}
};

class MyCharacteristicCallback : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
for (int i = 0; i < value.length(); i++) {
MySerial1.write(value[i]);
}
}
}
};

void setup() {
MySerial1.begin(115200, SERIAL_8N1, 9, 10);

BLEDevice::init("BLEtool");
BLEServer *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 MyCharacteristicCallback());
pCharacteristic->addDescriptor(new BLE2902());
pCharacteristic->setValue("Hello World"); 
pService->start();


BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}

void loop() {
if (deviceConnected) {
uint8_t dataBuffer[MAX_CHUNK_SIZE];
size_t dataSize = 0;

    while (MySerial1.available() && dataSize < MAX_CHUNK_SIZE) {
        dataBuffer[dataSize++] = MySerial1.read();
    }
    
    if (dataSize > 0) {
        pCharacteristic->setValue(dataBuffer, dataSize);
        pCharacteristic->notify();
    }
}
}

The configuration I want is this.
DEVICE<-----> SERIAL1<–ESP32(BLE)–>mobile
Pin 9, 10 hardware serial work very well.
The code using the built-in BLE of esp32 is not working well.

Below is the code that communicates through the hardware serial 0,1 by adding HM-10.
This code works very well.

#include <HardwareSerial.h>

HardwareSerial MySerial0(0);
HardwareSerial MySerial1(1);

void setup() {
  MySerial0.begin(38400, SERIAL_8N1, -1, -1);
  MySerial1.begin(38400, SERIAL_8N1, 9, 10);
}

void loop() {    
  if(MySerial0.available()){
    MySerial1.write(MySerial0.read());
  }
  if(MySerial1.available()){
    MySerial0.write(MySerial1.read());
  }
}