string compare problems

this string does not work

char MSG_ON = "turn on led";

if (strcmp(smsbuffer, MSG_ON) == 0) digitalWrite(8, HIGH);

whats its suppose to be doing;
turn on a led

i have done hours of testing and i came to strcmp value changes every time i upload a new sketch although the smsbuffer and the MSG_ON are exactly the same

Is smsbuffer a zero terminated array of chars ?
Can you print it ?
Print an identifying character immediately before and after it so that you can see any spaces before or after it.

I suggested nearly the same thing in the original thread.

Is smsbuffer a zero terminated array of chars ?

i think so im not exactly sure what u mean

yes i can print it (the smsbuffer right)

what do u mean by last question?

im kinda new to this sorry

could u give me an example? that would make my day

If strcmp fails, then your smsbuffer and MSG_ON, are most definitely NOT the same. Nobody her can help you figure out what is really happening. YOU need to look at the exact contents, and LENGTHS, of both strings. They ARE different! This is NOT a bug in strcmp.

Print them both, like this, and the REAL problem will be obvious:

Serial.print("smsbuffer=[");
Serial.print(smsbuffer);
Serial.print("] length=");
Serial.println(strlen(smsbuffer));

Serial.print("MSG_ON=[");
Serial.print(MSG_ON);
Serial.print("] length=");
Serial.println(strlen(MSG_ON));

Regards,
Ray L.

hooverdan:
Is smsbuffer a zero terminated array of chars ?
i think so im not exactly sure what u mean

https://www.tutorialspoint.com/cprogramming/c_strings.htm

this is what i got

smsbuffer=[turn on led] length=12

MSG_ON=[turn on led] length=11

so now i know the lengths are different but what now

again i am pretty new to this.

so how do i fix it i tried putting char MSG_ON = "turn on led\0"; but that didn't work

hooverdan:
this is what i got

smsbuffer=[turn on led] length=12

MSG_ON=[turn on led] length=11

so now i know the lengths are different but what now

again i am pretty new to this.

so how do i fix it i tried putting char MSG_ON = "turn on led\0"; but that didn't work

You defined MSG_ON as:
char MSG_ON = "turn on led\0";
This means there are TWO '\0' characters at the end of the string, because YOU put an extra one in your initializer, and the compiler put one AFTER your initializer. Get rid of the one in your initializer.
Regards,
Ray L.

RayLivingston:
You defined MSG_ON as:
char MSG_ON = “turn on led\0”;
This means there are TWO ‘\0’ characters at the end of the string, because YOU put an extra one in your initializer, and the compiler put one AFTER your initializer. Get rid of the one in your initializer.
Regards,
Ray L.

that wouldn’t affect strcmp()

Maybe there's an unprintable character in 'smsbuffer'. Try printing the numerical values of all the array elements.

EDIT: I just saw gfvalvo’s post after I wrote this. This code will do as suggested.

char MSG_ON[] = "turn on led";
MSG_ON=[turn on led]  length=11

That is correct for strlen, i.e. 11 characters before the terminating null.

Your issue is with smsbuffer. There is an extra character. See what prints out with this test and see if you can figure out what it is

void setup() {
  Serial.begin(115200);
  char MSG_ON[] = "turn on led";
  char smsbuffer[] = "turn on led\r";//example with CR
  Serial.println(MSG_ON);
  Serial.println(strlen(MSG_ON));
  for (byte i = 0; i <= (strlen(MSG_ON)); i++)
  {
    Serial.print(byte(MSG_ON[i]));
    Serial.print(" ");
  }
  Serial.println();

  Serial.println(smsbuffer);
  Serial.println(strlen(smsbuffer));
  for (byte i = 0; i <= (strlen(smsbuffer)); i++)
  {
    Serial.print((byte)(smsbuffer[i]));
    Serial.print(" ");
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

i put made it like this char MSG_ON = {"6"}; then the same thing happened the length of the MSG_ON was 1 less from the smsbuffer

sms recieved
6

DEBUG:SMS TEST
DEBUG:>
13 //this 13 is what value the strcmp returned
smsbuffer=[6] length=2
MSG_ON=[6] length=1

i just read your thred but ill post this than try your code
than i made it like this char MSG_ON = {'6'};and the led still didnt turn onn even when the to strings were the same. this is what the serial monitor said

sms recieved
6

DEBUG:SMS TEST
DEBUG:>
-16 // this -16 is what value the strcmp returned
smsbuffer=[6] length=2
MSG_ON=[6] length=2

notice the space of the MSG_ON
?
?

This:

MSG_ON = {'6'};

is NOT defining a string. It is defining an array containing a single element, the ASCII character 6. So, strlen will NOT give you the length of a string which is not a string. It will keep scanning until if finds a '\0', which is apparently the second character after the 6, and that byte is part of some other variable, or some unallocated part of memory. If you want a variable initialized as a string, put the initializer in double-quotes, not single quotes.

Print each character of both arrays as HEX values, and you will see where your strings do not match. You have a non-printing character in there somewhere.

Regards,
Ray L.

as soon as i put that \r after the MSG_ON message IT WORKED!!!!!!!!!!!!!!!!!!!!

the strcmp returned a 0 , finally!!!!

but why did it work?

Hard to say since you didn't print the HEX numerical values of the array elements as advised.

It works because the SMS is terminated by a "\r" (for some reason) this termination must either be used in the string to compare with or removed from the SMS (trimmed away). I'm not 100% sure about the GSM specs on this, but I believe that a the final "\r" is not supposed to be there by specification. It is either put there by the sender or the sending device or it is put there by the GSM module or the code handling it.

EDIT: It may be that the \r is used to separate chunks of large messages which exceeds 160 bytes.