Lora esp32 with Nextion display

Im trying to use a nextion display with my lora esp32 TTGO (combined board) but I can't seem to send data to the display via serial as i am trying to do with the tx two different pins. Communication works though as i can get this display to talk to a regular arduino board via tx so im perplexed as to why the esp32 lora board can't. I am thinking i must be missing something with how you are supposed to use the nextion displays with esp32s. Just making this post in case someone knows what i'm missing. included is the entirety of my code at the moment.

int variable1 = 0;  // This is a simple variable to keep increasing a number to have something dynamic to show on the display.

void setup() {  // Put your setup code here, to run once:
  
  Serial.begin(9600);  // Start serial comunication at baud=9600. For Arduino mega you would have to add a
                       // number (example: "Serial1.begin(9600);").
                       // If you use an Arduino mega, you have to also edit everything on this sketch that
                       // says "Serial" and replace it with "Serial1" (or whatever number you are using).
}  // End of setup
void loop() {  // Put your main code here, to run repeatedly:

  variable1++;  // Increase the value of the variable by 1.
  if(variable1 == 201){  // If the variable reach 201...
    variable1 = 0;  // Set the variable to 0 so it starts over again.
  }
  // We are going to send the variable value to the object called n0:
  // After the name of the object you need to put the dot val because val is the atribute we want to change on that object.
  Serial.print("n0.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  Serial.print(variable1);  // This is the value you want to send to that object and atribute mention before.
  Serial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  Serial.write(0xff);
  Serial.write(0xff);
delay(100);
}   // End of loop

Serial is the serial monitor.

For an ESP32, you'll want to use one of the 3 other hardware serial ports.

You'll use

#include <HardwareSerial.h>

to load the ESP32 hardware serial port library.

Here you can see 2 serial ports being defined.

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

GPIO_NUM_16 is the TX pin for serial (1), GPIO_NUM_17 is the RX pin for serial (1) are the natural pins for serial port (1).

To use serial (2) it's best to define the pins in setup.

In setup:

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

As you might see I have defined 3 serial ports. One for the serial monitor and two other hardware serial ports.

Here is a serial task to send data:

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

Here is a serial receiving task:

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 those give you a hand in getting the thing to work.

1 Like

Thanks for the info! I figured it out. I ended up with the following code. I defined my tx and rx pins and used the names to set up as the following: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
this can be seen in my code. A very important note is that for some reason to use these devices over hardware serial on esp32s you have to flip the rx and tx connections. In other words your rx pin on the screen or gps or whatever you have has to be connected to the tx and vice versa. I have no idea why but this is how it works. Attached is an example of a gps module being used.

int variable1 = 0;  // This is a simple variable to keep increasing a number to have something dynamic to show on the display.
#define RXD2 16
#define TXD2 17
void setup() {  // Put your setup code here, to run once:
  // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
  Serial.begin(9600);
  //Serial1.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial.println("Serial Txd is on pin: "+String(TX));
  Serial.println("Serial Rxd is on pin: "+String(RX));
 // Serial.begin(9600);  // Start serial comunication at baud=9600. For Arduino mega you would have to add a
                       // number (example: "Serial1.begin(9600);").
                       // If you use an Arduino mega, you have to also edit everything on this sketch that
                       // says "Serial" and replace it with "Serial1" (or whatever number you are using).
}  // End of setup
void loop() {  // Put your main code here, to run repeatedly:

  variable1++;  // Increase the value of the variable by 1.
  if(variable1 == 201){  // If the variable reach 201...
    variable1 = 0;  // Set the variable to 0 so it starts over again.
  }
  // We are going to send the variable value to the object called n0:
  // After the name of the object you need to put the dot val because val is the atribute we want to change on that object.
  Serial2.print("n0.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  Serial2.print(variable1);  // This is the value you want to send to that object and atribute mention before.
  Serial2.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  Serial2.write(0xff);
  Serial2.write(0xff);
delay(100);
}   // End of loop