I would like to store the current page number to a variable on my Nextion display.
"back" is a global variable on "page9". When I am on page6 I would like to store the page number to the variable "back". After that I switch to page9 and then I press a button with the event "page back.val".
The problem is that page.id is always 0, and I could not find any other method to get the number of my current page.
I would be very grateful if you could help me to solve this.
Ok I have found it out by accident: page9.back.val=dp
How did you find by accident? I hardly doubt you typed randomly and discovered.
Searching for information should never result in an accident, but should be conducted with purpose.
the system variable dp is volatile, as soon as pages are changed, the value of dp will reflect the changes.
It is listed in the Nextion Instruction Set - which should be the primary go to source of information.
But why use a component variable for this. The method to get the page is purposefully hard code it.
If you are changing pages via your HMI design, you know which page the user is on before button press
setting in the button press release
is easy .... then in the page 3 event that is to return user back to calling page
This results in no component variable required or globals needing to be set. Uses no resources.
But I would also have to point out that keeping track of your current page in your MCU is also easy.
Setting a byte variable to hold the value, and merely track the changes as the touch events are then
received is a simple assignment MCU side.
The bigger question and the one that will cause the most grief is which is in control of the project, and for this the answer is typically the MicroController and not the display. Having two devices in control is like the three-way hall switch and a contrary opponent. The setting set by one are overridden by another and this will cause synchronization issues or constant polling for each side to determine what the proper value is and redundantly adjust the values.
So when placing logic in the Nextion is to clearly have defined the roles for which is in control of what and that changes are reported so that both Nextion and MCU are in sync and dealing with the same data. I will also state that placing logic in the Nextion also alters the dynamics of repair and maintenance. If the logic is in only the MCU, then only a controller need be replaced, if logic is Nextion side only, then only the Nextion need be replaced. But when logic is in both, then both may need to be replaced when only one has failed as determining which code is in which device may become impossible to determine. Much more documentation is required when logic is spread across two devices.