Start a new topic

Beginners Options for a Nextion/Arduino Interface

Readers will note that I have been very dismissive of the Itead nextion.h file, especially for beginners.  I have also noted that there are issues with other nextion.h offerings.

So I present here a set of simple procedure/functions to read and write to the Nextion. There is no .h file, very little to learn . just put them directly into your sketch.

These are not sohisticated in any way, nor are they 'best practice' but they will assist beginners to quickly get communication going..

They do not include multiple commands to items. You can move on to more sophisticated .h programs as your experience grows, or develope your own

Just add these to an Arduino sketch

// Initiates a start to the page selected
void NextionInit(int pageId)
  String page = "page " + String(pageId);  //Page
  }//end Init

Example:  NextionInit(0);  // send the initialization commands to select Page 0

// -------------------------------------------
// Sets an integer value to Number or Variable
void NextionSetValue(String Component,int value)
  String compValue=Component+".val="+value;
  } // end SetValue

// Sends a command
void NextionSendCommand(const char* cmd)
  }//end sendCommand

Example: NextionSendCommand("vis h1,1");

// Sends text
void NextionSetText(String component, String txt)
  String componentText = component + ".txt=\"" + txt + "\"";//Set Component text
  }//end SetText

Example: NextionSetText("t9","Show this");

// Returns theint value from the val returned by nextion. Assumes this is collected as as String by the arduino
int NextionGetIntValue(String temp)
  unsigned int value;
  value = (temp[4] << 24) | (temp[3] << 16) | (temp[2] << 8) | (temp[1]);//Little-endian convertion
  }//end if
  return value;

Example:     int j=NextionGetIntValue(message)

// message is the String  that is recieved from Nextion on return of an integer

(See below as well)

// Gets a value
void NextionGetComponentValue(String component)
  String getValue = "get "+ component +".val";//Get componetn value

Example: NextionGetRealValue("no2")  ( See below)

Serial Connection

You will see in the SendCommand and SetText procedures I have used  Serial3.print(cmd); This is just to show how you would enter this for a Nextion connection to Serial3 on a Mega2560, on a Uno it would be just Serial.print(cmd);  or if using Software Serial mynextion.print(cmd); or similar

This is not the preferred way to do this!  Its' quick and dirty' but until you have other skills it will get you going guickly

Nextion Print

An easy way to just send a 'number' value to the Arduino without the beginnings and ends usually appended by Nextion is

- Set up a text variable va0.txt

- Add the following to the 'touch event' of n1

   cov n1.val,va0.txt,0      //coverts an integer value to text
   print va0.txt                // send the text

- Use the command "NextionSendCommand("click n1,1"); to get the value sent

So 1234 in n1 will be sent as the hex of 1234.... instead of

0x71 0xd2 0x04 0x00 0x00 0xff 0xff  which GetComponentValue will return

Use print t1.txt in exactly the same way

cov n1.val,va0.txt,0
print va0.txt


1 person likes this idea
1 Comment

Another option for SetValue

  void NextionSetValue(String Component,int value)


   String compValue=Component+".val="+value;



If you have problems with 

NextionSendCommand("vis h1,1");

then use the more correct form

String  command = "vis h1,1"



My acknowledgements to

Login or Signup to post a comment