Nextion: change page : now fixed

Hi,
Im using a UNO , Nextion display plus the "Nextion.h" library.

I have two pages set up page 0 and page 1:

//declare pages:
NexPage Calpage =NexPage(1,1,"Page 1");
NexPage Mainpage=NexPage(0,0,"Page 0");

The display works fine and I can switch between these pages in the Itead IDE simulator attached to the board via the serial link.
I can also change numbers etc on both pages from Arduino code and from the simulator.

The problem is I can find the commands/syntax for changing the displayed page from my Arduino sketch, anyone help ????.
( I had thought it was just "Calpage.show();" but this doesn't do anything !
my play code:

/*
   Paul Hammond
   Nextion display test uses "eec corner wt 2.tft"
   just to record the commands of interest and how they work
*/

#include "Nextion.h"



// LED pins
const int led1 = 13;
int X = 255;
int Y = 321;
int noloops = 0;



// Declare  Nextion objects - Example: (page id = 0, component id = 1, component name = "b0")
//calibration page:
NexButton LF = NexButton(1, 0, "r0");
NexButton RF = NexButton(1, 1, "r1");
NexButton LR = NexButton(1, 2, "r2");
NexButton RR = NexButton(1, 3, "r3");

NexText Wt = NexText(1, 4, "t4");

//Main page:
NexNumber LFwt = NexNumber(0, 0, "n0");
NexNumber RFwt = NexNumber(0, 0, "n1");
NexNumber LRwt = NexNumber(0, 0, "n2");
NexNumber RRwt = NexNumber(0, 0, "n3");

//declare pages:
NexPage Calpage = NexPage(1, 1, "Page 1");
NexPage Mainpage = NexPage(0, 0, "Page 0");

void setup()
{
  nexSerial.begin(9600);//coms to LCD

  nexInit;
  pinMode (led1, OUTPUT);

}
void loop()
{
  digitalWrite(led1, HIGH);


  delay(1000);
  // Mainpage.show();
  RFwt.setValue(Y);
  LFwt.setValue(999);
  RRwt.setValue(456);
  LRwt.setValue(noloops);
  digitalWrite(led1, LOW);

  delay(1000);
  RFwt.setValue(X);
  RRwt.setValue(654);
  LFwt.setValue(000);
  LRwt.setValue(noloops);
  noloops++;

  if ( noloops == 10) // change the page viewed
  {

    // move to display"1"


    delay(3000);

    //and back...



    noloops = 0;


  }
}

Update:
As this works with the simulator, my thinking is it’s a library problem.
I’m going to try programming without the library ( which might be more efficient anyway, as I don’t need all the commands).
I will report back !

Hello Hammy,

I saw your question when you first asked, but didn't answer as I was hoping someone with better knowledge than me would reply. I have plenty of experience of the Nextion and have 3 finished projects using the Nextion HMI. Unfortunately those projects use PICs not Arduino, so while I will try to help what I say might not be directly relevant to you. Here's what I think...

First, I have never changed the page from the host, what I have done is changed the page as a result of pressing a button and used, for example, page page1 to go to page 1 as a touch release event on a button. Then on page 1 I have put code to send the page ID as a post initialisation event. The host, seeing the new page ID then sends the relevant data to populate the various elements on the page. The reason I did it that way it it means that the data is not sent to the Nextion until it is ready to receive, that is, after the page has initialised.

I have never used a Nextion library, from the brief look I had of them I wasn't impressed and wrote my own code to send to the Nextion. Here is a sample of code, this is XC8, which is the C compiler for PIC:

sprintf(HMI_write_buffer[HMI_write_buffer_end], "B%1d.pco=%1u", i, green);
++HMI_write_buffer_end;

You need to know that HMI_write_buffer is a buffer that holds what is being sent to the Nextion while the code I have that sends it to the serial port can do so in the background while the rest of my program carries on. The particular sample I have given you makes the font of button Bi green. You will have to adapt this to Arduino, I don't think you need the buffer, just print the code to the serial port.

Have you found the Nextion instruction set? In case not, it is here Nextion instruction set. The one you want is page, or rather I think it is as that's the one that works on the Nextion, but as I mentioned, I've never had the host tell the Nextion to change page. Something like

Serial.print("page page1");

might be enough, but please understand I've not tried it.

I hope that helps, feel free to ask me more advice about the Nextion.

Perry

Thanks for the input Perry - I’ve not had chance to try it yet, but yes I found the set of instructions that you follow with FF FF FF. I agree with what you’ve said thx .

hammy:
Thanks for the input Perry - I’ve not had chance to try it yet, but yes I found the set of instructions that you follow with FF FF FF. I agree with what you’ve said thx .

Doh!

Sorry, yes, I forgot that important little detail!

For the benefit of anyone else reading this, so as to be clear, the Nextion expects each instruction to end with 0xffffff.

Also, it doesn't like random spaces where there shouldn't be spaces, send exactly what works in the simulator.

well got it working ok and can now change pages and alter numbers and radio buttons. I can't get uploading text to work tho...

t4.txt="hello" - works on the simulator , but I can't get that bit right in my sketch (can live without it tho)

//changing text 
mySerial.print("t4.txt=");
mySerial.print("fred");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);

or even...

mySerial.print("t4.txt=eric");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);

THe proble appears to be "" related , and the following has fixed it

 mySerial.print("t4.txt=\"fred\"");

  mySerial.write(0xff);
  mySerial.write(0xff);
  mySerial.write(0xff);

Do you need me to reply as you've fixed it?

What you say is what I was going to suggest. In case you or anyone else reading needs an explanation, the Nextion expects the text to be in quotes, so

mySerial.print("t4.txt=eric");

Will send t4.txt=eric

What the Nextion expects is t4.txt="eric", so you need

mySerial.print("t4.txt=\"eric\"");

As you discovered.

Something else that tripped me up, which you have not asked about (yet), is numbers. Don’t bother with the numbers built in for display. Use text boxes and send numbers as text. The built in number can only be unsigned integers, which are not useful or ornamental. You can’t use them with £ or $ or % or any other such thing, you can’t make them decimal and you can’t make them negative. Don’t waste time messing with them.

Thx for the help Perry. - noted about numbers . I’ve not come across this as I’ve only used “whole positive “ numbers up to now .

hammy:
I have two pages set up page 0 and page 1:

//declare pages:

NexPage Calpage =NexPage(1,1,“Page 1”);
NexPage Mainpage=NexPage(0,0,“Page 0”);




The problem is I can find the commands/syntax for changing the displayed page from my Arduino sketch, anyone help ????.
( I had thought it was just "Calpage.show();" but this doesn't do anything !

Your page names are wrong! They must "Page0"and “Page1”.
There should be no space characters between.
AND the CID of Page1 is wrong! page CID is always zero!

I defined it as follows and it works fine:

NexPage page0 = NexPage(0, 0, "pEpromParams");
NexPage page1 = NexPage(1, 0, "pModeSelection");
NexPage page2 = NexPage(2, 0, "pDispenseMode");

and I can open the page with

page1.show();

command.
my answer was a late. I saw it today. I write in the hope that others will benefit. :slight_smile:

3 Likes