Good day,
I am having issues with a Nextion screen hooked up to a ESP32 Wroom. I’ve got the screen connected to hardware serial 2. I can recieve data from the screen and am able to use the data in my coding. I can also print/write to the screen and update display values.
I am busy with the software of a cut to length machine. This machine de-coils thin steel plate coils and measures the distance de-coiled by means of an encoder. The user enters a distance on the screen which is send to the ESP for processing. There are other user define input on the screen as well that are updated, either by pressing a button or upon exiting the page, like feed rate etc, and the distance uncoiled is updated back to the Nextion display.
The problem I am stuck with is, if I use these functions simultaneously, the data from the screen to the ESP stops flowing if you flip through the pages. If I comment out the Update function, the data flows.
I checked it with my scope, if the update command is active, the screen sends out data without any intervention from the user at random intervals.
Below is the data bit that comes through, I can not read it, maybe you might find a clue in it.
WhatsApp Image 2026-01-04 at 11.25.20 (1)960×1280 127 KB
This is one of the intervals at which it happens, but as stated, they are random.
WhatsApp Image 2026-01-04 at 11.29.46720×1280 135 KB
I am adding my Main code:
#include "Adafruit_SHT31.h"
#include <Wire.h>
#define ClockPin 14
#define DataPin 12
volatile long EncoderCounter = 0;
volatile float SpeedInRPM = 0;
/*-----------------CONTROLS----------------------*/
//-----------------Feeder------------------------
//#define Speed //Analog out for speed
#define OutForward 26 //Digital pin for direction forward
#define OutReverse 27 //Digital pin for direction reverse
#define InForward 35 //Digital pin for direction forward
#define InReverse 32 //Digital pin for direction reverse
#define InStartStop 34 //Digital pin for direction start/stop
//------------------Cutter-----------------------
# define CutterPin 2 //Pin to activate cutter
# define BladeHome 33 //Home sensor of the blade pin
//------------------Recoiler---------------------
# define Actuate 4 //Decoiler open and close pin
/*-----------------Calculations------------------*/
//-----------------Length to go and speeds-------------------
int Length = 0;
float Feed;
float Circum = 556.48;
long Counter;
int RunSpeed = 255;
/*------------------Nextion--------------------------------*/
HardwareSerial mySerial(2);
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
//Data from Nextion
int SetLength;
int SetBatch;
float RunPer = 0.50;
float InchPer = 0.10;
float CrawlPer = 0.05;
int InchDis = 500;
int CrawlDis = 100;
int SetLenCal = 1000;
int ActLenCal;
int PulsePerRev = 1000;
float MMPerPulse;
//Data to Nextion
float LUpdate = 0;
int BUpdate = 0;
int AdjustPer;
int AdjustMM;
float StepsPerMM;
float MMPerRev;
union{
char charByte[4];long valLong;
}value;
String dfd = "";
void ARDUINO_ISR_ATTR onPin2CHANGECallBackFunction(){
bool DataPinVal = digitalRead(DataPin);
EncoderCounter += (DataPinVal) ? 1 : -1; // Should we step up or down?
Counter = EncoderCounter;
}
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(9600);
Wire.begin(21,19);
pinMode(ClockPin, INPUT);
pinMode(DataPin, INPUT);
attachInterrupt(ClockPin, onPin2CHANGECallBackFunction, RISING);
while(!Serial){
;
}
Serial.println("Goodnight moon!");
mySerial.begin(9600, SERIAL_8N1, 17, 16);
if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
Serial.println("Couldn't find SHT31");
}
}
void loop() { // run over and over
if (mySerial.available()) {
dfd+=char(mySerial.read());
NexRead();
}
if (millis() - previousMillis >= interval) {
previousMillis = millis();
I2CRead();
ScreenUpdate();
//noInterrupts ();
//Counter = EncoderCounter;
//interrupts ();
}
}
void SerialUpdate(){
static unsigned long SpamTimer;
if ( (unsigned long)(millis() - SpamTimer) >= interval) {
SpamTimer = millis();
Serial.print(Length ,2);
Serial.print(" mm ");
Serial.print(Feed , 1);
Serial.print(" m/Min ");
Serial.print(Counter);
//Serial.print(" m/Min");
SpeedInRPM = 0; // if no pulses occure in the next 100 miliseconds then we must assume that the motor has stopped
}
}
void I2CRead(){
float t = sht31.readTemperature();
float h = sht31.readHumidity();
if (! isnan(t)) { // check if 'is not a number'
//Serial.print("Temp *C = "); Serial.println(t);
}
else {
t=0.0;
Serial.println("Failed to read temperature");
}
if (! isnan(h)) { // check if 'is not a number'
//Serial.print("Hum. % = "); Serial.println(h);
}
else {
h=0.0;
Serial.println("Failed to read humidity");
}
Length = t*100;
BUpdate = h*100;
}
Here is the update function:
void ScreenUpdate(){
//if((millis() - SpamTimer) >= (250)) {
//SpamTimer = millis();
mySerial.print("LUpdate.val="+ String(Length));
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
//Serial.print(" LUpdate-");Serial.print(Length);
mySerial.print("BUpdate.val="+ String(BUpdate));
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
//Serial.print(" BUpdate-");Serial.print(BUpdate);
/*mySerial.print("SetBatch.val="+ String(SetBatch));
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
Serial.print(" SetBatch-");Serial.print(SetBatch);
mySerial.print("AdjustPer.val="+ String(AdjustPer*100)); //for HMI screen float
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
Serial.print(" AdjustPer-");Serial.println(AdjustPer);
mySerial.print("n0.val="+ String(Length));
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
Serial.print(" n0-");Serial.print(Length);*/
}
Here is the reading function:
void NexRead(){
if((dfd.substring(0,3)=="L&Q") && (dfd.length()==11)){ //Length and Quantity
Serial.println(dfd);
value.charByte[0] = char(dfd[3]);
value.charByte[1] = char(dfd[4]);
value.charByte[2] = char(dfd[5]);
value.charByte[3] = char(dfd[6]);
SetLength = (value.valLong); //Set Length
Serial.print(" SetLength-");Serial.println(SetLength);
value.charByte[0] = char(dfd[7]);
value.charByte[1] = char(dfd[8]);
value.charByte[2] = char(dfd[9]);
value.charByte[3] = char(dfd[10]);
SetBatch = (value.valLong); //Set Batch
Serial.print(" SetBatch-");Serial.println(SetBatch);
Serial.flush();
dfd="";
}
if((dfd.substring(0,3)=="S&D") && (dfd.length()==23)){ //*************Speeds and distances***************
Serial.println(dfd);
value.charByte[0] = char(dfd[3]);
value.charByte[1] = char(dfd[4]);
value.charByte[2] = char(dfd[5]);
value.charByte[3] = char(dfd[6]);
RunPer = value.valLong; //Run percentage
Serial.print(" RunPer-");Serial.println(RunPer);
value.charByte[0] = char(dfd[7]);
value.charByte[1] = char(dfd[8]);
value.charByte[2] = char(dfd[9]);
value.charByte[3] = char(dfd[10]);
CrawlPer = value.valLong; //Crawl percentage
Serial.print(" CrawlPer-");Serial.println(CrawlPer);
value.charByte[0] = char(dfd[11]);
value.charByte[1] = char(dfd[12]);
value.charByte[2] = char(dfd[13]);
value.charByte[3] = char(dfd[14]);
InchPer = value.valLong; //Inch percentage
Serial.print(" InchPer-");Serial.println(InchPer);
value.charByte[0] = char(dfd[15]);
value.charByte[1] = char(dfd[16]);
value.charByte[2] = char(dfd[17]);
value.charByte[3] = char(dfd[18]);
InchDis = value.valLong; //Inch distance
Serial.print(" InchDis-");Serial.println(InchDis);
value.charByte[0] = char(dfd[19]);
value.charByte[1] = char(dfd[20]);
value.charByte[2] = char(dfd[21]);
value.charByte[3] = char(dfd[22]);
CrawlDis = value.valLong; //Crawl distance
Serial.print(" CrawlDis-");Serial.println(CrawlDis);
Serial.flush();
dfd="";
}
if((dfd.substring(0,3)=="CAL") && (dfd.length()==11)){ //***************Calibration********************
Serial.println(dfd);
value.charByte[0] = char(dfd[3]);
value.charByte[1] = char(dfd[4]);
value.charByte[2] = char(dfd[5]);
value.charByte[3] = char(dfd[6]);
SetLenCal = value.valLong; //Length of test run
Serial.print(" SetLenCal-");Serial.println(SetLenCal);
value.charByte[0] = char(dfd[7]);
value.charByte[1] = char(dfd[8]);
value.charByte[2] = char(dfd[9]);
value.charByte[3] = char(dfd[10]);
ActLenCal = value.valLong; //Actual length of test
Serial.print(" ActLenCal-");Serial.println(ActLenCal);
Serial.flush();
dfd="";
}
if((dfd.substring(0,3)=="P&D") && (dfd.length()==11)){ //*********************Pulse and distance***************
Serial.println(dfd);
value.charByte[0] = char(dfd[3]);
value.charByte[1] = char(dfd[4]);
value.charByte[2] = char(dfd[5]);
value.charByte[3] = char(dfd[6]);
PulsePerRev = value.valLong; //PPR of the encoder
Serial.print(" PulsePerRev-");Serial.println(PulsePerRev);
value.charByte[0] = char(dfd[7]);
value.charByte[1] = char(dfd[8]);
value.charByte[2] = char(dfd[9]);
value.charByte[3] = char(dfd[10]);
MMPerPulse = (value.valLong)/100;
Serial.print(" MMPerPulse-");Serial.println(MMPerPulse);
Serial.flush();
dfd="";
}
if((dfd.substring(0,3)=="CPR") && (dfd.length()==3)){
Serial.println(dfd);
//Calibration();
Serial.flush();
dfd="";
}
}
If would really appreciate your help as I am not sure where to post on the forum and where to look for other help.
Kindest regards
Charles de Bruin

