Arduino Forum

Using Arduino => Displays => Topic started by: PerryBebbington on Mar 17, 2019, 05:58 pm

Title: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 17, 2019, 05:58 pm
Introduction

Nextion displays include either a resistive touch panel (RTP) or capacitive touch panel (CTP) and provide an easy way to create a touch screen interface, or human machine interface (HMI) for your project. The displays require a spare serial port to communicate with them. To get the best from them requires an understanding of how to drive them, and what works and doesn't work. The advice here is based on my work with multiple projects using Nextion displays with both PIC and Arduino hosts. I don't claim the ideas here are the best way or the only way to control one with an Arduino, just what I have found to work well in my projects. The sample code has been written by me and tested on an Elegoo Mega2560, and you are free to use it, modify or improve as much as you like. My methods and the examples shown here do not use any libraries as I never found them necessary, the displays are easy enough to drive without a library.
If you prefer to use a library have a look at Easy Nextion Library (https://forum.arduino.cc/index.php?topic=676240.0") by Seithan, his methods are different to mine, choose which works best for you (I cannot help you with Seithan's methods).
If you prefer to use the official Nextion libraries please see Ray Livingston's improved Nextion libraries (https://forum.arduino.cc/index.php?topic=620821.0).

I do not represent Arduino or Nextion.

Everything that follows in this tutorial is about my methods.

Nextion support

The displays have their own instruction set which can be found here.  (https://nextion.itead.cc/instruction-set/)The instructions provide the means to control the displays either through messages sent to the serial port or from using the touch screen. This tutorial and sample code uses some of the Nextion instructions and assumes you have made yourself familiar with them from the Nextion web site.

The displays are configured using a WYSIWYG editor, which you can download from here. (https://nextion.tech/nextion-editor/#_section1)The sample configuration attached to this tutorial can be opened with this editor, which you will need to use the example here. (Latest version is V1.61.2 added autumn 2020)

Compatibility with Arduino

I have used the basic 4.3" RTP version (NX4827T043) and the enhanced 7" CPT version (NX8048K070) with a WeMos M0, MKR WiFi 1010 and Elegoo Mega2560 without problems. There are problems using one with an ESP8266, see 'using Nextion displays with Arduino part 4' further down this tutorial. I have not tried other versions or other Arduinos. As the only requirement is a spare serial port on the Arduino to connect the display to, I would expect that any Arduino with a spare serial port would work with any Nextion display, but I've not tried combinations other than those mentioned here.

Demonstration
You will need:
 
 • The attached 'Arduino Nextion demo 43V1 HMI', which can be opened with the Nextion editor.
 • The attached 'Nextion_demonstration43.ino', which can be opened with the Arduino IDE.
 • An Arduino with at least 1 spare serial port.
 • A Nextion display.
 • A micro SD card.

Connecting the Nextion display to your Arduino

The displays come with a cable with 4 wires. I have only ever seen them in the following colours but if you get one with different colours please let me know so I can update this part of the tutorial.
 • Red - for connecting to +5V
 • Black - for connecting to 0V
 • Blue - Transmit data from the display to the Arduino, connect to RX on the Arduino
 • Yellow - Receive data from the Arduino to the display. Connect to TX on the Arduino
 
 Note, Arduinios use one serial port for communication with your PC, do not use this serial port for connection to your Nextion display, use a spare one.
 
 The displays run off 5v. All the ones I have output 3.3v on their serial transmit (blue wire). I have not had any problems using them with a WeMos M0 at 3.3v, an Elegoo Mega2560 at 5V or a PIC running on 5v. However, please check the voltage on the display transmit (blue) wire is not higher than maximum voltage your Arduino can tolerate.
 
 To connect a Nextion to an Arduino you need one free serial port on the Arduino. Connect the TX from the Arduino to the yellow wire and the RX to the Arduino to the blue wire. You will also need 0v to the black wire and +5v to the red wire. The current drawn by the display depends on the model and how bright the back light is. The 7" CPT display NX8048K070 requires up to 550mA, the smaller ones less. The 4.3" NX4827T043 used for this tutorial draws 235mA. In my experience they generate quite a bit of noise on the supply and, although not essential, I recommend a 470μF capacitor across the supply soldered to the connector on the back (see reply #68 for more about this).
Please see photo below:

(https://forum.arduino.cc/index.php?action=dlattach;topic=604185.0;attach=299720)

This shows the back of the display with a 470μF capacitor soldered to the outside 2 connections of the connector. Note that it is essential that the negative connection of the capacitor goes to GND and the positive to +5V, otherwise you will fill your room with nasty smelling smoke and scare your cat.

(https://forum.arduino.cc/index.php?action=dlattach;topic=604185.0;attach=299722)

(https://forum.arduino.cc/index.php?action=dlattach;topic=604185.0;attach=299724)

Demonstration software

This demonstration was created for the basic 4.3" RTP version, NX4827T043. You can adapt my configuration for a different display using the Nextion editor.
 
 Open the attached file 'Arduino Nextion demo 43V1.HMI' with the Nextion editor. If you are using a smaller display move the position of any objects that are outside the display area of the smaller display by changing X and Y for each object to a value that puts the object inside the boundaries of your display. Once you have done this select DEVICE ID from the menu and select your display from the list. If you have a bigger display you don't need to move anything around, just select the correct device ID.
 
 Compile the display configuration by clicking 'compile'.
 
 Click on File > Open build folder, this should open a window with a file 'Arduino Nextion demo 43V1.tft'
 
 Copy this file, and only this file, to a micro SD card (There must be nothing else on the SD card)
 
 Put the micro SD card into the card reader slot on the side of the display and connect the black wire to 0V and the red wire to 5V. You should see the display updating, which takes about 5 seconds.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 17, 2019, 06:22 pm
Using Nextion displays with Arduino part 2

Once it has finished disconnect the power and then remove the SD card. Connect the power again and the display should start up and look something like this:

(https://forum.arduino.cc/index.php?action=dlattach;topic=604185.0;attach=299729)

Whatever version of display and Arduino you use make sure you know how to reference the serial port you are using. Because there are many Arduinos and clones, with varying numbers of serial ports, and because the choice of serial port is up to you, I cannot cover all possibilities in this tutorial. To develop this tutorial I used serial port 1 on an Elegoo Mega2560. You will need to edit the code to match the serial port you are using.

By default the Nextion serial port is configured for 9600 Baud and if you are new to Arduino or Nextion or both I recommend you leave it at 9600 Baud so that is one less thing to worry about if something doesn't work.

All you need to set up the Arduino for a Nextion display is:
Code: [Select]
void setup() {
  //This is for the serial monitor. Remove the // if you need to use it.
  //Serial.begin(9600);
  
  //This is for serial port 1, which is the one used for the Nextion in this demonstration.
  //You might need to change this depending on which serial port you are using for your display.
  Serial1.begin(9600);
  
  //For demonstration
  HMI_startup_message();
}
void HMI_startup_message() {
  Serial1.print(F("t0.txt=\""));
  Serial1.print(F("Nextion demonstration by Perry Bebbington. For support go to https://forum.arduino.cc/index.php?board=7.0"));
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}


Demonstration sketch and Nextion configuration.

The demonstration sketch creates a real time clock using millis()as the timing source. If you have read the various Arduino tutorials or are experienced with Arduino you will know that millis() is not accurate enough for an accurate real time clock. The purpose of this tutorial is to demonstrate an Arduino controlling a Nextion, and a clock seemed a simple way to do so. I leave it to you to create a more accurate clock.

Sending data to the display.

Sending to the display requires an understanding of the Nextion instruction set and what the display expects to receive. In the demonstration configuration there is a text box at the bottom with objname t0. To send text to this the display needs to receive.

t0.txt="Your text here"0xff 0xff 0xff

This is typical of any instruction, text or data, sent to the display. The display expects 0xff 0xff 0xff to indicate the end of the instruction.

For example:
Code: [Select]
void HMI_display_page(uint8_t page) {
  Serial.print(F("t0.txt=""));
  Serial.print(F("This is page "));
  Serial.print(page);
  Serial.print(""");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}


Puts the page number on t0.

Sending data from the Nextion display to the Arduino

I developed my own way of sending data from the display to the Arduino, which has proved flexible enough to adapt to all the applications I have so far tried. The basic idea is to identify each object on a page by the page it belongs to, its type and an index of that type.

'Type' is whatever you want it to be. Your project will have any number of buttons, sliders and whatever else on each page, you can group similar ones together and give them the same type, which is a number from 0 to however many you need. I reserve type 0 for the page itself.

'Index of type' is a number from 0 to 1 less than however many instances of the type there are on the page.

Unlike the displays, I chose not to use 0xff 0xff 0xff as a terminator for the data sent to the Arduino. Instead I use 0xa5 0xa5 0xa5 as a start indicator followed by 5 bytes. Sometimes the last byte or 2 is just padding to bring the total to 5. You can modify the code to expect more or fewer bytes, or use a different start sequence. The 5 bytes are the page number, the type, the index of the type and 2 data bytes.

The receive function looks for 0xa5 repeated 3 times, then saves the next 5 bytes in an array, then uses 3 levels of nested switch statements to identify which object on which page sent the data. Each case of the lowest level switch statement represents an individual object on the display and can be used to call whatever code you need for your project. The receive function should be called once each time in the main loop and will empty the receive buffer each time it is called.


Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 17, 2019, 06:33 pm
Using Nextion displays with Arduino part 3

Complete demonstration code

The complete code for the demonstration is here and attached as 'Nextion_demonstration43.ino'

Setup and main loop

Code: [Select]

void setup() {
  //This is for the serial monitor. Remove the // if you need to use it.
  //Serial.begin(9600);
  
  //This is for serial port 1, which is the one used for the Nextion in this demonstration. You might need to change this depending on which serial port you are using for your display.
  Serial1.begin(9600);

  //For  demonstration
  HMI_startup_message();
}

void loop() {
  HMI_read();
  clock_run();
}

struct CLOCK {
  int8_t hour;
  int8_t minute;
  int8_t second;
};
struct CLOCK clock;

//This displays the clock
void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d ", clock.hour, clock.minute, clock.second);
  Serial.println(timestring);
  Serial1.print(F("t1.txt=\""));
  Serial1.print(timestring);
  Serial1.print("\"");
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

//This displays the page number
void HMI_display_page(uint8_t page) {
  Serial1.print(F("t0.txt=\""));
  Serial1.print(F("This is page "));
  Serial1.print(page);
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

void HMI_P1_display_slider(uint8_t slider_d1, uint8_t slider_d0) {
  uint16_t slider_val = (slider_d1 <<8 | slider_d0);
  
  //This displays byte 1 of the slider value in HEX
  Serial1.print(F("t2.txt=\""));
  Serial1.print(slider_d1, HEX);
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
  
  //This displays byte 0 of the slider value in HEX
  Serial1.print(F("t3.txt=\""));
  Serial1.print(slider_d0, HEX);
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);

  //This displays the complete slider value in decimal
  Serial1.print(F("t4.txt=\""));
  Serial1.print(slider_val);
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

void HMI_startup_message() {
  Serial1.print(F("t0.txt=\""));
  Serial1.print(F("Nextion demonstration by Perry Bebbington. For support go to https://forum.arduino.cc/index.php?board=7.0"));
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

//HMI_read takes the data sent from the Nextion to the serial port and processes it depending on what has been sent
//There are 3 levels of nested switch statements corresponding to the page, the type of object and the index of the object.
void HMI_read() {
  static uint8_t HMI_read_data[10];         //This is a temporary buffer to hold the data from the display. Space for 10 bytes although this demonstration only uses 6 bytes
  static uint8_t HMI_read_data_i;           //This is a count of how many bytes have been received from the display.
  static uint8_t a5count;                   //0xa5 repeated 3 times is used as a start indicator, this is a count of how many times it has been received.
  uint8_t readtemp;                         //This is to hold the last received byte to ensure that it is only read from the receive buffer once.
  
  while (Serial1.available() > 0) {         //Read every byte in the receive buffer
    readtemp = Serial1.read();
    if (readtemp == 0xa5) {                 //Count the number of times 0xa5 has been received
      ++a5count;
      if (a5count > 2) {
        a5count = 0;
        HMI_read_data_i = 0;
      }
    }
    else {
      a5count = 0;
    }
    HMI_read_data[HMI_read_data_i] = readtemp;
    if (HMI_read_data_i == 5) {
      switch (HMI_read_data[1]) {             //HMI_read_data[1] contains the page the data has come from
        case 0:                               //Case 0 means the data has come from page 0
          switch (HMI_read_data[2]) {         //HMI_read_data[2] contains the type of object on the page that the data has come from
            case 0:                           //In this demonstraton case 0 selects a page
              HMI_display_page(HMI_read_data[3]);
              break;
            case 1:                            //In this demonstration case 1 is a button for setting the clock
              switch (HMI_read_data[3]) {      //HMI_read_data[3] is the index of the type of button, so 0 to 5 as there are 6 buttons for setting the clock. Each case is a different button.
                case 0:
                  ++clock.hour;
                  if (clock.hour > 23) {
                    clock.hour = 0;
                  }
                  break;
                case 1:
                  --clock.hour;
                  if (clock.hour < 0) {
                    clock.hour = 23;
                  }
                  break;
                case 2:
                  ++clock.minute;
                  if (clock.minute > 59) {
                    clock.minute = 0;
                  }
                  break;
                case 3:
                  --clock.minute;
                  if (clock.minute < 0) {
                    clock.minute = 59;
                  }
                  break;
                case 4:
                  ++clock.second;
                  if (clock.second > 59) {
                    clock.second = 0;
                  }
                  break;
                case 5:
                  --clock.second;
                  if (clock.second < 0) {
                    clock.second = 59;
                  }
                  break;
              }
              HMI_display_clock();
              break;
          }
          break;
        case 1:                               //Case 1 means the data has come from page 1
          switch (HMI_read_data[2]) {
            case 0:                           //Data from the page itself, this is the post initialisation request to update the page, which displays the page number
              HMI_display_page(HMI_read_data[3]);
              break;
            case 1:                           //Data from the slider on page 1
              HMI_P1_display_slider(HMI_read_data[5], HMI_read_data[4]);      //HMI_read_data[5] is byte 1 of the slider value, HMI_read_data[4] is byte 0 of the slider value
              
              
              
              break;
          }
          break;
      }
    }
    ++HMI_read_data_i;
    if (HMI_read_data_i > 9) {
      HMI_read_data_i = 9;
    }
  }
}

void clock_run() {
  static unsigned long previousMillis;
  unsigned long currentMillis = millis();
  const unsigned long interval = 1000;
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    ++clock.second;
    if (clock.second > 59) {
      clock.second = 0;
      ++clock.minute;
      if (clock.minute > 59) {
        clock.minute = 0;
        ++clock.hour;
        if (clock.hour > 23) {
          clock.hour = 0;
        }
      }
    }
    HMI_display_clock();
  }
}
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 17, 2019, 11:12 pm
Updated 30/7/2020

How to use multi-line text boxes added to Nextion additional features

Using Nextion displays with Arduino part 4

Some things that might help you


ESP8266
The ESP8266 has one serial port with both Tx and Rx available, and one with only Tx available. The one with both Tx and Rx is already in use for the serial monitor, making connecting a Nextion a bit of a problem. To get around this first write you code to send information to the Nextion but let it go to the serial monitor. When you are satisfied it is correct then use  Serial.swap(), which will swap the serial port to GPIO15 (Tx yellow) and GPIO13 (Rx blue). Note that GPIO15 (Tx) is also used on boot of the ESP8266 and if high makes it boot from an SD card, which creates a problem if you have a serial device connected and press reset, as the serial device will hold GPIO15 high. To get round this put a 10k (or possibly lower) resistor between GPIO15 and ground, this will hold GPIO15 low during boot.

Numbers
You might have noticed that in this demonstration I have used a single text box, t1, to display the time, I have not used one of the pre-defined number objects available in the Nextion editor. I found the numbers to be pretty useless, they can only be integers and they can't have any symbols attached to them, so you can't easily do £123.65 or 23.85% for example. Using a text box and print is much simpler.

Sliders
Page 1 of the demonstration has a slider with 3 text boxes, the first shows 2 bytes in hex as received from the Nextion, the 3rd shows the decimal value of the slider. I've made the range 0 to 1023, which is 0x00 to 0x3ff.

Baud rate
You can change the baud rate from 9600. In the Nextion editor select page 0 and find post initialisation event under event. Put baud=<required baud rate> as the first entry, for example baud=19200. This will make the display initialise with a baud rate of 19200. In your sketch initialise the serial port with:

Code: [Select]
[/color]
Code: [Select]
void setup() {
   //This is for serial port 1, which is the one used in this demonstration.
   //You might need to change this depending on which serial port you are using for your display.
  Serial1.begin(19200);
}



If you want to change the baud rate during program execution you have to be careful not to end up with the Arduino at one baud rate and the display at another. For this reason you need to send the change of baud rate instruction to the display, wait for the instruction to be sent then change the rate on the Arduino, then send data and instructions at the new rate. This will tell the display to change its baud rate:

Code: [Select]

Code: [Select]
void HMI_baud_change(uint32_t baud) {
  Serial.print(F("baud="));
  Serial.print(baud);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}



However, you have to remember that serial transmission is slow, so after the last Serial.write(0xff); has completed the 'b' of baud will probably not have been sent to the display. Also, once the display has received the complete instruction it takes time to make the change. For this reason you need to wait before changing the Arduino baud rate with:

Code: [Select]

Code: [Select]
Serial.begin(new baud rate);


Changing pages
If you look at my demonstration configuration there are 2 buttons P0 and P1 on each page. P1 on page 0 and P0 on page 1 change the page displayed but don't tell the Arduino that the page has changed. On each page under post initialisation event is the code that tells the Arduino that the page has changed. This is to ensure that the page has properly initialised before the Arduino sends any data to it.

Using background images
The demonstration Nextion configuration I supplied in 'Arduino Nextion demo 43V1 HMI.zip' is about as simple as I could make it. It is possible to use 2 (or more) background images to provide buttons or whatever you like in any design you want. To do this means using the Nextion cropped image feature. Attached to this post is 'Arduino Nextion demo cropped images 43V1.zip', which is exactly the same Nextion configuration file but using cropped images for the buttons to demonstrate how to do this.

Nextion additional features includes:
A scrollable list
Having the Arduino trigger code to run on the Nextion
Using a slider
Displaying numbers without using number boxes
Using flags
A simple state machine
Measuring the duration of a button press
A simple calculator (added 3 November 2019)
A means to send the Nextion return codes to the serial monitor
How to use multi-line text boxes (added 30/7/2020)

Credits
Thank you Robin 2 (https://forum.arduino.cc/index.php?action=profile;u=174714) and
BallscrewBob (https://forum.arduino.cc/index.php?action=profile;u=261482) for your help with this tutorial
Title: Re: Using Nextion displays with Arduino
Post by: Watcher on Apr 01, 2019, 06:37 pm
Excellent Tutorial!

Does anyone know if its possible to create objects with serial commands just like its done via the editor?

I.e Is it possible to create a button from serial and the nhange all its attributes?





Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Apr 01, 2019, 07:38 pm
Excellent Tutorial!

Does anyone know if its possible to create objects with serial commands just like its done via the editor?

I.e Is it possible to create a button from serial and the change all its attributes?
Quote
Excellent Tutorial!
:) THANK YOU!!! :)


Quote
Does anyone know if its possible to create objects with serial commands just like its done via the editor?
I.e Is it possible to create a button from serial and the change all its attributes?
I think probably not. There are attributes that cannot be changed by sending instructions over the serial port. If you look in the Nextion editor at an object some of the attributes are in green text and some in black text. I have not tried every possibility but I was trying to change some of the attributes that are in black text by sending instructions over the serial port and concluded they they cannot be changed this way. I assume from this that if an object has attributes that cannot be changed over the serial port then it must be impossible to create an object over the serial port.

What are you trying to achieve? There might be a way round this restriction.

Title: Re: Using Nextion displays with Arduino
Post by: Watcher on Apr 01, 2019, 08:43 pm
Quote
What are you trying to achieve? There might be a way round this restriction.
I plan to develop a touch screen display for an existing home automation system which is RS485 based.


So basically I need to program a series of soft push button on the display whose description as well as status (on/off) will come from the Arduino.

Pushing the button should send Arduino a unique code which will then execute the associated home automation command.

If it was possible to create buttons with serial commands, then the actual screen layout could be controlled by the arduino.
I did something similar on web based home automation screen using the html dom (https://www.w3schools.com/js/js_htmldom.asp).

The button press return code whoukd also be remotely set (via serial command)  as well.



Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Apr 01, 2019, 09:22 pm

Quote
I need to program a series of soft push button on the display whose description as well as status (on/off) will come from the Arduino.
You can change the text on a button in the same way as you change the text in a text box. You can also change the background colour and the text colour with the appropriate serial commands. For example:


Code: [Select]
void HMI_button_on_message() {
  Serial1.print(F("b0.txt=\""));
  Serial1.print(F("On"));
  Serial1.print(F("\""));
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

Will change the text on button b0 to On.

Play around in the Nextion editor with the debug options and see what you need to send to an object to change particular attributes. For example, in debug, if you type

Code: [Select]
b0.bco=63488<return>

then b0 will become red.


Quote
Pushing the button should send Arduino a unique code which will then execute the associated home automation command.
That's what my method does! Each button in the example sends a unique code that is decoded by the 3 levels of switch() statements to a unique point from which you can call whatever functions you like.


Quote
If it was possible to create buttons with serial commands, then the actual screen layout could be controlled by the arduino.
I don't believe you can change the layout, only the text and colours of the buttons.


Quote
The button press return code should also be remotely set (via serial command)  as well.
You don't need to. You change how your program responds to each unique code sent from the buttons.

What you could do is have multiple pages on the Nextion and have the Arduino send instructions to change the page. Each page would have the different layouts and each button would have a unique identity on the page. If you look at my demonstration I included 2 pages to illustrate how this works.




Title: Re: Using Nextion displays with Arduino
Post by: Watcher on Apr 02, 2019, 09:21 am
Quote
lay around in the Nextion editor with the debug options and see what you need to send to an object to change particular attributes. For example, in debug, if you type

Code: [Select]

b0.bco=63488<return>
Yeap! Already tried all that!

eg  This function changes the text on a textbox:

Code: [Select]
void HMI_displayText(byte boxNo, char* text){
 
  nextion.print(F("t"));
  nextion.print(boxNo);
  nextion.print(F(".txt=\""));
  nextion.print(text);
  nextion.print(F("\""));
 
  HMICommandEnd();
 
}


and this changes the color of a button :

Code: [Select]
void showButtonActivated(byte buttonNo) {

char comd[20];
char temp[20];

strcpy(comd,"b");
sprintf(temp, "%01d.bco=63488", buttonNo);
strcat(comd,temp);
Serial.println (comd);
nextion.print(comd);
 
HMICommandEnd();
 
}


However, I have potentially more than 100 buttons to configure each controlling things from lights, heating, mechanical appliances to  audio, etc . The buttons are grouped together according to their logical operation. For example all bedroom lights are grouped together and should appear on the same page. I am using a 3.5in nextion (for now) which seems like it can accommodate about 8 12 or so buttons per page.

I am trying to figure out some method which will enable me to make changes on the available buttons on the nextion screen as well as their grouping without having to re-program the nextion every time since that would need physical access to the display's usb port which will not be easily accessible once mounted on the wall.

The arduino itself is  being controlled remotely via RS485 link. The same link will curry the command to be executed once a button is pressed.

Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Apr 02, 2019, 06:22 pm
Hello Watcher,

I get the impression you are getting the hang of this and don't need much more from me. My heating controller has 5 pages and buttons in one corner to switch between them. My advice to you is to look how I switch pages in the example in this tutorial; the button on the page switches to the desired page directly, it does not tell the Arduino to change pages. On each page under Postinitializationevent is the code that tells the Arduino that the page has changed. Once the Arduino receives the code that the page has changed it then replies with the data appropriate to the page. The reason for doing it this way is to make sure that the new data needed on the page is not sent until after the page has initialised. I had lots of problems with errors in the data whenever I changed pages until I realised it took ages for a page to initialise and that data sent before that might be lost.
Title: Re: Using Nextion displays with Arduino
Post by: Watcher on Apr 04, 2019, 12:06 pm
I decided to design a single page with 12 buttons the description of each changes as the user changes page.
I other words changing a page it will only change the button's description and the actual code each button sends when pressed.


Anyone knows where to find some nice looking "transparent" images/icons to display on these screens?

Things like arrows left,right, bulbs, thermometers etc ?
Title: Re: Using Nextion displays with Arduino
Post by: valbuz on Apr 04, 2019, 02:29 pm
hi.

my problems are with the fonts.....

some text fields on the page.
there are fields labeled by the editor other are labeled by sending commands from sketch.
but they are different on the page!
the fields by command aren't very good readable. the fields by editor are very well.
it's the same font and size for both.

anybody knows such a problem?

thanks
pat

Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Apr 04, 2019, 06:50 pm
Quote
Anyone knows where to find some nice looking "transparent" images/icons to display on these screens?
Things like arrows left,right, bulbs, thermometers etc ?
When I have needed them I have searched on the internet, there are loads and loads of free downloads if you go looking. Be aware that the Nextion does not support transparent images, although you might need them to construct a background to your needs.
Title: Re: Using Nextion displays with Arduino
Post by: RedyAu on Apr 21, 2019, 06:36 pm
I decided to design a single page with 12 buttons the description of each changes as the user changes page.
I other words changing a page it will only change the button's description and the actual code each button sends when pressed.


Anyone knows where to find some nice looking "transparent" images/icons to display on these screens?

Things like arrows left,right, bulbs, thermometers etc ?
https://www.flaticon.com/ is a good starting place. You should draw everything in another progam (inkscape, photoshop, whatever), and just build the functionality with nextion. That way it'll look very nice.
Title: Re: Using Nextion displays with Arduino
Post by: jtbennett on May 12, 2019, 04:37 am
I'm new to the C language so maybe people have a better way to avoid creating multiple text boxes just to get a line break.

I've been trying to put \n in my t0.txt command, but it just brings up a strange inline character rather than putting a line break...any ideas?
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on May 12, 2019, 09:56 am
I've been trying to put \n in my t0.txt command, but it just brings up a strange inline character rather than putting a line break...any ideas?
If you mean you want to send new line to a Nextion text box then I think you are wasting your time, I don't think they support it. (If I am mistaken and you know for sure that they do support it then I'd like to know). As far as I know the text boxes treat all text as a single line. Where I have needed multiple lines of text I have used multiple text boxes.
Title: Re: Using Nextion displays with Arduino
Post by: jtbennett on May 12, 2019, 06:44 pm
If you mean you want to send new line to a Nextion text box then I think you are wasting your time, I don't think they support it. (If I am mistaken and you know for sure that they do support it then I'd like to know). As far as I know the text boxes treat all text as a single line. Where I have needed multiple lines of text I have used multiple text boxes.
Yeah I wasted some time searching around and trying things, nothing to be done there. But, the good news is that your method is working out great now. Apart from the graphic interface, I have most of my coding and variables on the Arduino now and it receives all the user-entered data through the keyboard in bytes before being appended into a complete string. Anyway - thanks for figuring this thing out, I was going mental.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on May 12, 2019, 10:27 pm
Quote
Yeah I wasted some time searching around and trying things, nothing to be done there.
That's a shame, I was rather hoping you'd discovered something I'd missed!


Quote
Apart from the graphic interface.
What are you trying to do? In my first draft of this tutorial I had graphics in it, but I ditched them because having any graphics would make it much harder for someone to adapt the tutorial to different size displays, so the instructions I gave for using a different display to the one I wrote the tutorial for would have become more complex, and I didn't think that was a good idea for a tutorial aimed at someone new to Nextion displays, so I left it out.


Quote
Thanks for figuring this thing out, I was going mental.
My pleasure :)


Title: Re: Using Nextion displays with Arduino
Post by: jtbennett on May 13, 2019, 02:18 am
What are you trying to do? In my first draft of this tutorial I had graphics in it, but I ditched them because having any graphics would make it much harder for someone to adapt the tutorial to different size displays, so the instructions I gave for using a different display to the one I wrote the tutorial for would have become more complex, and I didn't think that was a good idea for a tutorial aimed at someone new to Nextion displays, so I left it out.
I have to be secretive about the thing I'm building, but I'll make a thread on this forum so I can show off when it's finished. I think if I said it, someone who's much better at programming would make one quicker than me and ruin my chance at being the first :D. It's a machine for manufacturing musical instrument parts.

The graphics I'm using are just bitmap images (actually nextion editor imports PNGs nicely too) to create all the buttons and icons for the UI, but I draw the backgrounds in manually using fill commands. That sort of thing can all be sent over serial from the arduino too using your method (I use it to do vis commands on elements and page changes etc). Theoretically, you could draw all your background UI elements via serial, but I dunno about creating buttons and pages from nothing.

It never occurred to me to control the images from the serial line - the Nextion editor is good enough for creating and navigating through the pages and elements, and you can do a lot of programming on it, but because the serial communication libraries are pretty much useless and there's absolutely no consideration for global variables, complex projects that require a proper MCU board should definitely use your method and store all their code on the board.



I'll post the a QWERTY keyboard code if it'll be useful around here - it's probably not the most attractive looking code, but but you receive all your user-entered data byte by byte and it's assembled into complete variables / strings / integers / floats on the arduino end.


What I'd really like to do with these Nextions though is work out the font issues...they looks so crusty and the sizing/spacing is all over the place, inconsistent, and no antialiasing. The Nextion font generator is absolute garbage, and they charge about $250 USD for a premade font. There's no repository online to download free fonts, and the only project to create proper fonts is still being worked on...who knows how long it'll take.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on May 13, 2019, 09:46 pm

Quote
The Nextion font generator is absolute garbage
Indeed. Search for zi font editor and you should find a tool to edit them to your own liking.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on May 13, 2019, 10:44 pm
Quote
I draw the backgrounds in manually using fill commands.
2 suggestions:

If you want to draw stuff, or do anything that would require a lot of data to be sent over the serial port, create a button and put all the code to do the drawing or whatever under touch release event for the button. Make the button 1 pixel * 1 pixel and give it the same colour as the background, and place it somewhere on the display where it is out of the way. Then you can use something like:


Code: [Select]
void HMI_draw_background() {
  Serial.print("click b0,1");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("click b0,0");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}


To click the button and the Nextion will execute all the code under the button and do the drawing or whatever.

Alternatively, have a look in part 4 of this tutorial at 'using background images', you can create a background with whatever you like on it. Create 2 almost identical backgrounds with one showing, for example, buttons not pressed, the other showing pressed, and use the cropped image feature to select which one shows when you press the button.
Title: Re: Using Nextion displays with Arduino
Post by: rubendargalan on May 14, 2019, 05:51 pm
Hello, thank you very much for this tutorial Perry Bebbington, it is excellent. I see that these screens consume a lot of energy (you said that the NX4827T043 consumes 235mA). Do you have any way to decrease its brightness? Or leave it in sleep mode? To try to reduce your consumption. Thank you very much again. A greeting.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on May 14, 2019, 06:18 pm

Quote
Hello, thank you very much for this tutorial Perry Bebbington, it is excellent.
Thank you :)


Quote
Do you have any way to decrease its brightness?
Look in the Nextion instruction set for 'dim' and 'dims', they control the brightness, and as you expect, lower brightness means lower current consumption. Unfortunately, what does not lower the current consumption is putting dark things on the display, you can make the background black or white and it will consume the same amount of power.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jun 08, 2019, 02:40 pm
I've modified the HMI files to include automatic display dimming, which happens after 10 seconds of no pressing any buttons.
Title: Re: Using Nextion displays with Arduino
Post by: jtbennett on Jun 08, 2019, 06:17 pm
I've modified the HMI files to include automatic display dimming, which happens after 10 seconds of no pressing any buttons.
Nextion needs to put you on the payroll for figuring out how to make their screens useful.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jun 08, 2019, 06:47 pm
Nextion needs to put you on the payroll for figuring out how to make their screens useful.
That would be nice :)
Title: Re: Using Nextion displays with Arduino
Post by: chuckhager on Jun 11, 2019, 06:00 pm
For some reason, I cannot find the HMI file. The zip file only has the TFT file.
 
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jun 12, 2019, 10:07 am
Hello chuckhager,

Apologies, I uploaded the wrong file when I made some changes a few days ago. Hopefully you will get the correct file now if you download again. If not, please let me know.

Thanks,
Perry
Title: Re: Using Nextion displays with Arduino
Post by: Fractoggen on Jun 18, 2019, 08:13 am
it is really good for me. :o
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jun 22, 2019, 10:52 am
In 'Using Nextion displays with Arduino part 4' I have added a demonstration of using a scrollable text list. This includes new HMI and ino files for you to download and try.
Title: Re: Using Nextion displays with Arduino
Post by: kn4ud on Jul 19, 2019, 01:12 pm
Perry, thank you very much for taking your time to share your knowledge and ability of using the Nextion with Arduino. I have a project I have been struggling with and I am finding this a big help.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jul 19, 2019, 04:19 pm
Perry, thank you very much for taking your time to share your knowledge and ability of using the Nextion with Arduino. I have a project I have been struggling with and I am finding this a big help.
My pleasure! Glad to be of help and I appreciate the feedback :)
Title: Re: Using Nextion displays with Arduino
Post by: jtbennett on Jul 19, 2019, 08:09 pm
Nextion has just released a new update for their editor - I downloaded it and it's actually a decent improvement, not what I expected from them. They've added font anti-aliasing - you have to regenerate your fonts again with their generator tool, but it looks fine so far. Just about to do some testing.

Anyway, thought you'd be interested to know if you hadn't seen it yet - I'm wondering what else they've added...will have to google it now.

e: https://nextion.itead.cc/instruction-set/ has been updated
Title: Re: Using Nextion displays with Arduino
Post by: kn4ud on Jul 19, 2019, 09:54 pm
I updated from 53 yesterday. So far no problems other than the ones I cause for myself. I am glad to know I will need to regenerate my fonts. In my older files they appear as they did. some seem to have irregular spacing as they did. I am going to regenerate them and see if they look better.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jul 20, 2019, 09:52 am
Thanks for the info JT. I had been concerned that ITEAD were neglecting the Nextion product as there have not been any updates to the IDE for ages. I've updated the link for downloading the editor at the start of this tutorial.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jul 20, 2019, 10:22 am
Well, it didn't take me long to find a bug in it! When I close it, instead of it closing gracefully I get the Windows warning message 'HMI has stopped working'. Does anyone else get this?
Title: Re: Using Nextion displays with Arduino
Post by: Watcher on Jul 20, 2019, 10:31 am
Well, it didn't take me long to find a bug in it! When I close it, instead of it closing gracefully I get the Windows warning message 'HMI has stopped working'. Does anyone else get this?
No. I have closed the application with no warning message.
Title: Re: Using Nextion displays with Arduino
Post by: kn4ud on Jul 20, 2019, 06:26 pm
Well, it didn't take me long to find a bug in it! When I close it, instead of it closing gracefully I get the Windows warning message 'HMI has stopped working'. Does anyone else get this?
So far I have not had this happen.
Title: Re: Using Nextion displays with Arduino
Post by: orion555 on Jul 22, 2019, 11:22 am
Well, it didn't take me long to find a bug in it! When I close it, instead of it closing gracefully I get the Windows warning message 'HMI has stopped working'. Does anyone else get this?
I have the same problem. Doesn't seem to affect the file, but is a bit annoying. But I'm glade the came along with a reasonable update, since the fonts were, to phrase it polite and politically correct, not quite up to standard...

Since I'm already posting here, I have got a couple of questions as well. To mention it up front, I'm a hobbyist programmer (but an extremely stubborn one though ;-)) and relatively new to Arduino (couple of month) and an absolute beginner with Nextion (two weeks). Was a very stony way so far with partly rather blank nerves but I slowly start getting the desired results...

So here are the questions:

Do you, or anybody else out there, know how to address the new xFloat function (sending a float number to the Display)? Couldn't find anything in their instruction set and NexNumber doesn't seem to work. (solved it for now by converting it to .txt)

Sending data to the  MCU with a button function followed by a page change isn't working. Had to place two buttons, one to send the data and the second one to change the page. Almost drove me nuts before I figured out what the problem was. If I remember it correctly did you have the same or a similar problem. What was your solution. Preinitialize? If so, how does it work?

Regards
Title: Re: Using Nextion displays with Arduino
Post by: orion555 on Jul 22, 2019, 12:22 pm
If you mean you want to send new line to a Nextion text box then I think you are wasting your time, I don't think they support it. (If I am mistaken and you know for sure that they do support it then I'd like to know). As far as I know the text boxes treat all text as a single line. Where I have needed multiple lines of text I have used multiple text boxes.
I'm not sure if this answer is what you are looking for, but there is a function right below txt_maxl called 'isbr'. Set to true, it allows, if you expand the text box down,  at least text wrap. Not sure (haven't tried it myself) if it can be addressed directly from the MCU with a line break. Also not sure on how to address the new 'multiline' function from the outside.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jul 22, 2019, 10:01 pm

Quote
Do you, or anybody else out there, know how to address the new xFloat function (sending a float number to the Display)? Couldn't find anything in their instruction set and NexNumber doesn't seem to work. (solved it for now by converting it to .txt)
I cannot help with anything related to the Nextion libraries, although there are others on here who might be able to help with them.

I tried:

Code: [Select]
x0.val=4567
Which works.


Quote
Sending data to the  MCU with a button function followed by a page change isn't working. Had to place two buttons, one to send the data and the second one to change the page. Almost drove me nuts before I figured out what the problem was. If I remember it correctly did you have the same or a similar problem. What was your solution. Preinitialize? If so, how does it work?
Assuming you are using my methods look in the sample '2019-06-08 Arduino Nextion demo 43V1.zip' at the start of this tutorial; there are buttons to change the page. The button itself changes the page. On the new page under post initialisation event is the code to tell the Arduino that you have changed pages. The Arduino then responds with whatever data you want on the page. The reason for this is you have to change the page first, then wait for it to initialise then send whatever data you want on it. Putting the request to the Arduino for the data under post initialisation event guarantees that the page will be ready by the time that the data arrives. You can't have a button on page 0 that puts data on page 1 unless the variables on page 1 are global. The variables on page 1 are otherwise out of scope while page 1 is not displayed, so sending anything to page 1 from another page will not work. Sending data to a page before it has had chance to initialise will result in lost or errored data.

Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jul 22, 2019, 10:10 pm
Quote
I'm not sure if this answer is what you are looking for, but there is a function right below txt_maxl called 'isbr'. Set to true, it allows, if you expand the text box down,  at least text wrap. Not sure (haven't tried it myself) if it can be addressed directly from the MCU with a line break.
Yes, thanks, aware of that. I think what was being asked for was something that did new line within a text box. The old version certainly does not respond to anything with a new line, it just allows one long line of text which can scroll across multiple lines in the text box, but it is only a single line of text.


Quote
Also not sure on how to address the new 'multiline' function from the outside.
Me neither, please report back!

Oh, and ++Karma; for being so helpful and asking intelligent questions on your first visit to the forum.
Title: Re: Using Nextion displays with Arduino
Post by: jtbennett on Jul 25, 2019, 02:38 am
My pleasure! Glad to be of help and I appreciate the feedback :)
I hate to bring back an old issue, but something I've just noticed (maybe the update changed something, or I've just not realized for this whole time):

When I send data in the fourth line of the serial out from the nextion (after index #) using "print t0.txt" or "print h0.val", and then I read it with HMI_read_data[4] and print it back out, I'm getting two digits that appear the same each time.

I have an h0 scrollbar with values from 500 to 5000, yet every time I print the value over the serial on release, I get a number between 41 and 53. Is this part of a hex code I'm not decoding properly? I'd like to convert it to a proper int.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jul 25, 2019, 09:21 am
I've read that several times and I am not clear what you are doing. Can you give me both the code on the scroll bar and the corresponding code on the Arduino please?


Code: [Select]
HMI_read_data[4]
Is 1 byte. You need 2 bytes for a range of 500 to 5000.

My heating controller has sliders to control the temperature setting with a range of 0 to 300. That gives me 30 degrees range in steps of 0.1 degree (yes, I know, I probably should be using 0 to 299). That means that the data sent from the sliders takes 2 bytes. The section of code that takes the slider values and updates the set temperature is this:

Code: [Select]
case 1:
       zone[HMI_read_data[3]].settemp = (HMI_read_data[4] | HMI_read_data[5] << 8) * 0.1; //Note 1
       zone[HMI_read_data[3]].flags_HMI |= HMI_text_settemp_update; //Note 2
       break;

Note 1, this is the update for the temperature setting:
Code: [Select]
(HMI_read_data[4] | HMI_read_data[5] << 8)
Combines 2 bytes into one integer, which is the value of the slider setting. I think you need something similar.

Note 2, this is a flag to tell the rest of the program that there has been an update

Although not the cause of your problem, I suggest you don't use that range. I don't know which Nextion you are using, but the highest number of pixels in one direction is 800, so the maximum possible number of unique values from a slider is 800. Any more than that cannot be resolved on the display, so while you might get meaningful values with a slider with a range of 4500, there must, I presume, be gaps in the numbers.

While 500 to 5000 should work from a software point of view, my suggestion would be to use 0 to 4500 and add the 500 in on the Arduino.

In case you are interested, this is another way to combine 2 or more bytes into and integer, or split an integer into bytes, something like:

Code: [Select]
union {
  struct {
     uint8_t data_0;
     uint8_t data_1;
  }
  uint16_t data;
}


If you write to data_0 and data_1 the two bytes will appear in data as a 16 bit int, if you write a 16 bit int to data then you can read the corresponding 2 byes from data_0 and data_1. You can of course expand this for larger ints and for floats.

Title: Re: Using Nextion displays with Arduino
Post by: jtbennett on Jul 25, 2019, 10:42 pm
I used the version in your demonstration file, it worked perfectly - I don't know why I didn't catch that before - I probably got lucky the first time I did it because that slider only goes from 1 to 6.
Title: Re: Using Nextion displays with Arduino
Post by: orion555 on Jul 26, 2019, 12:49 am
Got a bit 'side tracked' in the endless world of coding... especially as a newbie, sometimes it can be quite humbling and overwhelming looking at all those doors opening up in front of one's continuously growing eyes...

In a severe fit of megalomania I tried to write, well, re-write the NexNumber lib to a NexFloat and ended up in a very deep rabbit hole. took me a while to find my way out again, but got it from an endless list of errors down to a couple... and slowly a slightly broader spectrum of to this never ending subject.

But it 's definitely my turn to pass the flowers to you, Perry, for the help you are providing here. Nice little hints you are passing on, especially the last ones I found quite interesting, since it sort of unlocked a whole lot of new possibilities and you deserve every bit of Karma you can get for things like that...

Was using the Nex's own libraries and was a bit too lacy to re-write all the work I finished so fart, with your sample code in your first postings (nicely done, by the way). But I came to the conclusion, well better rephrase that, I was lucky enough to get my fit under control... ;), and now I'm humbly going to use your code and hope I can get back to you, in case I don't get it straight...but all in all work is progressing, even if it isn't 'quite' as fast as I first thought it might happen...but I find if always very satisfying if things slowly start to do the things they should.

Knowing now that I'm at least capable of asking reasonable intelligent questions, I'm glad I found somebody who is helpful  enough to answer them. Thanks a lot for that. Hope I won't have to bother you too much.

I presume that other people on the 'big search' are passing by here as well, so I thought I might pass on a little helper to the Serial.print subject, simple enough that even I could have thought of it... :D, well, I didn't...but going to pass it on anyway, since it safes time and memory...

Code: [Select]


void nexDataSend() {
  Serial2.write(0xff);       // We always have to send this three lines after each command sent to the nextion display.
  Serial2.write(0xff);       //use the Serial(x) you are communicating with your Nex
  Serial2.write(0xff);
}



...and I find it quite handy, maybe it helps someone else as well.

will try my best to surface once in a while and pop by... ;)

kind regards
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jul 26, 2019, 09:06 am
Thank you Orion, that makes helping you and others worthwhile :)
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Aug 10, 2019, 11:31 am
I have added to part 4 'Nextion additional features', which brings several different things together in one place. Enjoy!
Title: Re: Using Nextion displays with Arduino
Post by: altitudeap on Sep 11, 2019, 05:01 am
@PerryBebbington et. all.  I posted this in another section but not sure it will get attention there.

I have not yet been successful in understanding the communication from the Nextion.  I have a few pages that are set up and I only need to capture the string that is sent when pages changes.  from this I will create a CASE statement and execute accordingly . 

For instance, when I am viewing the debug, and I am moving from page0 to page1, I get an simulator output (I am assuming I will see this as a serial string?) of  "65 00 02 01 FF FF FF"
So I have recorded all of these strings associated with the page movements and created a table of string, pulse and direction.  This will not be a high frequency event. (it is a shifter for a car)

Can I just use a simple serial program to read the string and then act upon the string in a CASE Statement? 
Thank you for any assistance you may be able to render.  I have some pictures but dont know how to or if I can post them

Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Sep 15, 2019, 12:34 pm
Hello altitudeap,
Sorry for the late reply, I have been on holiday :)


Quote
For instance, when I am viewing the debug, and I am moving from page0 to page1, I get an simulator output (I am assuming I will see this as a serial string?) of  "65 00 02 01 FF FF FF"
Yes, what you see in the simulator window is what is sent to the Nextion serial port. I would hesitate to call it a 'string' as that words has specific meaning in the C and C++ languages, and I don't think this data qualifies. Those bytes, as shown in the simulator window, will be sent to the serial port.

It is clear from the rest of your question that you have not tried my examples as set out in this tutorial, the answers are in there. Try the examples, then come back when you need further explanation.

Title: Re: Using Nextion displays with Arduino
Post by: orion444 on Oct 12, 2019, 12:12 am
Hi Perry,

Misplaced my PW for orion555 and I'm still waiting for the recovery mail...well, I guess waiting a week or two longer won't make the difference either anymore...

...in the meantime I thought it might be nice to let you know that finally, by using your idea, my first project is almost done and worked out far better than expected.

After a few initial hickups (by adapting your code to my personal needs) it's working really nicely now and, what's most important, it handles the data flow from and to HMI in a very simple way and all that without the Nextion library.

Thanks a lot for your effort. Deserves Karma...
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Oct 12, 2019, 09:19 am
Hello Orion444,

Thanks very much, much appreciated :)
Title: Re: Using Nextion displays with Arduino
Post by: socalva on Oct 16, 2019, 07:43 pm
Very clear and well explained! Does anyone know if it's possible to move objects during runtime? I'm unable to figure out a way to set X and Y variables dynamically. Any suggestions?
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Oct 16, 2019, 08:55 pm

Quote
Very clear and well explained!
Thank you :)
Quote
Does anyone know if it's possible to move objects during runtime? I'm unable to figure out a way to set X and Y variables dynamically. Any suggestions?
The basic and enhanced versions do not support moving objects during run time. I'm not sure about the intelligent ones, I have not bought one.

If you look at attributes in the right of the IDE you will see that some of the attributes are in green and some in black. You can change the green ones during run time.

You could create multiple pages with the same objects in different places, or you could create multiple backgrounds with the same images in different places and use the cropped image feature to select the one you want.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Oct 20, 2019, 06:28 pm
Following on from a forum question a few weeks ago I have added to part 4 'Nextion additional features' a method of measuring how long a button has been pressed for. Pressing and releasing a button displays the duration of the press in milliseconds. You could use this to measure the length of a button press, then if it is less than some duration do one thing, it if it more than the duration do something else, giving different responses to short an long presses.
(If it was you who asked the question and you would like credit here then please send me a PM).

I have also identified and fixed the cause of the bug that crashed a Uno or Mega (and possibly others) when the scrolling list page was selected.

Please test these examples and let me know on here if you find any problems or have any suggestions for additional features.

Thank you.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Nov 03, 2019, 03:03 pm
I have updated part 4, I have added to Nextion additional features:

Enjoy.
Title: Re: Using Nextion displays with Arduino
Post by: Tsjompie on Nov 05, 2019, 08:04 pm
For anyone active in this thread, in particular Perry and Ray: Thanks a million for these details!
This is incredible valuable (as well as the other threads).

Getting the screen to work was a struggle earlier, but now I got the hang of it. In fact I've been building an application containing 5 Nextion pages, communicating from and to the Nextion display successfully.
From the very beginning of my coding I've been using the library Nextion provides.

Question 1:
I noticed Perry and Ray Livingston have alternative approaches, which I'm willing to read and understand.
Unfortunately, I cannot find the alternative library Ray created, some help pointing that out to me is highly appreciated.

Furthermore: I ran into the issue doing everything sequential, using the Arduino loop.
To get around that I'm changing my code into a state machine solution at the moment.
For that to proceed I'd like to get (see) the serial communication content from the Nextion screen to the Arduino. The state machine allows me to read the serial communication, but I need to know what the screen is sending to allow me to filter on incoming data.
My best guess is to use the details the Nextion simulator is giving back, like: 65 00 01 00 FF FF FF or
e\x00\x01\x00ÿÿÿ.

Question 2: Is there a way to change the Nextion.h serial configuration (or elsewhere) in a way my serial window shows the incoming serial data? Or suggest me another way of looking into and understanding the serial communication details the Nextion gives me?

My functionality depends highly on the Nextion screen buttons and sliders. So handing me a suggestion how to filter the serial data solves my showstopper at the moment.

Thank you
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Nov 05, 2019, 09:12 pm

Quote
For anyone active in this thread, in particular Perry and Ray: Thanks a million for these details!
This is incredible valuable.
Thank you :)

Quote
Unfortunately, I cannot find the alternative library Ray created, some help pointing that out to me is highly appreciated.
I don't understand this, there is a link to Ray's tutorial on the very first post of this tutorial, on the first page of Ray's the files are there, waiting for you to download. I've just checked, the link works, the files are there.


Quote
Is there a way to change the Nextion.h serial configuration (or elsewhere) in a way my serial window shows the incoming serial data?
That (Nextion.h) suggests you are using the Nextion libraries, about which I know next to nothing. Ray might know.

Look at part 4 of this tutorial and download 'Nextion additional features'. In there I've added code to take return data from the Nextion and send it to the serial monitor. This is, of course, designed to work with my methods not the library.


Quote
My functionality depends highly on the Nextion screen buttons and sliders. So handing me a suggestion how to filter the serial data solves my show-stopper at the moment.
That should be simple with my methods; Nextion additional features has 5 pages including buttons and sliders, all working together.
Title: Re: Using Nextion displays with Arduino
Post by: artart on Nov 13, 2019, 11:41 pm
I have found this incredibly helpful:

https://forum.arduino.cc/index.php?topic=634237.0 (https://forum.arduino.cc/index.php?topic=634237.0)

With it and what I have learned here I have a working project. Thanks Perry!
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Nov 14, 2019, 01:37 pm

Quote
Thanks Perry!
:)
Title: Re: Using Nextion displays with Arduino
Post by: Redpiperbob on Nov 22, 2019, 04:17 pm
Hi Perry
I have just started with the Nextion Displays and I am trying to your demo pro to work without much success. I am using an Arduino Mega and an Enhanced  Nextion  NX4024K032-011
First of all with your sketch as it is on the web site and the nextion library config.h file modified as :-

Code: [Select]
//#define DEBUG_SERIAL_ENABLE
//#define dbSerial Serial
#define nexSerial Serial1

And the Mega to Nextion linked as:-
Nextion RX (yellow) to TX1 (pin18) on the Mega
Nextion TX (yellow) to RX1 (pin19) on the Mega

I can get the page buttons to swop pages and the + and - buttons to change colour when pressed and the slider to slide. But that all.  No time.

If I change the the config.h to :-
Code: [Select]
#define nexSerial Serial
and change your sketch to serial
and the mega to Pins 0 and 1 (TX0)

and open the serial monitor I can see the time , if I press the + or - on the display the digits alter in the serial monitor and I get a message if I move the slider.
Can you help please?
thanks
Bob
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Nov 22, 2019, 04:52 pm
Hi Bob,
The code you have posted is not from the code I use in the demonstration.
Quote
First of all with your sketch as it is on the web site and the nextion library config.h file modified as :-
My code does not use the Nextion library.

Go back, remove all references to any Nextion libraries and use my code exactly as provided.

Nothing in my code should change the colour of anything. The + and - buttons set the time on the first page.
Title: Re: Using Nextion displays with Arduino
Post by: Redpiperbob on Nov 22, 2019, 06:07 pm
Ok Perry gone back to the start downloaded the hmi and ino and uploaded to Nextion and Mega. Run nothing comes out on the display.. The buttons seem to work and the slider moves.
If I change  all the "Serial1" in the ino to "Serial" then move the display leads to the pin 0 and 1 fo the Mega and open the serial monitor i can alter the time in the monitor by pressing buttons and i get a message if i move the slider.
which proves your program is working fine.
I just dont know what I am dooing wrong
Thanks
Bob
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Nov 22, 2019, 06:33 pm
Hi Bob,
Don't connect the Nextion to 0 and 1, these are used by the serial monitor. On a Mega you should have blue on 19 (RX1) and yellow on 18 (TX1).

Can you confirm that the Arduino code you are using is the code in:
Using Nextion displays with Arduino part 3 ?
The code there is both shown in code tags in the main message and as a downloadable file at the end of the post.
Also confirm that you have selected Board: Arduino / Genuino Mega or Mega 2560 under Tools in the IDE.

Also confirm that the Nextion HMI file you are using is the one in 2019-07-25 Arduino Nextion demo 43.zip , which can be found at the end of the first part of the tutorial.

In order to check that I've not made any mistakes with my code I have just downloaded both the Arduino code in part 3 and the HMI file from part 1 and uploaded them to a Mega and my Nextion and checked they work.

When you reset the Mega you should see the initial message on the Nextion at the bottom, which reads:

Quote
Nextion demonstration by Perry Bebbington. For support go to https://forum.arduino.cc/index.php?board=7.0
And the clock in the middle should start running from 00:00:00



Title: Re: Using Nextion displays with Arduino
Post by: Redpiperbob on Nov 22, 2019, 09:16 pm
Hi Perry
I tryied all four TX/RX positions. but i knew that the correct two were 18/19.
Yes I am using the HMI from part one and the INO from part three.
After pluging in and reseting the display does not change.
I think I may have a fualty display. I only have the one I will order a new one tomorrow.

Thanks
Bob
Title: Re: Using Nextion displays with Arduino
Post by: twiereng on Dec 03, 2019, 09:58 pm
Is it possible to send the Nextion a command from an Arduino to set the page?  Sending something like "page 3" doesn't work, although it does work locally on the Nextion.

Thanks,
Theron Wierenga
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Dec 04, 2019, 02:46 pm
Hi Theron,
Here is some demonstration code for a Nextion with 5 pages, this changes the pages 0, 1, 2, 3, 4 then back to 0. You can change the number of pages by changing pageCount to however many pages you have. I have set the baud rate to 9600 as that is the default for a Nextion.

Code: [Select]

/* Demonstrates changing pages with the Arduino telling the Nextion which page to change to */
/* Uses the Nextion configuration 'Nextion_additional_features', which can be found in part 4 of this tutorial */
/* Tested on a Mega using serial port 1 connected to a NX4827T043_11 */

#define waitTime 1000       // Delay between each change of page
#define pageCount 5         // Number of pages on the Nextion
#define serialBaud 9600     //Baud rate for serial monitor
#define NextionBaud 9600    //Baud rate for Nextion

void setup() {
  Serial.begin(serialBaud);       // For the serial monitor
  Serial1.begin(NextionBaud);     // For the Nextion
}

void loop() {
  change_page();
}

void change_page() {
  static uint8_t page = 0;    //Current page on the Nextion
  static uint32_t previousMillis;
  uint32_t currentMillis = millis();
  if (currentMillis - previousMillis >= waitTime) {
    previousMillis = currentMillis;
    Serial.print("Page changing to ");      // Prints onto the serial monitor
    Serial.println(page);
    Serial1.print("page ");                 // Tells the Nextion to change pages
    Serial1.print(page);
    Serial1.print("\xFF\xFF\xFF");
    page = ++page % pageCount;
  }
}
Title: Re: Using Nextion displays with Arduino
Post by: vagos21 on Dec 13, 2019, 06:53 am
Hello Perry and everyone else, this has been an amazing useful chunk of info packed for everyone to start using the Nextion :)

I've been playing around with a 4.3" and an arduino for an audio project, and yes it does generate some noise. Does the 470uF capacitor help with the noise, or is it for smoother run of the LCD? Have you investigated further into the noise thing?

Thank you,
Vangelis
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Dec 13, 2019, 12:01 pm
Quote
Does the 470uF capacitor help with the noise, or is it for smoother run of the LCD? Have you investigated further into the noise thing?
When I first used Nextion displays I noticed they were interfering with things. Most notable I had a GPS receiver that would only work if I put it a long way from the Nextion. I put a 1 Ohm resistor in series with the supply and used it to monitor the current with an oscilloscope, you can see the result below. As this is measured across a 1 Ohm resistor the peak of the spikes is about 930mA. The second image is the exact same set up but with a 470μF capacitor soldered across the connector on the Nextion. 470μF was the smallest value capacitor that completely eliminated the spikes.
(https://forum.arduino.cc/index.php?action=dlattach;topic=604185.0;attach=339129)

(https://forum.arduino.cc/index.php?action=dlattach;topic=604185.0;attach=339131)

Title: Re: Using Nextion displays with Arduino
Post by: chrisplusian on Jan 13, 2020, 12:35 am
Thanks for the tutorial! I am just getting started with the Nextion display and I have a question about how you are changing data sent from the display to the screen. You said:

"Unlike the displays, I chose not to use 0xff 0xff 0xff as a terminator for the data sent to the Arduino. Instead I use 0xa5 0xa5 0xa5 as a start indicator followed by 5 bytes. Sometimes the last byte or 2 is just padding to bring the total to 5. You can modify the code to expect more or fewer bytes, or use a different start sequence. The 5 bytes are the page number, the type, the index of the type and 2 data bytes."

 I am trying to understand how you accomplish this? I read through the tutorial and I may have missed it. Could you explain how you are doing so?

Also, I tried to open your .HMI file and  am getting the error message "Wrong Resource file or resource file has been damaged". It could be my computer, but I did go download the Visual studio files you suggested.

If you feel like the issue is my computer ok, but I am able to open other HMI files and create my own without issue, so I thought it may have something to do with the file?


My main question is how to change the serial output from the display to the arduino. Thanks in advance!
Title: Re: Using Nextion displays with Arduino
Post by: mazellan on Jan 13, 2020, 12:45 am
Hi Perry.

I'm successfully using your method to drive 3 displays. They are set to sleep after 30 seconds and wake on touch. I want to only update one display when it wakes  (It's mainly a clock and temperature display).

On wake, they send :
68 xx xx yy yy 01 FF FF FF  -> touch coordinate
68 xx xx yy yy 00 FF FF FF  -> release coordinate
0x87 0xFF 0xFF 0xFF

They don't register the wake touch as a touch/release event, so I can't use your 8 byte sequence.

Is it possible to integrate reading 0x87 0xFF 0xFF 0xFF  into your read code?
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jan 13, 2020, 09:43 am
Hello Chrisplusian

Quote
I tried to open your .HMI file and  am getting the error message "Wrong Resource file or resource file has been damaged". It could be my computer, but I did go download the Visual studio files you suggested.
As a test I have downloaded 2019-07-27 Arduino Nextion demo 43.zip from this tutorial, extracted the HMI file and opened it with V0.59 of the Nextion editor without difficulty, so I am confident that the file on this web site is not corrupted. The Visual studio files are required for V0.59 of the Nextion editor to run. If you have managed to open the editor then it working. If you can't open the HMI file then the problem is elsewhere, but I don't know what to suggest. I am assuming you extracted it from the zip file before trying to open it and that you are using V0.59 (V0.58 works too).


Quote
"Unlike the displays, I chose not to use 0xff 0xff 0xff as a terminator for the data sent to the Arduino. Instead I use 0xa5 0xa5 0xa5 as a start indicator followed by 5 bytes. Sometimes the last byte or 2 is just padding to bring the total to 5. You can modify the code to expect more or fewer bytes, or use a different start sequence. The 5 bytes are the page number, the type, the index of the type and 2 data bytes."
Everything I send from the Nextion is sent using printh, which sends a single hexadecimal byte, giving me complete control over what is sent. When you manage to open the HMI file take a look at touch release event under the buttons, you will see the code I use to send from the Nextion to the Arduino. Note that in the direction Arduino to Nextion the terminator is 0xff 0xff 0xff as there is no way that I know of to change this.




Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jan 14, 2020, 09:29 pm
Hi Perry.

I'm successfully using your method to drive 3 displays. They are set to sleep after 30 seconds and wake on touch. I want to only update one display when it wakes  (It's mainly a clock and temperature display).

On wake, they send :
68 xx xx yy yy 01 FF FF FF  -> touch coordinate
68 xx xx yy yy 00 FF FF FF  -> release coordinate
0x87 0xFF 0xFF 0xFF

They don't register the wake touch as a touch/release event, so I can't use your 8 byte sequence.

Is it possible to integrate reading 0x87 0xFF 0xFF 0xFF  into your read code?
I've never used sleep in any of my project, so not considered how to use it.
If you look at part 4 of the tutorial (reply #3) there is Nextion_additional_features.zip, download that. In the function HMI_read() (in the tab _01_Middle) I have added code to save Nextion return codes ending with 0xff 0xff 0xff so they can be sent to the serial monitor. You could use this as a way to capture the 'wake' messages and then do whatever you need to with them.

On 2 of my projects I dim the display to a low level if it is not touched for 30 seconds or so, then it goes back to full brightness when touched. I realise this is not the same as sleep, but it does mean I have full control over what happens when the display is touched.
Title: Re: Using Nextion displays with Arduino
Post by: chrisplusian on Jan 15, 2020, 12:56 pm
Hello Chrisplusian
As a test I have downloaded 2019-07-27 Arduino Nextion demo 43.zip from this tutorial, extracted the HMI file and opened it with V0.59 of the Nextion editor without difficulty, so I am confident that the file on this web site is not corrupted. The Visual studio files are required for V0.59 of the Nextion editor to run. If you have managed to open the editor then it working. If you can't open the HMI file then the problem is elsewhere, but I don't know what to suggest. I am assuming you extracted it from the zip file before trying to open it and that you are using V0.59 (V0.58 works too).

Everything I send from the Nextion is sent using printh, which sends a single hexadecimal byte, giving me complete control over what is sent. When you manage to open the HMI file take a look at touch release event under the buttons, you will see the code I use to send from the Nextion to the Arduino. Note that in the direction Arduino to Nextion the terminator is 0xff 0xff 0xff as there is no way that I know of to change this.





I was finally able to open the .HMI screen. I previously had verison 53, and I did the update and apparently it didn't work. I uninstalled and re-installed the nextion editor and for some reason it still installed V53. I used my wifes laptop and installed V59 with no problem. So I couldn't open the .hmi as a result of an older version. Thanks for the tutorial and quick responses!
Title: Re: Using Nextion displays with Arduino
Post by: chrisplusian on Jan 18, 2020, 07:02 pm
I have finally had the chance to sit down with the Code you provided and the HMI. I don't know why, but the display is not actually showing the clock value, however I am able to see the functionality of all the buttons, the slider, and the page concepts. I am guessing it has something to do with the fact I have an Arduino Uno, and am using one of the Basic displays, but I am not positive.

This tutorial has the tools necessary to allow me to develop the application I am creating.

One side note: The Nextion website lists print to be depreciated and it looks like it will add a length attribute, but it seems you can limit the number of bytes sent as well.

Thanks  again for taking the time to put this demo together.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Jan 18, 2020, 07:39 pm
Quote
I don't know why, but the display is not actually showing the clock value, however I am able to see the functionality of all the buttons, the slider, and the page concepts. I am guessing it has something to do with the fact I have an Arduino Uno, and am using one of the Basic displays, but I am not positive.
I would not expect that using a basic display would cause a problem, I was careful to design the code to use the minimum of the resources available on a Nextion and everything in this tutorial has been tested on a NX4827T043, which is one of the basic Nextion displays.

A Uno might be a problem as the only serial port is the one used for the USB connection to your PC, so using it for connection to a Nextion (or anything else) means you have 2 different serial connections fighting each other. I did originally test everything using a Uno and I didn't find any problems, however, given that, theoretically at least, there might be problems I have not stated in the introduction that any of this works with a Uno. For changes to this tutorial I mostly test on a Mega, and sometimes on an MKR1010.

Quote
One side note: The Nextion website lists print to be depreciated and it looks like it will add a length attribute, but it seems you can limit the number of bytes sent as well.
Thanks, I didn't notice that, I appreciate you pointing it out. I have updated the demonstration files to use prints, which I see replaces print. If you find I've missed any please tell me (I think I only used print for the scroll bar, but there's always the possibility I used it elsewhere and forgot!)
++Karma;
Title: Re: Using Nextion displays with Arduino
Post by: DRE50 on Mar 02, 2020, 02:15 am
I have a question? where do i set what RX TX port im using with my mega2560? i cant seem to find the info.

 //This is for serial port 1, which is the one used for the Nextion in this demonstration. You might need to change this depending on which serial port you are using for your display.
  Serial1.begin(9600);

If im using serial port 2 then do i put Serial2.begin(9600) in my code? or am i missing something?
thanks
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 02, 2020, 05:16 pm
If im using serial port 2 then do i put Serial2.begin(9600) in my code? or am i missing something?
thanks
Yes, correct. You also need to go through the code and change every instance of Serial1 to Serial2.
Title: Re: Using Nextion displays with Arduino
Post by: Squid171 on Mar 28, 2020, 06:48 pm
Hello Perry,

I recently tried contacting you about an issue with this project that I am experiencing and you told me to post it on the forum. I have tried that 3 separate times and even after following the the forum guidelines it keeps having errors and not posting so I am just going to post it here.

I am fairly new at using both Arduino and Nextion displays so I tried looking online for tutorials about how other people have made them work together. Most of these tutorials have use of the Nextion library so I have tried multiple uses of that but none of them worked quite right. The board and display would not communicate. So I finally found your demonstration about how to do this without the library and it seemed much more simple and made more sense to me. But since I was frustrated with not getting these devices to communicate together I just used your code (Nextion_demonstration43) and displays for starters so that I could change things around once I everything communicating.

The only difference between your display and mine is that mine is the 10.1" so when I loaded your screens onto it they stayed in the upper left corner. Not an issue when you're just trying t make something work. The code however was slightly changed but only by changing the serial port used like you had said to do. Since my board is an Arduino Uno I do not have any extra ports so I have to use pins 0 and 1. I had seen on other tutorials that people have made this work by loading the sketch to the board with the RX and TX of the HMI disconnected and then unplugging the board from the computer and powering it with an external power supply and reconnecting the RX and TX  to the TX and RX of the board. I did this and most of the program seemed to work like the messages "This is page 0" and "Please do not press this button again", the hex and decimal values for the slider on page 1 even read. But the clock on page 0 remains at "hh: mm: ss" with not number values there.

I guess my question is if there is a different way of using serial communication with my Uno or if I need an Arduino Mega? And are there any errors in my code that I missed? Like I said all I changed were the comm. ports but then it should have worked. Any help would be greatly appreciated.
Title: Re: Using Nextion displays with Arduino
Post by: Squid171 on Mar 28, 2020, 06:54 pm
Sorry this would not send as one piece...

Code: [Select]

void setup() {
  //This is for the serial monitor. Remove the // if you need to use it.
  //Serial.begin(9600);
 
  //This is for serial port 1, which is the one used for the Nextion in this demonstration. You might need to change this depending on which serial port you are using for your display.
  Serial.begin(9600);

  //For  demonstration
  HMI_startup_message();
}

void loop() {
  HMI_read();
  clock_run();
}

struct CLOCK {
  int8_t hour;
  int8_t minute;
  int8_t second;
};
struct CLOCK clock;

//This displays the clock
void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d ", clock.hour, clock.minute, clock.second);
  Serial.println(timestring);
  Serial.print(F("t1.txt=\""));
  Serial.print(timestring);
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

//This displays the page number
void HMI_display_page(uint8_t page) {
  Serial.print(F("t0.txt=\""));
  Serial.print(F("This is page "));
  Serial.print(page);
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

void HMI_P1_display_slider(uint8_t slider_d1, uint8_t slider_d0) {
  uint16_t slider_val = (slider_d1 <<8 | slider_d0);
 
  //This displays byte 1 of the slider value in HEX
  Serial.print(F("t2.txt=\""));
  Serial.print(slider_d1, HEX);
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
 
  //This displays byte 0 of the slider value in HEX
  Serial.print(F("t3.txt=\""));
  Serial.print(slider_d0, HEX);
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  //This displays the complete slider value in decimal
  Serial.print(F("t4.txt=\""));
  Serial.print(slider_val);
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

void HMI_startup_message() {
  Serial.print(F("t0.txt=\""));
  Serial.print(F("Nextion demonstration by Perry Bebbington. For support go to https://forum.arduino.cc/index.php?board=7.0"));
  Serial.print(F("\""));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

//HMI_read takes the data sent from the Nextion to the serial port and processes it depending on what has been sent
//There are 3 levels of nested switch statements corresponding to the page, the type of object and the index of the object.
void HMI_read() {
  static uint8_t HMI_read_data[10];         //This is a temporary buffer to hold the data from the display. Space for 10 bytes although this demonstration only uses 6 bytes
  static uint8_t HMI_read_data_i;           //This is a count of how many bytes have been received from the display.
  static uint8_t a5count;                   //0xa5 repeated 3 times is used as a start indicator, this is a count of how many times it has been received.
  uint8_t readtemp;                         //This is to hold the last received byte to ensure that it is only read from the receive buffer once.
 
  while (Serial.available() > 0) {         //Read every byte in the receive buffer
    readtemp = Serial.read();
    if (readtemp == 0xa5) {                 //Count the number of times 0xa5 has been received
      ++a5count;
      if (a5count > 2) {
        a5count = 0;
        HMI_read_data_i = 0;
      }
    }
    else {
      a5count = 0;
    }
    HMI_read_data[HMI_read_data_i] = readtemp;
    if (HMI_read_data_i == 5) {
      switch (HMI_read_data[1]) {             //HMI_read_data[1] contains the page the data has come from
        case 0:                               //Case 0 means the data has come from page 0
          switch (HMI_read_data[2]) {         //HMI_read_data[2] contains the type of object on the page that the data has come from
            case 0:                           //In this demonstraton case 0 selects a page
              HMI_display_page(HMI_read_data[3]);
              break;
            case 1:                            //In this demonstration case 1 is a button for setting the clock
              switch (HMI_read_data[3]) {      //HMI_read_data[3] is the index of the type of button, so 0 to 5 as there are 6 buttons for setting the clock. Each case is a different button.
                case 0:
                  ++clock.hour;
                  if (clock.hour > 23) {
                    clock.hour = 0;
                  }
                  break;
                case 1:
                  --clock.hour;
                  if (clock.hour < 0) {
                    clock.hour = 23;
                  }
                  break;
                case 2:
                  ++clock.minute;
                  if (clock.minute > 59) {
                    clock.minute = 0;
                  }
                  break;
                case 3:
                  --clock.minute;
                  if (clock.minute < 0) {
                    clock.minute = 59;
                  }
                  break;
                case 4:
                  ++clock.second;
                  if (clock.second > 59) {
                    clock.second = 0;
                  }
                  break;
                case 5:
                  --clock.second;
                  if (clock.second < 0) {
                    clock.second = 59;
                  }
                  break;
              }
              HMI_display_clock();
              break;
          }
          break;
        case 1:                               //Case 1 means the data has come from page 1
          switch (HMI_read_data[2]) {
            case 0:                           //Data from the page itself, this is the post initialisation request to update the page, which displays the page number
              HMI_display_page(HMI_read_data[3]);
              break;
            case 1:                           //Data from the slider on page 1
              HMI_P1_display_slider(HMI_read_data[5], HMI_read_data[4]);      //HMI_read_data[5] is byte 1 of the slider value, HMI_read_data[4] is byte 0 of the slider value
             
             
             
              break;
          }
          break;
      }
    }
    ++HMI_read_data_i;
    if (HMI_read_data_i > 9) {
      HMI_read_data_i = 9;
    }
  }
}

void clock_run() {
  static unsigned long previousMillis;
  unsigned long currentMillis = millis();
  const unsigned long interval = 1000;
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    ++clock.second;
    if (clock.second > 59) {
      clock.second = 0;
      ++clock.minute;
      if (clock.minute > 59) {
        clock.minute = 0;
        ++clock.hour;
        if (clock.hour > 23) {
          clock.hour = 0;
        }
      }
    }
    HMI_display_clock();
  }
}
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 28, 2020, 07:15 pm
Hello,

I would expect you to have problems using a Uno to communicate with a Nextion because the serial port is already in use communicating with the USB connection. The Uno schematic shows 1k Ohm resistors in series with the connection between the ATmega328 and then CH340 that controls the USB connection, so you might be able to get it working on the serial port, but this seems like a bit of a bodge to me. If it works then fine, it works. If not then yes, I suggest buying an Arduino with a spare serial port, such as a Mega. However, if you have some features of my code working that suggests the problem is not the serial port.

Quote
And are there any errors in my code that I missed?
I'm not sure how you expect me to answer that with code I cannot see....!  :smiley-confuse:

You need to find every single instance of Serial1.something and change it to Serial.something. Miss one and it won't work. If you are not seeing the clock then I guess you missed one in the function void HMI_display_clock();


[EDIT] You had not posted your code when I typed the above....
Also, my dinner is ready, back later :)
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 28, 2020, 08:21 pm
In the function:
Code: [Select]
//This displays the clock
void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d ", clock.hour, clock.minute, clock.second);
  Serial.println(timestring);
  Serial.print(F("t1.txt=\""));
  Serial.print(timestring);
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}


Is the line
Code: [Select]
  Serial.println(timestring);
Which sends the time to the serial monitor. When HMI_display_clock is used as intended, with the Nextion on serial port 1, this line doesn't matter. When you connect the Nextion to the serial port monitor then this line makes a mess of the data sent to the Nextion. You need to comment out that line then it will work, like this:

Code: [Select]
//This displays the clock
void HMI_display_clock() {
  char timestring[9];
  sprintf(timestring, "%02d:%02d:%02d ", clock.hour, clock.minute, clock.second);
  // Serial.println(timestring);
  Serial.print(F("t1.txt=\""));
  Serial.print(timestring);
  Serial.print("\"");
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}
Title: Re: Using Nextion displays with Arduino
Post by: Squid171 on Mar 28, 2020, 10:04 pm
Thank you that worked! The clock is now counting and everything appears to be communicating properly. And it appears that the dimming of the screen is 100% controlled by the program in the HMI and has nothing to do with the arduino code... Is that correct?

Thank you again.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 29, 2020, 12:03 am
It appears that the dimming of the screen is 100% controlled by the program in the HMI and has nothing to do with the Arduino code... Is that correct?
Yes, the screen dimming is on the Nextion, I leave you to discover how it works.
Title: Re: Using Nextion displays with Arduino
Post by: PerryBebbington on Mar 29, 2020, 12:04 am
I have now asked the moderators to lock this topic as there are enough questions and answers to cover most things.

Please do not send me a PM with questions about this tutorial or Nextion displays in general, I will just reply telling you to use the forum.

If you have a question about either this tutorial or about your project that includes a Nextion display then read General guidance (https://forum.arduino.cc/index.php?topic=652669.0") and
How to use this forum (https://forum.arduino.cc/index.php?topic=148850.0)
And start a new question in the appropriate forum.