Nextion dual state buttons not working

If i have my Nextion display on the RX2 TX2 ports of my mega2560 board i will get temp and humidity displayed on screen but none of my dual state buttons do anything? not sure what im doing wrong? any insight on what to look for?

#include "DHT.h"
#include "Nextion.h"
#include "NexDualStateButton.h"
#include "NexConfig.h"
#include "NexTouch.h"
#include "NexHardware.h"
#define DHTPIN 2  
#define DHTTYPE DHT11 

int R1 = 3, R2 = 4, R3 = 5, R4 = 6;                                                      
 
NexDSButton bt0 = NexDSButton(0, 2, "bt0");
NexDSButton bt1 = NexDSButton(0, 3, "bt1");
NexDSButton bt2 = NexDSButton(0, 4, "bt2");
NexDSButton bt3 = NexDSButton(0, 5, "bt3");
 
char buffer[100] = {0};
 
NexTouch *nex_listen_list[] = {

    &bt0, &bt1, &bt2, &bt3,
    NULL
};
float temperature = 0.0f;
float humidity = 0.0f;

DHT dht(DHTPIN, DHTTYPE);

void setup() 
{
 Serial2.begin(115200);
  dht.begin(); 
      nexInit();
    bt0.attachPop(bt0PopCallback, &bt0);
    bt1.attachPop(bt0PopCallback, &bt1);
    bt2.attachPop(bt0PopCallback, &bt2);
    bt3.attachPop(bt0PopCallback, &bt3);
    //bt4.attachPop(bt0PopCallback, &bt4);
    //bt5.attachPop(bt0PopCallback, &bt5);
    dbSerialPrintln("setup done"); 
 
}

void loop() {
  nexLoop(nex_listen_list);
  readSensor();
  sendHumidityToNextion();
  sendTemperatureToNextion();
  delay(2000);
}
void bt0PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    
    dbSerialPrintln("Callback");
    dbSerialPrint("ptr=");
  
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
    
  bt0.getValue(&dual_state);
  bt1.getValue(&dual_state);
  bt2.getValue(&dual_state);
  bt3.getValue(&dual_state);
  //bt4.getValue(&dual_state);
  //bt5.getValue(&dual_state);
 
   if(dual_state){digitalWrite(R1, HIGH);}else{digitalWrite(R1, LOW);}
   if(dual_state){digitalWrite(R2, HIGH);}else{digitalWrite(R2, LOW);}
   if(dual_state){digitalWrite(R3, HIGH);}else{digitalWrite(R3, LOW);}
   if(dual_state){digitalWrite(R4, HIGH);}else{digitalWrite(R4, LOW);}
  // if(dual_state){digitalWrite(R5, HIGH);}else{digitalWrite(R5, LOW);}
  // if(dual_state){digitalWrite(R6, HIGH);}else{digitalWrite(R6, LOW);}
}
void readSensor()
{
 humidity = dht.readHumidity();
 temperature = dht.readTemperature(true);
}

void sendHumidityToNextion()
{
  String command = "Humidity.txt=\""+String(humidity)+"\"";
  Serial2.print(command);
  endNextionCommand();
}

void sendTemperatureToNextion()
{
  String command = "Temperature.txt=\""+String(temperature,1)+"\"";
  Serial2.print(command);
  endNextionCommand();
}

void endNextionCommand()
{
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
}

I had this moved because it is not a question about the tutorial, it is a question about your project.

Without seeing your code or HMI file I don’t know how you think anyone can help you. Before you post your code please read How to use this forum
Especially item #7 on posting code.

As you originally posted this at the end of the Nextion tutorial I assume you are using the methods I set out there. None of my demonstration code is for a 2 state button, you will need to add code under touch press event and touch release event to signal to the Arduino that the button has been pressed and released, and modify the nested switch statements in the function HMI_read() to respond to the button pressed and button released messages.

I changed the code up alittle and removed the temp and humidity sensor, tried it again still no relay, but i do get a tx led when the button is pressed.

#include "Nextion.h"
#include "NexButton.h"
#include "NexDualStateButton.h"
 
int R1 = 2, R2 = 3, R3 = 4, R4 = 4;
 
NexDSButton bt0 = NexDSButton(0, 2, "bt0");
NexDSButton bt1 = NexDSButton(0, 3, "bt1");
NexDSButton bt2 = NexDSButton(0, 4, "bt2");
NexDSButton bt3 = NexDSButton(0, 5, "bt3");
//NexDSButton bt4 = NexDSButton(0, 1, "bt4");
//NexDSButton bt5 = NexDSButton(0, 1, "bt5");
 
char buffer[100] = {0};
 
NexTouch *nex_listen_list[] = 
{
    &bt0, &bt1, &bt2, &bt3, 
    NULL
};
void setup(void)
{    
    nexInit();
    bt0.attachPop(bt0PopCallback, &bt0);
    bt1.attachPop(bt0PopCallback, &bt1);
    bt2.attachPop(bt0PopCallback, &bt2);
    bt3.attachPop(bt0PopCallback, &bt3);
   // bt4.attachPop(bt0PopCallback, &bt4);
   // bt5.attachPop(bt0PopCallback, &bt5);
    dbSerialPrintln("setup done"); 
}
 
void loop(void)
{   
    nexLoop(nex_listen_list);
}
 
void bt0PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    
    dbSerialPrintln("Callback");
    dbSerialPrint("ptr=");
  
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
    
  bt0.getValue(&dual_state);
  bt1.getValue(&dual_state);
  bt2.getValue(&dual_state);
  bt3.getValue(&dual_state);
//  bt4.getValue(&dual_state);
 // bt5.getValue(&dual_state);
 
   if(dual_state){digitalWrite(R1, HIGH);}else{digitalWrite(R1, LOW);}
   if(dual_state){digitalWrite(R2, HIGH);}else{digitalWrite(R2, LOW);}
   if(dual_state){digitalWrite(R3, HIGH);}else{digitalWrite(R3, LOW);}
   if(dual_state){digitalWrite(R4, HIGH);}else{digitalWrite(R4, LOW);}
  // if(dual_state){digitalWrite(R5, HIGH);}else{digitalWrite(R5, LOW);}
 //  if(dual_state){digitalWrite(R6, HIGH);}else{digitalWrite(R6, LOW);}
}

Hello DRE50,

Thank you for posting your code. As it says in the introduction to my tutorial my methods don't use the Nextion libraries and I don't know anything about them. As you are using the libraries I am sorry I am unable to help you.

so i changed my code around alittle to see if its the screen or the code thats not working. i used this code with a DSbutton and the code worked and changed the tex in the text box.

#include "Nextion.h"

/*
 * Declare a dual state button object [page id:0,component id:1, component name: "bt0"]. 
 */
NexDSButton bt0 = NexDSButton(0, 2, "bt0");

NexText t0 = NexText(0, 3, "t0");


char buffer[100] = {0};

/*
 * Register a dual state button object to the touch event list.  
 */
NexTouch *nex_listen_list[] = 
{
    &bt0,
    NULL
};

/*
 * Dual state button component pop callback function. 
 * In this example,the button's text value will plus one every time when it is released. 
 */
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) 
    {
        t0.setText("HI! OPEN STATE");
    }
    else
    {
        t0.setText("HI! OFF STATE");
    }
}

void setup(void)
{    
    /* Set the baudrate which is for debug and communicate with Nextion screen. */
    nexInit();

    /* Register the pop event callback function of the dual state button component. */
    bt0.attachPop(bt0PopCallback, &bt0);
    
    dbSerialPrintln("setup done"); 
}

void loop(void)
{   
    /*
     * When a pop or push event occured every time,
     * the corresponding component[right page id and component id] in touch event list will be asked.
     */
    nexLoop(nex_listen_list);
}

so i changed the code alittle to try to fire the relays and i get nothing? im not sure what im missing? is it how i initialize the relays?

#include "Nextion.h"
int R1 = 3, R2 = 4, R3 = 5, R4 = 6;
/*
 * Declare a dual state button object [page id:0,component id:1, component name: "bt0"]. 
 */
NexDSButton bt0 = NexDSButton(0, 2, "bt0");
NexDSButton bt1 = NexDSButton(0, 3, "bt0");
NexDSButton bt2 = NexDSButton(0, 4, "bt0");
NexDSButton bt3 = NexDSButton(0, 5, "bt0");

char buffer[100] = {0};

/*
 * Register a dual state button object to the touch event list.  
 */
NexTouch *nex_listen_list[] = 
{
    &bt0,
    &bt1,
    &bt2,
    &bt3,
    NULL
};
/*
 * Dual state button component pop callback function. 
 * In this example,the button's text value will plus one every time when it is released. 
 */
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(R1, HIGH);
    }
    
    else{digitalWrite(R1, LOW);
    }
}
void bt1PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    dbSerialPrintln("b1PopCallback");
    dbSerialPrint("ptr=");
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
/* Get the state value of dual state button component . */
    bt1.getValue(&dual_state);
     if(dual_state){digitalWrite(R2, HIGH);
    }

    else{digitalWrite(R2, LOW);
    }
}
void bt2PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    dbSerialPrintln("b2PopCallback");
    dbSerialPrint("ptr=");
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
/* Get the state value of dual state button component . */
    bt2.getValue(&dual_state);
     if(dual_state){digitalWrite(R3, HIGH);
    }

    else{digitalWrite(R3, LOW);
    }
}
void bt3PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    dbSerialPrintln("b3PopCallback");
    dbSerialPrint("ptr=");
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
/* Get the state value of dual state button component . */
    bt3.getValue(&dual_state);
     if(dual_state){digitalWrite(R4, HIGH);
    }


    else{digitalWrite(R4, LOW);
    }
}
void setup(void)
{    
    /* Set the baudrate which is for debug and communicate with Nextion screen. */
    nexInit();

    /* Register the pop event callback function of the dual state button component. */
    bt0.attachPop(bt0PopCallback, &bt0);
    bt1.attachPop(bt1PopCallback, &bt1);
    bt2.attachPop(bt2PopCallback, &bt2);
    bt3.attachPop(bt3PopCallback, &bt3);
    dbSerialPrintln("setup done"); 
}
void loop(void)
{   
    /*
     * When a pop or push event occured every time,
     * the corresponding component[right page id and component id] in touch event list will be asked.
     */
    nexLoop(nex_listen_list);
}

ok so was trying to figure out whats going on? why is this not working? so i changed the code again and instead of relays i used LED. and guess what it works? so what am i missing?

#include "Nextion.h"

/*
 * Declare a dual state button object [page id:0,component id:1, component name: "bt0"]. 
 */
NexDSButton bt0 = NexDSButton(0, 2, "bt0");
NexDSButton bt1 = NexDSButton(0, 3, "bt0");
NexDSButton bt2 = NexDSButton(0, 4, "bt0");
NexDSButton bt3 = NexDSButton(0, 5, "bt0");

char buffer[100] = {0};

/*
 * Register a dual state button object to the touch event list.  
 */
NexTouch *nex_listen_list[] = 
{
    &bt0,
    &bt1,
    &bt2,
    &bt3,
    NULL
};
/*
 * Dual state button component pop callback function. 
 * In this example,the button's text value will plus one every time when it is released. 
 */
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(9, HIGH);
    }
    
    else{digitalWrite(9, LOW);
    }
}
void bt1PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    dbSerialPrintln("b1PopCallback");
    dbSerialPrint("ptr=");
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
/* Get the state value of dual state button component . */
    bt1.getValue(&dual_state);
     if(dual_state){digitalWrite(10, HIGH);
    }

    else{digitalWrite(10, LOW);
    }
}
void bt2PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    dbSerialPrintln("b2PopCallback");
    dbSerialPrint("ptr=");
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
/* Get the state value of dual state button component . */
    bt2.getValue(&dual_state);
     if(dual_state){digitalWrite(11, HIGH);
    }

    else{digitalWrite(11, LOW);
    }
}
void bt3PopCallback(void *ptr)
{
    uint32_t dual_state;
    NexDSButton *btn = (NexDSButton *)ptr;
    dbSerialPrintln("b3PopCallback");
    dbSerialPrint("ptr=");
    dbSerialPrintln((uint32_t)ptr); 
    memset(buffer, 0, sizeof(buffer));
/* Get the state value of dual state button component . */
    bt3.getValue(&dual_state);
     if(dual_state){digitalWrite(12, HIGH);
    }


    else{digitalWrite(12, LOW);
    }
}
void setup(void)
{    
    /* Set the baudrate which is for debug and communicate with Nextion screen. */
    nexInit();

    /* Register the pop event callback function of the dual state button component. */
    bt0.attachPop(bt0PopCallback, &bt0);
    bt1.attachPop(bt1PopCallback, &bt1);
    bt2.attachPop(bt2PopCallback, &bt2);
    bt3.attachPop(bt3PopCallback, &bt3);
//setting port OUTPUT
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
//initializing  port 
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    dbSerialPrintln("setup done"); 
}
void loop(void)
{   
    /*
     * When a pop or push event occured every time,
     * the corresponding component[right page id and component id] in touch event list will be asked.
     */
    nexLoop(nex_listen_list);
}

do i need to copy the code like i have for the LED but change it for relays?

/setting port OUTPUT
    pinMode(9, OUTPUT);
    pinMode(10, OUTPUT);
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
//initializing  port 
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);

any input thanks

something interesting i just found out. if i touch buttons 1234 then 4321 it works just fine. but if i go out of order or press any button i get nothing? must be something in my code??