Fio + Xbee communication and variables transfer

Hi,

I need to transfer some variables(3bytes of data) over the XBee connected to Fio on one side and XBee connected to Uno on the other side.

I have tried the EasyTransfer libraryhttp://www.billporter.info/easytransfer-arduino-library/.But this has quite long response for my 4WD robot.

So I have written my code to send the variables.It is sending the Start byte(255) the ID byte(to identify which data I am sending) 3 bytes of data(3 variables 0-255) and two bytes of some check if the data was recieved correctly. It is working but only when I send this packet one per +-100ms which is even worse than the EasyTransfer.

What could be the problem?

Hardware:
Arduino Uno
Arduino Fio
XBee S2 with XB24-ZB firmware(Coordinator and end device)

Software:
TX side

int x=0;
int y=5;
int z=10;

int a=0;
int b=5;
int c=10;
int SB = 255;

void setup() {
  Serial.begin(9600);
}

void loop() {

//increase the values to simulate variables change
  x++; 
  y++;
  z++;

  SEND(2,x,y,z); // send variables x,y,z with ID=2
  delay(100);
  SEND(3,a,b,c); // send variables a,b,c with ID=3
  delay(100);

}

void SEND(int ID, int A, int B, int C){

  //count the checksum
  int CB1 = (A+B+C)/255;
  int CB2 = (A+B+C)%255;

  Serial.write(SB);  // startbyte
  Serial.write(ID);  // ID byte
  Serial.write(A);   //data
  Serial.write(B);   //data
  Serial.write(C);   //data
  Serial.write(CB1); //checksum1
  Serial.write(CB2); //checksum2

}

RX side

#include <SoftwareSerial.h>

SoftwareSerial mySerial(5, 6); // RX, TX

int SB;
int ID;
int data1;
int data2;
int data3;
int CB1;
int CB2;

int a;
int b;
int c;
int x;
int y;
int z;

void setup() {

  mySerial.begin(9600); // for the XBee
  Serial.begin(9600); // for the PC terminal
}

void loop() {

  if(mySerial.available()==7){ //check if 7 bytes are available 
    serialread();          
    setparameters(ID);

  }
}

void serialread(){
  
  //read all bytes
  SB = mySerial.read();
  ID = mySerial.read();
  data1 = mySerial.read();
  data2 = mySerial.read();
  data3 = mySerial.read();
  CB1 = mySerial.read();
  CB2 = mySerial.read();
 

}

boolean check(){
  
  int crc1 = data1+data2+data3; // count the "crc"
  int crc2 = (CB1*255)+CB2;     

  if(crc1==crc2){               // if "crc" is the same return true
    return true;
  }
  else{
    return false;
  }
}

void setparameters(int ID){
  if(check()){                  // if "crc" is true set the 
    if(ID==2){                  // recieved data to local variables
      x = data1;                // for each ID
      y = data2;
      z = data3;
    }

    if(ID==3){
      a = data1;
      b = data2;
      c = data3;
    }
    printout();               // print data to terminal
  }
  else
    RESET();                  // if "crc" is false 
}

void printout(){

  Serial.println("ABC");
  Serial.println(a);
  Serial.println(b);
  Serial.println(c);
  Serial.println(" ");

  Serial.println("xyz");
  Serial.println(x);
  Serial.println(y);
  Serial.println(z);
  Serial.println(" ");
  Serial.println(" ");
  Serial.println(" ");
}

void RESET(){                  //set all local variables to zero
  a = b = c = x = y = z = 0;
}

I hope someone will find where is the problem.Thank you very much for every input :slight_smile:

Sending an int using Serial.write() is not a good idea. How many bytes does it actually send?

  if(mySerial.available()==7){ //check if 7 bytes are available

What happens if there are ever more than 7 bytes? You will never read another thing.

You are doing a lot of printing at a really slow speed. Why is the Serial baud rate so low?

Sorry that is an old code.(but that worked same as the new one) The new one doesnt have integers but bytes.The Serial speed is 9600 because later I want to use XBee 900 XSC and it cannot work on higher rates.

The Serial speed is 9600 because later I want to use XBee 900 XSC and it cannot work on higher rates.

The baud rate that the Arduino talks, via the USB cable, to the PC has nothing to do with the baud rate that the XBee uses to talk to the Arduino.

Do you have a link to the slow XBees you are talking about?

here is the link for the XBee XSC XBee Pro 900 XSC Wire - WRL-09085 - SparkFun Electronics

I have changed the baud rate for Serial on higher rate(115200) and the result is still same.I cannot get under some 100ms. It is 50ms delay now, but I think that it could be something like 10ms or less...

I cannot get under some 100ms

You've got to provide a clue as to what happens when you try.

When I try to set the delay to something lower than 50ms on the TX side the RX side will not recognize the packet(nothing on the terminal is printed).

Did you change

  if(mySerial.available()==7){ //check if 7 bytes are available

to

  if(mySerial.available() >= 7){ //check if at least 7 bytes are available

like I suggested?

Yes but it had no effect.I am allways sending 7 bytes so this should be okay.

I have changed the TX code like this

void loop() {

//increase the values to simulate variables change
  x++; 
  y++;
  z--;

  SEND(2,x,y,z); // send variables x,y,z with ID=2
  delay(z);
  SEND(3,a,b,c); // send variables a,b,c with ID=3
  delay(z);

}

so with each SEND() the delay will be decreased.

on the RX side I am printing to terminal just the z variable and the output is here:

9
9
8
8
7
7
6
6
5
5
4
4
3
3
2
2
1
1
0
0
255
255
254
254

so it looks like very short delay can be achieved.But when I set the delay manualy to 10ms the RX side is getting nothing again :frowning: