Serial communication between ESP32

Hi!

I'm trying to make two esp32 able to send and receive messages from each other in communication via Serial, using Serial2 on the board.

Searching I can find several examples with Arduino UNO, but not with ESP32. I made an example here, but it's not working.

#include <HardwareSerial.h>

HardwareSerial SerialTwo(2);


void setup() {
  // put your setup code here, to run once:

  SerialTwo.begin(9600);

}

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

  String received = "";
  while (SerialTwo.available())
  {
    received = SerialTwo.read();
    SerialTwo.println(received);
  }

}
#include <HardwareSerial.h>

HardwareSerial SerialTwo(2);

void setup() {
  // put your setup code here, to run once:

  SerialTwo.begin(9600);

}

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

  SerialTwo.println("TEST");
  delay(2000);

}

Circuit:

esp1 esp2
gnd to gnd
tx2 to rx2
rx2 to tx2

Can anyone give me any tips or material that explains this communication?

UnitA

#include <HardwareSerial.h>

HardwareSerial SerialTFMini( 1 );
HardwareSerial SerialBrain( 2 );

setup()
{
Serial.begin( SerialDataBits );
  SerialBrain.begin( SerialDataBits );
  SerialTFMini.begin(  SerialDataBits, SERIAL_8N1, 27, 26 );
}

void fSendSerialToBrain( void *pvParameters )
{
  struct stu_LIDAR_INFO pxLIDAR_INFO;
  xSemaphoreGive ( sema_SendSerialToBrain );
  String sSerial;
  sSerial.reserve ( 300 );
  for (;;)
  {
    xEventGroupWaitBits (eg, evtSendSerialToBrain, pdTRUE, pdTRUE, portMAX_DELAY);
    // Serial.println ( " fSendSerialToBrain " );
    if ( xSemaphoreTake( sema_SendSerialToBrain, xZeroTicksToWait ) == pdTRUE ) // grab semaphore, no wait
    {
      int CellCount = 1;
      xSemaphoreTake( sema_LIDAR_INFO, xSemaphoreTicksToWait );
      xQueueReceive ( xQ_LIDAR_INFO, &pxLIDAR_INFO, QueueReceiveDelayTime );
      xSemaphoreGive( sema_LIDAR_INFO );
      sSerial.concat( "<!," ); //sentence begin
      sSerial.concat( String(ScanPoints) + "," );
      for ( CellCount; CellCount <= ScanPoints; CellCount++ )
      {
        sSerial.concat( String(pxLIDAR_INFO.Range[CellCount]) + "," );
      }
      sSerial.concat( ">" ); //sentence end
      // Serial.println ( sSerial );
      SerialBrain.println ( sSerial );
      //      ////
      //      xEventGroupSetBits( eg, evtSendLIDAR_Info_For_ALARM );
      //      xSemaphoreGive ( sema_SendSerialToBrain );
      sSerial = "";
    }
  }
  vTaskDelete( NULL );
} // void fSendSerialToBrain( void *pvParameters )

Receiver:

#include <HardwareSerial.h>

HardwareSerial LIDARSerial ( 2 );
setup()
{
LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 );
}

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  sSerial.reserve ( StringBufferSize300 );
  char OneChar;
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == ‘>’)
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
              xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          sSerial.concat ( OneChar );
        }
        else
        {
          if ( OneChar == ‘<’ )
          {
            sSerial = “”; // clear string buffer
            BeginSentence = true; // found begining of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
  }
  vTaskDelete( NULL );
} //void fReceiveSerial_LIDAR( void * parameters  )

Hope the code helps.

google is always worth a quoogling (a quick googling)
https://www.google.de/search?q=esp32+hardware+serial+democode

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.