My guess is that you connected the ESP32, a 3.3V device, directly to the MEGA, a 5.0V device? If you have, get a new ESP32 and do some research into “Level Shifters”.
Next, after auto formatting your code, post it here between code tags.
I’ve not looked at those image files.
The ESP32 has 3 serial ports that you can use, the ports are: (0), (1), (2).
Unless you really know what is going on you should NOT use serial port (0), GPIO1 and GPIO3. The programing port that the Arduino IDE is going to use to send and receive data.
That leaves (1) and (2). (2) is the easy one to use on GPIO17 (TXD), and GPIO16 (RXD).
You will need to have declared: #include <HardwareSerial.h>.
To use (2) you will need to have instantiated a port; like this: HardwareSerial LIDARSerial ( 2 );.
Here is a serial receive concept that I have used for the UNO, Mega, Mini-Pro, Due, STM32 Bluepill, and ESP32:
void fReceiveSerial_LIDAR( void * parameters )
bool BeginSentence = false; // used to determine that the beginning of a sentence has been received and to prevent the begin process of a sentence in the middle of the sentence
sSerial.reserve ( StringBufferSize300 ); // this is the String buffer to hold the received sentence
char OneChar; // this is to hold the received character
for ( ;; ) // this loop runs forever
EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY); // this causes a wait till an event trigger
// vTaskDelayUntil( &xLastWakeTime, xFrequency ); // not used here
if ( LIDARSerial.available() >= 1 ) //The DUE, STM32 and ESP32 work better if there is at least a full byte in the receive buffer.
while ( LIDARSerial.available() ) // as long as there is something to receive the receive loop will run
OneChar = LIDARSerial.read();
if ( BeginSentence )
if ( OneChar == '>') // the end of a sentence
if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE ) // take a semaphore
xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial ); // write the new value to a single queue.
xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial ); // run the parser task on the other core
BeginSentence = false;
sSerial.concat ( OneChar );
if ( OneChar == '<' ) // this is the beginning of a sentence
sSerial = ""; // clear string buffer
BeginSentence = true; // found begining of sentence
} // while ( LIDARSerial.available() )
} //if ( LIDARSerial.available() >= 1 )
xSemaphoreGive( sema_ReceiveSerial_LIDAR );
// Serial.print( "fReceiveSerial_LIDAR " );
// Serial.print(uxTaskGetStackHighWaterMark( NULL ));
vTaskDelete( NULL ); // if the for loop is somehow exited then kill this task and regain all its in use assets
} //void fParseSerial( void * parameters )
The above code is what I use on the ESP32 running freeRTOS. The code commented out near the bottom is used to get a handle on how much ram the code is using so I can set the task proper stack setting. I know that code works up to being ran once every .25uS; faster than that, your on your own.
About serial (1).
The natural pinout on the ESP32 module for serial (1) is GPIO10 and GPIO11. If you do not know how/why do not use GPIO 6, 7, 8, 9, 10, 11. If, during programming the ESP32 any of those pins change state, your programming will fail. Like using a module which just starts spitting stuff out at its power on. That means that to use serial port (1), you will need to, in setup, set the pin matrix; easy to do.
So you’d do the HardwareSerial GPSSerial ( 1 ); thing and in setup you’d define the pin matrix for (1) like so: GPSSerial.begin ( GPS_DataBits, SERIAL_8N1, 2, 15 ); // begin GPS hardware serial
In the above I used GPIO 2 and 15 for rx and tx.
Remember that the GPIO pins 0 to 32 can be inputs and outputs and all the pins above 32 are input only.
Note, The ESP does not have Serial1 or Serial2 or Serial3 or Serial79.
But first, if you connected the ESP directly to the Mega, get a new ESP.