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);
}
}
