-2

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.

VE7JRO
  • 2,515
  • 19
  • 27
  • 29

3 Answers3

2

I think this method inefficient. Because ESP32 continuously listens to the button state.

You can try this method . Working for me .

    int R1 = 2;
    NexDSButton bt0 = NexDSButton(0, 8, "bt0");
    uint32_t dual_state=0;
void setup(){
nexInit();
pinMode(R1, OUTPUT);
}
void loop(){
bt0.getValue(&dual_state);

if(dual_state==1) //When pressed dual state button dual_state =1
  {
    digitalWrite(R1, HIGH);
  }
  else if(dual_state==0)
  {
    digitalWrite(R1,LOW ); 
  }

}

Rohit Gupta
  • 618
  • 2
  • 5
  • 18
Alper Aslan
  • 29
  • 1
  • 6
0

This is my updated code to display the temperature and humidity in Nextion display and also to control the relay switching in Nextion display. It works good.

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

#define DHTPIN 27
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

int R1 = 2;
NexDSButton bt0 = NexDSButton(0, 8, "bt0");
NexText tTempC = NexText(0, 4, "tTempC");
NexText tHumidity = NexText(0, 6, "tHumidity");
uint32_t dual_state=0;

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

void loop(){
  bt0.getValue(&dual_state);

  if(dual_state==1) //When pressed dual state button dual_state =1
  {
    digitalWrite(R1, HIGH);
  }
  else if(dual_state==0)
  {
    digitalWrite(R1,LOW ); 
  }
  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 ");

}
VE7JRO
  • 2,515
  • 19
  • 27
  • 29
0

There is a simpler way to print the temperature to the display and call the BT button without using the Nextion Library. In this example, I am using a TCA9548A because I am using 2 BMP280s.

void TEMP() {
  delay(125);
  TCA9548A(0);
  HMISerial10.print("t8.txt=\"");
  HMISerial10.print((bmp.readTemperature() * 1.8) + 32);
  HMISerial10.print("\"");
  HMISerial10.write(0xff);
  HMISerial10.write(0xff);
  HMISerial10.write(0xff);
  Serial.print("Inside Temp: ");
  Serial.println((bmp.readTemperature() * 1.8) + 32);

TCA9548A(1); HMISerial10.print("t7.txt=""); HMISerial10.print((bmp.readTemperature() * 1.8) + 32); HMISerial10.print("""); HMISerial10.write(0xff); HMISerial10.write(0xff); HMISerial10.write(0xff); Serial.print("Outside Temp: "); Serial.println((bmp.readTemperature() * 1.8) + 32); }

Here I have it looking for a button from the Nextion

void dfd_listen() {

delay(30); dfd = ""; //dfd means Data From Display while (HMISerial5.available()) { //This will look to see if there is something coming from the Nextion dfd += char(HMISerial5.read()); }

if (dfd.substring(1, 4) == "bt4" && dfd.substring(0, 1) == "0") { digitalWrite(R6, LOW); // Roof Lights HMISerial5.write(0xff); // We always have to send this three lines after each command sent to the nextion display. HMISerial5.write(0xff); HMISerial5.write(0xff); Serial.println("Roof Lights OFF"); } else if (dfd.substring(1, 4) == "bt4" && dfd.substring(0, 1) == "1") { digitalWrite(R6, LOW); // Roof Lights HMISerial5.write(0xff); // We always have to send this three lines after each command sent to the nextion display. HMISerial5.write(0xff); HMISerial5.write(0xff); Serial.println("Roof Lights ON"); }

if (dfd.substring(1, 4) == "b17") { HMISerial5.print("page 1"); HMISerial5.write(0xff); // We always have to send this three lines after each command sent to the nextion display. HMISerial5.write(0xff); HMISerial5.write(0xff); Serial.println("Change to Page 1"); dfd = ""; delay(100); }

Now this shows both BT and B buttons. The HMISerial are my serial ports the Nextion are attached to. The "dfd" is set as a string before your setup. For the Nextion button, you just have to add a value and set it as a string, in this case, my value is va11. Then on the button on the press event, you would put something like this.

  if(bt4.val==1)
{
  cov bt4.val,va11.txt,1
  va11.txt+="bt4"
  print va11.txt
  t4.pco=17576
}else if(bt4.val==0)
{
  cov bt4.val,va11.txt,1
  va11.txt+="bt4"
  print va11.txt
  t4.pco=57033
}
sempaiscuba
  • 1,042
  • 9
  • 21
  • 32