XBee hangs... why?

Hi! Here is my problem…

I have connected one XBee to an Arduino Uno R3. Coordinator connected to PC, executing a python process that reads the messeages sent from the Arduino, related to sensort (temperature, humidity…)

In the Arduino side, I’m trying to adjust the time between readings, using Time library (the first command i send from the arduino is requesting the time to the Coordinator). I also use TimeAlarms library.

My problem: when I increase the time between readings (sending values to Coordinator), when arduino restarts after uploading code (the only mofification is the ’ Alarm.timerRepeat(XX,repeats);’) the Arduino crash, the led in the XBee shield starts blinking (5 times a second) and no request detected in the controller… With 3 seconds, it works…

I tried modifying SoftwareSerial PINS, Using typicall millis() instead of Time library, but same results… More than 3 seconds… CRASH!!

Any help? :slight_smile:

Thank youuuuu!

#include <SoftwareSerial.h>
#include <XBee.h>

#include <Time.h>
#include <TimeAlarms.h>


#define PIN_PWR 4
#define DHT22_PIN 5

# define PIN_RX 10
# define PIN_TX 9

#include <DHT22.h>
DHT22 myDHT22(DHT22_PIN);

char verNum[] = "Version 0";
XBee xbee = XBee();
SoftwareSerial XbeeSerial(PIN_RX, PIN_TX);
char Dbuf[100]; 
char Dbuf_in[100]; 


// SH + SL Address of receiving XBee
//XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x00000000);
//XBeeAddress64 addr64 = XBeeAddress64(0x00000000, 0x0000ffff);

XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, 0x40ABC308);
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
ZBRxResponse rx = ZBRxResponse();

int pin5 = 0; //Next Moisture Sensor

unsigned long previousMillis = 0; 
unsigned long currentMillis = 0;
//long interval = 10000; 

void setup() {
  pinMode(PIN_PWR,OUTPUT);
  digitalWrite(PIN_PWR,HIGH);  
  pinMode(PIN_RX,INPUT);
  pinMode(PIN_TX,OUTPUT);
  Serial.begin(115200);
  Serial.println(verNum);

  XbeeSerial.begin(9600);
  xbee.setSerial(XbeeSerial);

  delay(1000);
  actualiza_hora();
  while(now()<5000){
    actualiza_hora();
    delay(500); 
  }
//  previousMillis =millis();
  Alarm.timerRepeat(3,repeats); //THIS IS THE ONLY CHANGE I DO
}

void loop() { 
/*   
  currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    //digitalWrite(PIN_PWR,HIGH);
    //delay(2000);
    repeats();
    //digitalWrite(PIN_PWR,LOW);
  }
*/ 
Alarm.delay(10);
}

void actualiza_hora(){
  sprintf(Dbuf,"TIME_Q");
  enviaDatos(Dbuf);  
  xbee.readPacket(200);   
  if (xbee.getResponse().isAvailable()) {
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE){
        xbee.getResponse().getZBRxResponse(rx);
        char txt_time[]="TIME:";
        if (strstr((char*)rx.getData(),txt_time)==NULL){
          return;
        }
        String timeString((char*)rx.getData());   
        Serial.println(timeString); 
        int firstColon = timeString.indexOf(":");
        timeString=timeString.substring(firstColon+1);
        setTime ((time_t)timeString.toInt());
      }
  }     
}

void enviaDatos(const char* Dbuf){
  sendXbee(Dbuf);
  Serial.print("Enviando...");
  Serial.print(Dbuf);
  if (xbee.readPacket(500)) {
    // got a response!
    // should be a znet tx status      
    if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
      xbee.getResponse().getZBTxStatusResponse(txStatus);
      // get the delivery status, the fifth byte
      if (txStatus.getDeliveryStatus() == SUCCESS) {
        Serial.print("OK!!...");
      } else {
        // the remote XBee did not receive our packet. is it powered on?
        Serial.print("NOOOOOOOOK!!...0x");
        Serial.println(txStatus.getDeliveryStatus(),HEX);
      }
    }
  } else if (xbee.getResponse().isError()) {
    //nss.print("Error reading packet.  Error code: ");  
    //nss.println(xbee.getResponse().getErrorCode());
  } else {
    // local XBee did not provide a timely TX Status Response -- should not happen
    Serial.println("Sin ACK");
  }
  Serial.println();
}

void sendXbee(const char* command){
  ZBTxRequest zbtx = ZBTxRequest(addr64, (uint8_t *)command, strlen(command));
  xbee.send(zbtx);
}
void repeats(){
  pin5 = analogRead(5);
  sprintf(Dbuf,"PRUEBA");  
  DHT22_ERROR_t errorCode;
  errorCode = myDHT22.readData();
  if (errorCode == DHT_ERROR_NONE){
    float temperature=myDHT22.getTemperatureC();
    float humidity= myDHT22.getHumidity();
    Serial.println(temperature);
    sprintf(Dbuf,"TEMP:%hi.%01hi#HUM_REL:%i.%01i#MOIST:%04d",
      myDHT22.getTemperatureCInt()/10, abs(myDHT22.getTemperatureCInt()%10),
      myDHT22.getHumidityInt()/10, myDHT22.getHumidityInt()%10,pin5); 
    //enviaDatos(Dbuf);  
  }  
   enviaDatos(Dbuf);
   //digitalWrite(PIN_PWR,LOW);
}