Skip to main content
2 of 2
Added a second sketch (with different results) that uses a different method to find the string.
VE7JRO
  • 2.5k
  • 19
  • 28
  • 31

If you set your Serial Monitor's line ending to "Both NL & CR", then this code will find the word "send". It will NOT find the word "send" in this string, "ssend" or in this string, "sendd".

void setup(){
  Serial.begin(9600);
}

void loop(){
  while(Serial.available() > 0 ){
    String str = Serial.readString();
    if(str.substring(0) == "send\r\n"){
      Serial.println("identified");} 
    else{
      Serial.println("unknown");
    }
  }
}

If you want to find the string "send" in "1234send" or "send1234" or "1234send5678" then use indexOf();

void setup(){
  Serial.begin(9600);
}

void loop(){
  while(Serial.available() > 0 ){
    String str = Serial.readString();
    if(str.indexOf("send") > -1){
      Serial.println("identified");} 
    else{
      Serial.println("unknown");
    }
  }
}
VE7JRO
  • 2.5k
  • 19
  • 28
  • 31