Grab callerid from string

Hi there,

I have an Adafruit Fona 800 and want to trigger the callerid, if the callerid is valid then hangup en start some function.

But the indexOf() won't work at all, what's wrong with this code?

String validPhoneNumbers[] = {"+45000000", "+45000001"};
String inputString = "";
String phonenumber;
int f;

void loop() {
  while (! Serial.available() ) {
    if (fona.available()) {
        f = fona.read();
        Serial.write(f);
        
        if (f == 10) { // newline
          if (inputString.indexOf("CLIP:") >= 0) {
            Serial.print("incoming call...");
            
            // here with substring() first the first ", and filter the phonenumber.

            if (checkPhoneNumber(phonenumber)) {
               Serial.println("all ok my friend");
               // call some function
            }
          }
          
          inputString = ""; //reset string
        }
        else {
          char inChar = Serial.read();
          inputString += inChar;
        }
        
    }
  }
}

boolean checkPhoneNumber(String number) {
  int i = sizeof( validPhoneNumbers )/sizeof( String );
  while(i--) {
    if (validPhoneNumbers[i] == number)
      return true;
  }
  
  return false;
}

You could add some serial prints to see what the data really is and what branch your code took.

Sorry but I don't use String class. Have you tried C char arrays and string.h? MUCH better behaved.

I'd run the input through a state machine watching for key char sequences like your "+45000000" and "+45000001" and trigger on every completion. I do my parsing as the data is read, not after.

The examples in serial input basics save the incoming data in a string and then you can easily parse that to find things that you are interested in.

...R