How to send values from Nextion and store them on Arduino

*This is an example from the previous site. It can be used as an example for a custom protocol.


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

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.

/*  
     /\\\\\\\\\\\    /\\\\\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\\\\\\\  /\\\        /\\\     /\\\\\\\\\     /\\\\\     /\\\         
    /\\\/////////\\\ \/\\\///////////  \/////\\\///  \///////\\\/////  \/\\\       \/\\\   /\\\\\\\\\\\\\  \/\\\\\\   \/\\\        
    \//\\\      \///  \/\\\                 \/\\\           \/\\\       \/\\\       \/\\\  /\\\/////////\\\ \/\\\/\\\  \/\\\       
      \////\\\         \/\\\\\\\\\\\         \/\\\           \/\\\       \/\\\\\\\\\\\\\\\ \/\\\       \/\\\ \/\\\//\\\ \/\\\      
          \////\\\      \/\\\///////          \/\\\           \/\\\       \/\\\/////////\\\ \/\\\\\\\\\\\\\\\ \/\\\\//\\\\/\\\     
              \////\\\   \/\\\                 \/\\\           \/\\\       \/\\\       \/\\\ \/\\\/////////\\\ \/\\\ \//\\\/\\\    
        /\\\      \//\\\  \/\\\                 \/\\\           \/\\\       \/\\\       \/\\\ \/\\\       \/\\\ \/\\\  \//\\\\\\   
        \///\\\\\\\\\\\/   \/\\\\\\\\\\\\\\\  /\\\\\\\\\\\       \/\\\       \/\\\       \/\\\ \/\\\       \/\\\ \/\\\   \//\\\\\  
           \///////////     \///////////////  \///////////        \///        \///        \///  \///        \///  \///     \/////  
*/  
//           ______________________________________________
            //--------CREATED BY SEITANIS THANASIS--------\\
           //---------------VERSION 13/9/2019--------------\\
          //----------------- www.seithan.com --------------\\
         //----------------seithagta@gmail.com---------------\\
        //------ code for Sent values from Nextion and -------\\
       //----------- store them on Arduino variables ----------\\
                               
/*  Example Code on How to send values from Nextion and store them on Arduino for any use.
 *  No need for Nextion Library
    
 *  Copyright (C) 2019 author: THANASIS SEITANIS contact: seithagta@gmail.com
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.

 *  Not legally state for the distribution terms:
 *  It is permitted to copy and distribute native copies of the Program source code
 *  exactly as you have obtained it, on any storage medium, provided that: 
 *  you will prominently and appropriately publish, in each copy, a copyright notice 
 *  and a disclaimer of guarantee - that you will integrally include all notes mentioned
 *  in this License and in the absence of any warranty - and, finally, that you will provide
 *  each other recipient of the Program with a copy of this License together with the Program.
 *  You should make sure that the modified files provide prominent notes
 *  indicating the modification of the files and the date of modification.
 */
//------END OF COPYRIGHT NOTICE AND DISCLAIMER OF GUARANTEE------//

#define Nextion Serial
/* define that Serial will de writed as Nextion (when we write Nextion.print the  compiler read Serial.print)    
 *By define the Nextion as Serial you can change at ones the serial port at the hole code,
 * with changing the Serial to Serial1 for example
 */

 int Step_var = 0 ;       // Variable to store the Step value that we sent from Nextion
 int Direction_var = 0 ; //  Variable to store the direction value that we sent from Nextion

void setup(){

 
  
  Nextion.begin(9600); // starting the serial port at 9600. NEXTION MUST HAVE THE SAME RATE. For this we write at
                         //  first page to the preinitialize event the command @  baud=9600  @ 
                         // NOTE "bauds" will change the default baud rate off 9600 until it changed again
  delay(500);
}


void loop(){

  Nextion_serial_listen();
  
  
 
}


void Nextion_serial_listen() 

{
    if(Nextion.available() > 2){                // Read if more then 2 bytes come (we always send more than 2 <#> <len> <cmd> <id>
        char start_char = Nextion.read();      // Create a local variable (start_char) read and store the first byte on it  
        if(start_char == '#'){                // And when we find the character #
          uint8_t len = Nextion.read();      // Create local variable (len) / read and store the value of the second byte
                                            // <len> is the lenght (number of bytes following) 
          unsigned long tmr_1 = millis();
          boolean cmd_found = true;
            
          while(Nextion.available() < len){ // Waiting for all the bytes that we declare with <len> to arrive              
            if((millis() - tmr_1) > 100){    // Waiting... But not forever...... 
              cmd_found = false;              // tmr_1 a timer to avoid the stack in the while loop if there is not any bytes on Serial
              break;                            
            }                                     
            delay(1);                            // Delay for nothing delete it if you want
          }                                   
                                               
            if(cmd_found == true){            // So..., A command is found (bytes in Serial buffer egual more than len)
              uint8_t cmd = Nextion.read();  // Create local variable (cmd). Read and store the next byte. This is the command group
                                             
              switch (cmd){
                                    
                case 'V': /*or <case 0x56:>  IF 'V' matches, we have the command group "Sent Variables". 
                           *The next byte  according to our protocol is the value for the <Step_var>
                           */
                     Step_var = Nextion.read() * 10 ;  // we read and store the byte at the variable
                     
                     // The next byte  according to our protocol is the value for the <Direction_var>
                     
                     Direction_var = Nextion.read();   // we store and store the byte at the variable
                     



                      Nextion.print("n0.val="); // Those lines is for the debug on Nextion
                      Nextion.print(Step_var);  // they sent the variables at the         
                      NextionEndCommand();      // numeric Component  n0 
                      
                      Nextion.print("n1.val=");    // Those lines is for the debug on Nextion
                      Nextion.print(Direction_var);     // they sent the variables at the     
                      NextionEndCommand();            // numeric Component  n1
                     
                  break;
                
                                
              }
            }
        }  
    }    
}

void NextionEndCommand()
{
    // with this two ways we can sent the 3 bytes so Nextion can understand the end of a command. Choose freerly.
    
  /* Nextion.write(0xff);
     Nextion.write(0xff);
     Nextion.write(0xff); */
  
    Nextion.print("\xFF\xFF\xFF");
  
 
}

The .zip file contains a .HMI file for Nextion and a .ino file for Arduino



File downloaded 2086 times.