Your browser version is outdated. We recommend that you update your browser to the latest version.

How to send values from Nextion and store them on Arduino

 No need for Nextion Library

As you have already understood it very difficult to assign the commands that you want in every Touch Event and much more difficult to read them separately for every event from the Serial and attach the function that you want on Arduino.
In this example - project we need to update 2 variables x and y or Step and Direction
as is useful for sending values to step motors.

By pressing on Nextion button, 2 numeric values goes to Arduino, they stored on variables
and Arduino returns the values to n0 and n1 numeric components on Nextion.

Case

 

1

2

3

4

5

6

7

8

9

10

Step

X

790

540

1040

1290

790

250

250

500

540

250

Direction

Y

0

0

0

0

1

1

0

0

1

0

We must simplify this a lot, this example is going to help on this.


First we must develop our own custom protocol so we can handle the commands that we need.

Second, sent to Arduino the command that you want through the Touch Event


Last, just read them.


Let's make a group command for this and named < Sent Variables >  

We may assign the capital letter 'V' in this command group to identify it later from Arduino serial port.
The character 'V' is the hex 0x56 (google for Ascii character table).

We are going to use the following Data Format:
<#> <len> <cmd> <Step_var> < Direction_var >
*                    <#> declares that a command is followed
*                    <len> declares the number of bytes that will follow (len = 3, <cmd> is the one Byte < Step_var > and < Direction_var >is the other)
*                    <cmd> declares the group of commands (V = Sent Variables),you can add a different command group  later.
*                    < Step_var > is the value for the Step variable
*                    < Direction_var > is the value for Direction variable



So as example for the case 1 we must sent the number < 790 > as Step and the < 0 > as the Direction
Sending the following from Nextion Touch Event in hex :
< # 3 V 790 0 >

To send this trough Serial, we must write on < Touch Press Event > of the button component on Nextion the following:
< printh 23 03 56 316 00 >
Where the HEX is: <23 = #> <03=3> <56=V> <316=790> <00=0>
And that's it.

So far, the problem is that we can't send a number bigger than 255 in a single byte, that means that you can't sent the <316> on hex = <790> in decimal.

To do this we need 2 bytes, a high byte and a low byte and the things start to complicate. We are going to see this option through an other future example.

In this project this is not a problem because as you can see the values can be divided with 10 and the result is going to be a number lower than 255,
that we can multiply it with 10 on Arduino code after the Serial reading.

At the end, we must sent: < # 3 V 79 0 >
With writing: < printh 23 03 56 4F 00 > Where is: <23 = #> <03=3> <56=V> <4F=79> <00=0>
You can find the HEX of a DEC number from on line tables or calculators or from windows calculator in programmer mode.
We must do the same with all the cases BE AWARE: uncheck the < Send Component ID >

 

X value to Hex

790÷10 = 4F

540÷10 = 36

1040÷10 = 68

1290÷10 = 81

790÷10 = 4F

 

250÷10 = 19

250÷10 = 19

500÷10 = 32

540÷10 = 36

250÷10 = 19

 

 

 

 

 

 

 

 

 

 

 

 

 

Much more, at the comments inside the code.

For more on how to learn to code with Nextion and Arduino, visit our Nextion Tutorial.

To get the .HMI and .ino files, for Nextion and Arduino, go to our Download Section,