     Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
     while(1);
   }
+  
+  configureLightSensor();
+  
+  //Delay for 30 sec at startup after a manual RST to allow for the wireless programmer to transition 
+  // the board into the bootloader, or change other XBEE settings remotely, c.f. disable pin sleep mode to keep the XBEE up
+  blink(30, 500);
+}
+
+void blink(int times){
+  blink(times, 250);
+}
+
+void blink(int times, int delayValue){
+   for(int i=0; i<times; i++){
+      digitalWrite(LEDPIN, LOW);
+      delay(delayValue); 
+      digitalWrite(LEDPIN, HIGH);
+      delay(delayValue); 
+   }
 }
 
 void loop() {
-  digitalWrite(LEDPIN, HIGH);
   digitalWrite(XBEESLEEPPIN, LOW);  //Wake UP XBEE
-
-  configureLightSensor();
-   
+  delay(2000);
+
+  blink(25, 50);
+  digitalWrite(LEDPIN, LOW);  
+  delay(1000);
+
+  digitalWrite(LEDPIN, HIGH);  
+
+  sample += 1;
+
+  if (!pingCoordinator()){          //check if xbee woke up and got its association address. blink LED rapidly and quit loop otherwise
+    blink(10);
+    digitalWrite(LEDPIN, LOW);
+    sleep(10000); 
+    return;
+  }
+
   Serial.print("{'_sensor': 'weather', '_sample': {'_id':"); 
-  Serial.print(sample++); 
+  Serial.print(sample); 
   Serial.print(", "); 
   
   float h = dht.getHumidity();

    Serial.print(", ");
   }
  } 
//  else {
//   Serial.println("Failed to read from DHT");
//  }
  
  
  sensors_event_t event;
  bmp.getEvent(&event);
 
  Serial.print("'AtmoPressure':");
  if (event.pressure)
  {

  }
  Serial.println("}}");
  
  delay(2000);   //Delay at the end to allow xbee to flush its send buffer 

  digitalWrite(LEDPIN, LOW);
  digitalWrite(XBEESLEEPPIN, HIGH);  //Put XBEE to sleep
  
  sleep(60000);
}


int pingCoordinator(void){
    
    for(int i = 0; i<10; i++){
      blink(i);
      while(getCh() != NULL) ;       //Drain any pending chars on the serial.
      Serial.println("@PING");
      int c=0;
      while(Serial.available()<2 && c++<10){
          delay(400);
          blink(2, 50);
      }
      if (getCh()=='O' && getCh()=='K'){
          //while(getCh() != NULL) ;       //Drain any pending messages on the serial.
          return 1;
      }
      delay(500);
    }
    return 0;
}
/*
    Wake up the XBEE from sleep. 
    For hibernation periods longer than 10sec, or so, XBee will have to re-join the PAN. 
    Use ATMY command to confirm that the node got its network address before attempting to send data to DH/DL 
*/
/*
int wakeUpXbee(void){
    while(getCh() != NULL) ;       //Drain any pending messages on the serial.
    
    Serial.print("+++");
    delay(500);
    if (getCh()=='O' && getCh()=='K'){
        for(int j=0; j<5; j++){
          Serial.println("ATMY");
          if (!(getCh()=='F' && getCh()=='F' && getCh()=='F' & getCh()=='E')){
             Serial.println("ATCN");  //Got non-FFFE address. Exit command mode immediately.
             return 1;
          }
          delay(1000);
        }
    }
  return 0;
}
*/

char getCh(){
  if (Serial.available()){
    return Serial.read();
  }
  return NULL;
}

void configureLightSensor(void)
