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)