Start a new topic

Arduino SimpleNextion library

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:

https://github.com/EdwinCroissantArduinoLibraries/SimpleNextion


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 :)









2 people like this idea

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.


Regards


... and update the appropriate display if need be.
If you mean page instead of display: The Arduino doesn't need to know how the Nextion display is organized. It sent values and receive commands. Let say that we have an application with a small display and later on change that to a larger display. On a larger display we can show more information on a page so instead of 6 pages we only need to use three pages. The way it's done now this means that the program on the host must be changed as barometric pressure moved from page 2 on the small screen to page one on the large screen. With what I am proposing only the code in the larger screen has to be changed and the screens can be swapped without any modification of the host.

So I change a global variable on the Nextion and on this moment the only way to update the Nextion is to do the trick with the timer. A procedure that can be called would be handy and an onChange event would be better because the update can be very selective. Both would be great :) And if the procedure accepts parameters that would be super :)
 

 

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.

Login or Signup to post a comment