Notifications
Clear all

ESP32 to Nextion HMI Lessons Learned

25 Posts
3 Users
0 Likes
285 Views
(@rebeljd)
Member
Joined: 1 year ago
Posts: 16
Topic starter  

 

I’ve been using ESP32’s on many of my recent projects and decided to incorporate a Nextion HMI into my latest.  I’ve never used a touch screen display before so I did the usual and looked on line for some help.  There was plenty of information using an Arduino but the content I found on using an ESP32 seemed lacking and often useless.  Fortunately the DroneBot Workshop Forum came through again and I found some help that pointed me at the ”Easy Nextion Library”. Thanks to member HuckOhio for the great tip.  I’m not an experienced programmer and I’m sure there are different and perhaps better ways of doing this, but I learned a lot about the Nextion and thought it would be appropriate to share my lessons learned as a small pay back to past and current help from this forum. 

I wanted to use Serial Port 2 on the ESP to interface with the Nextion but I could not get that to work using the Nextion Library.  However, I was able to get it to work with the “Easy Nextion Library” thanks to the fine work by Athanasisos Seitanis on the library.   You can communicate with the device without using a library and I was successful in both sending a receiving data that way, but libraries make things much easier by doing all the heavy lifting for you.  So that is the path I took.

The Nextion, as you likely know, is much more than a display.  It is a smart device that if used to its full capabilities can greatly simplify coding in whatever MCU you are using with it.  It does need to be programmed, compiled and uploaded to the HMI itself using the Nextion Editor.  And there are many additional features and add-ons that I have not tried yet.  The product seems to be growing and improving.

So, what follows is a list of the lessons that I learned while developing my project.   I kept the USB connection between the IDE and the ESP32 and used the SD card to transfer the *.tft file from the Nextion Editor to the HMI.  In the end I’ll provide a simple example of interfacing an ESP32-WROOM 32 Dev Kit with a Nextion NX4832K035 Enhanced 3.5” HMI.  It is a simple application but it does demonstrate many of the features to send data back and forth between the two devices. 

Lessons Learned:

  • Familiarize yourself with the Nextion website and learn about the different versions and sizes of the HMI’s. This is where you can download the Nextion Editor and sample programs.  Be sure to look thru the user’s forum as there is plenty of help and advice there.  There is also a “Sunday Blog” that I found very informative.  Spend time understanding the capabilities of the device as this will allow you to be most efficient with your coding and design.
  • When you write a program with the Nextion Editor it generates an *.HMI file. When you output a file to transfer to the HMI it generates a *.tft file.  You can then use an SD card to load the tft file onto the HMI.  The SD card can contain only the file you are transferring.
  • I keep the USB connection between the Arduino IDE and the ESP32.
  • Since the Nextion a 5v device I assumed the serial port would be 5v TTL but when I looked at the specifications, I learned the “Input High Voltage” (RX) ranged from 2.0 to 5.0v with a typical of 3.3v. So, the ESP32 serial port TTL (TX) voltage of 3.3 should work without a logic level converter, and it does.   And, the Nextion TX has a typical voltage of 3.2 so that too is directly compatible with the ESP32.  I’ve been testing it without level converters for about a week now with no issues.
  • I initialize Serial Port 2 ESP32 in the sketch directly and commented out two lines in the Easy_Nextion_Library.cpp file.

            //_serial->begin(baud);   // We pass the initialization data to .........

           // delay(100);   // Wait for the Serial to initialize

  • When you configure your display, pick the Horizontal 270 layout so the SD card slot is on the top. Assuming of course you are doing a horizontal layout.
  • Generate any font you want to use before you begin developing screens. Font generation in the Editor is an area I think Nextion could improve on, it’s a little cumbersome.
  • Use the Debug and the Compile tools in the Nextion Editor as you develop the program, so you can correct errors as you go.
  • Spend time playing around with the attributes for each object you create and you’ll start to appreciate the various features.

The simple example program I have included uses two buttons on the Nextion screen to increase or decrease a value that is stored in the ESP32.  This value is then sent back to the Nextion and displayed as a number and on a bar graph.  The program in the Nextion will change the color of the bar graph when the value is greater than 165.  I’ve included the sketch file, the HMI file and tft file.  Hopefully someone will benefit from this post.

/*
filenames: ESP32_Nextion.ino
           ESP32NextionSimpleExample.HMI
           ESP32NextionSimpleExample.tft


JDean Rebel Garage  3-28-2024


This is a very simple sketch to demonstrate how to use an ESP32
to communicate with a Nextion HMI using Serial Port 2 on the ESP32.
Two buttons on the Nextion screen are used to increase or decrease a value
that is stored in the ESP32.  This value is then sent back to the Nextion
and displayed as a number and on a bar graph.  The program in the Nextion
will change the color of the bar graph when the value is greater than 165.


The only thing in the loop is the Nextion Listen function which simply
watches for data coming from Nextion.  Two functions are then defined that
respond to the button pushes to increase or decrease the value and
send it back to the Nextion HMI.  Pretty simple.


Hardware:
ESP32-WROOM32 Dev Kit  (38 pin DIP)
Nextion Enhanced 3.5" HMI NX4832K035


This sketch uses the "Easy Nextion Library".  Many thanks to
Athanasios Seitanis for his work on the library.  
**********  Important Note*************
The following 2 lines must be commented out in Easy_Nextion_Library.cpp.
Serial port 2, which is used in the ESP32 to communicate with the Nextion HMI is
instead initialized in the ESP32 sketch, not in the library.
    //_serial->begin(baud);   // We pass the initialization data to .........
    // delay(100);   // Wait for the Serial to initialize


*/
   
// *********  Declarations  **********
#include "EasyNextionLibrary.h" // Include EasyNextionLibrary
EasyNex myNex(Serial2); // Tell Easy Nextion to use Serial Port 2      
int TestValue =150;    


void setup() {
Serial.begin(9600);  // for serial monitor
Serial2.begin(9600);// Baud rate for serial port 2 to Nextion display
}


void loop() {
myNex.NextionListen();  // looks for data from Nextion HMI
}


void trigger0(){
  Serial.println ("Increase Button Pushed");
TestValue=TestValue +1;
myNex.writeNum("TestValue.val",TestValue);
}


void trigger1(){  
  Serial.println ("Decrease Button Pushed");
TestValue=TestValue -1;
myNex.writeNum("TestValue.val",TestValue);
}


/*
************  Nextion program  *******************
in tm0
// The ESP32 sends a number to the Nextion HMI in "TestValue.val"
n0.val=TestValue.val // update the number
j0.val=TestValue.val*100/250   // scale the value for the progress bar.  Progress bars are 0 to 100
// If the value is greater than 200 change the progress bar from green to red.
if(n0.val>165)
{
  j0.pco=RED
}else
{
  j0.pco=1024  //   greeen
}
*/
This topic was modified 4 weeks ago 2 times by RebelJD

   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6972
 

@rebeljd WHOA, modifying a library is a big nono. And the lines you modified are normal so why?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@rebeljd)
Member
Joined: 1 year ago
Posts: 16
Topic starter  

@zander 

based on advise by another user to make the ESP32 port2 work on the Nextion.  It works fine.  I did not try it without modifying the library but I might.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6972
 

@rebeljd You are basically removing the Serial.begin(baud) from one place and sticking it in another. I doubt that does anything useful. What it does do, is prevent you from getting updates to your library since doing so would break it again if it is indeed broken. Have you checked if there is an outstanding issue for this?

It is EXTREMELY rare to find a library bug, I think I have only encounter two.

If it is a problem, I think you can use github to manage it (I know almost nothing about github). Just a guess, but maybe a fork?

I had a look at the documentation the author created and I think maybe either you are setting up the display wrong or the author made a mistake. See attached pic

This release is 2 years old so I find it difficult to believe he has not fixed the bug in that length of time.

The second pic is of the open Issues. I see nothing about a Serial issue.

 

 

Screenshot 2024 03 29 at 14.03.11
Screenshot 2024 03 29 at 14.07.21

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
 

@zander 

Ron, I also had to comment out a line in the EasyNextionLibrary for my project I documented last fall.  This is from my project post:

I used the Easy_Nextion_Library for the displays because it was the easiest for me (based on my limited experience).  I did make one change in the EasyNextionLibrary.cpp file - I commented out the _serial->begin(baud) line (line 24 – see below) and initialized the object in my code.  I was having trouble assigning the Nextion display to Serial2 when I also had to assign the serial pins.  Getting the Nextion display to work with the ESP32S3 was the biggest issue I had in this project. 

The ESP32 needs you to assign your serial pins, something the EasyNextionLibrary does not allow (you may not remember, but you were advising me when I was having this issue).  I commented out the Serial.begin in the library and added it to my code.

For reference, here is the link to my posts.  

 


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180

   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
 

@zander 

One more thing...I did sent an email to the library owner asking to change the library to allowed to assigning serial port pins in the library, but never heard back.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6972
 

@huckohio What is needed is to use github and enter an issue. Given what I see on github this library has not been touched for over a year so either the maintainer has died or is otherwise no longer maintaining the library. If it was me I would use a different library.

I just realized another reason. Library maintainers are bombarded by incorrect reports of errors, and it is simply too time consuming to respond.

Can I see just the one line of code you used in your setup to perform the Serial initialization?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6972
 

@huckohio @rebeljd Show me your 1 line of startup code that initializes the Serial port.

Library authors are inundated with false error reports and do not have time to respond to them. I just tried one of the samples and it is setup for Serial as I expected and compiles clean even at ALL. I can see the error in rebeljd's code and even after I pointed it out I am hearing nothing.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
 

@zander 

Ron, here is the line from the library that I commented out: 

 //_serial->begin(baud);  // We pass the initialization data to the objects (baud rate) default: 9600

And here is the line I used in my Setup:

  // Init Serial2
  Serial2.begin(9600, SERIAL_8N1, RXp2, TXp2);

 


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
 

This is from the declaration section of the code :

EasyNex myNex(Serial2);

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6972
 

@huckohio Ok, if you look at the example sketches they do NOT use Serial.begin, they use MyNex.begin(9600); 

Both you and @rebeljd are using Serial.begin which I believe is incorrect. I base that on the fact that the examples are coded as MyNex.begin(9600), NOT Serial.begin. I am certainly no expert in C++ but I am pretty sure the example sketches are coded correctly. I looked at 6 examples in the Easy_Nextion library and they are all coded this way.

I may very well be wrong, but I am basing my comments not on an expert knowledge of C++ which I do not have, but on pattern recognition, and my experience with open source authors. 

I had a look at the library code, and the following is the constructor

EasyNex::EasyNex(HardwareSerial& serial){ // Constructor's parameter is the Serial we want to use
_serial = &serial;
}

If you did NOT call myNex.begin(9600), then commenting out that code does nothing.

If you do call myNex.begin(9600) then the removed code does exactly what your Serial2.begin does but it does a lot more. Look at the library code if you doubt me.

I urge you to try a test, put the removed library code back, then change your statement of Serial2.begin( to myNex.begin(9600)

Here is the begin code you may be not using if I am understanding you. I am including the constructor as well.

EasyNex::EasyNex(HardwareSerial& serial){ // Constructor's parameter is the Serial we want to use
_serial = &serial;
}

void EasyNex::begin(unsigned long baud){
_serial->begin(baud); // We pass the initialization data to the objects (baud rate) default: 9600

delay(100); // Wait for the Serial to initialize

_tmr1 = millis();
while(_serial->available() > 0){ // Read the Serial until it is empty. This is used to clear Serial buffer
if((millis() - _tmr1) > 400UL){ // Reading... Waiting... But not forever......
break;
}
_serial->read(); // Read and delete bytes
}
}

 

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
 

@Zander

Posted by: @zander

but I am pretty sure the example sketches are coded correctly.

I am sure the examples ARE coded correctly (for the vision of the developer at the time), unless you need to assign the RX & TX pins to a serial port on the ESP32.  The library will not allow you to use the this statement 

Serial2.begin(9600, SERIAL_8N1, RXp2, TXp2);

to assign Serial2 to the GPIO pins. I tried.  

You were trying to help me with this problem last year until you told me that this topic bored you and you stopped.  This was the only way that I found that worked.  My approach was verified some months later when I saw the exact approach in the Nextion forum (but I don't have the link).

I believe the library example uses:  EasyNex myNex(serial);

...and then in setup uses: myNex.begin(9600) 

But I needed Serial2 which required assigning GPIO pins (example above) which the library does not support.

Either way,  what I did works and has been working on three ESP32S3s with NEXTION devices for more than 9 months.

I fail to see the issue with moving the initialization of a serial port (which is typically in setup) from the library back to the program ino file.  Like I said...it worked for me and it seems to have worked for @rebeljd


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6972
 

@huckohio Agreed, but I am a purist and when I was younger would have persued the author to fix his code. All that is required is to add parameters for the additional config with default values. I will raise an issue with the maintainer.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6972
 

@huckohio I don't see the extra args in the docs, for my personal education could you point me at the appropriate doc? I only see baud and 8N1 amongst others, but nothing after that.

Did you consider using Serial for the Nextion then the library could be used as is, and using Serial2 for your other needs?

Now that I more fully understand what is going on I wish I had a display, but they are very confusing. Many are over $100 some even over $500 and the less expensive models under $50 all say they work with old Pi's, no mention of ESP32. Do you have any recommendations?

I am seriously considering modifying the library code and then releasing it as a new better library if I can find a reasonably priced display.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Page 1 / 2