Nextion the serial is always busy

hi guys
I have a nextion panel connected to my arduino mega.
In My project there is a page where I show the temperature and humidity from the DHT22 sensor.
There is also a Dual State Button that turns on and off the output13.
Finally there is a text thats shows me if the Input12 is on or off.

It works more or less... But it looks like the serial is always too busy.
Updating the temperature and umidity values or the status of the input or when I push the button. Sometimes it could happen all together and I lose something.
for example the input is on but the text shows me OFF.
or the dualstate button is ON but the led is OFF.
I modified the code and now I update the values only when they change but anyway sometime they could change all together and the issue appears.

Is there a way to improve my code somehow?
I attach the HMI file
this is my arduino code:

#include "Nextion.h"
#include "DHT.h"
DHT dht(8,DHT22); //Definisco il pin al quale è collegato il sensore e il tipo
/*
 * Declare a dual state button object [page id:0,component id:1, component name: "bt0"]. 
 */
NexProgressBar j0 = NexProgressBar(0, 4, "j0");
NexText txt_temp  = NexText(0, 2, "t0");
NexText txt_umid  = NexText(0, 3, "t2");
NexDSButton bt0 = NexDSButton(0, 5, "bt0");
NexText t1 = NexText(0, 8, "t1");

char buffer[100] = {0};
char buffer2[100] = {0};
char buffer3[100] = {0};
int temp = 0;
int umid = 0;
int tempMemory = 0;
int umidMemory = 0;
bool Input12Memory = 0;

/*
 * Register a dual state button object to the touch event list.  
 */
NexTouch *nex_listen_list[] = 
{
    &bt0,
    NULL
};

void setup(void)
{    
    //Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
    dht.begin();
    pinMode(12, INPUT);
    pinMode(13, OUTPUT);
    nexInit();
    bt0.attachPop(bt0PopCallback, &bt0);

    tempMemory = temp;
    umidMemory = umid;

    if (digitalRead(12))
    {
       Input12Memory = HIGH;
    }
}



void bt0PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    dbSerialPrintln("b0PopCallback");
    dbSerialPrint("ptr=");
    dbSerialPrintln((uint32_t)ptr); 
    //memset(buffer, 0, sizeof(buffer));

    /* Get the state value of dual state button component . */
    bt0.getValue(&dual_state);
    if(dual_state) 
    {
        digitalWrite(13, HIGH);
    }
    else
    {
        digitalWrite(13, LOW);
    }
}


void DthSensor()
{ 

    //Conversion of integer values to string
    memset(buffer2, 0, sizeof(buffer2));
    itoa(temp, buffer2, 10);
    memset(buffer3, 0, sizeof(buffer3));
    itoa(umid, buffer3, 10);

     // Updates Nextion values
    j0.setValue(temp);
    txt_temp.setText(buffer2);
    txt_umid.setText(buffer3);
}


void loop(void)
{   
    nexLoop(nex_listen_list);
    // DHT sensor reading
    temp = dht.readTemperature();
    umid = dht.readHumidity();
  
    if (temp != tempMemory || umid != umidMemory)
    {
       DthSensor(); 
       tempMemory = temp;
       umidMemory = umid;
    } 

    if (digitalRead(12) != Input12Memory)
    {
        if (digitalRead(12))
        {
        t1.setText("ON");
         }else 
         {
         t1.setText("OFF");
         }
    Input12Memory = !Input12Memory;
    }
}

Thanks

Temp_Arduino_2.zip (131 KB)

Hello again robotronico!

Did you get your previous problem fixed? Did Itead supply a new display? Nextion RTC problem

Support for Nextion on these fora is pretty much as follows:
You can follow the methods I set out in using Nextion displays with Arduino. My methods do not use the Nextion libraries, so while I am happy to offer help on using my methods I cannot offer anything very helpful for any Nextion library.
The original Nextion libraries are full of bugs. There is a link from my tutorial to some improved Nextion libraries created by Ray Livingston, I suggest those might be worth trying if you prefer to use the official Nextion library.
There's also a separate Easy Nextion Library by Seithan, his methods are different to mine, choose which works best for you.
Beyond that the odd person occasionally offers a bit of help but not much.

As to the specific cause of your problem I think, but I am not certain, that the official libraries work by continuously querying the display about it's status and the status of each object on the page. If I am correct in that then it's the reason the serial port is always active and it is, in my opinion, crap (English word for something vary badly done).

Hi PerryBebbington.
Yes theu are sending me another panel. I mean I didn't solve anything for now but I'm pretty sure it was an hardware problem.

Ok for the libraries I didn't know I have got options :slight_smile:
I'll check and I'll decide what is the best for me.
thanks for your answer and yes I agree... crap is the right word :grinning: