Using Fio and xBee digital i/o pins

I my project i am using a Fio to monitor some analog inputs and need to turn on a remote relay when a condition is meet. To the best of my knowledge (read NOOB) the xBee digital i/o pins are not connected to the Fio. As a work around i’m trying to use AT commands to set the pins on the xBee so that the remote xBee pins will get updated with this change and turn on the relay. I put togather the following test and it works but after it changes the i/o pin a couple of time it seems like the Fio locks up? Is there a better way to go about this or is there something i did wronge in the code below?

Thanks in advanced for your asistance :slight_smile:

int pinStatus = 1;

void setup(){
  Serial.begin(9600);  //Start the serial connection with the computer
  
}

void loop (){
  Serial.print("Pin Status=");Serial.println(pinStatus);
  delay(5000);
  Serial.print("+++");      // put the XBee in command mode
  delay(2000);
    
  if (returnedOK() == 'T') {
    // if an OK was received then continue 
    
    //turn pin on or off
    if (pinStatus==0){
      Serial.println("ATD0=5,AC,CN");
      pinStatus = 1;
    }else{
      Serial.println("ATD0=4,AC,CN");
      pinStatus = 0;
    }
    
    Serial.print("Pin Status changed to ");Serial.println(pinStatus);
  }else{
    Serial.println("");Serial.println("Failed to enter command mode.");
  }

}

char returnedOK () {
  // this function checks the response on the serial port to see if it was an "OK" or not
  char incomingChar[3];
  char okString[] = "OK";
  char result = 'n';
  int startTime = millis();
  while (millis() - startTime <= 2500 && result == 'n') {  // use a timeout of 5 seconds
    if (Serial.available() > 1) {
      // read three incoming bytes which should be "O", "K", and a linefeed:
      for (int i=0; i<3; i++) {
        incomingChar[i] = Serial.read();
      }
      if ( strstr(incomingChar, okString) != NULL ) { // check to see if the respose is "OK"
//      if (incomingChar[0] == 'O' && incomingChar[1] == 'K') { // check to see if the first two characters are "OK"
        result = 'T'; // return T if "OK" was the response
      }else {
        result = 'F'; // otherwise return F
      }
    }
  }
  return result;
}