HELP!! Nexton Display - Using Serial info to control 2 relay module

Hi all

Im new to all this and really need your help.

Im using an arduino uno and a 3.5inch nextion display

I am not using the nexton library’s but I am using software serial thru ports 10 and 11 these are both receiving the info from the button presses on the arduino but i can only get the 1st relay to turn on and off not the second , I’m also reading temps but this works and is irrelevant. Please can someone advise for me

I have a booleans for storing button states and I have two separate strings for the message (not sure this is correct though!)

The idea is I press the button and relay 1 turns on and off
and i press buttonb relay 2 turns on and off

If anyone can correct the code i would be so grateful at a loss here due to being a noob at this!

Thanks in advance - he’s my scrabbled together code!! I have checked the message numbers and they are correct if i switch them around then i can use button b but not both at the same time, interestingly if i turn the relay on with the first ‘button’ then i can turn it off my pressing button b, something wrong with the if else code maybe? Let me know your thoughts??

Code → // turn on off 2 relay module button (turns on/off relay 1) , buttonb (turns on/off relay2)

include <DallasTemperature.h>
#include <OneWire.h>
#include <SoftwareSerial.h> //Include the library
boolean button = false; //will store the button state
boolean buttonb = false;
SoftwareSerial mySerial(10, 11); // RX, TX

String message;
String message1;

OneWire oneWire(7);

DallasTemperature sensors(&oneWire);

// 2 relays 240v 10A max to run pumps 1 and 2 on pins 3 and 4

#define Relay1 3

#define Relay2 4

void setup()

{
sensors.begin();
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only

}
Serial.println(“Serial On”);
// set the data rate for the SoftwareSerial port
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
mySerial.begin(9600);

}

void loop()

{

sensors.requestTemperatures();
int currentTemp0;
currentTemp0 = sensors.getTempCByIndex(0);
int currentTemp1;
currentTemp1 = sensors.getTempCByIndex(1);
int currentTemp2;
currentTemp2 = sensors.getTempCByIndex(2);

//Number values

Serial.print(“t7.val=”);
Serial.print(currentTemp0);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(250);

Serial.print(“t8.val=”);
Serial.print(currentTemp1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(250);

Serial.print(“t9.val=”);
Serial.print(currentTemp2);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(250);

Serial.print(“t10.val=”);
Serial.print(currentTemp1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(250);

// Pointer values

Serial.print(“z0.val=”);
Serial.print(225/130*currentTemp0-5);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(250);

Serial.print(“z1.val=”);
Serial.print(225/120*currentTemp1-5);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(250);

Serial.print(“z2.val=”);
Serial.print(225/120*currentTemp2-5);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(250);

Serial.print(“z3.val=”);
Serial.print(225/120*currentTemp0-5);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(250);

while (!mySerial.available()){ //While no data is coming in from the Nextion
if (message.length() > 14){ //This was the easiest way for me to check that the whole message has been received. Some messages might be longer, but the shortest ones are 15 characters.
Serial.println(message); //See what the entire message that the Arduino receives from the Nextion
Serial.print("button state: ");
Serial.println(button); //See what state the button is in

Serial.print("message[3]: "); //according to the Nextion message structure, item 4 will be the page number.
Serial.println(message[3]); //See what message[3] (the fourth character in the message) is

Serial.print("message[4]: "); //according to the Nextion message structure, item5 will be the component number.
Serial.println(message[4]); //See what message[4] (the fifth character in the message) is

if (button == false && message[3] == ‘3’ && message[4] == ‘3’ ){ //Test that the button is off and that it was indeed this button that has been pressed
button = true; //The button is now pressed
digitalWrite(Relay1, LOW); //Switch on the relay 1
Serial.println(“case 1”); //For debugging. Check that the ‘if’ was entered
message = “”; //Clear the message, otherwise the next if will also be activated
}
else{ //Else the button is on and that it was indeed this button that has been pressed
button = false; //The button has been deactivated
digitalWrite(Relay1, HIGH); //Switch the relay 1 off
Serial.println(“case 2”); //For debugging. Check that the ‘if’ was entered
message = “”; //Clear the message, otherwise we might get mixed messages
}
}

if (!mySerial.available()) {

if (buttonb == false && message1[3] == ‘3’ && message1[4] == ‘4’) { //Test that the button is off and that it was indeed this button that has been pressed
buttonb = true; //The button is now pressed
digitalWrite(Relay2, LOW); //Switch on the relay 2
message1 = “”; //Clear the message, otherwise we might get mixed messages
}
else{ //Else the button is on and that it was indeed this button that has been pressed
buttonb = false; //The button has been deactivated
digitalWrite(Relay2, HIGH); //Switch the relay 2 off
message1 = “”; //Clear the message, otherwise we might get mixed messages
}
}
}
while (mySerial.available()){ //Is data coming through the serial from the Nextion?
message.concat(mySerial.read()); //If so, construct the message
}

}

and this is the piece I'm stuck on

button works fine but buttonb doesn't both items are on page 3 and button id is 3 , and buttonb1 id is 4

section of code that I'm stuck on--->>

while (!mySerial.available()){ //While no data is coming in from the Nextion if (message.length() > 14){ //This was the easiest way for me to check that the whole message has been received. Some messages might be longer, but the shortest ones are 15 characters. Serial.println(message); //See what the entire message that the Arduino receives from the Nextion Serial.print("button state: "); Serial.println(button); //See what state the button is in

Serial.print("message[3]: "); //according to the Nextion message structure, item 4 will be the page number. Serial.println(message[3]); //See what message3 is

Serial.print("message[4]: "); //according to the Nextion message structure, item5 will be the component number. Serial.println(message[4]); //See what message4 is

if (button == false && message[3] == '3' && message[4] == '3' ){ //Test that the button is off and that it was indeed this button that has been pressed button = true; //The button is now pressed digitalWrite(Relay1, LOW); //Switch on the relay 1 Serial.println("case 1"); //For debugging. Check that the 'if' was entered message = ""; //Clear the message, otherwise the next if will also be activated } else{ //Else the button is on and that it was indeed this button that has been pressed button = false; //The button has been deactivated digitalWrite(Relay1, HIGH); //Switch the relay 1 off Serial.println("case 2"); //For debugging. Check that the 'if' was entered message = ""; //Clear the message, otherwise we might get mixed messages } }

if (!mySerial.available()) {

if (buttonb == false && message1[3] == '3' && message1[4] == '4') { //Test that the button is off and that it was indeed this button that has been pressed buttonb = true; //The button is now pressed digitalWrite(Relay2, LOW); //Switch on the relay 2 message1 = ""; //Clear the message, otherwise we might get mixed messages } else{ //Else the button is on and that it was indeed this button that has been pressed buttonb = false; //The button has been deactivated digitalWrite(Relay2, HIGH); //Switch the relay 2 off message1 = ""; //Clear the message, otherwise we might get mixed messages } } } while (mySerial.available()){ //Is data coming through the serial from the Nextion? message.concat(mySerial.read()); //If so, construct the message }

}

thanks!!