XBee Serial Communication slows down with time, cable connection doesn't.

Hello guys,
This is my setup. I have two arduinos(Mega2560,UnoR3). I am using these to make a RC plane with the help of XBee S2 (XB-24) modules. Lets say that I am trying to control 3 Servos with the help of 2 joysticks(X-Y, 2 pots with a push button).

/* TX
 */
 
#define aileron 2
#define elevon 3
#define esc 4

#define elevontrim 0
#define ailerontrim 1
#define esctrim 5

int ailerontrimVal,elevontrimVal,aileronVal,elevonVal,esctrimVal,escVal;

void setup()
{
  Serial.begin(9600);//1152200 only 9600 works, check XBEE forum
}

void loop()
{
  aileronVal = analogRead(aileron);
  aileronVal = map(aileronVal, 0, 1023, 0, 179);
  ailerontrimVal = analogRead(ailerontrim);
  ailerontrimVal = map(ailerontrimVal, 0, 1023, -25, 25);
  aileronVal = aileronVal+ ailerontrimVal;
  constrain(aileronVal, 0, 179);
  
  elevonVal = analogRead(elevon);
  elevonVal = map(elevonVal, 0, 1023, 0, 179);
  elevontrimVal = analogRead(elevontrim);
  elevontrimVal = map(elevontrimVal, 0, 1023, -25, 25);
  elevonVal = elevonVal+ elevontrimVal;
  constrain(elevonVal, 0, 179);

  escVal = analogRead(esc);
  escVal = map(escVal, 0, 1023, 0, 179);
  esctrimVal = analogRead(esctrim);
  esctrimVal = map(esctrimVal, 0, 1023, -25, 25);
  escVal = escVal+ esctrimVal;
  constrain(escVal, 0, 179);
  
  Serial.print("L");
  Serial.print(aileronVal);
  Serial.print("R");
  Serial.print(elevonVal);
  Serial.print("S");
  Serial.print(escVal);
  //delay(5);
}
/* RX
 * State machine
 */
 
 #include <Servo.h>
 
 #define laileron 11
 #define raileron 10
 #define throttle 9
 
 typedef enum{NONE,GOT_L,GOT_R,GOT_S} states;
 Servo left,right,esc;
 states state = NONE;
 unsigned int currentValue;
 
 void setup()
 {
   Serial.begin(9600); //1152200 only 9600 works, check whats wrong
   state=NONE;
   left.attach(laileron);
   right.attach(raileron);
   esc.attach(throttle);
 }
 
 void processLeft(const unsigned int value)
 {
   left.write(value);
 }
 
  void processRight(const unsigned int value)
 {
   right.write(value);
 }
 
  void processEsc(const unsigned int value)
 {
   esc.write(value);
 }
 
 void hanldlePreviousState()
 {
   switch(state)
   {
     case GOT_L:
       processLeft(currentValue);
       break;
     case GOT_R:
       processRight(currentValue);
       break;
     case GOT_S:
       processEsc(currentValue);
       break;
   }
   currentValue=0;
 }
 
 void processIncomingByte(const byte c)
 {
   if(isdigit(c))
   {
     currentValue *= 10;
     currentValue += c-'0';
   }
   else
   {
     hanldlePreviousState();
     
     switch(c)
     {
       case 'L': state=GOT_L;
         break;
       case 'R': state=GOT_R;
         break;
       case 'S': state=GOT_S;
         break;
       default: state=NONE;
     }
   }
 }
 
 void loop()
 {
   if(Serial.available())
     processIncomingByte(Serial.read());
 }

I know the code works fine because when I connect the two Arduinos using the TX,RX pins it works without any trouble. But when I try to use the Serial connection of the 2 XBEEs it gets slow with the time. It works like the wire connection at first but after some time it doesn't even work.

My Xbees are setup like this.

One I am using at the transmitter is the Router which is set to AT mode (Zigbee router AT: command set in X-CTU) has following set.
PanID (ATID) =0488
Channel verification (ATJV) = 1
ATDH = 0

The other one is the Coordinator which is the receiver for my plane is also in AT mode (Zigbee coordinator AT) has following set
PanID = 0488

others are all set to the factory defaults.

Why is my serial connection laggy with XBEEs?
Anyone could point me to an answer would receive my gratitude! :slight_smile:

Bhashithe

One I am using at the transmitter is the Router which is set to AT mode (Zigbee router AT: command set in X-CTU) has following set.
PanID (ATID) =0488
Channel verification (ATJV) = 1
ATDH = 0

The other one is the Coordinator which is the receiver for my plane is also in AT mode (Zigbee coordinator AT) has following set
PanID = 0488

First, why are you using mesh network models when you want point to point communications?

Second, the 4 important values are SH, SL, DH, and DL. They define which XBee each is to talk to. Leaving the XBees in broadcast mode is NOT the way to get speed out of them.

Hello Paul,
I have interchanged SH and SL with DH and DL and networked the XBEEs according to this : XBee Series 2 Point to Point Communication tutorial, I have the same problem.

It works fine if i keep changing the values of the POTs but when i keep idle for about 30s. it stops comunicating with the other XBee. Why is this?

I have tried with EasyTransfer library and it presents the same problem. I know this is not a programming problem on the Arduino since it works absolutly fine when I have connected the two with the TX RX pins.

Plus I have the ZB ZigBee module not an 802.15.4 OEM RF Module. Since i needed more range than the 802.15.4 OEM RF module. And for ZB having a Coordinator is a must.

I have tried like this, if I cut the power to the coordinator and reset the power (restart it) it works fine again.
Is there a system where you stop the coordination if there was no communication for a time period? (like sleep?) I guess someting like this is happening. I don't know much about XBee networks to know for sure.

OK I think i have a fix for the situation.

I tried interchanging the router and coordinator, but it didn't work. I have found out that there is a Sleep time and a number of time for a end device to go to sleep. But i havent stumbled upon any post to say that a router goes to sleep(end device routers do right?) I have found a nice article about common xbee mistakes: http://www.faludi.com/projects/common-xbee-mistakes/ Under Arduino mistakes they have specified us to make a delay atleast 10ms. I have added that in to the TX code and now it works. without any trouble.

Didn't know that a delay could lead to such an error. Thank you paul! :slight_smile:

Didn't know that a delay could lead to such an error.

It's not the delay() that causes a problem. It's that the lack of a delay does not allow the XBees time to deal with a missed or mangled packet. Adding a small delay() on the sending end allows the XBees time to retransmit a mangled/missed packet.

There was an I Love Lucy episode that illustrated the point perfectly. Lucy and Ethel were working in a candy factory, wrapping pieces of candy as they were made. At first, the conveyor belt was moving slowly, and Lucy and Ethel had no problems wrapping all the candy and chatting and goofing off. Then, the speed picked up, and they no longer had time to goof off. Faster, and they couldn't even talk. Faster, and they could simply not keep up.

That's what you are doing to the XBees - sending them data faster than they can reliably transmit data. Note that this is not the same as "faster than they can transmit data". If the communication didn't need to be reliable, you wouldn't need the delay. On the other hand, if it didn't need to be reliable, you wouldn't be using XBees.

Yeah thanks mate. I am new to this whole thing :slight_smile:
Nick Gammons website and you pointing that out in another thread did helped me a lot.

I now need to create the code snippet where one XBee goes out of the range and loose signal. I hope i don't have an option to trigger an envent in the Xbee itself to do this? Then using the Data pins in Xbee send that to the arduino?

I hope i don't have an option to trigger an envent in the Xbee itself to do this?

You hope you don't have that option? Well, then, you're in luck, because you don't.

Ah sorry English is not my mother tongue :relaxed:

Well back to the program again!