Reading data received from Nextion on Serial Monitor

Can anyone suggest a tutorial which can show how to read data received from a Nextion on the Arduino serial monitor, Similar to how Serial.print would work IF the Nextion was not attached to Aruino

Have you tried reading and printing the data received ?
What format is it in ?
Can you post an example and describe what actions on the Nextion screen created the output

@PerryBebbington may be able to help

1 Like

While I am sure my Nextion and Arduino codes are wrong ,I am looking for some way to display
the data received on the Arduino so I can see where I am going wrong. Similar to the Arduino Serial.print command which displays to the Serial monitor.
Attached are HMI and Arduino codes.

tvoid setup() 
{
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  digitalWrite(8,HIGH);
  delay (2000);
  digitalWrite(8,LOW);
}

void loop()
{
  String Data_From_Display = "";
   if(Serial.available())
 {
 String Data_From_Display = "";
  delay(100);
  while(Serial.available())
  {
    Data_From_Display += char(Serial.read());
  }
    if( Data_From_Display == "ON")
     { digitalWrite(8,HIGH);}
    else if( Data_From_Display == "OFF")
     { digitalWrite(8,LOW); }
    else
    { 
       
       Serial.print("page0.t0.txt=" + String('"') + Data_From_Display + String('"'));       
       Serial.write(0xff);
       Serial.write(0xff);
       Serial.write(0xff);
    }
 }
 delay(2000);
}ype or paste code here

What I am after is some way I can Serial.print to the Serial Monitor to show me what I have
received from the Nextion.

You will need 2 serial ports. One for the Nextion and one to connect to serial monitor.

I made a library to handle Serial, that supports CRLF as well as the Nextion's 0xFFs.

This is a simple sketch that allows you to play with a Nextion via command line/Serial monitor.

#include <WhandallSerial.h>  // https://github.com/whandall/WhandallSerial

// simple handler just prints some infos about the received line

void processLine(const char* buf) {
  Serial.print(F("len = "));
  Serial.print((uint8_t)buf[-1]);
  Serial.print(F(", strlen = "));
  Serial.print(strlen(buf));
  Serial.print(F(", "));
  dump(buf, buf[-1]);
}

void processConsoleLine(const char* buf) {
  Serial.print(F("sent \""));
  Serial.print(buf);
  Serial.println(F("\""));
  Serial1.print(buf);
  Serial1.write(0xFF);
  Serial1.write(0xFF);
  Serial1.write(0xFF);
}

SSerial console(Serial, processConsoleLine); // used serial and handler
SSerial nextionSerial(Serial1, processLine); // used serial and handler

void setup() {
  Serial.begin(250000);
  console.begin(64, optIgnoreLF); // buffer size and options
  Serial1.begin(115200);
  nextionSerial.begin(64, optTripleFF | optKeepDlm); // buffer size and options
}

void loop() {
  console.loop();         // collect chars and call handler for each line
  nextionSerial.loop();   // collect chars and call handler for each line
}

void dump(const void* ptr, int len) {
  const byte* adr = (const byte*) ptr;
  byte idx;
  if (len) {
    for (; len > 0; len -= 16, adr += 16) {
      for (idx = 0; (idx < 16) && (idx < len); idx++) {
        phByte(adr[idx]);
        Serial.write(' ');
      }
      Serial.write('"');
      for (idx = 0; (idx < 16) && (idx < len); idx++) {
        Serial.write(adr[idx] < 0x20 ? '.' : adr[idx]);
      }
      Serial.write('"');
      Serial.println();
    }
  }
}

void phByte(byte byt) {
  if (byt < 16) {
    Serial.write('0');
  }
  Serial.print(byt, HEX);
}

You will probably have to adjust the baud rates.

Thanks Whandall,but it seems just a little complicated for a Dummy like me .Not sure how to implement it in my current code.

Maybe someone could hhelp me set up the 2 ports as suggested by groundFungus.

What Arduino are you using? If an Uno or Nano, or other board with only one hardware serial port, one would use a software serial port for a second serial port. There are a few different libraries for that. SoftwareSerial, NeoSWSerial and AltSoftSerial to name a few.

For other boards with multiple hardware ports like the Mega, just use one of those ports.

I would use the first hardware port for the serial monitor and program upload and debug and the second (hardware or software) port for the display. Note that software serial will most likely not work at more than 38400 baud.

@Farticus
I can't stop laughing when I see your username :joy:

Anyway...
You've had some good answers already, here's mine.

As others have indicated forget using a Uno or similar with only one serial port, which is already tied up with the serial monitor, buy something with a spare hardware serial port. My preferred board is the Nano Every, but there are others.

My approach to reading the serial data to and from a Nextion is to have a separate Arduino to sniff the data and send it to the serial port, rather than trying embed extra code in my main sketch for the purpose. There are a number of reasons for this approach:

  • My original use of a Nextion was on a PIC, not an Arduino board, so no serial monitor. Using an Arduino and the serial monitor made life so much easier.

  • Having separate code on a separate board means that if you mess something up in the sketch controlling the Nextion it won't mess up the sketch providing the serial monitor functionality, meaning you can concentrate on what you got wrong in the Nextion sketch, knowing the serial monitor data is telling you the truth of what is happening.

Utility 1
The utility below takes data sent from the Arduino to the Nextion and copies it to the serial monitor.
To use it:
You need a separate Arduino with the code below connected to your PC with the serial monitor open and set at 115200 Baud. Connect the Arduino controlling the Nextion to the Nextion as normal (Arduino Tx -> Nextion Rx, Arduino Rx <- Nextion Tx) and connect an additional wire* from the monitor Arduino serial port 1 Rx to the wire linking the controlling Arduino Tx -> Nextion Rx. Also link the grounds together of the 2 Arduino boards.

*Preferably put a resistor between 1k and 10k in this link wire to prevent possible phantom powering if one board is powered and the other is not. I know from experience that this is enough to kill an Nano Every.

// Receives data on serial port 1 as sent to Nextion (received by Nextion)
// Formats for easy reading in serial monitor

const uint8_t bufferSize = 128;
char NextionDataRx[bufferSize];
uint8_t NextionDataIndex = 0;
bool dataReady = false;
bool overflow = false;

uint16_t baudNextion = 9600;  // Change this to match whatever board rate you are using for the Nextion.

void setup() {
  char fileName[] = __FILE__;
  Serial.begin(115200);
  delay(2000);
  Serial.println("Serial monitor started");
  Serial1.begin(baudNextion);
  Serial.println("Serial 1 started");
  Serial.println(" ");
  Serial.println(sizeof(fileName));
  Serial.println(fileName);  
}

void loop() {
  RxNextionData();
  PrintNextionData();
}

void RxNextionData() {
  char RxTemp;
  static uint8_t charCount;
  static uint8_t ffCount;
  while (Serial1.available() > 0) {
    RxTemp = Serial1.read();
    if (RxTemp == 0xff) {
      ++ffCount;
      if (ffCount >= 3) {
        dataReady = true;
        ffCount = 0;
        NextionDataRx[NextionDataIndex] = 0;
        NextionDataIndex = 0;
      }
    } else {
      NextionDataRx[NextionDataIndex] = RxTemp;
      ++NextionDataIndex;
      if (NextionDataIndex >= bufferSize) {
        NextionDataRx[bufferSize - 1] = 0;
        dataReady = true;
        overflow = true;
      }
    }
  }
}

void PrintNextionData() {
  if (dataReady) {
    dataReady = false;
    if (overflow) {
      overflow = false;
      Serial.print("Buffer overflow: ");
    }
    Serial.println(NextionDataRx);
  }
}

If you are using the Nextion standard to send data to the Arduino, ending 0xff 0xff 0xff, then the code above will work for data going the other way, from the Nextion to Arduino. If you are not using that standard, such as your own or the way I show in my tutorial Using Nextion displays with Arduino then it won't work.

Utility 2
This utility is different, it just echoes what it gets on serial port 1 and sends it to the serial monitor. However, it displays it both as a character and as hex. Each byte is shown on a new line. There is a character count, which resets to 0 every time 0x00 is received. Connection is as for the first utility above. As it does not care about the Nextion 0xff or anything similar this utility works with anything.

// Receives data on serial port 1 and sends to the serial monitor with an index.
// Each character is on a new line.

uint16_t baudNextion = 9600;

void setup() {
  char fileName[] = __FILE__;
  Serial.begin(115200);
  delay(2000);
  Serial.println("Serial monitor started");
  Serial1.begin(baudNextion);
  Serial.println("Serial 1 started");
  Serial.println(" ");
  Serial.println(sizeof(fileName));
  Serial.println(fileName);  
}

void loop() {
  serialMonitor();
}

void serialMonitor() {
  char RxTemp;
  static uint8_t charCount;
  while (Serial1.available() > 0) {
    RxTemp = Serial1.read();
    Serial.print(charCount);
    Serial.print(" byte ");
    Serial.print((byte)RxTemp, HEX);
    Serial.print(" char ");
    Serial.println((char)RxTemp);
    ++charCount;
    if ((uint8_t)RxTemp == 0) {
      charCount = 0;
      Serial.println("charCount = 0");
    }
  }
}

Tx for your relies and help.
I would like to try out all your suggestions but to avoid too much confusion I would like to go through each suggestion one at a time'
Perry i have downloaded your suggested sketch but when I attempt to compile i get the following error;
C:\Users\Farticus\AppData\Local\Temp\arduino_modified_sketch_320792\sketch_oct16a.ino: In function 'void RxNextionData()':

sketch_oct16a:30:10: error: 'Serial1' was not declared in this scope

while (Serial1.available() > 0) {

      ^~~~~~~

C:\Users\Farticus\AppData\Local\Temp\arduino_modified_sketch_320792\sketch_oct16a.ino:30:10: note: suggested alternative: 'Serial'

while (Serial1.available() > 0) {

      ^~~~~~~

      Serial

exit status 1

'Serial1' was not declared in this scope

I am using a Uno for this sketch

Apologies. The code compiles to a MEGA but not a UNO

To be clear, both utilities require a spare, unused serial port to work. A Uno does not have a spare serial port, the one it has is used by the serial monitor. A Mega has 3 serial ports in addition to the one used by the serial monitor, a Nano Every has 1 spare serial port. There are other boards with spare serial ports too, but I am not even going to try to list them.

Got it all wire up but not receiving anything from the Nextion.


The following shows the debug values of the two presses to each of the "number Boxes"
on the Nextion, from this code should i expect to see something arrive on the Nextion?

Have wired as follows
Nextion TX to Arduino (Nex) RX
Nextion RX to Arduino (Nex) TX
Arduino (Nex) RX to SerialMonitorArduino TX
Common GND to all boards

Post the latest code that you are uploading.

To be clear do you have a separate Arduino with one of my utilities running on it?

The Arduino with my utility code should have Rx 1 connected to the Nextion Tx to see data from the Nextion, which is not, I think, what you have.

Photos and schematics help a lot.

Code on the Arduino to Serial Monitor

int LED = 5;                                //Define the pin for the LED

void setup() {
  Serial.begin(9600);                       //The default baud rate of the Nextion TFT is 9600.            
  pinMode(LED,OUTPUT);                      //Define pin as OUTPUT

}

void loop() {
  if(Serial.available()>0)                  //If we receive something...
  {
    String Received = Serial.readString();  //Save the received String in the Received variable
    if(int(Received[0]) == 234)               //If the first character of "Received" is "1"
    {
      digitalWrite(LED,HIGH);               //We turn on the LED
    }
    if(int(Received[0]) == 12)               //if is a "0"
    {
      digitalWrite(LED,LOW);                //We turn the LED off
    }
  }

}

Code on the Arduino to Nextion

const uint8_t bufferSize = 128;
char NextionDataRx[bufferSize];
uint8_t NextionDataIndex = 0;
bool dataReady = false;
bool overflow = false;

uint16_t baudNextion = 9600;  // Change this to match whatever board rate you are using for the Nextion.

void setup() {
  char fileName[] = __FILE__;
  Serial.begin(9600);  //   ................Had to change this to see an output
  delay(2000);
  Serial.println("Serial monitor started");
  Serial1.begin(baudNextion);
  Serial.println("Serial 1 started");
  Serial.println(" ");
  Serial.println(sizeof(fileName));
  Serial.println(fileName);  
}

void loop() {
  RxNextionData();
  PrintNextionData();
}

void RxNextionData() {
  char RxTemp;
  static uint8_t charCount;
  static uint8_t ffCount;
  while (Serial1.available() > 0) {
    RxTemp = Serial1.read();
    if (RxTemp == 0xff) {
      ++ffCount;
      if (ffCount >= 3) {
        dataReady = true;
        ffCount = 0;
        NextionDataRx[NextionDataIndex] = 0;
        NextionDataIndex = 0;
      }
    } else {
      NextionDataRx[NextionDataIndex] = RxTemp;
      ++NextionDataIndex;
      if (NextionDataIndex >= bufferSize) {
        NextionDataRx[bufferSize - 1] = 0;
        dataReady = true;
        overflow = true;
      }
    }
  }
}

void PrintNextionData() {
  if (dataReady) {
    dataReady = false;
    if (overflow) {
      overflow = false;
      Serial.print("Buffer overflow: ");
    }
    Serial.println(NextionDataRx);
  }
}

Your diagram shows the left hand Mega Tx connected, when it should be Rx as you want to receive data to monitor. As you have it you are trying to monitor data sent from the right hand Mega to the Nextion, is that what you intended?

Your serial monitor code is not my monitor code. It does not receive anything from serial port 1 or send anything to the serial monitor.

Thanks Perry. I have tried to download your monitor code from your Tutorial but it wont compile because I have a TJC Nextion and I cant set up the Nextion as per your Tutorial.
The tutorial I can find is one which transmits time TO the Nextion and I am having difficulty understanding the code which is read FROM the Nextion
Do you have a more basic version which would allow me to read just one (or 2) numbers from the Nextion n0 or n2