Update variables before displaying page

Hello folks,
I know this has been asked before, but I am still unclear on how to do this.

What is the best way to update variable / text before displaying the page?
I tried the printh 23 02 54 3E method, but couldn’t get it to work.

I am populating the time and day/date as a text field from an arduino but shows the placeholder text at the time of page change.


ezgif.com-gif-maker (7)

To be honest, I have no idea what the printh 23 02 54 3E method is.

To avoid these issues, Nextion has a preinit and a postinit event for the page loading. The preinit is executed before the screen is updated. Ideally, the date is already set somewhere in a global variable on Nextion, because it makes life a whole lot easier. The Arduino only needs to update that one global variable, and the preinit event on Nextion only needs to copy that one variable into the local one.

If it really needs to be done the way you described it, meaning the Arduino sends the date only after entering the page, things get a little bit more complicated. Waiting for incoming data is one of those things that are noticeably less intuitive in Nextion than you’d expect.

  • The simple and dirty way (everything in the preinit event)
    • Notify the Arduino that it needs to send data.
    • Wait a fixed delay within which the Arduino must send said data.
    • That’s it. As long as the correct value has been set before the preinit event finishes, you won’t see glitches on screen.
  • The cleaner, more complicated way
    • Preinit notifies the Arduino that it needs to send data.
    • Preinit event stops screen updates using ref_stop
    • Preinit starts a timer running at 50ms (lowest value you can set in the editor)
    • Timer event checks if the value has been updated. If so, it enables screen updates again, using ref_star and disables itself.

You can’t wait in a loop because serial data isn’t processed while a loop is running (even if you include doevents).

Kind regards,

Thank you @Max !
Option 2 seems like a cleaner implementation.
I may have to implement a “page handler” on arduino to send the relevant data for that page.
The time and date was just one of the few fields on each screen.

Thank you!