Skip to main content
1 of 3

To control the relay switching in nextion using esp32 Microcontroller

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 need to switch on/off the relay on a single tap on the dual sate button on 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.