ESP32 Inter-Chip Serial Communication

Hi there,

I'm trying to connect two ESP32 DOIT DevKit V1 boards (AliExpress) through a serial connection. My intention is for the master board to send a command over serial to the slave board, which would then start its Bluetooth functionality. This would allow a device to connect and stream audio to an external DAC via I2S.

With the test code below, I was simulating this, but it doesn't work as expected. I only see "Master -> hello" on the slave's serial monitor, but nothing appears on the master's serial monitor. However, if I change the code to use a string instead of the structured data, it works perfectly.

Any clues as to what I might be doing wrong here?

Physical Wiring:
Master TX (GPIO17) → Slave RX (GPIO16)
Master RX (GPIO16) ← Slave TX (GPIO17)
GND → GND

Master code

HardwareSerial CommSerial(2);

const int MASTER_RX_PIN = 16;
const int MASTER_TX_PIN = 17;

unsigned long last_ms = 0;
const int interval_ms = 1000;

typedef struct struct_message {
  byte command;
  char device_name[50];
} struct_message;

void setup() {
  Serial.begin(9600);
  CommSerial.begin(9600, SERIAL_8N1, MASTER_RX_PIN, MASTER_TX_PIN);
}

void loop(){ 
  if (millis() - last_ms > interval_ms) {
    last_ms = millis();

    SendBTCommands(1, "Master -> hello");
  }

  handleBTIncomingDate();
}

void SendBTCommands(byte cmd, const char* name) {
  struct_message myMessage;
  myMessage.command = cmd;
  strcpy(myMessage.device_name, name);

  CommSerial.write((byte*)&myMessage, sizeof(myMessage));
}

void handleBTIncomingDate() {
  if (CommSerial.available()) {
    struct_message rx_msg_struct;

    uint8_t incomingData[sizeof(rx_msg_struct)];
    size_t  rx_msg_length;

    rx_msg_length = CommSerial.readBytesUntil('\n', incomingData, sizeof(incomingData));

    if (rx_msg_length == sizeof(incomingData)) {  // got a msg from a sensor
      memcpy(&rx_msg_struct, incomingData, sizeof(rx_msg_struct));

      Serial.println(rx_msg_struct.device_name);
    }
  }
}

Slave code

HardwareSerial CommSerial(2);

const int SLAVE_RX_PIN    = 16;
const int SLAVE_TX_PIN    = 17;

unsigned long last_ms = 0;
const int interval_ms = 1000;

typedef struct struct_message {
  byte command;
  char device_name[50];
} struct_message;

void setup() {
  Serial.begin(9600);
  CommSerial.begin(9600, SERIAL_8N1, SLAVE_RX_PIN, SLAVE_TX_PIN);
}

void loop(){ 
  if (millis() - last_ms > interval_ms) {
    last_ms = millis();

    CommSerial.println("Slave -> hello");
  }

  handleBTIncomingDate();
}

void SendBTCommands(byte cmd, const char* name) {
  struct_message myMessage;
  myMessage.command = cmd;
  strcpy(myMessage.device_name, name);

  CommSerial.write((byte*)&myMessage, sizeof(myMessage));
}

void handleBTIncomingDate() {
  if (CommSerial.available()) {
    struct_message rx_msg_struct;

    uint8_t incomingData[sizeof(rx_msg_struct)];
    size_t  rx_msg_length;

    rx_msg_length = CommSerial.readBytesUntil('\n', incomingData, sizeof(incomingData));

    if (rx_msg_length == sizeof(incomingData)) {  // got a msg from a sensor
      memcpy(&rx_msg_struct, incomingData, sizeof(rx_msg_struct));

      Serial.println(rx_msg_struct.device_name);
    }
  }
}

The memcpy function does not null terminate a character sequence and the buffer is not initialised. You also presumably should be copying to the character buffer i.e. to rx_mgs_struct.device_name, rather then the struct itself.

Initialise the message buffer to null.

memset(rx_msg_struct.device_name, 0, sizeof(rx_msg_struct.device_name));

Use strncpy or strlcpy to copy the incoming data.

The length needs to be sizeof(incomingData) - 1 to accomodate the NULL terminator. Your message cannot be longer than 49 characters + NULL.

Thank you for your reply,

however I am confused as to where I should correct my code.

There was a small mistake in my slave side code. I wasn’t using the SendBTCommands() function to send the message, which I corrected as show below.

wrong code:

void loop(){ 
  if (millis() - last_ms > interval_ms) {
    last_ms = millis();

    CommSerial.println("Slave -> hello");
  }

  handleBTIncomingDate();
}

corrected code:

void loop(){ 
  if (millis() - last_ms > interval_ms) {
    last_ms = millis();

    SendBTCommands(1, "Slave -> hello");
  }

  handleBTIncomingDate();
}

However, it still didn’t give me the outputs as I expected. meaning I did not see a fullduplext communication between the board visually on both the serial monitors.

So just test I changed the delay on the slave side of the code. Both had 1 second delay, I increased the slave to 2 seconds.

const int interval_ms = 2000;

And now I can visually see both boards are communicating. And the above changes are all I did to my originally posted codes.

master monitor. See the 2 seconds gap as per the delay change. previously, I did not see anything here.

slave monitor. 1 second messages.

so, can I prasume that, hardware serial is NOT full duplex?

Or my understanding of the serial communication is wrong.

Thanks.

I think you are not terminating the message in the struct and you are seeing a timeout.

CommSerial.println("Slave -> hello");

.println will add the termination expected.

Try

SendBTCommands(1, "Slave -> hello\n");

All the documentation I see says that Serial2 is full duplex.

You are aware that the ESP32 has 2 cores that can be used individually. I don't think you need 2 boards.

hwSerial is full-duplex.

That is possible. What are the complete recent codes you are using.

Yes sir, but the issue is that as soon as I use the libraries, the sketch becomes huge, more than the available memory in the board.

#include "AudioTools.h"
#include "BluetoothA2DPSink.h"

thanks sir, but didn’t work for me.

some libraries are certainly very large, e.g. BLE
you can increase the available flash by using Tools>Partition Scheme and selecting Hugh APP

did that already with Huge App partition scheme, but still it was way above the available memory size.

interesting - I have implemented projects using Hugh APP on ESP32 microcontrollers using WiFi, BLE and LoRaWAN plus various sensors and other IO devices without problems
perhaps we need more details of the project?
if it is too large for ESP32 microcontrollers you may need to move to something an order of magnitude more powerful, e.g. Raspberry Pi 5

SendBTCommands(1, "Slave -> hello\n");

thanks sir, but didn’t work for me.

For some reason the '\n' did not append correctly into the message in the struct.

With this modification to your code I am getting good bidirectional full duplex communication with a 100 ms period.

const int interval_ms = 100;
if (millis() - last_ms > interval_ms) {
    last_ms = millis();
    SendBTCommands(1, "Slave -> hello");
    CommSerial.write('\n');
  }

if (millis() - last_ms > interval_ms) {
    last_ms = millis();

    SendBTCommands(1, "Master -> hello");
    CommSerial.write('\n');
  }
Slave Print Out

19:02:30.053 -> Master -> hello
19:02:30.147 -> Master -> hello
19:02:30.243 -> Master -> hello
19:02:30.338 -> Master -> hello
19:02:30.433 -> Master -> hello
19:02:30.576 -> Master -> hello
19:02:30.670 -> Master -> hello
19:02:30.766 -> Master -> hello
19:02:30.861 -> Master -> hello
19:02:31.003 -> Master -> hello
19:02:31.094 -> Master -> hello
19:02:31.188 -> Master -> hello
19:02:31.283 -> Master -> hello
19:02:31.426 -> Master -> hello
19:02:31.521 -> Master -> hello
19:02:31.617 -> Master -> hello
19:02:31.712 -> Master -> hello
19:02:31.854 -> Master -> hello
19:02:31.949 -> Master -> hello

Master Print Out

19:02:30.087 -> Slave -> hello
19:02:30.179 -> Slave -> hello
19:02:30.274 -> Slave -> hello
19:02:30.417 -> Slave -> hello
19:02:30.512 -> Slave -> hello
19:02:30.607 -> Slave -> hello
19:02:30.703 -> Slave -> hello
19:02:30.845 -> Slave -> hello
19:02:30.940 -> Slave -> hello
19:02:31.031 -> Slave -> hello
19:02:31.125 -> Slave -> hello
19:02:31.267 -> Slave -> hello
19:02:31.362 -> Slave -> hello
19:02:31.457 -> Slave -> hello
19:02:31.552 -> Slave -> hello
19:02:31.696 -> Slave -> hello
19:02:31.791 -> Slave -> hello
19:02:31.886 -> Slave -> hello
19:02:31.981 -> Slave -> hello