I am using Arduino library for nextion and developing an user interface with Nextion. There are cases that I need to update a variable of an inactive page and need to use the updated value on several pages.
Defining the variable on the global scope lets me reach the value of the variable from different pages but the value of a variable can not be updated from Arduino if its page is not active.
In addition even if a variable is updated when its page is active whenever the page of the variable becomes inactive and active again the previously updated value of the variable is initialized and the updated value is lost.
To be able to define variables independent of the pages which can be updated anytime not depending on the active pahe and which are only initialized once with the startup of the nextion would be very handy?
If anyone is also in need of such a feature, please post a (+1) reply to this post.
Maybe a developer notice this request.
You CAN NOT refresh an object in a NON active page, without adding additional code in a PreInitialize Event of the loading page to store values and Post Initialize Event of the loading page to retrieve values.
Order of execution:
Load HMI design and set variables and objects to default of HMI design
Wait for external events to occur (touch, receiving commands, timer, etc)
sys0, sys1, and sys2 are page independent numeric global variables.
There are 3584 bytes (out of 8K) of RAM available for an active page. Once a page has changed, what was in RAM has been lost and is actually rebuilt. It is a design flaw to make changes and updates to what is not active and will not be reflected.
Global Variables, usually embedded into the first page are called with pagename in the variable name
global numeric variable va0 on page0 in other pages is referred to as page0.va0.val
The more accurate means of doing what you are attempting is to have your variable in your Arduino code defined in a global scope, and make changes to it. By using the sendme command, every time a user changes pages, the Nextion will inform you of such with response of 0X66 0X02 0XFF 0XFF 0XFF
(Meaning 66 - page info next 02 - user changed to pageID 2 FF FF FF - Data Termination)
Good design in Arduino is to catch these page changes, then as required by the page the user has changed into, send the necessary global Arduino variable values to the pages placeholders.
t0.txt="12:00pm" or whatever may be required.
Thank you for the explanation. Since the Arduino code is running on its limits, I preferred to solve it on the Nextion side. I will solve it on Arduino side. But It would be nice to have 3 string variables independent from the page, just like sys0, sys1 and sys2.
You are understanding the first lines ... IF you do a store in a PreInitialize event of the loading page, and a retrieve in the PostInitialize Event, you can overcome the Page loading the default values from the HMI defined defaults? But this has to be consciously done for each variable and each page where the variable might be effected.
Time as a string "13:15:27" in page0.va0.txt where va0 is set .vscope global and .sta String .txt-maxl 10
In the Preinitialize Event of say page4, the page being loaded
In the Postinitialize Event of say page4, the page being loaded
I am now reviewing all of the Feature Requests, this will take some time, patience please.
- Global variables in project start page exist with some advanced coding to learn.
Although this wasn't a real feature request, it is now marked as reviewed.