logo elektroda
logo elektroda
X
logo elektroda

ESP8266 - problem comparing strings in commands from phone

BuildEver 2601 13
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 16000167
    BuildEver
    Level 7  
    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  
    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
  • #3 16000263
    BuildEver
    Level 7  
    Thanks, I didn't know about that. Although I've been comparing like that forever and it usually works :/
  • #4 16000299
    Anonymous
    Anonymous  
  • ADVERTISEMENT
  • #5 16001434
    krisRaba
    Level 31  
    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.
  • ADVERTISEMENT
  • #6 16002098
    BuildEver
    Level 7  
    I know, it's a bit hard. I'm agonising over it a lot, I can't find the error for anything :/
  • #7 16002752
    nolens_volens
    Level 15  
    And don't you happen to have one string as "Info" and the other as "info"?
    Just a thought...
  • #8 16002781
    BuildEver
    Level 7  
    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  
    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.: | Company Website: http://oskar-info.pl
  • ADVERTISEMENT
  • Helpful post
    #10 16003219
    ppc
    Level 18  
    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
  • Helpful post
    #11 16003270
    krisRaba
    Level 31  
    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  
    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  
    \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  
    Thank you for all your answers.
ADVERTISEMENT