[solved] Send data to a Nextion display

Hi there,

I am using a Nextion display for a small project.
I have read the different threads on the Nextion displays here, in particular the pined one, and followed a bunch of tutorials but none of these resources helped me figure out what is blocking my progress.

I am trying to do a simple thing.
When you push a button (a physical push button), it changes the image on the Nextion display.

For this project I use:

  • Nextion NX4024T032_011R
  • Arduino Micro

On my way to understand and learn how to use the Nextion display I followed a few tutorials successfully.
For my demonstration here I will take this one as a reference:

Basically, you chose a number on the Nextion display and you send that number via serial and print it to see on the Serial monitor.

It works, as well as the other example of the link I posted above.

So, so far sending datas from the Nextion to the Arduino is OK.
On the other hand, the other way around doesn’t work.
I can’t send any data from the Arduino to the Nextion. It’s my issue.

To test this part, I used the code from the example above and I added a small piece of code saying that if the number returned is equal to 7, then we change the value of a box to 200 (just a random number).
This part is at the very bottom.
→ See files attached

/*
This code is based on the software serial example on Arduino's website
 */

#include <SoftwareSerial.h> //Include the library

SoftwareSerial mySerial(10, 11); // RX, TX

String message;
int shadowFax, numMessages, endBytes;
byte inByte;

void setup() {
  numMessages, endBytes = 0;
  
  Serial.begin(57600); //open the serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
  ;
  }

  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port
}

void loop() { // run over and over

  if (numMessages==1) { //Did we receive the anticipated number of messages? In this case we only want to receive 1 message.
    Serial.print("The number you've entered is: ");
    Serial.println(shadowFax);//See what the important message is that the Arduino receives from the Nextion 
    numMessages = 0; //Now that the entire set of data is received, reset the number of messages received    
  }  
  
  if (mySerial.available()){ //Is data coming through the serial from the Nextion?
    inByte = mySerial.read();


    Serial.println(inByte); //See the data as it comes in
    
    if (inByte>47 && inByte<58){ //Is it data that we want to use?
      message.concat(char(inByte)); //Cast the decimal number to a character and add it to the message
    }
   else if (inByte==255){ //Is it an end byte?
     endBytes = endBytes + 1; //If so, count it as an end byte.
   } 
   
   if (inByte==255 && endBytes==3){//Is it the 3rd (aka last) end byte?
     shadowFax = message.toInt(); //Because we have now received the whole message, we can save it in a variable.
     message=""; //We received the whole message, so now we can clear the variable to avoid getting mixed messages.
     endBytes=0; //We received the whole message, we need to clear the variable so that we can identify the next message's end
     numMessages  = numMessages + 1; //We received the whole message, therefore we increment the number of messages received.
     
   //Now lets test if it worked by playing around with the variable.
     if (shadowFax>0){
       Serial.println("The number you've entered is positive");
     }
   
     if (shadowFax>5){
       Serial.println("The number you've entered is larger than 5");
     }
  
     if (shadowFax<10){
       Serial.println("The number you've entered is smaller than 10");
     }     

      if (shadowFax==7){
        Serial.print("n1.val="); // Changing the value of box n1
        Serial.print("200");
        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);
     }     
   }   
 }
}

I am sure my issue comes from the serial communication between both devices.
(btw I connected Nextion RX to pin 10 and Nextion TX to pin 11.)

To send data to the Nextion, only the 3 Serial.write(0xff); lines are necessary ?
Shouldn’t we send data to a different serial than the one we receive data from ?

If I understood correctly, the Micro only has 1 hardware serial port, pin 0 / pin 1 (or RX/TX) using the Serial1 class. And we are using the Serial classe, like in this example, it means we are using the software serial ? is that correct ?

I think I am not sure how to use serial ports correctly, if I can use the same serial for sending and receiving data. It’s a bit confusing to me.

I also tried to connect the display to the arduino RX/TX (in that case, in the code, I replaced Serial by Serial1) but it didn’t work much better.

If someone here can show me the correct way to follow it would be much appreciated !

Tutorial02Test.zip (6.77 KB)

Hello Dyspho,

From your sketch:

      if (shadowFax==7){
        Serial.print("n1.val="); // Changing the value of box n1
        Serial.print("200");
        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);
     }

You appear to be sending that to the serial monitor, not to the software serial port, so it is not going to the Nextion. You are also missing the opening and closing " which have to be sent at the beginning and end of the value you send. Look at my code from the example in my tutorial

//This displays the page number
void HMI_display_page(uint8_t page) {
  Serial1.print(F("t0.txt=\""));
  Serial1.print(F("This is page "));
  Serial1.print(page);
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

Note in particular:

Serial1.print(F("t0.txt=\""));

At the end is ", which sends a " before "This is page"
And:

Serial1.print(F("\""));

Which sends the final " to the Nextion.

Also, read part 4 of my tutorial, '****Some things that might help you', in particular the bit about numbers. I don't use the Nextion number objects because, basically, they are crap!

Edit, correction to comments about "

Thank you so much Perry !

ok, so, following your indications I changed this to my sketch:

In the void setup I added the serial port 1 set up

void setup() {
  numMessages, endBytes = 0;

  Serial1.begin(9600);
  Serial.begin(57600); //open the serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
  ;
  }

  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port

}

and in the last part I changed the serial references to serial1 and the missing symbols:

if (shadowFax==7){
        Serial1.print("n1.val=\""); // Changing the value of box n1
        Serial1.print("200");
        Serial1.print("\"");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
     }

On addiction, I connected the yellow cable (Nextion RX) to my arduino TX pin.
The blue cable is till plugged to the pin 10.

And.....nothing happened :sob:
To be sure I tried to move the yellow cable back to the pin 11, but I got the same result.
I am not moving the constant to the flash memory like in your exemple, it's not a problem in this case, right ?

As for the part 4 of your tutorial, I've had already read it previously but as I am just going to use it for the sake of the exercice of changing something on the screen, just a simple number, it thought it might be ok just like that, isn't ?

Hummmm what am I missing ?? :confused:

I connected the yellow cable (Nextion RX) to my arduino TX pin.

Can you confirm that is labelled TX1 on the Arduino?

The blue cable is till plugged to the pin 10.

Connect to RX1 (I don't know which pin number that is, sorry).

if (shadowFax==7){
        Serial1.print("n1.val=\""); // Changing the value of box n1
        Serial1.print("200");
        Serial1.print("\"");
        Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        Serial1.write(0xff);
        Serial1.write(0xff);
     }

That looks right , but are you sure that at some point shadowFax does equal 7? If it doesn't then nothing will be sent.

I am not moving the constant to the flash memory like in your exemple, it's not a problem in this case, right ?

Not putting it in flash shouldn't cause a problem.

I've just had a look at the specs for the Micro Arduino Micro and it has one serial port.
Pin 0 RX1 connect to blue
Pin 1 TX1 connect to yellow

I am not clear if this is the same serial port used for the USB connection or a different one. If it's the same then I can see that being a problem that I don't know how to fix, sorry. I guess software serial should work but I am not familiar with software serial. You'd need to know which pins software serial is using and I'm not sure I'm the right person to help with that.

Right.....

I have had a look at the information about software serial and this is what I think:
In your original code the problem was not sending the start and finish " and not sending to the software serial port. You've modified the original code to send the " but now you are trying to send to the hardware serial port used for communications with your PC. Try:

if (shadowFax==7){
        mySerial.print("n1.val=\""); // Changing the value of box n1
        mySerial.print("200");
        mySerial.print("\"");
        mySerial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        mySerial.write(0xff);
        mySerial.write(0xff);
     }

You will also need to initialise my serial as you did in the code you posted at the start.

Connect
10 RX blue
11 TX yellow

I hope that works!

(If someone else reading this thinks I've got it wrong about serial ports then please feel free to help!)

Perry,

Thanks a lot for checking the infos regarding serial ports !

Ok, so here what happened today:

I tried your code and it didn’t work.

Then I decided to write a more simple sketch where I only send data because the previous one, even if it was simple too, was a bit confusing with sending and receiving data + writing many things in the monitor.

So, on the Nextion I only put a text block (t0) with written “empty” in it.
With the sketch, on launch it would write “test” in this block instead.

/*
This code is based on the software serial example on Arduino's website
 */

#include <SoftwareSerial.h> //Include the library

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  
  Serial.begin(57600); //open the serial port
  while (!Serial) { // wait for serial port to connect. Needed for native USB port only
  ;
  }

  Serial.println("Serial On"); //Print this messages when the serial port is connected
  mySerial.begin(9600); // set the data rate for the SoftwareSerial port

 
}

void loop() { // run over and over

  // Write "test" in the text box t0
  mySerial.print("t0.txt=\""); // Changing the value of box n1
  mySerial.print("test");
  mySerial.print("\"");
  mySerial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  mySerial.write(0xff);
  mySerial.write(0xff);

}

It actually DID work !

I don’t know what’s the difference with the previous one, but this one worked !
Maybe you were right all along about saying the numeric block were crap !

Thanks so much for the help !!

I am probably not done here yet thought haha
I will try to go back to the previous sketch and make it work.

ok,

so I went back to the initial sketch with the "shadowfax" thing.

I modify the Nextion file adding replacing the number box by a text box and using this code:

if (shadowFax==7){
        mySerial.print("t1.txt=\""); // Changing the value of box n1
        mySerial.print("test");
        mySerial.print("\"");
        mySerial.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
        mySerial.write(0xff);
        mySerial.write(0xff);
        Serial.println("END");
     }

and it worked !

I consider this thread as Solved.
Thanks a lot to PerryBebbington for his help !

I have other issues with this project but I created another thread as it doesn't concern the same topic.

You can follow it HERE

(Spoiler: it's about How to change an image on a Nextion display :smiley: )

Dyspho:
I consider this thread as Solved.
Thanks a lot to PerryBebbington for his help !

I have other issues with this project but I created another thread as it doesn't concern the same topic.

You can follow it HERE

(Spoiler: it's about How to change an image on a Nextion display :smiley: )

My pleasure! :slight_smile:

Do I have to read your new problem now....??? :confused: