Nextion Intelligent Display & EasyNextionLibrary Trigger() Problem

Good day,

I am having a problem with my current project where the trigger function from the EasyNextionDisplay library is not executing the code within it while specifically using a Nextion Intelligent display.

The display is connected to its' own 5V power supply separate from the Arduino Nano with a shared ground. Using SoftwareSerial I receive data from a external scale via a RS232 connection which is displayed onto the HMI. The data is received parsed and sent to the Nextion without any noticible delay or problems.

I need to enter a "target value" for the scale to reach to trigger another event. I currently have it set up so that the Nextion stores this text as a string in a variable on the Nextion itself. This variable is then sent to the arduino to be converted as a float value to be used for further processing on the Arduino. The Float Value is also formatted to two decimal places and then sent back to the Nextion display as a String. This way when the screen value is updated I know that the value is also on the Arduino.

My problem is that the Nextion Intelligent display does not execute the trigger function when the HMI is connected. When using the debug option on the Nextion Editor the code works as intended.

I also have a Nextion Enhanced display which functions correctly and executes the code within the trigger function. To confirm I loaded the trigger examples from the EasyNextionLibrary to the Intelligent and Enchanced display's with the same results. The Enhanced display executed the code in the trigger function while the Intelligent display does not.

Any guidance to where I should start searching for possible solutions ?

Below is the current Arduino code:

  • It uses trigger3() due to future buttons being allocated for trigger1() & trigger2()
  • I confirmed that all the baud rates are the same, on the scale, arduino and the display.
  • I have previously removed the scaleRead() from the code to eliminate possible data conflicts
/*
  2024/11/10
  Program can read data from the scale
  Program can read target number from Nextion to arduino for convertion and back to Nextion.
  It only works in the debugger & with the Enchanced display
  Trigger function not working on this program with the Intelligent display
*/

#include <SoftwareSerial.h>
#include "EasyNextionLibrary.h"

// --- BAUD RATE ---
long baud = 9600;

// --- SOFTWARE SERIAL ---
SoftwareSerial scaleSerial(3, 4); // RX = pin 3, TX = pin 4

// --- SCALE DATA PARSING VARIABLES ---
char polarity = ' ';
String data = "";
String units = "";
bool readingData = false;
String scaleReceivedWeight = "";
String scaleReveivedUnits = "";

// --- NEXTION SCREEN ---
EasyNex myNex(Serial); // Start Serial for Nextion Screen

// --- CONSTANTS & VARIABLES ---
char buffer[10] = {0};
String tempTarget = "yes";

// Timeout variables
unsigned long lastReceivedTime = 0;
//const unsigned long timeoutDuration = 5000;

bool targetUpdated = false; // Flag to indicate when the target has been updated

// --- SETUP ---
void setup() {
  Serial.begin(baud);
  myNex.begin(baud);
  scaleSerial.begin(baud); // Matches the default baud rate of the scale
  lastReceivedTime = millis(); // Initialize the last received time

  // Display Default Nextion Values
  myNex.NextionListen();
  myNex.writeStr("textTarget.txt", tempTarget);
}

// --- LOOP ---
void loop() {
  scaleRead();  // Continuously read data from the scale
  myNex.NextionListen();  // Non-blocking listen for Nextion input

  // Check if target update is required
  if (targetUpdated) {
    trigger3(); // Process the target update if necessary
    targetUpdated = false; // Reset the flag after processing
  }
}

// --- SCALE READ ---
void scaleRead() {
  // Check if there is any data available to read from the scale
  while (scaleSerial.available()) {
    char c = scaleSerial.read(); // Read a single character from the scale

    if (c == '+' || c == '-') {
      polarity = c;
    } else if (isDigit(c) || c == '.') {
      data += c;
      readingData = true;
    } else if (c == ' ' && readingData) {
      // Ignore spaces once we've started reading data
    } else if (isAlpha(c)) {
      units += c;
    } else if (c == '\r' || c == '\n') {
      displayData(); // End of transmission detected
    }
  }
}

// --- DISPLAY DATA ---
void displayData() {
  scaleReceivedWeight = String(polarity) + data;
  scaleReveivedUnits = units;

  myNex.NextionListen();
  myNex.writeStr("textNumber.txt", scaleReceivedWeight);
  myNex.writeStr("textUnit.txt", units);

  // Reset variables
  polarity = ' ';
  data = "";
  units = "";
  readingData = false;
}

// --- TRIGGER FOR TARGET ENTRY ---
void trigger3() {
  // Read the target value from the Nextion screen, but only do so once per update
  String targetStr = myNex.readStr("targetVariable.txt");

  // Ensure that the target string is valid and has content
  if (targetStr.length() > 0 && targetStr.toFloat() != 0.0) {
    float targetValue = targetStr.toFloat();
    targetValue = round(targetValue * 100.0) / 100.0; // Round to 2 decimal places
    String targetFormatted = String(targetValue, 2); // Convert back to string with 2 decimals

    // Display the formatted target value on the Nextion screen
    myNex.writeStr("textTarget.txt", targetFormatted);
  }
}

I use the EasyNextion library, but my experience is with a Discovery level Nextion, and I don't have experience with Enhanced and Intelligent displays. However, I can find no information in the Nextion documentation which would explain your issue. You indeed have a puzzling issue.

To confirm I loaded the trigger examples from the EasyNextionLibrary to the Intelligent and Enchanced display's with the same results. The Enhanced display executed the code in the trigger function while the Intelligent display does not.

The library trigger examples are all with event triggers in the HMI code, sending the 4 bytes printh 23 02 54 xx to the Arduino. The examples do not call trigger functions directly from the Arduino code as you are doing.

Are you saying that if you code a button release event to call a trigger function, using the standard Easy Nextion techniques, it will not do so with the Intelligent display?

I have only used the trigger functions in response to events on the Nextion, and have never called them from the Arduino code as you are doing. If you do not call the target entry update function a trigger function but instead use a more generic name, do you still see the issue?

// --- TRIGGER FOR TARGET ENTRY ---
//void trigger3() {
void writeTargetValue() {

That is correct. In both cases the RX light on the arduino does light up when press/release event occurs on the HMI so I believe some data is being received and just not processed correctly.

For now I am going to rewrite the code using the Nextion.h library and test to see if this solves the problem. But ideally I would like to avoid this library.

Instead of using the .NextionListen( ) and having the library parse and respond to the incoming message, you could experiment with more generic serial reading code and see what is being sent in each case.

I tried a few methods with my limited knowledge to see if anything is being sent.

With the below code I checked if any bytes were being received with a button press and release event. Is this what you meant by "a more generic serial reading"?

The Enhanced Display returned bytes matching up with button press / release events on the serial monitor and the Nextion editor debugger. Where as the Intelligent Display returned nothing on the serial monitor or the Nextion Editor debugger.

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    byte incomingByte = Serial.read();
    Serial.print(incomingByte, HEX);
  }
}

Makes me think that I possibly might have a faulty display?

Just to make sure I tried two more programs, one using the Nextion library and one using the EasyNextionLibrary. Both with a single button b0 that turns on the built in LED when the button is pressed and turns it off when the button is released. Again the Enchanced display did its' thing and the Intelligent display had no response.

Code using the EasyNextionLibrary:

#include "EasyNextionLibrary.h"

EasyNex myNex(Serial); 

void setup(){
  myNex.begin(9600);
  
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}

void loop(){
  myNex.NextionListen();
}

void trigger0(){
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  if(digitalRead(LED_BUILTIN) == HIGH){
    myNex.writeNum("b0.bco", 2016); 
    myNex.writeStr("b0.txt", "ON");
    
  }else if(digitalRead(LED_BUILTIN) == LOW){
    myNex.writeNum("b0.bco", 63488);
    myNex.writeStr("b0.txt", "OFF");
  }
}

Code using the Nextion library:

#include <Nextion.h>

NexButton b0 = NexButton(0, 1, "b0");

NexTouch *nex_listen_list[] =
{
  &b0,
  NULL
};

void b0PushCallback(void *ptr)
{
  digitalWrite(13, HIGH);
  Serial.write("BUTTON PRESS = LED HIGH");
}

void b0PopCallback(void *ptr)
{
  digitalWrite(13, LOW);
    Serial.write("BUTTON RELEASE = LED LOW");
}

void setup() {
  Serial.begin(9600);
  delay(500);

  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  b0.attachPush(b0PushCallback);
  b0.attachPop(b0PopCallback);
}

void loop(){
  nexLoop(nex_listen_list);
}

@RayLivingston , @PerryBebbington , @Seithan I know you guys are far more knowledgeable than me on this matter. Do you have any insights to where I can look into to solve this problem?

My next step would be to buy another Intelligent display, if that works then problem solved. If not then I am an owner of two displays that I cannot make use of (for now).

Just to cover the basics, I have already checked the below:

  • Hardware connections
  • Ensured a common ground
  • Used a stable external voltage supply
  • Used multiple Arduino Nano's
  • Used 2x different Enhanced displays
  • Created an HMI .tft file from scratch
  • Checked that all the software and libraries are up to date

In both cases the RX light on the arduino does light up when press/release event occurs on the HMI

The Arduino Rx light behaviour previously reported is not consistent with what you are seeing now.

Makes me think that I possibly might have a faulty display?

I would agree. If the touch/release event bytes are not sent over Serial to the Arduino, it is indeed possible that the display is faulty. It's certainly not an issue with the EasyNextionLibrary.

Or else, there is something about the Intelligent Display we don't understand. Is it a capacitive or resistive screen? Is it the same as the Enhanced Display?
Does it possibly require more power than the Enhanced?

When you press the button on the Intelligent Nextion screen does it do the image changes you would expect?

EDIT:
Where do you purchase your displays?

I'm not familiar with the library you are using nor then trigger function you are trying to use.

Having read your post several times I find I don't really understand what you are trying to do, your description seems to be more about how and not so much what.

One of the common things that comes up on this forum from newbies is that they want to use 2 (or more) Arduinos for their project when one would do the job and be much easier to write code for. If I understand you correctly (I'm not sure I do) then you are running code both on a Nano and the Nextion, which is the same level of difficulty as running on 2 Arduinos when one will do. Be very sure you really need to run code on the Nextion before trying to do so.

If you try to use my methods and get stuck I will try to help, but I can't help with the libraries you are trying to use.

Finally, while it is possible to use the hardware serial port on a Uno / Nano for communicating with a Nextion is possible, that it is shared with the serial port adds an unhelpful level of difficulty. I suggest a Mega, which has 3 spare hardware serial ports.

@cattledog Apologies, forgot to mention that this is still true for both displays. The light on the arduino does light up when a button is pressed on the Nextion but with the Intelligent display no data was received at the arduino that I could see.

It is a capacitive screen.
NX4827P043-011C-Y

The 4.3" Intelligent display does require more power (5V/1A) vs the 2.8" Enhanced (5V/0.5A).

All functions seem to work correctly, it dims the screen, changes pages, changes images, save and recall EEPROM values. Nothing to suggest any issues.

These I purchased directly from ITEAD/NEXTION as it was cheaper than to source them locally.

I have logged a ticket with Nextion, perhaps they can give some advise how to further troubleshoot the device.

Thanks for the reply Perry. Apologies for being too vague with the big picture. I am building a feeder that feeds pellets via a motor driven auger onto a scale. The scale communicates to the arduino via a RS232/TLL connection.

I created a keypad on the display with numbers 0-9. These numbers write their values to a text box "textTargetln.txt". Once the user is happy with the number they press a button "ENTER". With the button press the text value is sent to the arduino for processing. The arduino converts the string value to a float value where this value is then used to compare to the information received and drive the appropriate response for the motor to feed the pellets. I've attached a image showing the interface.

I hope I understood correctly what you were asking.
I

Oh I've done that as well before! But you make a good point about the Nextion being a second arduino, never though about it like that

Only basic instructions like hiding/ showing text or disabling buttons etc. The only code I have currently is for the numbers when enters and to clear a certain textbox, but I guess it's code none the less.

textTargetIn.txt=textTargetIn.txt+"0" //"Each button has a similar code with "1", "2" etc for the number button it represents

AND

textTargetIn.txt=textTargetIn.txt-20

Thank you, I am aware of your method an I have read through your tutorials few times but unfortunately I have yet to understand the idea behind it. My basic knowledge of programming language still has a lot of room for improvement. Will need to commit some time to it.

I do not own one yet but I am considering using one for development purposes as some extra serial port would be great for debugging!

I would try using an oscilloscope or logic analyser on the serial line.

Another idea is to use a TTL to USB adaptor on the the serial output from the Nextion and receive that with a terminal program on a PC. You should be able to monitor the hex bytes sent with the button event.

I contacted Nextion and they said the following:

Ensure that the Intelligent Series Serial voltages are correct.

"Arduino" is no longer ubiquitous with 5.0V AVR UNO/MEGA
so I am making an educated guess.

I assume that your MCU is perhaps 3.3V without 5V tolerance on its UART/pins

Unlike the Enhanced displays where the used Nextion side MCU is 3.3V
and RX 3.3V, TX 3.3V (with 5V Tolerant pins on Nextion MCU side)
The Intelligent Series is RX 3.3V, TX 5.0V (with 5V Tolerant pins)

Therefore, if I use an MCU (project side) that is 3.3V without 5V Tolerance
(such as an ESP32) then it can damage the MCU side RX pin or entire port when Nextion side TX is at 5.0V and MCU max tolerance is 3.3V. If such a case, then I would use a bidirectional level shifter.

---------------

The other possible for MCU TX to function with Nextion RX but not
for the Nextion side MCU TX to your MCU RX would be a continuity issue:
- ie: fractured wire, broken wire, damaged solder

As one direction of transmission is functioning, then the likelihood of it being any mismatched baudrate is greatly reduced.

---------------

Proper testing would be to use a Logic Analyzer or a DSO Oscilloscope
- the oscilloscope would show voltages without guessing.
- the Logic Analyzer would show exactly what is on the wire and when it is.
Logic Analyzers capable of Nextion's fastest Serial of 921600 baud can be had
for as inexpensively as $12.00 USD (Saelee compatible 24Hz clone) and is
and invaluable tool to remove any guessing and allowing you to KNOW.

---------------

Technical about TTL Serial voltages:

When transmission is IDLE (no bytes moving) and connected, the line is HIGH
- if fractured wire or no continuity, then IDLE can not be HIGH when not connected.
0.0 to 0.8V is deemed as LOW for a Logic 0
2.5V to 5.0V is deemed as HIGH for a Logic 1
** Some documentation states 2.8V to 5.0V as HIGH
Voltages that are between LOW (0.8V) and HIGH (2.5V) are INDETERMINATE.

Indeterminate voltages would be ignored / discarded, neither LOW nor HIGH.

------------------

NOW, what COULD happen is
if you transposed RX-TX and TX-RX connection with RX-RX and TX-TX
and a point where BOTH the Nextion and MCU transmit in a TX-TX connection.

NO UART module is designed to sink inbound current through an outbound pin.

If such were to occur it can weaken or even damage the TX pin (both sides).
A pin when using an Internal UART is generally using internal resistors
But such sinking of input current CAN break the internal resistors
IF this were to occur THEN:
- this could show up as in a floating Z state without the internal resistor able to keep high
- such might be able to be held high with an external resistor and continue to function.

===========

Faulty from factory?
- we go through dual (2x through) QA testing to ensure that displays from the factory are upholding the Nextion brand quality before shipping out.
- such would NOT be missed in such QA testing if just once, certainly not twice.

Had you purchased from an eBay Reseller, then we indeed can not control the
business practices of another, and I have indeed heard of some resellers that
have sold "as-if-new" someone else's problematic-returned.

Purchasing from Nextion (itead.cc webstore) directly means that you are receiving NEW-in-BOX.

I hope these help discover your issue and helps resolve.

In the meantime I purchased another 4.3" Intelligent display from a local reseller and this one behaves exactly the same. Which most likely eliminates any hardware issues on the Nextion side.

I am looking into if there isn't some sort of startup / initialization command that I missed that must be added to the Intelligent series keeping it from sending data.

The Nextion support said pretty much what has been discussed before.

I think you really are going to need an oscilloscope to see what is coming from the Nextion. There have been other threads about issues with Intelligent Series Nextions with an ESP32 where the Rx voltage levels were not pulled low by the Nextion due to some resistor chain on the Intelligent displays.

EDIT: Do you have a ttl to usb converter? Did you try it with a terminal program? With the EasyNextionLibrary code posted in reply#5 you can see the hex code from the trigger0 printh 23 02 54 00 on the pc. The interface hardware of the converter is going to be different than the software serial uart on the Arduino and it may respond differently and pass the signals.

Other than the oscilloscope, I would recommend getting an Arduino Mega or Nano Every, so that you have a 5v Arduino with an independent hardware Serial port. It may not help solve the current problem where the incoming signals can't be read.

Hi @stefaans5111
Have you tried using other pins? I found my USB to UART converter on the dev boards I used was causing issues with the intelligent displays specifically.

Edit: I just saw that the nano only has a single hardware UART. You can maybe try it over softwareSerial to test if that solves your issue. You might have issues trying to run 2 instances of softwareSerial simultaneously (or not?) I'm unfortunately not that familiar with it. Maybe try just isolating the display with a simple sketch and see if you're getting your values first.