Start a new topic

Arduino Mega and Nextion: button data

Hi guys, this is a really short and easy way to retreive the button data, absolutely interesting if you have many buttons.


/*nextion brute shortcut to data that matters!

 * I slimmed and optimized this code for 2560 Mega board, with thanks to Gideon Rossouwv,


 * (for arduino Uno)

 * Just tested for buttons, but much shorter than the Itead Library (fe. my 21 key keypad)

 * Works with nearly every HMI file, if it has buttons



#include "Nextion.h"// needed for sendCommand


int numBytes, endBytes;

byte inByte, pageNum, buttonNum;


void setup() {

  endBytes = 0; //initialise counter variables

  pageNum, buttonNum = 255;//initialise page number. It is unlikely that you'll have 255 pages.


  Serial.begin(9600); //open the serial port

  while (!Serial) { // wait for serial port to connect. Needed for native USB port only;


  Serial.println("Serial On");

  Serial2.begin(9600); // set the data rate for the SoftwareSerial port

  sendCommand("page 0");// to page 0 of nextion LCD

  sendCommand("dim=100");// intensity LCD=100%



void loop() {

  // The loop will determine which page and which button was tapped.


   if (Serial2.available()) {//Is data coming from the Nextion?

    inByte =; //Serial.print(" inByte=");Serial.print(inByte);


    if (inByte > -1 && inByte < 255) { //The component ID message mostly consists of between 0 and 255

      numBytes = numBytes + 1; //Keep track of the number of data bits received.


    if (inByte == 255) {//Is it an end byte?

    endBytes = endBytes + 1;//Increment the number of end bytes


    if (numBytes == 2) {//The second data bit is always the pageID in "Send component ID"

      pageNum = inByte; Serial.print(" pageID=");Serial.print(pageNum);


    if (numBytes == 3) {//The 3rd data bit is always the buttonID in "Send component ID"

      buttonNum = inByte; Serial.print(" buttonID=");Serial.print(buttonNum);


    if (inByte == 255 && endBytes == 3) { //Has the entire message been received.

      endBytes = 0;//Re-initialise counter variables

      numBytes = 0;





Sorry, but there are conditions which sending data from the Nextion where this will fail.

Not every 0xFF is a byte belonging to a data terminator sequence.

I'll try and expand on this rather than a simple sorry.

At some point your program will need to expand from your base above.

The moment you use sendCommand to send back to the Nextion,

incoming bytes are moved to the buffer for function NexLoop.

These bytes of course now read from serial are not present in loop

- so that is possibly a missed command or data.

Your 0xFF counter is based on 3rd 0xFF encountered

- but Nextion isn't based on every 3rd occurrence of 0xFF

- Nextion termination is based on three 0xFFs running consecutively.

  - getting a number such as white in a 0x71 return will contain 5 0xFFs

Hi Patrick,

You are right, I do get sometimes errors. And indeed, checking for the 3rd 0xFF is a risk... It is a brute way and some workarrounds/limitations will be needed (thanks for noticing the problem 'white') .

It is a base, which in my case works ok, but I hope somebody will further optimize ( the compactness is a pro)

Agreed, indeed! compactness is desired.

My point is you are on the right path, attempting to better existing

- many such considerations are required

- now with such in mind, you get further than you originally thought.

@Karl Lambrechts

It may also be a good idea to use an input buffer, because you don't know if your microcontroller receives all bytes inside the same loop.

You will find some explanations on these two websites:

Login or Signup to post a comment