String comparison troubles

Hi,
I’m not very educated in arduino code, kinda on a “copy, paste and modify”-level, so I guess my question here is also what I should read up on, or good strategies for testing.

I got a CC3000 wifi shield from Adafruit nicely connecting and displaying the values I send to it (SR1 + numbers) but when I try to use an if function ( if ((String)command == “SR1”)) to determine what message is coming in it does not react to it at all. Am I converting from char to String properly?

Any input is highly appreciated :slight_smile:

void loop()
{
  // initialization of the variables
  char command[3];      // commandstring
  unsigned int vnumber; // the value as integer
  boolean flag = true;  // a flag to seperate the command from the value
  
  Serial.println("I'm in grabInput");
  
//------check client socket open----//
    if(client.connected()) {
      Serial.print(F("connected to UDP port  ")); 
      Serial.println(port);
    } 
//-------end of socketcheck------//

  int inData;
  Serial.print("inData start value = ");Serial.println(inData);
  sockLen = sizeof(sockaddr_in);
 Serial.println("receive test ...");
 inData = recvfrom(sockfd, buff, sizeof(buff), 0, (sockaddr*)&from, &sockLen);
  
 //-----collect buffer contents------//
  value = "";
  for(int i=0;i<sizeof(buff);i++){
      if (i < 3)  // the first 3 characters
      {
      command[i]=buff[i];
      }
      else        // the last characters are the value
      {
        value += (char)buff[i];
      }
    }
    
    value += "\0";      // necessary - the string ends here
    command[3] = '\0';
    vnumber = value.toInt();  // string to integer
    

// SEND THE COMMANDS TO THE SERVOS
    if ((String)command == "SR1")
    {
      Serial.println("writing to SR1");
      Servo1.write(vnumber);
    }
 
   if(inData > 0){
     Serial.println("in");}
       else { Serial.println("nothing received");
       }
    //delay(10);
 }

Don't cast command to a String. Instead use if(strcmp(command, "Sr1") == 0))

"char command [3]" gives you an array with 3 elements numbered from 0 to 2 there is not element [3].

So this line     command[3] = '\0'; will over write something else.

Mark

You don't say how the data is being sent and in what form, but you might try the "readString.indexOf("on")" like in the below to detect the presence of "SR1" in the captured String.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

//A very simple example of sending a string of characters 
//from the serial monitor, capturing the individual 
//characters into a String, then evaluating the contents 
//of the String to possibly perform an action (on/off board LED).

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}