I'm sorry for the late reply. It was not possible to answer.
@/dev, your answers are logical and perfect but there are a couple of points.
/dev:
Right. Then I don't think you're having a serial problem.I would suggest starting with a simpler program, maybe just one button.
Check return values to make sure each call is successful. For example, you don't check the return value for
nexInit().
I checked Serial port of a simple program:
#include "Nextion.h"
NexButton b0 = NexButton(0, 2, "b0");
NexText t0 = NexText(0, 8, "t0");
char buffer[100] = {0};
NexTouch *nex_listen_list[] =
{
&b0,
NULL
};
void b0PopCallback(void *ptr)
{
uint16_t len;
uint16_t number;
NexButton *btn = (NexButton *)ptr;
dbSerialPrintln("b0PopCallback");
dbSerialPrint("ptr=");
dbSerialPrintln((uint32_t)ptr);
memset(buffer, 0, sizeof(buffer));
/* Get the text value of button component [the value is string type]. */
btn->getText(buffer, sizeof(buffer));
number = atoi(buffer);
number += 1;
memset(buffer, 0, sizeof(buffer));
itoa(number, buffer, 10);
/* Set the text value of button component [the value is string type]. */
btn->setText(buffer);
}
void setup(void)
{
nexInit();
b0.attachPop(b0PopCallback, &b0);
}
void loop(void)
{
//t0.setText("sdfsfs");
delay(2000);
nexLoop(nex_listen_list);
}
Even with a delay in the loop all working perfectly. But if uncommented sending text t0 appear serious problems as before (We need very often to press the button to be worked).
As I wrote, the problem in full duplex, but not in the delay (as I thought initially).
Serial can not operate in full duplex, or I'm doing something wrong.
Accordingly, your library would fit perfectly, but the display does not work with it. ![]()
/dev:
And you should callGetWeightonly at certain times, not every loop iteration:
A good idea. I already wrote about this trick. But I have done a little differently:
void loop()
{
nexLoop(nex_listen_list);
if (t == 10000)
{
weight = Get_Weight(); //get weight
memset(buffer, 0, sizeof(buffer)); //clear buffer
itoa(weight, buffer, 10); //write weight in buffer
t0.setText(buffer);
t = 0;
}
t++;
}
/dev:
Also note that the text elements are updated only when the weight is measured. I assume the Nextion display will redraw them if they are visible. I don't think you need to set them again in loop.
I thought about this, too. I probably will. The text should be sent to the output only when it has changed. Then the problems with full-duplex should fall.
/dev:
Do the same thing for buttons that change a displayed value:
I already did yesterday, works well:
uint32_t pump_state_show = 0; //for a one-off show pump status (button)
uint32_t heater_state_show = 0; //disposable heater display state (button)
...
void bt00PopCallback(void *ptr)
{
uint32_t dual_state;
NexDSButton *btn = (NexDSButton *)ptr;
bt00.getValue(&dual_state);
if(dual_state)
{
//start pump
analogWrite(ENA, 255);
pump_state = TRUE;
pump_state_show = 1;
}
else
{
analogWrite(ENA, 0);
pump_state = FALSE;
pump_state_show = 1;
}
}
...
void loop()
{
nexLoop(nex_listen_list);
Serial.println(readTemperature(SensorWater));
switch(page)
{
case 0:
if (pump_state && pump_state_show)
{
pump_state_show = 0;
bt00.setValue(pump_state);
}
break;
...
Pump button is on the page 0, so each button, returns to the page 0, updates the value pump_state_show = 1, because the default value of dual_state is FALSE in Nextion.
Also attached draft screenshots for a better understanding.
Thanks,
Marat





