Start a new topic

Nextion and the Arduino

The Nextion display is a great unit but with all the various examples around it can be a bit difficult to get a clear sense of how to use it.

I put together a simple example of what worked for me. I hope this helps others.


 

[code]
/*  This body of work was done to help familiarize others with the Nextion series of displays for use with Arduino micros,
 *  in hopes of demystifying the procedures required to use the displays.
 *  Please note this is the authors own interpretation and this is not the only way to accomplish the same tasks.
 *  It is intended to allow use of the display as a means of console operations. Therefore displaying its own return responses.
 *  This will allow the user to become familar with the input and output via the display to Arduino connection.
 *  As configured it will allow use of the Nextion Editor in debug mode. Also note the Nextion.h is NOT required for this example.
 *  Change Serial.begin(9600) as needed for correct ports. (ie. Serial.begin(9600), Serial1.begin(9600), Serial2.begin(9600), etc.).
 *  Incoming data is stored and compared as decimal strings.
 *  All variable names may be changed as needed to allow simple recognition or inclusion to your code.
 *  This example does include all error codes but does NOT include all data return codes or screen codes.
 *  Please see the Nextion instruction set page for futher codes and details. https://www.itead.cc/wiki/Nextion_Instruction_Set#Format_of_Device_Return_Data
 *  You are free to use this code in anyway that you like. However when redistributing it please do so in its entirety.
 *  Thanks. mikie@warsawwireless.net
 */
 
String inputString = ""; // Variable for incoming data
boolean dataAvail = false; // Variable to indicate valid data

/*  Define Operation Error Codes
 *  All error codes are hex byte+end
 *  end = 255 255 255 (0xFF 0xFF 0xFF)
 */
String II = "0255255255"; // Invalid instruction (0x00)
String SEOI = "1255255255"; // Successful execution of instruction (0x01)
String CII = "2255255255"; // Component ID invalid (0x02) 
String PII = "3255255255"; // Page ID invalid (0X03)
String PicII = "4255255255"; // Picture ID invalid (0X04)
String FII = "5255255255"; // Font ID invalid (0x05)
String BRSI = "17255255255"; // Baud rate setting invalid (0x11)
String CCINI = "18255255255"; // Curve control ID number or channel number is invalid (0x12)
String VNI = "26255255255"; // Variable name invalid (0x1A)
String VOI = "27255255255"; // Variable operation invalid (0x1B)
String FTA = "28255255255"; // Failed to assign (0x1C)
String OEEPROMF = "29255255255"; // Operate EEPROM failed (0x1D)
String PQI = "30255255255"; // Parameter quantity invalid (0x1E)
String IOF = "31255255255"; // IO operation failed (0x1F)
String UEC = "32255255255"; // Undefined escape characters (0x20)
String TLVN = "35255255255"; // Too long variable name (0x23) 
 

// Define Return Codes

/*  Touch event return data:
 *  Format 0X65+Page ID+Component ID+TouchEvent+End.
 *  Definition of TouchEvent: Press Event 0x01, Release Event 0X00)
 *  Add additional variables as needed. (ie. TERD1, TERD2, etc.)Change ID and TouchEvent as appropriate.
 */
// String TouchEventReturnData = "101020255255255";
String TERD0 = "101020255255255";
/*  Current Page ID Number Returns:
 *  Format 0X66+Page ID+End
 *  The device returns this data after receiving “sendme” instruction
 *  Add additional variables as needed. (ie. page0, page1, etc.)
 *  Change Page ID as appropriate.
 */
 // String CurrentPageIdNumberReturn = "1020255255255";
String Page0 = "1020255255255"; //Sample page 0 return. use showme command embedded in page to return this code.
String Page1 = "1021255255255"; // Sample page 1 return.

void setup(void) 
{ 
 Serial.begin(9600); // Initialize serial port and set baud rate.
 inputString.reserve(500); // Reserve buffer space for input data.
}
 
void loop(void) 
{
  if (dataAvail == true) { // Check for valid data.
  
    errorCheck(); // Check for instruction success or failure messages.
    delay(10); // short delay for incoming data stabilization. 
    dataCheck(); // Check for usable return data.
    delay(10);
  
  }

}

/*  This event runs each time main is looped. ensure the header and all tags inside the function match the serial port used.
 *  (ie. serialEvent2(), Serial2.available(), Serial2.read(), etc.)
 */
 
void serialEvent()
{
  while (Serial.available()){ // while data comes in serial port construct the message. 
    inputString.concat(Serial.read()); // concate each byte into a single string.
  }
  if (inputString.length() >1){ // If there is valid data set a flag so main loop can process it.
    dataAvail = true;
  }
}

void dataCheck()
{
  if(inputString == Page0){
/*
 * Print a String to the display.
 * xstr x, y, w, h, fontid, fontcolor, backcolor, xcenter, ycenter, sta, string 
 * x: x coordinate starting point;
 * y: y coordinate starting point;
 * w: area width;
 * h: area height;
 * fontid: Font ID;
 * fontcolor: Font color;
 * backcolor: Background color (when set sta as Crop Image or Image, backcolor means image ID );
 * xcenter: Horizontal alignment (0 is left-aligned, 1 is centered, 2 is right-aligned);
 * ycenter: Vertical alignment (0 is upper-aligned, 1 is centered, 2 is lower-aligned);
 * sta: Background fill(0-crop image;1-solid color;2-Image; 3-No backcolor, when set sta as Crop Image or Image, backcolor means image ID);
 * Example: xstr 0, 0, 100, 30, 1, RED, BLACK, 1, 1, 1, "Nextion"
 * When sending a text command via Serial.print(), send a literal string. you must place it inside quotes. Example: Serial.print("command") . 
 * To send text variables inside the literal command string use \" around it. Example: Serial.print("command,\"variable\"") .
 */
      Serial.print("xstr 0,0,500,30,0,BLACK,WHITE,0,0,3,\"Page 0\""); // Send xstr command to display with data. 
      Serial.write(0xff); // send end command. 0x255 0x255 0x255
      Serial.write(0xff);
      Serial.write(0xff);
      inputString=""; // clear input string for next read.
      dataAvail = false; // reset valid data flag.
    }

    if(inputString == Page1){
      Serial.print("xstr 0,0,500,30,0,BLACK,WHITE,0,0,3,\"Page 1\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }
}

void errorCheck()
{
       
   if(inputString == II){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Invalid Instruction\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == SEOI){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Succesful Execution of Instruction\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == CII){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Component ID Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == PII){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Page ID Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == PicII){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Picture ID Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == FII){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Font ID Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == BRSI){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Baud Rate Setting Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == CCINI){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Curve Control ID Number Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == VNI){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Variable Name Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == VOI){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Variable Operation Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == FTA){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Failed to Assign\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == OEEPROMF){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Operate EEPROM Failed\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == PQI){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Paramter Quantity Invalid\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == IOF){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"IO Operation Failed\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == UEC){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Undefined Escape Characters\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

    if(inputString == TLVN){
      Serial.print("xstr 0,40,500,30,0,BLACK,WHITE,0,0,3,\"Too Long Variable Names\"");
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      inputString="";
      dataAvail = false;
    }

}
[/code]

 


Login or Signup to post a comment