Writing to nextion screen

I finally received my 5" nextion screen about a week ago and have been trying everything I can to send data to textboxes on the screen and the only way I was able to get it to work was with the following code that I found in an instructable by Gideon Rossouwv. Using his code and nextion screen tutorial everything works fine. However when I pull out his code and put it in my sketch I never see the "serial on" and nothing shows up in the textbox. My code is the last bit. PS-The sensor shows correctly in the Arduino ide serial monitor.

Gideon Rossouwv's code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); /*Even though you can use the hardware serial port in this case I think it is better to
  leave the hardware serial open for debugging purposes*/

void setup() {
  // put your setup code here, to run once:
  pinMode(0, INPUT); //This is our analog input pin

  Serial.begin(57600); //open the hardware 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() {
  String sendThis = ""; //Declare and initialise the string we will send

//  delay(300); //Probably unneccessary, but I give the screen some time to respond
  sendThis = "n0.val="; //Build the part of the string that we know
  sendThis.concat(analogRead(0)); //Add the variable we want to send
  writeString(sendThis); /*Use a function to write the message character by character to the Nextion because
  mySerial.write(sendThis) gives you an error due to a datatype mismatch*/
}

//NOTE: A great big thanks to: RamjetX for writing this function. You can find his/her post here: http://forum.arduino.cc/index.php?topic=89143.0. Please go give him/her some Karma!
void writeString(String stringData) { // Used to serially push out a String with Serial.write()

  for (int i = 0; i < stringData.length(); i++)
  {
    mySerial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass  
  }

  mySerial.write(0xff); //We need to write the 3 ending bits to the Nextion as well
  mySerial.write(0xff); //it will tell the Nextion that this is the end of what we want to send.
  mySerial.write(0xff);

}// end writeString function

My code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11);

void setup() {

  pinMode(dataIN, INPUT);    //IR SENSOR INPUT

  Serial.begin(57600);          //SERIAL BEGIN
  while (!Serial) {                 //SCREEN SERIAL
    ;                                     //SCREEN SERIAL
  }                                      //SCREEN SERIAL
  Serial.println("Serial On");  //SCREEN SERIAL
  mySerial.begin(9600);       //SCREEN SERIAL
}

void loop() {
  rpm = 0;                                                                  //RPM MEASUREMENT
  currentstate = digitalRead(dataIN);                                       //RPM MEASUREMENT
  if ( prevstate != currentstate)                                           //RPM MEASUREMENT
  { //RPM MEASUREMENT                                                       //RPM MEASUREMENT
    if ( currentstate == HIGH )                                             //RPM MEASUREMENT
    { //RPM MEASUREMENT                                                     //RPM MEASUREMENT
      duration = ( micros() - prevmillis );                                 //RPM MEASUREMENT
      rpm = (60000000 / duration);                                          //RPM MEASUREMENT
      prevmillis = micros();                                                //RPM MEASUREMENT
    }                                                                       //RPM MEASUREMENT
  }                                                                         //RPM MEASUREMENT
  prevstate = currentstate;                                                 //RPM MEASUREMENT

  Serial.print("RPM = ");                                                   //SERIAL DEBUGGING
  Serial.println(rpm);                                                      //SERIAL DEBUGGING
 
 String t1text = "";                                                       //SCREEN SERIAL
  t1text = "page1.t1.txt=";                                                 //SCREEN SERIAL
  t1text.concat(rpm);
  writeString(t1text);                                                      //SCREEN SERIAL
}

void writeString(String stringData) {                                       //SCREEN FUNCTION
  for (int i = 0; i < stringData.length(); i++)                             //SCREEN FUNCTION
  { //SCREEN FUNCTION                                                       //SCREEN FUNCTION
    mySerial.write(stringData[i]);                                          //SCREEN FUNCTION
  }//SCREEN FUNCTION                                                        //SCREEN FUNCTION
  mySerial.write(0xff);                                                     //SCREEN FUNCTION
  mySerial.write(0xff);                                                     //SCREEN FUNCTION
  mySerial.write(0xff);                                                     //SCREEN FUNCTION
}
1 Like
t1text = "page1.t1.txt=";

Are you sure you are printing to page 1? The first page is page 0. In any case, you need to have the screen on the page you want to see the data on. You don't need to send the page number. So use

t1text = "t1.txt=";

Is your text box definitely t1?
Maybe I have missed it but I can't see the closing quote after rpm. You need to close with

" 0xff 0xff 0xff
rpm = 0;

I can't see where you declare this variable. I have assumed in the sample below it is uint8_t, but you need to declare it.

Here is some sample code to play with

void HMI_display_page(uint8_t rpm) {
  mySerial.print(F("t1.txt=\""));
  mySerial.print(F("RPM = "));
  mySerial.print(rpm);
  mySerial.print(F("\""));
  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.write(0xff);
}

PerryBebbington:

t1text = "page1.t1.txt=";

Are you sure you are printing to page 1? The first page is page 0. In any case, you need to have the screen on the page you want to see the data on. You don't need to send the page number. So use

t1text = "t1.txt=";

I'm definitely printing to page 1. page 0 I=on my nextion is just a opening page with a logo and delay to open page 1. This is why thought I'd have to put page1.t1.txt. The closing quote was included in the writeString function. Is this wrong.

Is your text box definitely t1?
Maybe I have missed it but I can't see the closing quote after rpm. You need to close with

" 0xff 0xff 0xff
rpm = 0;

I can't see where you declare this variable. I have assumed in the sample below it is uint8_t, but you need to declare it.

rpm was declared in the normal way int rpm at the beginning of my code but I had to copy and paste my code piece by piece because it's over the 9000 character count allowed in a post. I'm going to play with your code sample below and see if I can get it to work. Thank you for your advice!

Here is some sample code to play with

void HMI_display_page(uint8_t rpm) {

mySerial.print(F("t1.txt=""));
 mySerial.print(F("RPM = "));
 mySerial.print(rpm);
 mySerial.print(F("""));
 mySerial.write(0xff);
 mySerial.write(0xff);
 mySerial.write(0xff);
}

It's working! The problem turned out to be using a variable with a textbox. Changed it to a number box and it works.

I still have some textboxes that I will need to put a variable into. Does anyone have any advice on how to change this bit to concatenate a variable correctly for nextion.

String t1text = ""; //SCREEN SERIAL
t1text = "t1.txt="; //SCREEN SERIAL
t1text.concat(rpm);
writeString(t1text); //SCREEN SERIAL
}

Thanks in advance

Hello HCM2001,
I've given you Karma because, unlike too many new people on here, you are getting stuck in and trying stuff rather than expecting us to do everything for you. Well, done, keep it up!

It's working!

Good!

The problem turned out to be using a variable with a textbox. Changed it to a number box and it works.

I'm convinced that wasn't your problem. What you were trying to do before was basically right. You will soon find that the number boxes are next to useless as they only can show integers. You can's use them for things like £12.45 or 26.32% or indeed for RPM = 72.9. I don't use them for this reason, I use text boxes the way you are trying to do.

You don't need to send the page number for most things. If t1 exists on the page currently displayed then t1 is enough to identify it. In any case, unless a variable on the Nextion is declared as global its value is lost when it is not on the display. If you are displaying page 1 and you send to a variable on page 2, that data will be lost unless that variable has been declared global on the Nextion.

Difficult for me to comment on your use of concat as I have not needed to use it so don't know what pitfalls there are. I have re-read your original code and I still can't see where you send the closing ". For clarity and to be sure we are definitely talking about the same thing you have to send as per this example:

t1.txt="RPM = 4321"0xff0xff0xff

If you are sure I am mistaken (I might be!) and the " between 1 and 0xff is really there, please point out to me exactly where it is in your code.

Have you tried my code? I am confident it does what you need. You don't need to concatenate strings before you send them, just use separate print and write in the right order to put the separate parts together. The Arduino serial software will take care of the rest. Do you need me to explain my code better? The one thing you have to correct in my code is my declaration of rpm as uint8_t. It should match however you declared it.

PerryBebbington:
Hello HCM2001,
I've given you Karma because, unlike too many new people on here, you are getting stuck in and trying stuff rather than expecting us to do everything for you. Well, done, keep it up!

Thank you very much

I'm convinced that wasn't your problem. What you were trying to do before was basically right. You will soon find that the number boxes are next to useless as they only can show integers. You can's use them for things like £12.45 or 26.32% or indeed for RPM = 72.9. I don't use them for this reason, I use text boxes the way you are trying to do.

I agree because some of my sensors I need at least 2 decimal places.

Difficult for me to comment on your use of concat as I have not needed to use it so don't know what pitfalls there are. I have re-read your original code and I still can't see where you send the closing ". For clarity and to be sure we are definitely talking about the same thing you have to send as per this example:

t1.txt="RPM = 4321"0xff0xff0xff

If you are sure I am mistaken (I might be!) and the " between 1 and 0xff is really there, please point out to me exactly where it is in your code.

void writeString(String stringData) { //SCREEN FUNCTION
** for (int i = 0; i < stringData.length(); i++) //SCREEN FUNCTION**
** { //SCREEN FUNCTION //SCREEN FUNCTION**
__ mySerial.write(stringData*); //SCREEN FUNCTION*__
* }//SCREEN FUNCTION //SCREEN FUNCTION*
* mySerial.write(0xff); //SCREEN FUNCTION*
* mySerial.write(0xff); //SCREEN FUNCTION*
* mySerial.write(0xff);*
It's hiding within the function for writestring.[/b]
Have you tried my code? I am confident it does what you need. You don't need to concatenate strings before you send them, just use separate print and write in the right order to put the separate parts together. The Arduino serial software will take care of the rest. Do you need me to explain my code better? The one thing you have to correct in my code is my declaration of rpm as uint8_t. It should match however you declared it.
I did try your code and wasn't able to get it to work. I even tried leaving rpm to int like I have it declared. The only way I have been able to get it to work is changing to numberboxes. I will try your code more tonight since I'm heading in the right direction. Thanks again.
[/quote]

Just realized you meant the " not oxff. Sorry

hcm2011:
Just realized you meant the " not oxff. Sorry

YES!
I still think it is missing. I still do not think your writestring function sends it.

Try calling my function with just some just some number passed to it, for example

HMI_display_page(25);

Hopefully it will display RPM = 25

PerryBebbington:
YES!
I still think it is missing. I still do not think your writestring function sends it.

Try calling my function with just some just some number passed to it, for example

HMI_display_page(25);

Hopefully it will display RPM = 25

Just ran with your code and floats in a textbox are working perfectly! Thank you so much!

Just ran with your code and floats in a textbox are working perfectly! Thank you so much!

Oh good! :slight_smile:
My pleasure! :slight_smile:

Your next challenge is to make a better function that mine, one that doesn't just write to textbox t1.

Have fun experimenting :slight_smile:

PerryBebbington:
Oh good! :slight_smile:
My pleasure! :slight_smile:

Your next challenge is to make a better function that mine, one that doesn't just write to textbox t1.

Have fun experimenting :slight_smile:

Already been playing with it. Changed it to page1.t1 because it was sending text to my page2 textbox as well and it works fine that way as well.