Start a new topic

Arduino code to know when page change (command 'sendme')

I want to know when Nextion opens page with id=3.

I tried with command 'sendme'.

aa.PNG

In debug sends: 0x66 0x03 0xff 0xff 0xff

But i dont know whats the code for arduino and if the nextion library have something for this.

Tried this, from the example but i dont understand what exactly does.

  

#include "Nextion.h"

NexPage page3 = NexPage(3, 0, "page3");

NexTouch *nex_listen_list[] = {
  &page3,
  NULL
};

void page3PopCallback(void *ptr){
    dbSerialPrintln("page3PopCallback");
    page3.show();
}

void setup(){
  nexInit();
  page3.attachPop(page3PopCallback);
}

void loop(){
  nexLoop(nex_listen_list);
}

  


I think this is not i just expect from you sir 



thank you any way

Three quarters of the fun is writing a function, testing it and observing its behaviour.

In this manner we get to learn what to expect from what we write.

sir !!

i have another problem..i just add code for nextion page change to

if(value>=50) {

Serial.print("page page1");

Serial.write(0xFF);

Serial.write(0xFF);

Serial.write(0xFF);

} else {

Serial.print("page page0");

Serial.write(0xFF);

Serial.write(0xFF);

Serial.write(0xFF);

delay(50);

}

above code and compile with it.when i test with display its page change work fine but problem is

all pages and components  refresh every millisecond.if i add delay(1000) it will refresh every one second.i just attached video and my arduino sketch.give me some advice

on this..

thank you  !!

mp4
ino
(965 Bytes)

Thank you very much sir ..i will try using Ur valuable tip

I can not teach you how to program, but you may be looking for something like

if(value>=50) {

serial.print("page page0");

serial.write(0xFF);

serial.write(0xFF);

serial.write(0xFF);

} else {

serial.print("page page1");

serial.write(0xFF);

serial.write(0xFF);

serial.write(0xFF);

}


This is a pseudo code, and may not compile. It depends on so many other settings, how you declared your serial etc.


1 person likes this

Sir I want to change nextion page into one another page using arduino coding (ex: page0 -->  page1--> page2 <--page0 or page1) like this without using any nextion library ..Another way what was I mean if we using analog input in some value (Ex; value =50 , nextion page0 should change in to page1)  like that. 

 

please help me on this ..I’m totally stuck with this ,,, 

 

 

Thank you very much

I tried the second solution and works perfect, Its just what I was searching for! :)

Im not pro on this things, but I'll keep trying.

Thanks man, you are my hero!

Mario, just another thought.


Programming is an art of finding an approach that accomplishes what you need.  And just like if you tasked 10 people to find a route from one side of a city to the other side, you would probably end up with 10 different paths that vary in some way - programming is the same. 10 programmers, 10 variations.


It is possible - if you unchecked the page Send Component ID is the Pressed / Released events that you could repurpose these codes.  Since a Page Touch Pressed/Released events basically turns the whole page (except for where other components occupy) into one great big button - this is probably not the function you are actually looking to implement.  Therefore repurposing it's touch pressed/released events in your Arduino code doesn't change your functionality or your design goal.


Now to do this, you are not using any of the page component's Touch Pressed/Released Events in the HMI.  But there is some mechanism in your HMI that does calls your page command to change pages (maybe inside a button pressed/released event).  If on the line just before that page command that changes pages, you were to send a formatted page touch pressed code, in the IteadLib Arduino Nextion library you could make use of the attachPush and attachPop Arduino functions.


example

   if in button b3, it will change the page to page1 and page1 has Page sequence number 1.

   inside the b3 Touch Pressed Event

       printh 65 01 00 01 FF FF FF  // simulate page press for page1 or   page.attachPush

       page page1

   or inside the b3 Touch Released Event

       printh 65 01 00 00 FF FF FF  // simulate page release for page1    page.attachPop

       page page1


You could then use the Arduino example:

#include "Nextion.h"

int current_page = 0;


NexPage page0 = NexPage(0, 0, "page0");

NexPage page1 = NexPage(1, 0, "page1");

NexPage page2 = NexPage(2, 0, "page2");

NexPage page3 = NexPage(3, 0, "page3");


NexTouch *nex_listen_list[] = {

      &page0,&page1,&page2,&page3,NULL

};


void page0PopCallback(void *ptr) {

   dbSerialPrintln("HMI User is arriving at page0");

   current_page=0;

   // any other code important to the user arriving at page0

}


void page1PopCallback(void *ptr) {

   dbSerialPrintln("HMI User is arriving at page1");

   current_page=1;

   // any other code important to the user arriving at page1

}


void page2PopCallback(void *ptr) {

   dbSerialPrintln("HMI User is arriving at page2");

   current_page=2;

   // any other code important to the user arriving at page2

}


void page3PopCallback(void *ptr) {

   dbSerialPrintln("HMI User is arriving at page3");

   current_page=3;

   // any other code important to the user arriving at page3

}


void setup(){

   nexInit();

   page0.attachPop(page0PopCallback);

   page1.attachPop(page1PopCallback);

   page2.attachPop(page2PopCallback);

   page3.attachPop(page3PopCallback);

}


void loop(){

    nexLoop(nex_listen_list);

}


This basically accomplishes what the sendme command would do.

Send notification for which page is the current page.  In the sendme command, the page return code is sent out just after each new page is loaded.  In this method you are triggering it just before the page is being changed (and in about 3ms, the user will on the new page and it will be loaded).


You would need to make sure to customize your printh 65 01 00 01 FF FF FF to accurately represent the page the user is going to in all the events that trigger your page changes.  So it can be done.



2 people like this

Hey Mario


When you look at the example, not only the Arduino .ino  code but also the *.hmi in the Nextion Editor

you will notice that each page is responding to the Send Component ID checked in each of the Page Touch Release Events.  The compiled TFT is also included with the examples.


an .attatchPop is going to catch the Send Component ID in a Touch Release Event

an .attatchPush is going to catch the Send Component ID in a Touch Pressed Event


The Arduino Library is not completely comprehensive,

but you are certainly welcome to edit your local library and customize it for your projects needs.


At this point, I do not have any working code for you on this, but the gist would be:

- edit NexHardware.cpp and expand the nexLoop function

- inside the nexSerial.available while statement AND not inside the event_touch_head

- create an if clause to capture the 0x66 return code


   if (NEX_RET_CURRENT_PAGE_ID == c) {

      if (nexSerial.available()  >= 5) {

         __buffer[0] = c;

         for (i = 1; i < 6; i++) {

           __buffer[i] = nexSerial.read();        

         }

        __buffer[i] = 0x00;

        if (0xFF == __buffer[2]  && 0xFF == __buffer[3] && 0xFF == __buffer[4])

        {

           // 0x66 page return command should not be received and page number in buffer[1]

           // create a function and pass __buffer[1] to your function to handle your page change

        }

      }

   }


The above code is following the format provided in nexLoop - and is DEFINITELY untested.

Login or Signup to post a comment