Start a new topic

How to find which page (Nextion) you are in via Arduino

Hi,

I was wondering if there is a easy way to see which page you are in when running code on the Arduino?

ie

if (page==0)

{b0 - do something;}

else if (page==1)

{b0 - do something;}

else if (page==2)

{b0 - do something;}


Hope that makes sense :).

Thanks

Matt


When you send the command to Nextion , in its reply, there is some byte is the page ID. Maybe you can find it on the wiki page?

sendmeInstance:

sendme  //No parameter

Remarks:

Device will immediately send the current page ID to serial port after receiving this instruction.

The specific sending format of data, please refer to the table: Format of Device Return Data.

@Matt

If you use the ITEAD Arduino library as I saw in your other post ( http://support.iteadstudio.com/support/discussions/topics/1000062462 ), I think you need to use different names for the different b0 buttons in the Arduino code. Something like this :

NexButton b0_0 = NexButton(0, 1, "b0");
NexButton b0_1 = NexButton(1, 1, "b0");
NexButton b0_2 = NexButton(2, 1, "b0");

and in setup :

    b0_0.attachPop(b0x0xPopCallback);
    b0_1.attachPop(b0x1xPopCallback);
    b0_2.attachPop(b0x2xPopCallback);

then you write a "void b0x_xPopCallback(void *ptr)" for each button.


Otherwise you can send the "sendme" command as proposed by Wade Hassler.

You can send this command from the Arduino, or you can make the Nextion to send the answer automatically when a page is opened by writing "sendme" under "Initialization" in Nextion Editor. See picture :

Answer : 0X66+Page ID+End

.

There's also a command "sendCurrentPageId" in the Arduino library that sends "sendme" and waits for the answer :

/**
 * Return current page id.   
 *  
 * @param pageId - output parameter,to save page id.  
 * 
 * @retval true - success. 
 * @retval false - failed. 
 */
bool sendCurrentPageId(uint8_t* pageId)
{

    bool ret = false;
    uint8_t temp[5] = {0};

    if (!pageId)
    {
        goto __return;
    }
    NexTouch::sendCommand("sendme");
    delay(50);
    nexSerial.setTimeout(500);
    if (sizeof(temp) != nexSerial.readBytes((char *)temp, sizeof(temp)))
    {
        goto __return;
    }

    if (temp[0] == NEX_RET_CURRENT_PAGE_ID_HEAD
    && temp[2] == 0xFF
    && temp[3] == 0xFF
    && temp[4] == 0xFF
    )
    {
        *pageId = temp[1];
        ret = true;
    }

    __return:

    if (ret) 
    {
        dbSerial.print("recvPageId :");
        dbSerial.println(*pageId);
    }
    else
    {
        dbSerial.println("recvPageId err");
    }

    return ret;

}

NB : With this function, it seems like the Arduino cannot do anything else (for instance reading inputs) while it is waiting for the answer, minimum 50 ms, maximum 550 ms.


In actual testing process,the "sendme" command does not need 50 ms.As you see, the 50 ms in the library is just for data stable from Nextion screen.  If you need to speed up it ,you can edit it.

The arduino library used to have functions sendCurrentPageId, setCurrentBrightness and several others.  However, I just updated to the latest library and now these two functions no longer exist.  Are there replacements that I am not finding or will these be rewritten in a future version?


3 people like this

Also I don't think you should set the getObjPid() as a protected function because it is really nice to be able to do this:

    uint8_t CurrentPage;
    sendCurrentPageId(&CurrentPage);
    if (CurrentPage == HomePage.getObjPid())
    {
       // do some screen updates if I am on my home page
    }

 



1 person likes this
In my project there are about 100 paramaters that are possibly updated to the nextion every second, which was slowing my system down to the point where it was useless.
. It was/is essential to know the Nextion page id, so that I only update paramaters relevant to the page displayed .  So I used Wade Hassler suggestion, but it was not obvious to me how this data is read by the Arduino.
I looked at Raphael's technique of using "sendCurrentPageId", but my libraries do not
contain that code and I really want to know the page id immediatily without asking for it.
.
the method I used  was to modify the NexHardware library so that it picks up the page id and makes it available to the Arduino code.
Include in nexLoop

 

       //this has been implemented by lee VK3PK
		if (c==NEX_RET_CURRENT_PAGE_ID_HEAD)                 //ie 0x66
        {
            if (nexSerial.available() >= 4)                  //ie 4 or more
            {
                __buffer[0] = c;                             //which is the 0x66  
                for (i = 1; i < 5; i++)
                {
                    __buffer[i] = nexSerial.read();   //read subsequent bytes
                }
                __buffer[i] = 0x00;                   //terminate text buffer  -  probaly not required
                
                if (0xFF == __buffer[2] && 0xFF == __buffer[3] && 0xFF == __buffer[4])
                {
					page_id=__buffer[1];   //save it if it looks good  				
                }
                
            }
		}

 There is little overhead code in the Arduino sketch to make this work.


In addition I updated to 115200 baud and got rid of or shortened a few delays in the library.  The system now flies.



 

 

 

I did a simular thing but I made the page ID command create a function call. Then on the nextion, when a page loads I send the page id. So I get a function called to notify me when a page changes and to which one.

Hi Guys,

I'm trying to implement the code Lee de Vries or Bspranger is talking about.

Being a newbie with libraries where is that code entered in the newHardware library?

Could you attach that file here please?


Knowing what page I'm on would be great!


Rod

hello rod parsons, here is the solution i used to get the page number from the nextion display to arduino. 

am using the library at: itead library. But the library does not contain the function sendCurrentPageId. Thus you have to edit the library in order to get the page number.in the library you should edit the nexHardware.cpp and nexHardware.h. i have attach a file which you should replace in the library.


When you have successfully replace the two file, restart arduino ide(close and reopen it).



 

//Declarartion of button, text.....


uint8_t number = 0;//will store the page number.

void set up(){
//call to button, text etc...

}

void loop() {
// do not worry worry about the nowtime and update, just paste it in the loop like this.
  unsigned long nowtime = millis(); 
    unsigned long update = 0;
    if (nowtime > update)
    {
        pagenumber();
        update += 10000;
    }
    nexLoop(nex_Listen_List);
}

void pagenumber()

{
    sendCurrentPageId(&number);// call the method to get the page number.


switch(number){// use a switch statement for each page. 
        
        case 0:    //for page0
          dbSerialPrintln(number);// print the page number again for testing.
       // do something
        break;
        
           case 1:    //for page1
          dbSerialPrintln(number);// print the page number again for testing.
          // do something
            break;
          
           case 2:    //for page0
          dbSerialPrintln(number);// print the page number again for testing.
       // do something
        break;

       }// close switch statement.
 delay(100);
}

 

rar

* note that the code snippet above i posted contain the arduino code to get the page number. 

Awesome. Thanks Arduinextion.

Hello everyone.


I test with the above code. However, the same error returned. not returning the value of the page ID. Is it possible you to post the .HMI files, and .ino with working example?


Is there any way to know the name of the current page, instead of ID?


I thank everyone.


which arduino are you using the mega or uno? post ur arduino code here i try to debug it. and the hmi file also
Login or Signup to post a comment