logo elektroda
logo elektroda
X
logo elektroda
Dostępna jest polska wersja

Czy wolisz polską wersję strony elektroda?

Nie, dziękuję Przekieruj mnie tam

ESP8266 - problem comparing strings in commands from phone

BuildEver 2748 13
Best answers

Why does comparing a command string received from my phone with another string in my ESP8266 code not work, even though the received command prints correctly on Serial?

Your comparison fails because the received command likely contains hidden line-ending characters such as `\r` and `\n`, so `
Summary generated by AI based on the discussion content.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16000167
    BuildEver
    Level 7  
    Posts: 153
    Help: 1
    Rate: 3
    Hello forum,
    i have this problem :

    I download a command from my phone under the form of a string, when I want to compare this command to some other string nothing happens :/

    The best thing is that the recived: command displays on the serial. That is, the command received is correct. The comparison if(command == "cos") doesn't work anymore. Does anyone have any suggestions for a solution?

    Full code here at the very bottom.


    Piece of code:
    Code: C / C++
    Log in, to see the code
  • ADVERTISEMENT
  • #2 16000237
    krisRaba
    Level 31  
    Posts: 1999
    Help: 94
    Rate: 434
    Eeee, hmm... because that's not how you compare strings ;) That's how you can compare numbers, single characters (it works for you e.g. checking the last character in the "c" variable) etc. For strings you should use for example strcmp
    https://pl.wikibooks.org/wiki/C/strcmp
  • ADVERTISEMENT
  • #3 16000263
    BuildEver
    Level 7  
    Posts: 153
    Help: 1
    Rate: 3
    Thanks, I didn't know about that. Although I've been comparing like that forever and it usually works :/
  • #4 16000299
    Anonymous
    Anonymous  
  • #5 16001434
    krisRaba
    Level 31  
    Posts: 1999
    Help: 94
    Rate: 434
    And you see, I got distracted because in the header of the code frame is "Code: c" ;) Somehow that obscured the syntax visible further down :D
    I also thought that the strings were not properly terminated by incorrectly adding consecutive characters (no null), but it looks ok though.... hmm.
  • #6 16002098
    BuildEver
    Level 7  
    Posts: 153
    Help: 1
    Rate: 3
    I know, it's a bit hard. I'm agonising over it a lot, I can't find the error for anything :/
  • ADVERTISEMENT
  • #7 16002752
    nolens_volens
    Level 15  
    Posts: 68
    Help: 10
    Rate: 5
    And don't you happen to have one string as "Info" and the other as "info"?
    Just a thought...
  • #8 16002781
    BuildEver
    Level 7  
    Posts: 153
    Help: 1
    Rate: 3
    No, all commands are always in lower case. I even angered the newline character '\n' and it didn't do anything either :/
  • #9 16003072
    oskar777

    Level 26  
    Posts: 1264
    Help: 76
    Rate: 243
    Maybe start with a single character, then you will know if the comparison works at all.
    Company Account:
    Oskar-info
    Gidzińskiego 24/1, Warszawa, 02-293 | Tel.: 501XXXXXX (Show) | Company Website: http://oskar-info.pl
  • Helpful post
    #10 16003219
    ppc
    Level 18  
    Posts: 203
    Help: 28
    Rate: 120
    You have special characters in the string(\nmaybe also \r) and that's probably why it doesn't work. Try this:

    Code: C / C++
    Log in, to see the code
  • ADVERTISEMENT
  • Helpful post
    #11 16003270
    krisRaba
    Level 31  
    Posts: 1999
    Help: 94
    Rate: 434
    And you don't happen to be using a terminal that terminates lines by /r/n? Then when you compare "info" and the received "info/r" (and followed by the /n you are checking), you have different strings.... Add yourself a character counter to verify this, or use a terminal like TeraTerm for example, which displays CR and LF characters....

    Edit: I bookmarked myself and in the meantime fellow ppc added his post, so I'm late :-) I gave a helpful post, because according to me this is the solution :-)
  • #12 16003393
    BuildEver
    Level 7  
    Posts: 153
    Help: 1
    Rate: 3
    Indeed, the culprit was the sign \r (what does this sign mean?). Now everything works, thank you very much.
  • Helpful post
    #13 16003856
    ppc
    Level 18  
    Posts: 203
    Help: 28
    Rate: 120
    \carriage return(Carriage Return, CR)
    \new line (Line Feed, LF)

    Depending on the operating system, terminal program, etc., the end of a line is encoded differently. This also applies to text files. You can read more about this here:

    https://pl.wikipedia.org/wiki/Koniec_linii
    https://pl.wikipedia.org/wiki/CRLF

    You can find a list of control characters in the C and C++ languages here:

    https://en.wikipedia.org/wiki/Escape_sequences_in_C
  • #14 16177053
    BuildEver
    Level 7  
    Posts: 153
    Help: 1
    Rate: 3
    Thank you for all your answers.
ADVERTISEMENT