Although I am a new signed-in user, I have been watching and learning from the community of arduino.cc .
I just finished the tutorial I had been preparing and already uploaded it at my website, https://www.seithan.com .
Here is the Tutorials Web page:
And the link to download the zip file with both the .HMI file for Nextion and .ino file for Arduino, of the Nextion Tutorial
If you prefer to see more colors, you can go to my site, but I will also upload it here. If you want, you can see a short video on youtube on how the project works. Nextion Tutorial: How to write code with Nextion and Arduino - YouTube
There are many comments on both the .HMI file and .ino file, to explain better what we are doing.
Now, we are beginning to write the tutorial:
Tutorial on how to write code with Nextion and Arduino
In this tutorial, one can find simple tips and hints on how to control a Nextion display with an Arduino. There are examples on:
• How to initialize Nextion’s Serial baud rate
• How to send commands from Nextion and assign them to functions on Arduino
• How to organize the commands from Nextion and read them through the Serial
• How to send commands from Arduino to Nextion
• How to change components’ attributes at Nextion from Arduino
• How to use < Serial.print > commands
• How to use < sprintf > command
• How to make a useful pop-up message on Nextion
How to use the following on Nextion with simple examples on where and how they can be applied:
• Read the current page id using Nextion < dp > command and a useful example on where it can be applied
• How to make a variable, change it to global scope and use it across different pages
• How to use Array[index] for p and b
• How to use < vis > command to make components visible or invisible
• How to use the timer variable, a non-visible component
• How to use the < xstr > command for printing text using defined area for text rendering (see t1 textbox Press Event on page with ID=1)
• How to print a moving text on Nextion with < xstr >
• How to use the < covx > command to convert numeric to text or the opposite (see examples at the b0 and b1 Events on page with ID=2)
• How to use the return character < “\r” > on text boxes for new line
• How to add a numeric variable inside a text (same with arduino)
• How to revert the minval and maxval of a slider
• How to make a slider horizontal or vertical
To present all the above in action, we made a single project in which we included all of them.
As a scenario for the project, we choose to read from a text array, stored in the Arduino.
We chose that scenario, because we can have many alternative choices and needs, in the code of both Nextion and Arduino without the need of any external hardware, e.g. sensors.
In this way, we can run the project using only an Arduino and the Nextion Editor, from the debug mode.
In the project, the pages might look the same, but the methods are different. We also use pop-up messages to show the code or comments.
Communication protocol between Arduino and Nextion Display:
The communication between Nextion Display and Arduino is quite simple. There is no need for libraries and complicate commands as Nextion uses a simple and complete instruction set. A library cannot cover and guess all the needs of the projects wide field.
A custom communication protocol is used because of the following advantages:
- We don’t use any Nextion LCD library
- Nextion has a protocol to send the ID of different components via Serial, but it is very difficult to use it.
- The produced code size is small.
- The communication data length is sort. This causes faster communication between Arduino and Nextion LCD.
- The protocol can be extended from user by adding his own commands.
- It is more practical to send a specific command and then from Arduino’s Serial identify the command that is sent and assign it to the function we want,
than trying to identify and assign the IDs from a big number of objects to a function, as Nextion and the most libraries do this with the event’s ID protocol.
The only thing that we must do, is to organize the commands that we are going to use with the simplest possible method.
Identifying them with the Arduino code and assigning them to the functions we want.
For this protocol, we want to thank again Vassilis Serasidis for his advises on this.
We use the following Data Format: <#>
An example with the group command “Page” is:
- From Nextion we send in HEX, by writing: < printh 23 02 50 00 >
- Means: < # 2 P 0 >
- <#> declares that a command is following
- declares the number of bytes that follows (len = 2,
is the one Byte and <0> is the other)
- declares the group of commands ( P = page L=Line)
- declares the ID, to which the command is referring to. (id = 0)
This means that we are in page 0 (we have written in every page’s “preinitialize event” the command printh 23 02 50 xx (where xx is the page id in HEX, 00 for 0, 01 for 1, etc.).
We have assigned a function named < first_refresh_page (uint8_t page) > and with a switch case command, we send to each page the data that must updated in first load.
A second example is this with the “Line” command group (<#> <nextion_var> )
- From Nextion we send in HEX the following:< printh 23 03 4C 04 01 >
- Means < # 3 L 4 1 >
- <#> declares that a command is followed
- declares the number of bytes that will follow (len = 3, is the one Byte <4> and <1>is the other)
- declares the group of commands (P = page L=Line)
- <nextion_var> is the Number of the variable on Nextion that we want to write
- is the number of the text array Line that we want to store into Nextion’s variable
We have assigned the function < sending_text (byte nextion_var, byte cnt) > where we send the last 2 bytes to local variables of the function