To Display the Temperature and Humidity in Nextion Display

Hi All,
I am currently working on a project to show the temperature and humidity of a room from DHT22 Sensor and ESP32 Microcontroller. I am struggling to make the temperature readings to show on the nextion display. I would like the readings to be updated on the nextion display automatically without any user input. I got the temperature and Humidity values in the Serial Monitor window in Arduino IDE. Now I want to send the received temperature and Humidity readings which I see on the serial monitor on Arduino IDE to the nextion display in text format.

I have tried looking at the examples on forums but I had no luck with getting the values to be sent to the nextion display. If anyone can recommend a simple solution to just send the received temperature values to the nextion display I will be very happy as this is the last thing I'm stuck at.

Below I have attached my code for this project.

#include "Nextion.h"
#include "DHT.h"

#define DHTPIN 27
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);


NexText tTempC = NexText(0, 4, "tTempC");
NexText tHumidity = NexText(0, 6, "tHumidity");


NexTouch *nex_listen_list[] = 
{
   
   &tTempC,
   &tHumidity,
   NULL
};

void setup(void) {
  dht.begin();
  Serial.begin(115200);
  
  
  Serial.println("DHT Sensor Reading");
  dbSerialPrintln("setup done"); 
}

void loop() {
 delay(2000);
 float t = dht.readTemperature();
 float h = dht.readHumidity();
 if (isnan(h) || isnan(t)) {
   Serial.println(F("Failed to read from DHT sensor!"));
   return;
 }

 Serial.print(F("Humidity: "));
 Serial.print(h);
 Serial.print(F("%  Temperature: "));
 Serial.print(t);
 Serial.print("°C ");

 static char temperatureCTemp[6];
 dtostrf(t, 6, 2, temperatureCTemp);
 tTempC.setText(temperatureCTemp);

 char hTemp[10] = {0}; 
 utoa(int(h), hTemp, 10);
 tHumidity.setText(hTemp);

}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

ArduinoTempMonitor1.ino (1010 Bytes)

Hello Abhishekh12,
Welcome to the Arduno fora.

I have tried looking at the examples on forums but I had no luck with getting the values to be sent to the Nextion display.

I'm struggling to accept that statement as accurate because there is a tutorial at the top of the Display's forum 'using Nextion displays with Arduino'. There is enough information in there to enable you to have a good try at doing this and probably succeed. There's certainly enough for you to come here, post your effort and ask for help.

Please can you also read:
General guidance
And
How to use this forum
Especially item #7 on posting code, then go back and modify your original post using code tags for your code.

Please read the tutorial, understand how to send things to a Nextion, then if you get stuck ask for help.

Please also note that 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 I can offer help on using my methods but 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 to some improved Nextion libraries created by Ray Livingston, I suggest those might be worth trying if you prefer to use a library.

There's also a guy on here called Seithan who has also developed his own ways of dealing with Nextion displays, you can find his methods in his tutorial on how to write code with Nextion and Arduino.

Beyond that the odd person occasionally offers a bit of help but not much.

[EDIT] - I have just see that you have posted at the end of the tutorial, so I guess you've seen it already. That doesn't really change my response above, there is enough in the tutorial for you to at least attempt to do this. Nothing in your question suggests you've tried.

I've deleted your other cross-post @Abhishekh12.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Please don't hijack threads.

Repeated hijacking will result in a suspension of your forum account.

Thanks in advance for your cooperation.

Hi All,
I have developed the code to control the relay switching on/off using nextion display and esp32. But whenever I press my dual state button in the nextion display there is some delay and I need to press my dual state button continuously in nextion display for about 10 to 15 seconds to switch on/off the relay.

I am stuck in this work and I 'm looking for help. I nned to switch on/off the relay on single tap on dual sate button in nextion display. Below I have attached my code,

#include "NexDualStateButton.h"
#include "Nextion.h"

#include "DHT.h"


#define DHTPIN 27
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
int R1 = 2;
uint32_t dual_state ;



NexText tTempC = NexText(0, 4, "tTempC");
NexText tHumidity = NexText(0, 6, "tHumidity");
NexDSButton bt0 = NexDSButton(0, 8, "bt0");

char buffer[100] = {0};

NexTouch *nex_listen_list[] =
{
   &bt0,
   NULL
};

void bt0PopCallback(void *ptr)
{
   uint32_t dual_state;
   NexDSButton *btn = (NexDSButton *)ptr;
  
   dbSerialPrintln("bt0PopCallback");
   dbSerialPrint("ptr=");

   dbSerialPrintln((uint32_t)ptr);
   memset(buffer, 0, sizeof(buffer));
  
 bt0.getValue(&dual_state);

 if(dual_state>0)
 {
   digitalWrite(R1, LOW);
 }
 else
 {
   digitalWrite(R1, HIGH);
   }

}
void setup(void) {
  dht.begin();
  Serial.begin(9600);
  pinMode(R1, OUTPUT);


  nexInit();
  bt0.attachPop(bt0PopCallback, &bt0);
  
  
  
  Serial.println("DHT Sensor Reading");
  dbSerialPrintln("setup done");
  
}

void loop(void) {

 nexLoop(nex_listen_list);
 float t = dht.readTemperature();

 float h = dht.readHumidity();



 if (isnan(h) || isnan(t)) {
   Serial2.println(F("Failed to read from DHT sensor!"));
   return;
 }

 static char temperatureCTemp[10];
 dtostrf(t, 6, 2, temperatureCTemp);
 tTempC.setText(temperatureCTemp);

 char hTemp[10] = {0};
 utoa(int(h), hTemp, 10);
 tHumidity.setText(hTemp);


 Serial.print(F("Humidity: "));
 Serial.print(h);
 Serial.print(F("%  Temperature: "));
 Serial.print(t);
 Serial.print("°C ");
 //Serial2.print(F("tTempC.txt="));

 //Serial.print("");
 //Serial.print(F("tHumidity.txt:\ "));
 //Serial.print(h);
 //Serial.write(0xff);
 //Serial.write(0xff);
 //Serial.write(0xff);

}

Thanks in Advance.

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

ArduinoTempMonitor1.ino (1.83 KB)

Hello Abhishekh12,

In response to the PM you sent me I asked you to read the forum instructions, especially the bit about posting code. Clearly you have not read them or are ignoring them, as you have not used code tags. You are more likely to get friendly, helpful answers if you follow the forum instructions.

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 I can offer help on using my methods but 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 to some improved Nextion libraries created by Ray Livingston, I suggest those might be worth trying if you prefer to use a library.
There's also a guy on here called Seithan who has also developed his own ways of dealing with Nextion displays, you can find his methods in his tutorial on how to write code with Nextion and Arduino.
Beyond that the odd person occasionally offers a bit of help but not much.

@Abhishekh12, stop cross-posting. Do not hijack. Last warning.