Nextion no changing pages or printing text

I will try to make this as clear as possible. My project requires 2 Nextion displays. If I do a simple 2-page 2-button setup, I can make the buttons on one display change the page and print text on both displays. But when I do this in a function, it seems like the displays can't keep up and miss the text or change page commands. I have tried using the delay to try and slow things down, but that isn't helping. Can anyone please help me with this?

Start by posting a full sketch that illustrates the problem

How do I correctly put the code here? Be warned, this is a large code.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

The problem is that it sometimes prints the text and changes pages. It is not continually updating as it should. The dfd_listen is for commands from a 5-inch Nextion display to the esp32. I am working first to get the 10-inch display to show all of the data the ESP32 is sending it. This is for the 10.1 Nextion display for the dashboard of an electric UTV car I am building. It has a GPS, MPU6050, and 2 BME/BMP280 to show inside and outside Temp.
The int R is for a 16-channel relay. The print statements in the setup just check and show that the MPU6050 is working on both displays. Once that is completed the 5-inch shows a login screen, once you log in, the screen goes to the control panel and the 10.1 goes to the dashboard page 1. Oh, and I am using an ESP32 Dev Mod (38-pin).
If I do a simple sketch of just changing pages and printing text, everything works just fine. But, when I add other functions, it is like the data is being sent too fast for the nextion to read. I don't know much about setting up timers, should I just put a short 50ms delay after each time it sends text or values to the display?
Here is the code:

#include <SoftwareSerial.h>
#include <MPU6050_light.h>
#include <Wire.h>
#include <TinyGPSPlus.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp;  // I2C

MPU6050 mpu(Wire);
TinyGPSPlus gps;
String(dfd) = "";
long temp_timer = 0;
long timer = 0;
String(page_num) = "";
//int t1 = 0;
//int t2 = 0;


SoftwareSerial HMISerial5(0, 4);
SoftwareSerial ss(15, 2);

// For stats that happen every 5 seconds
unsigned long last = 0UL;

int R1 = 36;
int R2 = 39;
int R3 = 35;
int R4 = 34;
int R5 = 32;
int R6 = 33;
int R7 = 14;
int R8 = 12;
int R9 = 27;
int R10 = 26;
int R11 = 25;
int R12 = 13;
int R13 = 18;
int R14 = 5;
int R15 = 19;
int R16 = 23;

void TCA9548A(uint8_t bus) {
  Wire.beginTransmission(0x70);
  Wire.write(1 << bus);
  Wire.endTransmission();
}

void setup() {

  Serial.begin(115200);
  HMISerial5.begin(9600);
  ss.begin(9600);
  Wire.begin();

  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(R5, OUTPUT);
  pinMode(R6, OUTPUT);
  pinMode(R7, OUTPUT);
  pinMode(R8, OUTPUT);
  pinMode(R9, OUTPUT);
  pinMode(R10, OUTPUT);
  pinMode(R11, OUTPUT);
  pinMode(R12, OUTPUT);
  pinMode(R13, OUTPUT);
  pinMode(R14, OUTPUT);
  pinMode(R15, OUTPUT);
  pinMode(R16, OUTPUT);
  /* TCA9548A(0);
  if (!bmp.begin(0x76)) {
    Serial.println("Sensor 1 Failed");
    while (1)
      ;
  }

  TCA9548A(1);
  if (!bmp.begin(0x76)) {
    Serial.println("Sensor 2 Failed");
    while (1)
      ;
  }*/

  // TCA9548A(2);
  byte status = mpu.begin();

  delay(1000);
  page_num = "0";
  changePage();
  
  delay(1000);
  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("Booting");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  Serial.println("Booting");
  delay(2000);


  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("MPU6050 status: ");
  HMISerial5.print(status);
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  Serial.print("MPU6050 status: ");
  Serial.println(status);
  delay(2000);
  while (status != 0) {}  // stop everything if could not connect to MPU6050

  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("Calculating offsets, do not move");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  Serial.println("Calculating offsets, do not move");
  mpu.calcOffsets(true, true);  // gyro and accelero*/
  delay(2000);
  // mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down

  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("Done!");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  Serial.println("Done!");
  delay(2000);
  page_num = "1";
  changePage();
}

void loop() {
  GPS();
  TEMP();
  mpu_update();
  dfd_listen();
}

void changePage() {
  HMISerial5.print("print " + (page_num));
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
}

void GPS() {
  while (ss.available() > 0)
    gps.encode(ss.read()); 

  if (gps.location.isUpdated()) {
    HMISerial5.print("t6.txt=\"");
    HMISerial5.print(gps.location.lat(), 6);
    HMISerial5.print("\"");
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("Latitud: " + String(gps.location.lat(), 6));
    HMISerial5.print("t5.txt=\"");
    HMISerial5.print(gps.location.lng(), 6);
    HMISerial5.print("\"");
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("Lonitud: " + String(gps.location.lng(), 6));
  }
  if (gps.speed.isUpdated()) {
    HMISerial5.print("va5.val=" + String(gps.speed.mph()));
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("Speed: " + String(gps.speed.mph()));
  }
  if (gps.course.isUpdated()) {
    if (gps.course.deg() <= 271){
    HMISerial5.print("z1.val=" + String(gps.course.deg() + 45));
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("Course: " + String(gps.course.deg()));
  }
  else if (gps.course.deg() > 270){
     HMISerial5.print("z1.val=" + String(gps.course.deg() - 45));
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
  }
  }
  if (gps.altitude.isUpdated()) {
    HMISerial5.print("t9.txt=\"" + String(gps.altitude.feet()));
    HMISerial5.print("\"");
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("Altitude: " + String(gps.altitude.feet()));
  }

  if (gps.satellites.isUpdated()) {
    HMISerial5.print("n1.val=");
    HMISerial5.print(gps.satellites.value());
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.print("Satellites: ");
    Serial.println(gps.satellites.value());
  }
  if (millis() - last > 10000) {
    Serial.println();

    Serial.print(F("DIAGS      Chars="));
    Serial.print(gps.charsProcessed());
    Serial.print(F(" Sentences-with-Fix="));
    Serial.print(gps.sentencesWithFix());
    Serial.print(F(" Failed-checksum="));
    Serial.print(gps.failedChecksum());
    Serial.print(F(" Passed-checksum="));
    Serial.println(gps.passedChecksum());

    if (gps.charsProcessed() < 10)
      Serial.println(F("WARNING: No GPS data.  Check wiring."));

    last = millis();
    Serial.println();
  }
}

void TEMP() {
  if ((millis() - temp_timer) > 1000) {
    TCA9548A(0);
    HMISerial5.print("t8.txt=\"");
    HMISerial5.print((bmp.readTemperature() * 1.8) + 32);
    HMISerial5.print("\"");
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.print("Inside Temp: ");
    Serial.println((bmp.readTemperature() * 1.8) + 32);

    TCA9548A(1);
    HMISerial5.print("t7.txt=\"");
    HMISerial5.print((bmp.readTemperature() * 1.8) + 32);
    HMISerial5.print("\"");
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.print("Outside Temp: ");
    Serial.println((bmp.readTemperature() * 1.8) + 32);
    temp_timer = millis();
  }
}


void mpu_update() {
  //  TCA9548A(2);
  mpu.update();
  if ((millis() - timer) > 1000) {  // print data every 10ms
    Serial.print("X : ");
    Serial.println(mpu.getAngleX() + 50);
    HMISerial5.println("va7.val=" + String(mpu.getAngleX() + 50));
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    delay(300);
    HMISerial5.print("va6.val=" + String(mpu.getAngleY() + 43));
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.print("Y : ");
    Serial.println(mpu.getAngleY() + 43);
    timer = millis();
  }
    delay(300);
}

void newpass() {
  while (!HMISerial5.available()) {
    dfd += char(HMISerial5.read());
  }
  if (dfd.substring(1, 4) == "b16") {
    Serial.println("New Passcode Saved");
    page_num = "4";
    changePage();
  }
  if (dfd.substring(1, 4) == "b17") { 
    Serial.println("Back to Login Screen");
    page_num = "1";
    changePage();
  }
}

void shutdown() {
  page_num = "0";
  changePage();
  delay(1000);
  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("Shutting Down");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  delay(2000);
  HMISerial5.print("t1.txt=\"");
  HMISerial5.print("systems in");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  delay(2000);

  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("5");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
    delay(300);
  HMISerial5.print("t1.txt=\"");
  HMISerial5.print(" ");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  delay(1000);

  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("4");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  delay(1000);
  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("3");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  delay(1000);

  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("2");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  delay(1000);

  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("1");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  delay(1000);

  digitalWrite(R1, LOW);

  HMISerial5.print("t0.txt=\"");
  HMISerial5.print("Turn Ignition Off");
  HMISerial5.print("\"");
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
  HMISerial5.write(0xff);
}

void dfd_listen() {


  delay(30);
  dfd = "";
  while (HMISerial5.available()) {
    dfd += char(HMISerial5.read());
  }

  if (dfd.substring(1, 4) == "bt0" && dfd.substring(0, 1) == "0") {
    digitalWrite(R2, LOW);  // Parking Lights
    Serial.println("Parking lights OFF");
  } else if (dfd.substring(1, 4) == "bt0" && dfd.substring(0, 1) == "1") {
    digitalWrite(R2, HIGH);  // Parking Lights
    Serial.println("Parking lights ON");
  }

  if (dfd.substring(1, 4) == "bt1" && dfd.substring(0, 1) == "0") {
    digitalWrite(R3, LOW);  // Head Lights
    HMISerial5.print("p2.pic=132");
    HMISerial5.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("Headlights OFF");
  } else if (dfd.substring(1, 4) == "bt1" && dfd.substring(0, 1) == "1") {
    digitalWrite(R3, HIGH);  // Head Lights
    HMISerial5.print("p2.pic=131");
    HMISerial5.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("Headlights ON");
  }

  if (dfd.substring(1, 4) == "bt2" && dfd.substring(0, 1) == "0") {
    digitalWrite(R4, LOW);  // High Beams
    HMISerial5.print("p2.pic=131");
    HMISerial5.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("High Beams OFF");
  } else if (dfd.substring(1, 4) == "bt2" && dfd.substring(0, 1) == "1") {
    digitalWrite(R4, LOW);  // High Beams
    HMISerial5.print("p2.pic=130");
    HMISerial5.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("High Beams ON");
  }

  if (dfd.substring(1, 4) == "bt3" && dfd.substring(0, 1) == "0") {
    digitalWrite(R5, LOW);  // Dome Lights
    Serial.println("Dome Lights OFF");
  } else if (dfd.substring(1, 4) == "bt3" && dfd.substring(0, 1) == "1") {
    digitalWrite(R5, LOW);   // Dome Lights
    HMISerial5.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    HMISerial5.write(0xff);
    HMISerial5.write(0xff);
    Serial.println("Dome Lights ON");
  }

  if (dfd.substring(1, 4) == "bt4" && dfd.substring(0, 1) == "0") {
    digitalWrite(R6, LOW);  // Roof Lights
    Serial.println("Roof Lights OFF");
  } else if (dfd.substring(1, 4) == "bt4" && dfd.substring(0, 1) == "1") {
    digitalWrite(R6, LOW);  // Roof Lights
    Serial.println("Roof Lights ON");
  }

  if (dfd.substring(1, 4) == "bt5" && dfd.substring(0, 1) == "0") {
    digitalWrite(R7, dfd.substring(0, 1).toInt());  // Fog Lights
    Serial.println("Fog Lights OFF");
  } else if (dfd.substring(1, 4) == "bt5" && dfd.substring(0, 1) == "1") {
    digitalWrite(R7, dfd.substring(0, 1).toInt());  // Fog Lights
    Serial.println("Fog Lights ON");
  }

  if (dfd.substring(1, 4) == "bt6" && dfd.substring(0, 1) == "0") {
    digitalWrite(R8, LOW);  // Windshield Wipers
    Serial.println("Windshield Wipers OFF");
  } else if (dfd.substring(1, 4) == "bt6" && dfd.substring(0, 1) == "1") {
    digitalWrite(R8, LOW);  // Windshield Wipers
    Serial.println("Windshield Wipers ON");
  }

  if (dfd.substring(1, 4) == "bt7" && dfd.substring(0, 1) == "0") {
    digitalWrite(R9, LOW);  // A-C
    Serial.println("A-C OFF");
  } else if (dfd.substring(1, 4) == "bt7" && dfd.substring(0, 1) == "1") {
    digitalWrite(R9, LOW);  // A-C
    Serial.println("A-C ON");
  }

  if (dfd.substring(1, 4) == "bt8" && dfd.substring(0, 1) == "0") {
    digitalWrite(R10, LOW);  // Generator
    Serial.println("Generator OFF");
  } else if (dfd.substring(1, 4) == "bt8" && dfd.substring(0, 1) == "1") {
    digitalWrite(R10, LOW);  // Generator
    Serial.println("Generator ON");
  }

  if (dfd.substring(1, 4) == "bt9" && dfd.substring(0, 1) == "0") {
    digitalWrite(R11, LOW);  // Extra 1
    Serial.println("Extra 1 OFF");
  } else if (dfd.substring(1, 4) == "bt9" && dfd.substring(0, 1) == "1") {
    digitalWrite(R11, LOW);  // Extra 1
    Serial.println("Extra 1 ON");
  }

  if (dfd.substring(1, 4) == "bt10" && dfd.substring(0, 1) == "0") {
    digitalWrite(R12, LOW);  // Extra 2
    Serial.println("Extra 2 OFF");
  } else if (dfd.substring(1, 4) == "bt10" && dfd.substring(0, 1) == "1") {
    digitalWrite(R12, LOW);  // Extra 2
    Serial.println("Extra 2 ON");
  }

  if (dfd.substring(1, 4) == "bt11" && dfd.substring(0, 1) == "0") {
    digitalWrite(R13, LOW);  // Extra 3
    Serial.println("Extra 3 OFF");
  } else if (dfd.substring(1, 4) == "bt11" && dfd.substring(0, 1) == "1") {
    digitalWrite(R13, HIGH);  // Extra 3
    Serial.println("Extra 3 ON");
  }


  if (dfd.substring(1, 4) == "b11") {
    page_num = 3;
    changePage();
  }
  if (dfd.substring(1, 4) == "b12") {
    page_num = 2;
    changePage();
    digitalWrite(R1, HIGH);
    delay(100);
  }
  if (dfd.substring(1, 4) == "b13") {
    digitalWrite(R14, dfd.substring(0, 1).toInt());  // Horn on-off
    delay(100);
  }
  if (dfd.substring(1, 4) == "b14") {
    page_num = "0";
    changePage();
    shutdown();
  }
  if (dfd.substring(1, 4) == "b16") {
    page_num = 1;
    changePage();
    delay(100);
  }
  if (dfd.substring(1, 4) == "b17") {
    page_num = 1;
    changePage();
    delay(100);
  }
  if (dfd.substring(1, 4) == "b18") {
    page_num = 1;
    changePage();
    delay(100);
  }
}

Hello @crwhite5,

Are the 2 software serial ports the ones connected to the Nextion? I'm no expert on software serial but I have a feeling it doesn't play well with 2 instances (anyone like to comment please?). My first step would be to investigate this.

When I add other functions, it is like the data is being sent too fast for the Nextion to read.

I doubt it. At 9600 Baud I would not expect the Nextion to have a problem. I have hit problems above 57600 but only when sending a great deal of data to a graph. For what you are doing and at 9600 I think you can be sure that whatever the problem is, this is not it.

I can't specifically point to the problem but there's a lot of blocking code or code I suspect is blocking, which means that while that is happening nothing else is, including getting data from or sending it too the Nextion. I cannot say 'this bit of code is the cause' but every time you include code that blocks you run the risk of something else not being responsive or not working well. The first one is delay, probably OK in setup as long as you are aware and leave it to do its thing on power up, but don't expect to be able to enter anything while the delays are delaying.

Before I continue, which serial port is for what purpose? It would help if you gave them names or at least comments to identify them.

Thanks for responding. First, the code I posted is meant to run 2 Nextions, but I have cut it down to run 1 display until I get this one working. HMISerial5 will be changed to HMI10Serial, this serial is for the nextion and the Serial is just for the serial monitor. I was suggested to use while statements on each thing it is to pass to the Nextion, but I have no clue how to have my Arduino read say t0 to see if it got what was sent to it.

Hello,

There's a limit to the help I can give with the code as it is. First off I consider myself to be reasonably good hobby programmer in C (not C++), as is evident to more expert programmers when they see my style of coding. Because of that there is a limit to what I know and what I can see in other people's code.

Some things I think you need to consider
Most important, to get real help from me about Nextion HMI you need to follow my methods in my tutorial, which I think you are aware of. The further you stray from that the harder it is for me to help you.

I'm not clear how many serial ports you need for your project, but obviously more than 1. Is it 2 for Nextions, plus 1 for GPS? Plus the serial monitor? Yes? I don't know how many instances of software serial can work at the same time, but I am suspicious of using more than one. You either need someone (not me) to give you a definitive answer that it is OK or will be a problem, or you need to investigate for yourself and find out. My assumption would be that 2 or 3 instances would be a problem unless and until I found out otherwise.

Do not use any blocking code

  • Do not use delay(), not even 1ms. Probably OK in setup as long as you are happy for setup to take as long as the delays delay it, but absolutely not in main code. You have 300ms delay in mpu_update, why? You seem to know how to use millis as timer, why mix it with delay? That's 300ms when nothing else will happen.

  • While() is blocking and should be used with care. ONLY use while when you can be sure it will not wait for anything, it will just deal with what is already there. For example, taking my code for reading data from the Nextion there is this:

  while (Serial1.available() > 0) {               //Read every byte in the receive buffer
    readtemp = Serial1.read();
    if (readtemp == 0xa5) {                       //Count the number of times 0xa5 has been received
      ++a5count;
      if (a5count > 2) {
        a5count = 0;
        HMI_read_data_i = 0;
      }
    }

Serial is very slow compared to the processor, and it is also dealt with by code in the background, code you will never see unless you dig behind the the Arduino API to see how stuff is implemented. While you code is doing its thing serial data is silently being deposited in a buffer waiting for you to check it. When you do check there might be 0 or 1 or several characters waiting for your code to process. The while loop you see there does not wait for anything, it just processes whatever is already there. This is important, if it waited then it would slow the processor down to the speed of the serial port, or worse to a stop if the external device didn't send the expected data. Be careful of this when using while().

while (status != 0) {}  // stop everything if could not connect to MPU6050

Think about what this does, if status is not 0 you enter this while loop and are stuck, with no explanation as to what happens. Nothing can change status in the while loop so that's, it, stuck.

Do not use Strings

  • There is a long running debate on this forum about whether Strings cause a problem or not. I'm not going to summarise the debate here, do some searching. However, I never use Strings, so if you do you immediately stop me helping because they are outside my knowledge and I won't know if what you are using them for is going to work or not. Use C strings AKA char arrays. Look at my code for sending multiple things to a Nextion, I use a sequence of serial.Print() to concatenate different things, easy to do, easy to read and not much room to go wrong.

What do you thing this does:

  while (!HMISerial5.available()) {
    dfd += char(HMISerial5.read());
  }

I was suggested to use while statements on each thing it is to pass to the Nextion, but I have no clue how to have my Arduino read say t0 to see if it got what was sent to it.

This makes no sense, the Arduino cannot read what is sent to the Nextion. What you can do is use a separate board via the serial monitor to see what has been sent, seen next reply.

I think that's enough for now, I suggest you read my tutorial and build on what is there. If you don't understand it ask.

I use a Nano Every and this code to monitor what is being sent to a Nextion:

// Receives data on serial port 1 up to 0xff 0xff 0xff Nextion end marker
// Sends to serial monitor
// Intended for boards with at least 1 spare serial port

const uint16_t bufferSize = 256;
char DataRx[bufferSize];
uint16_t DataIndex = 0;
bool dataReady = false;
bool overflow = false;

const uint16_t baudDefault = 9600;
const uint16_t baudHeating = 57600;
const uint16_t baudHeatingNextion = 38400;

void setup() {
  char fileName[] = __FILE__;
  Serial.begin(115200);
  Serial.println("Serial monitor started");
  //Serial1.begin(baudDefault);
  //Serial1.begin(baudHeating);
  Serial1.begin(baudHeatingNextion);
  Serial.println("Serial 1 started");
  Serial.println(sizeof(fileName));
  Serial.println(fileName);
  pinMode(13, OUTPUT);
}

void loop() {
  RxData();
  PrintData();
  runLED();
}

void RxData() {
    char RxTemp;
    static uint8_t charCount = 0;
    static uint8_t FFcount = 0;
    while (Serial1.available() > 0) {
        RxTemp = Serial1.read();

        //if ((uint8_t)RxTemp != 0xa5) {
        
        if ((uint8_t)RxTemp != 0xff) {
            DataRx[DataIndex] = RxTemp;
            ++DataIndex;
            if (DataIndex >= bufferSize) {
                DataRx[bufferSize - 1] = 0x00;
                DataIndex = 0;
                dataReady = true;
                overflow = true;
            }
        } else {
            ++FFcount;
            if (FFcount >= 3) {
                FFcount = 0;
                DataRx[DataIndex] = 0x00;
                DataIndex = 0;
                dataReady = true;
            }
        }
    }
}

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

void runLED() {
  uint32_t currentMillis = millis();
  static uint32_t lastMillis;
  const uint32_t interval = 500;
  if (currentMillis - lastMillis >= interval) {
    lastMillis += interval;
    digitalWrite(13, !(digitalRead(13)));
  }
}

This code is not Nextion specific, and can be used to monitor any serial data:

// Receives data on serial port 1 and sends to the serial monitor with an index.
// Each character is on a new line.
// Receiving 0x00 resets the count to 0. (Need to make this easily definable, eg 0xff for Nextion)

uint16_t baudHeating = 57600;
uint16_t baudHeatingNextion = 38400;


void setup() {
  char fileName[] = __FILE__;
  //Serial.begin(230400);
  Serial.begin(115200);
  delay(2000);
  Serial.println("Serial monitor started");
  //Serial1.begin(9600);
  Serial1.begin(baudHeatingNextion);
  //Serial1.begin(baudHeating);
  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");
    }
  }
}

This is the most important information.

and not a "oh" (as an additional goody)

As you are using an ESP32
are you using this software-library especially written for ESP32?

a few more things that might have an impact:
some of the ESP32-io-pins do have pull-up or pull-down-resistors and / or are creating PWM-pulses on boot
You should use IO-pins that have none of these oddities
You can read about this here:

The ESP32 has in summary three hardware serial ports
You can read about it here

The Rx/Tx pins for the UART can be configured
using the second and third UART works different than Serial1, Serial2

Another thing. espressif has developped some variants of the ESP32-chip some with only a single-core

They might differ in the internal hardware too. I don't know.
This means you have to specify with more precision than just "ESP32"
Here is the comparison-table and which extension which ESP32-type has
ESP32-S2, ESP32-C3, ESP32-S3
https://docs.espressif.com/projects/esp-idf/en/stable/esp32/hw-reference/chip-series-comparison.html
best regards Stefan

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