How to know the current page of my nextion screen with arduino

Hi,
I am working on a project in which I need to know the code to know in what page is the nextion screen, because I would like to use: If (page1=true && digitalRead(botonPin) == LOW) ->do something, but I have searched on internet but I haven't found anything that works for me.
Can someone explain a library or the code for this?

?? Aren't you, as the programmer, in control of what page is being displayed?

Yes, but in the nextion program but I do not know how to say to arduino in what page I am

I use the EasyNextionLibrary by Seithan and it is available through the library manger.

The library has an integer variable currentPageID and it uses a very simple method with the preInitialize event for a page to automatically update that variable behind the scenes anytime a page changes.

/* In order to update currentPageId variable with the current Id of the page,

  • you must write at the Preinitialize Event of every page: printh 23 02 50 XX , where XX the id of the page in HEX.
  •  For page0: `printh 23 02 50 00`
    
  •  For page9: `printh23 02 50 09`
    
  •  For page10: `printh 23 02 50 0A`
    

*/

There are library examples showing how to use the currentPageID.

I get you, I made it the printh in Nextion editor but now
how I can read the page sended from Nextion?
Can you give me maybe an example?

This is what I have for now;

#include "EasyNextionLibrary.h"  

EasyNex myNex(Serial);
int pag;
const int ledPin = 13;
 
void setup(){

  myNex.begin(9600);

  myNex.writeStr("page 0");

}

void loop(){

  myNex.NextionListen();

 pag = myNex.currentPageId;
if (pag==0){                  //If page is 0 the led is on
digitalWrite(ledPin,HIGH);
}
If (    //If page is 1 led is off  )
{ digitalWrite(ledPin,LOW);
}
}





This is what I have for now

The posted code does not compile.

What is this?

If (    //If page is 1 led is off  )
{ digitalWrite(ledPin,LOW);
}

You also need to make the ledPin an OUTPUT in setup().

If you fix that, does the led light up on page0?

What Arduino are you using?

It's unusual for the serial connection between the Nextion and the Arduino to be on the usb serial port like this
EasyNex myNex(Serial);

More typical would be either a software serial connection or a connection to one of the additional hardware serial ports available on the Arduino if they exist.

Please explain how you are handling the serial connection between the Arduino and the Nextion with your program.

I am working with an arduino uno with the pins 0(RX) and 1(TX)
I have fixed what you had said and the led is always on.

#include "EasyNextionLibrary.h"  


EasyNex myNex(Serial);
int pag;
const int ledPin = 12;
 
void setup(){
  myNex.begin(9600);

  myNex.writeStr("page 0");
  pinMode(ledPin, OUTPUT);
}

void loop(){

  myNex.NextionListen();

 pag = myNex.currentPageId;
if (pag == 0){                  //If page is 0 the led is on
digitalWrite(ledPin,HIGH);
}
if (pag == 1){                  //If page is 1 the led is off
digitalWrite(ledPin,LOW);
}
}






I have tested your code in my setup (which uses Serial1 on a Nano Every connected to the Nextion) and the led turns on and off between page0 and page1.

Do you have any other Serial communication between the Nextion and the Uno in your program? Can you read and write between the Uno and the Nextion?

Can you write a simple test program to turn the led on and off with the Uno without Nextion? That will verify the led wiring and the pin12 functionality.

I made it the printh in Nextion editor

What did you add, and where did you put it?

EDIT:
I just tried your code with a Nextion connected to the hardware serial pins of a Uno, and indeed the led goes on and off with the switching of the page from 0 to 1.

Do you have a ground connection between the Uno and the Nextion? The code does not work without it.

The pin 12 works properly, in nextion editor I put in page 0 in Preinitialize event printh 23 02 50 00 and in page 1 in Preinitialize event printh 23 02 50 01,
Is it okay?

I think the problem is in RX, but I change the motherboard and still not working, I have to change something in the library or I have to use an official Arduino Uno

Yes, that is correct.

I think the problem is in RX,

Your problem certainly sounds like a hardware issue to me.

As I said, I can run your Arduino program on a Uno connected by Serial to my Nextion and see the page changes with an led. The Ground connection was important and of course there is cross connection Tx>Rx and Rx>Tx

Do you have another Arduino or perhaps a usb to ttl converter that you can use to test the operation of the Rx on the board you are using? The Nextion serial output is 3.3v and typically that level is high enough for the Uno to read, but perhaps there is an issue with your board.

You can try using a bidirectional logic level shifter to boost the signal level.
https://www.sparkfun.com/products/12009
https://www.adafruit.com/product/757

Can you read any values from your Nextion with the Arduino?

Can you use the library trigger function with a button on a page?

Do you have a generalized reading problem or just with the page number?

Can you write anything from the Arduino to the Nextion? Change a text field or a number?

I make a code to read a number from nextion and write it in an other number val and it works property.
what is a trigger function?

what is a trigger function?

It is one of the key features of this library. It lets you have something like a button release event trigger an operation on the Arduino. It uses some of the key library core functions in common with the page id detection code.

https://github.com/Seithan/EasyNextionLibrary/tree/master

See the FourStepExample library example code for the hmi and arduino code.

It is a good practice when first using a library, to run some of the library example codes. If they don't work properly, then there are issues other than the code involved.

I make a code to read a number from nextion and write it in an other number val and it works

This would lead me to think it is not the serial interface, and there is something else at work. What Nextion model do you have. We may need to test an HMI program.

I will have a look to that examples.
I am using a NX4024T032_011

Here is a very simple hmi file with two pages which couples with the Uno program in this thread. The only way to attach the hmi files in forum posting is in zipped format.

Two page ID Test.zip (15.7 KB)

WOW, I had to change the model in your HMI that was diffent to mine but know the led works fine, I look the code of nextion and we have the same code, maybe was samething wrong in my file of nextion. These days I will be working on my proyect thanks to you, thanks for helping me

Good news.

Let me know if you solve the mystery of why the page detection was not working with the hmi file you were using.

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