I would like to send and receive serial communication to BLE through Xiao esp32cS

First of all, please know that i have no idea about coding.

Device <-Serial-> ESP32c3+HM-10 <-BLE-> Smartphone App

I want to control the device with the above configuration and it was successful.
Below is the code.

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

I want to configure using ESP32c3 own BLE instead of ESP32c3+HM-10 to reduce the size, but I don’t know how.

Like HM-10, smartphone apps are built based on TI’s chips and send and receive Rx and Tx with one Characteristic UUID.
However, the BLE example of ESP32c3 uses two Rx, Tx Characteristic UUID to send and receive, can I use only one like HM-10?

Below is the code I made when I exchanged conversations with ChatGPT.
The code worked very well when we exchanged texts like putty and hyperterminal.
However, controlling the device failed.

#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() {
    Serial.begin(115200);
    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();
        }
    }
}

LOL,
How did you know? :slight_smile: Hi there,
Yes it is possible but it takes some additional steps and I don’t think the Hardware serial supports 9, 10 AFAIK?
Are you not confusing the UUID service vs, UUIDcharachteristic You would need one that’s READ & WRITE with Notify to do it.
Can you show the connections you’ve made (picture) and clarify what failed in controlling the device?
HTH
GL :slight_smile: PJ

Oh, there was a typo using the translator… lol

1 Like

I’m not sure what AFAIK is, but the hardware serial on pin 9,10 works fine.
A program that allows you to input commands into a specific machine to receive feedback or give commands directly.
It’s not a personal program, so I don’t think I can take pictures because of security…
Loading does not proceed after connecting to the device.
I just know it’s based on TI’s cc25xx BLE.

Below is the manual provided by TI.

Serial Port Service
The serial port service is designed to be as versatile and easy to use as
possible. It is not protocol specific, and can be adapted to most UART
communication protocols (RS-232, RS-485, etc).
The serial port service has been given the
UUID : F000C0E0-0451-4000-B000-00000000-0000
The service comprises of three characteristics :
• Data characteristic (UUID : F000C0E1-0451-4000-B000-00000000-0000 )
• Status characteristic (UUID : F000C0E2-0451-4000-B000-00000000-0000 )
• Config characteristic (UUID : F000C0E3-0451-4000-B000-00000000-0000 )

Data characteristic
The data characteristic is used to transfer the serial port data. It can
handle packets of arbitrary length up to 20 bytes per packet.
Properties of the data characteristic :
• Notify
• Write
When central (client) has data to send, it writes this data to the
peripheral’s data characteristic, and will receive notifications when the
peripheral (server) has received data from it’s serial port.

Hi there,
OK I so if the hardware is working then You can duplicate the Serial Port service and Characteristics the same way.
AFAIK, shorthand for " As Far As I Know… :v:"
So either side write the data char, and sends a NOTIFY when data is available for each side of the FDX connection (full duplex) or Half duplex with a line turnaround with Status notify?
Is what I would expect.
Configuration is in another channel it appears. with out seeing the code it’s my best guess.
I don’t see any issues with this should be Easy to emulate.
Appears to be a BLE serial cable replacement project. Cool
HTH
GL :slight_smile: PJ

Haha, I’m sorry.
I don’t understand what you mean…
What should a beginner search for to study something like this from scratch.

Hi there,
LOL, Ok so start with this one first , and the second one has more of what you want to do.
check those out and then come back and ask any questions you might have. It’s not Rocket Science but there’s a procedure of steps to follow and success will be yours :v:
HTH
GL :slight_smile: PJ

if you can read and comprehend at least a little of these you’ll get very proficient at BLE comms.

also this third one is kinda dated , but good to know stuff.

Maybe you can check this wiki:

My old esp32c3 died with the smell of burning… haha
I’ll try again when a new product comes.

Hi there,
Congratulations… :sweat_smile:
When your out on the cutting edge , expect to bleed. :smile: :v:
GL :slight_smile: PJ