Not understanding what is wrong

Hi everyone,
I am developing a MQTT Client on a arduino uno, with a ethernet shield and a 4ch relay control.

My code was working yesterday, today i don’t know why it is not anymore, i havent changed anything.
For example, looking at the void callback() on the serial port, it prints that the command “ALLON” was recognized, but the variable inMessage is null within the void.
I really can’t understand what is happening, why it worked yesterday and today is not. Does it has to do with using string variables ?

Please help,
thanks.

MQQT_Client_RelayControl.ino (6.63 KB)

i havent changed anything.

Well, if it used to work and now doesn't then something has obviously changed.

To make it easier to examine with an editor please post your code here using code tags when you do.

Hi,
thank you for answering. For example, in this void what happens is that the Serial prints “ALLON” when I send the command through mosquitto, but the inMessage variable is always empty, even though I am putting the value on it as you can see. Then when it gets to the if’s, it doesn’t stop at any point :frowning:

void callback(char* topic, byte* payload, unsigned int length) 
{
 String inMessage = "";
 Serial.print("MQTT message arrived [");
  Serial.print(topic);
  Serial.print("] ");
 for (int i=0;i<length;i++)
 {
   Serial.print((char)payload[i]);
   inMessage = inMessage + (char)payload[i];
 }
 Serial.println(inMessage);
 if(inMessage == "C1ON")
   digitalWrite(CH1, HIGH);
 else if(inMessage == "C1OFF")
   digitalWrite(CH1, LOW);
 else if(inMessage == "C2ON")
   digitalWrite(CH2, HIGH);
 else if(inMessage == "C2OFF")
   digitalWrite(CH2, LOW);
 else if(inMessage == "C3ON")
   digitalWrite(CH3, HIGH);
 else if(inMessage == "C3OFF")
   digitalWrite(CH3, LOW);
 else if(inMessage == "C4ON")
   digitalWrite(CH4, HIGH);
 else if(inMessage == "C4OFF")
   digitalWrite(CH4, LOW);
 else if(inMessage == "ALLON")
   {
     Serial.println("CA ESTA");
     digitalWrite(CH1, HIGH);
     digitalWrite(CH2, HIGH);
     digitalWrite(CH3, HIGH);
     digitalWrite(CH4, HIGH);
   }
  else if(inMessage == "ALLOFF")
   {
     digitalWrite(CH1, LOW);
     digitalWrite(CH2, LOW);
     digitalWrite(CH3, LOW);
     digitalWrite(CH4, LOW);
   }
 //client.publish("/house/switchConfirm1/", "1");
 strLastMQQTMessage = inMessage;
 inMessage = "";
}

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)

UKHeliBob: Well, if it used to work and now doesn't then something has obviously changed.

To make it easier to examine with an editor please post your code here using code tags when you do.

Following proper forum procedures will encourage people to be more interesting in helping you solve your problem.

done! thanks, sorry, i’m a newbie :slight_smile:

@vaz83, you need to post the complete program, or at least a complete program that illustrates the problem.

Very often the reason newbies can't find the problem is because it is not in the piece of code where they think it is.

...R

Robin2: @vaz83, you need to post the complete program, or at least a complete program that illustrates the problem.

Very often the reason newbies can't find the problem is because it is not in the piece of code where they think it is.

...R

I have the ino file available for download on my first post

You might have CR or LF in your message. You can start by printing the length in the beginning of the callback function; does it match what you expect (e.g. 5 for ALLON)?

If not, you can print the hex values of the received data in the callback.

void callback(char* topic, byte* payload, unsigned int length)
{
  ...
  ...
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
    Serial.print("[");
    Serial.print((char)payload[i], HEX);
    Serial.print("]");
    
    inMessage = inMessage + (char)payload[i];
  }
  ...
  ...
}

For ALLON, you should see

A[41]L[4C]L[4C]O[4F]N[4E]

Nothing should follow. If something follows, your compare will fail.

The above assumes a software issue. In case this is a hardware issue, can you provide a link to the relay board that you use as well as a (photo/scan of) a hand-drawn wiring diagram. Please include power distribution as well; if you switch all relays on and you power the relays from the Uno, you might be overloading the Uno.

PS
the code is small enough to include in the post; might have required a 2nd post due to 9000 character limit)

vaz83: I have the ino file available for download on my first post

A lot of us here are hesitant to open any kind of unknown file. For that reason, attaching a file usually doesn't help you much to get the answer you need.

sterretje:
You might have CR or LF in your message. You can start by printing the length in the beginning of the callback function; does it match what you expect (e.g. 5 for ALLON)?

If not, you can print the hex values of the received data in the callback.

void callback(char* topic, byte* payload, unsigned int length)

{
  …
  …
  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
    Serial.print("[");
    Serial.print((char)payload[i], HEX);
    Serial.print("]");
   
    inMessage = inMessage + (char)payload[i];
  }
  …
  …
}



For ALLON, you should see


A[41]L[4C]L[4C]O[4F]N[4E]



Nothing should follow. If something follows, your compare will fail.

The above assumes a software issue. In case this is a hardware issue, can you provide a link to the relay board that you use as well as a (photo/scan of) a hand-drawn wiring diagram. Please include power distribution as well; if you switch all relays on and you power the relays from the Uno, you might be overloading the Uno.

PS
the code is small enough to include in the post; might have required a 2nd post due to 9000 character limit)

Hi, thank you for your answer, the serial is printing exactly what I am expecting. I also switched to another arduino (uno) and has the same issue. I will try with Hex values and will let you know as soon as I get home

vaz83: I have the ino file available for download on my first post

Sorry. I missed that.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

the serial is printing exactly what I am expecting.

Nevertheless I would check what it is actually printing by preceding the print with a < and printing a > immediately afterwards. That way you can see whether there is a hidden character such as a carriage return or an invisible character at the end of the string.

Robin2: Sorry. I missed that.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

yes, I was just reading this article:

https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/

I will change to cstrings, because I think that is the problem

vaz83:
yes, I was just reading this article:

The Evils of Arduino Strings | Majenko's Hardware Hacking Blog

I will change to cstrings, because I think that is the problem

That depends; the description of your problem is not clear enough to say yes or maybe.

If you reboot your Arduino and initially don’t have the problem and later it shows up (e.g. after one hour or after 2 hours or 24 hours or 5 days), the answer is yes.

If you reboot your Arduino and it immediately does not work, the answer is maybe.