 int RECV_PIN = 8;
 IRrecv irrecv(RECV_PIN);
 decode_results results;
-int deviceStatus = 0;
-int pendingTimer;
+
 char deviceId[3] = "??";
 char XBeeInString[50];
-int state = IDLE;
+
 unsigned int ledStateInterval = 500;
 unsigned long randomDelay = 0;
 unsigned long start_time;

  // set the data rate for the SoftwareSerial port
  XBee.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  randomSeed(analogRead(5));

  readXBeeDeviceId();
  toggle_time = millis();
  signal_time = millis();


void loop()
{
  if(irrecv.decode(&results)) {
    delay(5);
    DEBUG_PRINT("\nIR received: ");
    DEBUG_PRINTLN(results.value);
    sendBackDeviceID();
    irrecv.resume();
  }
  else if (XBee.available()) {
    delay(5);
    readXBeeString(XBeeInString);

    if (XBeeInString[0] == '\\') {      // escape sign indicating commands
      sendBackDeviceID();
      // We will need to send back the device status also, here
    }
    else {
      // filter only the commands that is targeting at this client
      char id[2];
      string_copy(id, XBeeInString, 0, 1);
      if (atoi(id) == atoi(deviceId)) {
        // pass this message to the function of client
        lampClient(XBeeInString);
      }      
    }
  }
}

void sendBackDeviceID() {
  randomDelay = random(1000);
  // avoid conflicts
  delay(randomDelay);
  // send back acknowledge packet
  sendXBeePacketFromRaw(&XBee, deviceId, "a", "0000");
}

void lampClient(char *msg) {
  // for lamp, it provides functions as turning on and off
  // so what it does regarding the 
   
  digitalWrite(ledStatePin, HIGH);
  char data[4];
  string_copy(data, msg, 3, 6);

  data[4] = '\0';
  DEBUG_PRINTLN("[CONNECTED] command issued");
  if (strcmp(data, "0001") == 0) {
    digitalWrite(controlledPin, HIGH);
  }
  else if (strcmp(data, "0002") == 0) {
    digitalWrite(controlledPin, LOW);
  }
}

int readXBeeString (char *strArray) {
  DEBUG_PRINT("start read XBee: ");
  int i = 0;
  while (XBee.available()) {
    strArray[i] = XBee.read();
    i++;
  }
  strArray[i] = '\0';
  DEBUG_PRINT("read XBee: ");
  DEBUG_PRINT(strArray);
  DEBUG_PRINT(" length: ");
  DEBUG_PRINTLN(i);
  return i;
}

void readXBeeDeviceId() {
