Re PLS HELP! - Nextion (tft) screen to control 2 relay module only 1 relay works

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
}

}

thanks in advance!!

SoftwareSerial mySerial(10, 11); // RX, TX

You want to post a picture of the mySerial device connected to those pins? Wouldn't a more meaningful name make sense?

String message;
String message1;

Bad idea pissing away resources on the String class. Learn to use C style strings, instead.

int currentTemp0;
currentTemp0 = sensors.getTempCByIndex(0);

Or, you could look like you know what you are doing:

int currentTemp0 = sensors.getTempCByIndex(0);
while (!mySerial.available()){ //While no data is coming in from the Nextion

The available() method does NOT return a boolean. Do NOT use the return value like it was a boolean.

It does not make sense to be using a while statement there. An if statement, maybe.

 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.

The damn device needs to send an end of packet marker, or, somehow, tell you how many characters to expect. Guessing is for buying lottery tickets, NOT writing code.

    Serial.println(button); //See what state the button is in

More guessing. You have NOT assigned a value to button, based on anything read from the mySerial (whatever the hell that is) instance.

You REALLY need to learn to use Tools + Auto Format AND to post your code CORRECTLY.

hey thanks for the advice, I know I have a lot of learning to do thanks for the pointers, if anyone else can top me up with more info or corrected code I would very much appreciate any further help, im new at this but im trying to get to grips with it!

Cheers all :)

if anyone else can top me up with more info or corrected code I would very much appreciate any further help

But you don't intend to fix any of the issues I pointed out, to at least make your code easier to follow. Given that, I can't really see why anyone else would want to comment.

I have fixed the issues that you spoke of in the code the only bit thats troubling me is the relay control. I have spent time today working on a servo based water control system. I am now going back to the relay control. As i mentioned im new and there has been a lot of copying and pasting so I apologise but you must remember I know very little about progamming ive only been doing it a couple of weeks. So please bare with me I know it must seem frustrating :(

The bit that im stuck with

I have two buttons both are on page 3 of the nextion LCD with id of 3 (hence the serial message checks) and 4 respectively

I want to control the relays when i push the buttons i want each to be and on/off switch as my goal is to turn pump1 and pump2 on or off.

I think I might try and use the dual state button tonight see if that works.

This is the section that doesnt work - there must be a simpler way? I have been made aware about the boolean issue too.

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 guys

So please bare with me

You go right ahead and take your clothes off if you want. I don't know you, though, so I'm keeping mine on.

I do NOT understand all the "while there is no serial data" crap. Most times, you want to do something because there IS serial data (to be read and dealt with).

I prefer to put curly braces on the next line, after the line with the if statement, for statement, function declaration, etc. If you are not going to do that, atleastusesomespacebetweenthestatementandthecurlybrace.

Make your code as easy to read as possible, not as hard to read.

This is the section that doesnt work

We have seen no serial output, so we would just be guessing what is in message or message1. Since we would just have to guess, it really is pointless to do that.

Use Serial.print() and Serial.println() to print the data with identification, so we know what was printed.

Then, we can help with using the data.

PaulS: while (!mySerial.available()){ //While no data is coming in from the Nextion

The available() method does NOT return a boolean. Do NOT use the return value like it a boolean.

In the midst of many, many other problems the OP needs to address, this is not one of them. There is a long history of implicit boolean casting, long before there was even a bool type.

I would argue that "while not serial available" is more readable (i.e., easier to understand) than "while serial available equals zero", and the number of cases where you really care about the numeric value is very small.

The available return result is usually, perhaps even properly, treated as a boolean value. The stylistic suggestion is arguable of course, but the overwhelming majority of code, including the dark ages of C, uses the implicit cast.

YMMV, /dev

In the midst of many, many other problems the OP needs to address, this is not one of them.

It is my opinion that it is. I much prefer to see code that properly compares the VALUE returned by available() to some known value. That proves that the programmer KNOWS what available() returns.

I know that my opinion is not widespread, but I do not intend to change it.