Pages: [1]   Go Down
Author Topic: Xbee Network Troubles  (Read 419 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have four series one xbees in a network. One is set as the coordinator and the other three are end devices. All the configurations to get them to communicate are correct. They are operating at a baud rate of 9600.

Each end device is connected to an RFID scanner (baud rate is 9600) and an Atmega328 chip. When an RFID card is swiped across the scanner, the code is read and 6 bytes are transmitted to the coordinator that is connected to a computer. The coordinator is run by a program written in Processing. This all works just fine. However, when I want to send an array containing all the id's for acceptable cards, I run into problems.

The card list is stored as a 360 byte array on the Atmega chip and as 360 int array on the computer. This is because java only deals in signed bytes and the c code uses unsigned bytes. When I send data to the end device, I just type cast it to a byte.

I don't know why, but I don't think the end device is reading the correct data. I'm doing this in the AT mode for now, but I might switch to the API mode if I can figure it out.

Here is the section of code on the end device that receives the 360 byte list (in c):
Code:
int i=0;
  while(i<200) { //this is a timeout counter in case things go wrong
    delay(100); //used to make blue LED flash slower
    digitalWrite(ledBlue, LOW);
    if(Serial.available() > 0){                       //checks serial port
      if(Serial.read() == EXECUTE) {             //coordinator xbee saying it is ready to transmit
        Serial.write(EXECUTE);                     //telling coordinator that end device is ready to receive
        while(i<360){                                  //get next 360 bytes from serial
          if(Serial.available()>0){                   //check serial port
            cards[i] = (byte)Serial.read();       //store id data in array
            i++;                                          //increment counter
          }
        }
        i=0;                                            //reset counter so it can act as timeout counter
        numcards = Serial.read();               //store number of card id's
      }
      if(Serial.read() == CONTINUE){         //coordinator telling end device to continue normal operation
        Serial.write(CONTINUE);               //end device confirming command
        break;                                       //jump out of loop
      }
    }
    delay(100);                                    //another delay for LED flashing
    digitalWrite(ledBlue, HIGH);
    i++;                                             //increment timeout counter
  }

Here is the coordinator code that transmits the list of id's (in java):
Code:
if(send_command(EXECUTE)) {                 //tells end device to prepare to receive data, continues if end device responds
        println("----Update List-----");
        for(int j=0; j<360; j++){
          if(j%6 == 0) {
            print("\n");                                       //output for testing
            ccard = (j/6)+1;
            println(" -Card " +  ccard + "- ");
          }
          print(cards[j] + " ");
          xbee.write(cards[j]);                         //sends the id numbers
          delay(100);
        }
        print("\n");
        println("--------------------");
        println("Current Number of Saved Cards: " + current_number);
        xbee.write(current_number);                       //sends the number of id's
      }
    }
  }
    for(int i=1; i<2; i++){
    if(connected[i-1]){                 //ignore this for now
      connect_to_door(i);              //this makes sure it transmits to the correct end device
      send_command(RESUME);     //tells end device to continue normal operation
    }
  }

Any thoughts?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  while(i<200) { //this is a timeout counter in case things go wrong
    delay(100); //used to make blue LED flash slower
    digitalWrite(ledBlue, LOW);
    if(Serial.available() > 0){                       //checks serial port
      if(Serial.read() == EXECUTE) {             //coordinator xbee saying it is ready to transmit
        Serial.write(EXECUTE);                     //telling coordinator that end device is ready to receive
        while(i<360){                                  //get next 360 bytes from serial
First, code snippets never tell the whole story. Where is i defined? Why is it used in nested loops? Where is i set?

That last comment is wrong, since the loop variable is not set to 0, it does not get the next 360 bytes. For all we can tell, it will get anywhere from 161 to 360 bytes.

Where is cards defined? How is cards defined?

Wouldn't it make more sense to send the number of cards first, then send that number of ids, instead of sending all 360 values, most of which could be junk?

Quote
I don't know why, but I don't think the end device is reading the correct data.
You're not even sure that you have a problem, but you want us to fix it for you?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After I posted this I figured out the same things you mentioned. The counter was incrementing even if no new data was read and it was causing the end device to stop reading before all the data was sent. You are also right about the counter in that code snippet. I had it set to zero in the wrong place and it was causing the same problems you described.

Thank you for taking the time to point out my mistakes, though.
Logged

Pages: [1]   Go Up
Jump to: