String comparison troubles

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  ")); 
//-------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
      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

    if ((String)command == "SR1")
      Serial.println("writing to SR1");
   if(inData > 0){
       else { Serial.println("nothing received");

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.


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() {
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track

void loop() {

  while (Serial.available()) {
    char c =;
    readString += c; 

  if (readString.length() >0) {

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

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