Problems with using Serial2 communication between 2 esp32

I am using espnow for sending data wirelessly between 2 esp32's and then i have the receiver that also sends that data through serial2 to the other esp32 which that sends data into cloud.
But the problem is that the serial2 communication doesn't work, i used pins 16 and 17 and connected them rx-tx tx-rx.

https://pastebin.com/hZEhdf6Q

Would really appreciate some tips.

Please post a full sketch here illustrating the problem

Hi, @ifsaguozg
Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

Post your code in code tags, using an off forum location means some forum members platforms will not be able to read it.

Thanks.. Tom.... :grinning: :+1: :coffee: :australia:

Ok

// Receiver
 
#include <esp_now.h>
#include <WiFi.h>
#include <HardwareSerial.h>
 
unsigned long previousMillis = 0, currentMillis;
const long interval = 3000; // time after the code executes again
// HardwareSerial Serial1(1); // use uart1
 
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message
{
  int id;
  int h1;
  int h2;
  int h3;
  int h4;
  int h5;
  int h6;
  int h7;
  int h8;
} struct_message;
 
// Create a struct_message called myData
struct_message myData;
 
// Create a structure to hold the readings from each board
struct_message board1;
struct_message board2;
 
// Create an array with all the structures
struct_message boardsStruct[2] = {board1, board2};
 
// Create peer interface
esp_now_peer_info_t peerInfo;
 
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *incomingData, int len)
{
  char macStr[18];
  Serial.print("Packet received from: ");
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.println(macStr);
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
 
  // Update the structures with the new incoming data
  boardsStruct[myData.id - 1].h1 = myData.h1;
  boardsStruct[myData.id - 1].h2 = myData.h2;
  boardsStruct[myData.id - 1].h3 = myData.h3;
  boardsStruct[myData.id - 1].h4 = myData.h4;
  boardsStruct[myData.id - 1].h5 = myData.h5;
  boardsStruct[myData.id - 1].h6 = myData.h6;
  boardsStruct[myData.id - 1].h7 = myData.h7;
  boardsStruct[myData.id - 1].h8 = myData.h8;
 
  Serial.printf("h1 value: %d \n", boardsStruct[myData.id - 1].h1);
  Serial.printf("h2 value: %d \n", boardsStruct[myData.id - 1].h2);
  Serial.printf("h3 value: %d \n", boardsStruct[myData.id - 1].h3);
  Serial.printf("h4 value: %d \n", boardsStruct[myData.id - 1].h4);
  Serial.printf("h5 value: %d \n", boardsStruct[myData.id - 1].h5);
  Serial.printf("h6 value: %d \n", boardsStruct[myData.id - 1].h6);
  Serial.printf("h7 value: %d \n", boardsStruct[myData.id - 1].h7);
  Serial.printf("h8 value: %d \n", boardsStruct[myData.id - 1].h8);
}
 
void setup()
{
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
  // Initialize Serial Monitor
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, 16, 17);
 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);
 
  // Init ESP-NOW
  if (esp_now_init() != ESP_OK)
  {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
 
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop()
{
 
  currentMillis = millis();
 
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
 
    // Acess the variables for each board
    int board1H1 = boardsStruct[0].h1;
    int board1H2 = boardsStruct[0].h2;
    int board1H3 = boardsStruct[0].h3;
    int board1H4 = boardsStruct[0].h4;
    int board1H5 = boardsStruct[0].h5;
    int board1H6 = boardsStruct[0].h6;
    int board1H7 = boardsStruct[0].h7;
    int board1H8 = boardsStruct[0].h8;
 
    int board2H1 = boardsStruct[1].h1;
    int board2H2 = boardsStruct[1].h2;
    int board2H3 = boardsStruct[1].h3;
    int board2H4 = boardsStruct[1].h4;
 
 
      Serial2.write("OK!!!");
      Serial2.write(boardsStruct[myData.id - 1].h1);
      Serial2.write(boardsStruct[myData.id - 1].h2);
      Serial2.write(boardsStruct[myData.id - 1].h3);
      Serial2.write(boardsStruct[myData.id - 1].h4);
      Serial2.write(boardsStruct[myData.id - 1].h5);
      Serial2.write(boardsStruct[myData.id - 1].h6);
      Serial2.write(boardsStruct[myData.id - 1].h7);
      Serial2.write(boardsStruct[myData.id - 1].h8);
  }
}
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
// Gateway
 
#define THINGER_SERIAL_DEBUG
 
#include <ThingerESP32.h>
#include <WiFi.h>
#include <HardwareSerial.h>
#include "secrets.h"
 
ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
 
unsigned long previousMillis = 0, currentMillis;
float SensorValue1 = 0, SensorValue2 = 0, SensorValue3 = 0, SensorValue4 = 0, HumidityValue1 = 0, HumidityValue2 = 0, HumidityValue3 = 0, HumidityValue4 = 0;
const long interval = 3000; // time after the code executes again
char data[61];
 
// HardwareSerial Serial1(1); // use uart1
 
void setup()
{
 
  // Initialize Serial Monitor
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, 16, 17);
 
  thing.add_wifi(SSID, SSID_PASSWORD);
}
 
void loop()
{
 
  currentMillis = millis();
 
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;*/
 
      data[60] = Serial2.read();
      //Serial.printf(data, "\t");
      Serial.println(data);
      Serial.println(data[0]);
      Serial.println(data[1]);
    
    thing.handle();
 
    thing["soil_humidity"] >> [](pson &out)
    {
      out["Sensor1"] = data[0];
      out["Sensor2"] = data[1];
      out["Sensor3"] = data[2];
      out["Sensor4"] = data[3];
      out["Humidity1"] = data[4];
      out["Humidity2"] = data[5];
      out["Humidity3"] = data[6];
      out["Humidity4"] = data[7];
    };
  }
  delay(200);
}

receiver ESP32

#include <HardwareSerial.h>

HardwareSerial GPSSerial ( 1 );
// pin 2=RX, pin 15=TX
HardwareSerial LIDARSerial ( 2 );
// pin 26=RX, pin 25=TX

void setup()
{
LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 );
  GPSSerial.begin ( GPS_DataBits, SERIAL_8N1, 2, 15 ); // begin GPS hardware serial
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
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  )
//////////////////////////////////////////////////////////////////////////////////////////

sender esp32

#include <HardwareSerial.h>

HardwareSerial SerialTFMini( 1 );
HardwareSerial SerialBrain( 2 );
////// serial(1) = pin27=RX green, pin26=TX white
////// serial(2) = pin16=RXgreen , pin17=TX white

void setup() 
{
 Serial.begin( SerialDataBits );
  SerialBrain.begin( SerialDataBits );
  SerialTFMini.begin(  SerialDataBits, SERIAL_8N1, 27, 26 );
  // Initialize the TFMini LIDAR
  tfmini.begin(&SerialTFMini);
}

void fSendLIDAR_InfoSerialToBrain( void *pvParameters )
{
  struct stu_LIDAR_INFO pxLIDAR_INFO;
  for ( ;; )
  {
    xEventGroupWaitBits (eg, evtGetIMU, pdTRUE, pdTRUE, portMAX_DELAY);
    xSemaphoreTake( sema_LIDAR_FOR_ALARM, xSemaphoreTicksToWait );
    xQueueReceive ( xQ_LIDAR_FOR_ALARM, &pxLIDAR_INFO, QueueReceiveDelayTime );
    xSemaphoreGive( sema_LIDAR_FOR_ALARM );
    int CellCount = 1 ;
    //    Serial.print ( " fSendLIDAR_InfoSerialToBrain " );
    //          // send LIDAR info for alarm
    String sSerial = "";
    sSerial.reserve ( 300 );
    sSerial.concat( "<#," ); //sentence begin
    sSerial.concat( String(ScanPoints) + "," );
    sSerial.concat( String(pxLIDAR_INFO.ServoSweepUp) + "," ); // get direction of scan
    for ( CellCount; CellCount <= ScanPoints; CellCount++ )
    {
      sSerial.concat( String(pxLIDAR_INFO.Range[CellCount]) + "," );
    }
    sSerial.concat( ">" ); //sentence end
    vTaskDelay( 10 );
    SerialBrain.println ( sSerial );
  }
  vTaskDelete( NULL );
} // void fSendLIDAAR_InfoSerialToBrain( void *pvParameters )
////
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 )

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