Start a new topic

Arduino Sketch code to receive Data from Nextion

 Here is a bit of code that should read all 7 numbers from the Nextion:

if (Serial3.available() > 0) {
    char1 = Serial3.read(); 
    delay(20);
        static char input[8];
        static uint8_t i; // to hold the count of the char
        char c = Serial3.read ();
        if (c != '0Xff' && c >= 0 && c<100 && i < 7 ){ // assuming "Carriage Return '\r' " is chosen in the Serial monitor as the line ending character Or in my case it will be 255 "0Xff"
          input[i++] = c;
          Serial.println( c );
        }
        else
        {
          input[i] = '\0'; 
          i = 0;
          int Number = atoi( input );
          Serial.print("Number: ");
          Serial.println( Number );
        }
  }
}

 The issue is that I only get every other number.

If I send 123456 from the Nextion

I get 135 on the serial Monitor on the Arduino 1.6.4 using Mega

What is missing?


I have not read your code in detail, but on line 2 you have 

"char1 = Serial3.read();", 

and on line 6 you have 

"char c = Serial3.read ();".

What happens with char1 ? It seems like it gets lost :-)

Once I read the first char1 which is 112 "0X70 then I use this code to read the rest  

else if (char1 == 112) {
      Serial.println("String variable data returns ");               // 0x70    0X70+Variable Content in ASCII code+End
      //When the variable obtained through get command is string type, return this data
      //Instance: 0X70 0X31 0X32 0X35 0XFF 0XFF 0XFF == 112 1, 2, 5, 255, 255, 255
      
      //Now we read the last six chars returned
      delay(20);
      char input[6];   // An char array "String" to hold the last five chars 0-4 plus a closing '\0'
      for (int i = 0; i < 6; i++) {
        input[i] = Serial3.read ();
        if (input[i] == '255' || input[i] < 0) i = i--;
        else Serial.println( input[i] );
        if (i == 5) {
          input[6] = '\0'; // adding a closing
          long number = atol( input ); //notice the function change to atoL
          Serial.print("Number: ");
          Serial.println(number); // Should and does print "125"
        }
      }
    }

  This does work if any one is interested I have most of the rest of the Nextion return commands done and arduino sketch that reads each and prints them to the serial monitor


1 person likes this

Chris, I would like to get a copy of your code.  What ITEAD is not doing is create an asychronous library for the display.  It is possible to send a command out to the display and before you get the ack/nack data, you may get data after a touch event.  If ITEAD can guarantee this will NEVER happen then it makes the code simpler but I doubt this is the case.  If ITEAD won't do it, I can start building something based on your work :)


3 people like this

The new Arduino Nextion Library (https://github.com/itead/ITEADLIB_Arduino_Nextion) will release next weekend ~  Let's see if the new version can meet your requirements




3 people like this

Paul lets wait on the next release before we put to much time in this if it still goes not work then I will finish what I started and post it. Thanks


while we wait here is how I send data to the Nextion screen to set the progress bar "j0.var" and it text "t2.txt" from my mega

 

void printNextion() {
  if (NewValue == OldValue) {
    delay(20);
    Serial3.print("j0.val=");
    Serial3.print(numericalData);
    Serial3.write(0xff);
    Serial3.write(0xff);
    Serial3.write(0xff);
    Serial3.print("t2.txt=");
    Serial3.write(0x22);
    Serial3.print(Text);
    Serial3.write(0x22);
    Serial3.write(0xff);
    Serial3.write(0xff);
    Serial3.write(0xff);
    delay(20);
    int array[4];
    while (Serial3.available() > 0) {
      for (int i = 0 ; i < 4; i++) {
        array[i] =  Serial3.read();
       // Serial.println(array[i]);
        delay(20);
      }
    }
    array[4] = '\0';
    NewValue = array[0];
    Serial.println(" ");
    if (array[0] == 0) Serial.println("Retransmitting");
    else Serial.println("Transmission Success");
  }
}

 



2 people like this

And this is how I receive data from the Nextion. Well one way!!


 

void readDataSimple2 () {
 int array[16];
  while (Serial3.available() > 0) {
    for (int i = 1 ; i < 16; i++) {
      array[i] =  Serial3.read();
      Serial.println(array[i]);
      delay(20);
    }
    array[16] = '\0';
    int pageNumber = array[2];
    int componentNumber = array[3];
    int pressEvent = array[4];
    int numericalData = array[9];
    Serial.print("pageNumber: ");
    Serial.println(pageNumber);
    Serial.print("componentNumber: ");
    Serial.println(componentNumber);
    Serial.print("pressEvent: ");
    Serial.println(pressEvent);
    Serial.print("numericalData: ");
    Serial.println(numericalData);
  }
}

 


2 people like this

Here  is the almost complete Arduino code


  

#include <hmi.h>

HMI myhmi(Serial3);  //Arduino Mega TX3/RX3 14/15 to Nextion RX/TX

int array[16];
int pageNumber, componentNumber, pressEvent, inputNumber, numericalData;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  getData();
}



void getData() {
  if (Serial3.available() > 0) {
    array[16];
    while (Serial3.available() > 0) {
      for (int i = 0 ; i < 16; i++) {
        array[i] =  Serial3.read();
        Serial.println(array[i]);
        delay(20);
      }
      array[16] = '\0';
    }
  }

  if (array[0] < 28) {
    if (array[0] == 1) Serial.println("Successful execution of instruction");             // 0x01
    else if (array[0] == 3) Serial.println("Page ID invalid");                                    // 0x03
    else if (array[0] == 4) Serial.println("Picture ID invalid");                                 // 0x04
    else if (array[0] == 5) Serial.println("Font ID invalid");                                     // 0x05
    else if (array[0] == 26) Serial.println("Variable name invalid");                         // 0x1A
    else if (array[0] == 27) Serial.println("Variable operation invalid ");                  // 0x1B
  }

  ///////////////////////////////////if array[0]  == 101////////////////////////////////////////////
  if (array[0] == 101) {
    //Definition of TouchEvent: Press Event 0x01, Release Event 0X00)
    //Instance: 0X65 0X00 0X02 "0X01 or 0X00" 0XFF 0XFF 0XFF'    We have already read the first "0X65"
    //Meaning: Page 0, Button 2, Press
    Serial.println("Touch event return data ");                     // 0x65    0X65+Page ID+Component ID+TouchEvent+End

    // Now we assign them to a Variable
    //array[1] is the pageNumber
    pageNumber = array[1];
    //array[2] is the component number
    componentNumber = array[2];
    // array[3] is the button press
    pressEvent = array[3];  // a 1 or a 0 "0X01 or 0X00"
    // The last three are the end statements from the Nextion "255" we will not assign them

    // Now we print these Variable's to the serial monitor
    Serial.print("pageNumber: ");
    Serial.println(array[1]);
    Serial.print("componentNumber: ");
    Serial.println(array[2]);
    Serial.print("pressEvent: ");
    Serial.println(array[3]);
    
    if (array[7] == 113) {
    Serial.print("Numerical variable data returns: ");          // 0x71  0X71+Variable Binary Data (4-byte is Little-endian Mode, low-order front)+End
    //When the variable obtained by get instruction is numerical, return to the data
    //Instance: 0X71 0X66 0X00 0X00 0X00 0XFF 0XFF 0XFF
    //Meaning: Return to numerical data: 102
    //If your data comes in from a touch event then you will have
    //First: Instance: 0X65 0X00 0X02 "0X01 or 0X00" 0XFF 0XFF 0XFF'
    //Then: Instance: 0X71 0X66 0X00 0X00 0X00 0XFF 0XFF 0XFF
    Serial.print("Numerical data: ");//From a progress bar
    Serial.println(array[8]);
    numericalData = array[8]; //From a progress bar
    //else if (      If you are expecting any other commands put them here 
  }
    
  }

  ///////////////////////////////////if array[0]  == 102////////////////////////////////////////////
  else if (array[0] == 102) {
    Serial.println("Current page ID number returns ");        // 0x66   0X66+Page ID+End
    //The device returns this data after receiving “sendme” instruction)
    //Instance: 0X66 0X02 0XFF 0XFF 0XFF
    //Meaning: Current page ID is 2

    //array[1]  is the pageNumber
    pageNumber = array[1];

    // Now we print these Variable's to the serial monitor
    Serial.print("Current page number: ");
    Serial.println(pageNumber);
    // Now we can use these Variable's in some code if we wish

  }
  /*
      ///////////////////////////////////if array[0]  == 103////////////////////////////////////////////
      else if (array[0] == 103) {
        Serial.println("Touch coordinate data returns ");          // 0x67   0X67++ Coordinate X High-order+Coordinate X Low-order+Coordinate Y High-order+Coordinate Y Low-order+TouchEvent State+End
        //When the system variable “sendxy” is 1, return this data at TouchEvent occurring
        //Definition of TouchEvent: Press Event 0x01, Release Event 0X00
        //Instance: 0X67 0X00 0X7A 0X00 0X1E 0X01 0XFF 0XFF 0XFF
        //Meaning: Coordinate (122,30), Touch Event: Press
      }

      ///////////////////////////////////if array[0]  == 104////////////////////////////////////////////
      else if (array[0] == 104) {
        Serial.println("Touch Event in sleep mode ");              // 0x68    0X68++Coordinate X High-order+Coordinate X Low-order+Coordinate Y High-order+Coordinate Y Low-order+TouchEvent State+End
        //When the device enters sleep mode, return this data at TouchEvent occurring
        //Definition of TouchEvent: Press Event 0x01, Release Event 0X00
        //Instance: 0X68 0X00 0X7A 0X00 0X1E 0X01 0XFF 0XFF 0XFF
        //Meaning: Coordinate (122,30), Touch Event: Press
      }
  */
  ///////////////////////////////////if array[0]  == 112////////////////////////////////////////////
  else if (array[0] == 112) {
    Serial.println("String variable data returns ");               // 0x70    0X70+Variable Content in ASCII code+End
    //When the variable obtained through get command is string type, return this data
    //Instance: 0X70 0X31 0X32 0X35 0XFF 0XFF 0XFF == 112 1, 2, 5, 255, 255, 255

    char input[3];   // An char array "String" to hold the chars 2, 3, 4 plus a closing '\0'
    input[0] = array[1];
    input[1] = array[2];
    input[2] = array[3];
    input[3] = '\0'; // adding a closing
    long number = atol( input ); //notice the function "atoL"
    inputNumber = number;
    Serial.print("number: ");
    Serial.println(inputNumber);
  }

  ///////////////////////////////////if array[0]  == 113////////////////////////////////////////////
  else if (array[0] == 113) {
    Serial.print("Numerical variable data returns: ");          // 0x71  0X71+Variable Binary Data (4-byte is Little-endian Mode, low-order front)+End
    //When the variable obtained by get instruction is numerical, return to the data
    //Instance: 0X71 0X66 0X00 0X00 0X00 0XFF 0XFF 0XFF
    //Meaning: Return to numerical data: 102
    //If your data comes in from a touch event then you will have
    Serial.print("Numerical data: ");//From a progress bar
    Serial.println(array[1]);
    numericalData = array[1]; //From a progress bar
  }
  ///////////////////////////////////////////////////////////////////////////////
  // http://wiki.iteadstudio.com/Nextion_Instruction_Set#Format_of_Device_Return_Data
}

  I only have Nextion commands 103 and 104 which is 0X67 and 0X68 left to do.


3 people like this

Chris, good work!  I look forward to seeing what ITEAD releases next weekend.  Ideally, the entire send/receive code is written as an FSM (finite state machine) to handle all command/receive sequences.  What is not clear is whether touch events are asynchronous (arrive any time) even during a command-ack/nack sequence or if the Nextion synchronizes the data stream.  So when you send a command and expect a response (ack/nack/data), you won't get touch event instead.  The touch event data would be slotted AFTER the ack/nack/data response.  This will make the FSM much simpler. :)

Hi,
what is that <hmi.h>
and where you can download / see ?!

 


1 person likes this
I haven't used the nextion for a while now but it seembed I was having communication issues if there was a lot polling done by the arduino. So I started to implement more events from the display so the arduino would receive more function call backs. It seemed to help my problems a lot.
Hello Chris sipes I am using arduino uno and to my it give a lot errors can you help to me please?

Francesco, you will have to give us more to go on!

What errors are you getting?

What are you trying to do?

Show us your code.

I am really busy right now but I will try to help. 


1 person likes this


Hi chris sipesI would like to help me with my project since I am new to this and I would like to learn how to create multiple sliders to control the intensity of the led with this code since I do not understand it very much or if it has some code that helps me to achieve the goal.

ino

chris sipes

What library you use? Sketch won't compile please need help!

Login or Signup to post a comment