In my humble opinion I think that most of the interaction between the Arduino and the Nextion can be done by setting global variables on the Nextion and sending Type-Id-Value pairs back to the Arduino.
To put my money where my mouth is I made a simple -proof of concept- library for the Arduino to show that this concept works. This library can be found on Github:
With this library it should be possible to connect as much Nextion displays as there are hardware serial ports available. I got only one 3.5” display so this is an untested feature :).
This library is intended for a project that involves measuring barometric pressure and temperatures, sounding alarms and logging to a SD card and functions will be added if needed.
Basic working principle:
The only have to sent the information we want to show on the Nextion by setting global variables in the Nextion like temperature, Alarm setting, Alarm occurrence etc. The Nextion display these values on the appropriate screens or takes an action like changing the page to an alarm page if an alarm is raised. If the user changed the alarm setting and presses the Ok button, the Nextion sent a type, the id and the changed value back to the Arduino. The Arduino updates it’s own alarm setting and the appropriate variable in the Nextion so the user can see that the setting is accepted. So the Arduino only have to send commands like var.va0.val=7814 (meaning 78.14 Celsius) and only have to respond to messages consisting of a Type-ID-Value pair.
Unfortunately variables have no onChange event so the Nextion must be notified that a variable has changed. There is no direct command to do that. A screen can be updated on the Preinitialize event but that doesn’t work on the current screen.
However a Timer component on the current screen can be enabled with tm0.en=1 and when the last line in the Timer event is tm0.en=0 it will function as a one shot timer. So sent the updated values en enable tm0 and the Nextion can do it’s job by executing the commands in the Timer event. With print and printf custom messages from the Nextion to the Arduino are composed.
Have fun :)
Sounds like a good project. I would offer a thought regarding onChange ...
When temperature and pressure changes, this is typically captured with the Arduino and not the Display (The Enhanced Series is creating an exception). So the Arduino would normally know of all variables, and their respective values (or at least where to find them if they are stored on the Nextion). Therefore, when an onChange type event would occur, it is usually the Arduino that has captured the new value, can check against current, and update the appropriate display if need be.
The idea of multi-displays is definitely intriguing - I figure a mini three display setup and I could replicate a scaled mini workstation.
No Edwin, I was meaning to say the appropriate display (as in Nextion 1, Nextion 2, Nextion 3) as this project is allowing for multiple serial ports and thus the cool ability to connect multiple Nextions.
Just asking, Why not update your visual components at the same time as the global variables and bypass the timer requirement? The same routine that receives the new value (say barometric pressure) and decides if that value is different than the current value stored in the Nextion global variable, and hence whether or not to update the Nextion global variable, could also run the series of commands needed to bring the rest of the display up to date.