-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,497
  • 15
  • 24
  • 29
  • What is the `NexDualStateButton` class? Is that defined by Nextion? And what is Nextion? – Duncan C Feb 09 '20 at 20:06
  • If I didn't include the NexDualStateButton in the include statement, I am getting an error as NexDSButton was not declared. So only I included the NexDualStateButton in the include statement – Abhishekh Aaron Feb 10 '20 at 17:30
  • Also, I downloaded the nextion stable environment library(Nextion 7.0.0) and I added that in Arduino IDE. I can't able to add the Nextion unstable version which contains the dual state button library. – Abhishekh Aaron Feb 10 '20 at 17:33
  • That doesn't answer my question. What is that class? I understand that you need it, but what does it do? You're using classes without explaining where they come from, what they are for, or where they are documented. – Duncan C Feb 10 '20 at 17:51
  • If I include that class my dual state button works. The Nextion library is not so stable. – Abhishekh Aaron Feb 11 '20 at 12:55
  • You are not providing enough information for us to help you. I've voted to close your question. – Duncan C Feb 11 '20 at 13:00
  • Actually I am new to Arduino programming and whatever I am is like trial and error method. The Class #include "NexDualStateButton.h" is used to declare the library of NextionDualState Button. – Abhishekh Aaron Feb 11 '20 at 13:11

2 Answers2

1

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

You will 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 ); 
  }

}
Alper Aslan
  • 21
  • 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,497
  • 15
  • 24
  • 29