I am building a small network with about 10 XBee Series 2 transceivers and when I start up the network I want to do a node discovery to find all of the nodes on the network.
I developed the code shown below and it works find when there are only 3 nodes, coordinator and 2 routers. When I add additional nodes problems arise, the number of bytes read in is only 63. The packet length for a node discovery response is 29. The first two sets of 29 bytes of the 63 bytes are correct and correspond correctly to two of the routers on the net.
The code is rough right know so ignore the routines that get input from the user, this works fine and the node addresses are being parsed out correctly. I think the problem lies in the following statement:
n_bytes = Serial1.available();
Is there a buffer that is being overloaded when the 3rd or more router is added into the net?
Thanks for looking,
wade
#define max_Nodes 40
#define net_delay_time 10000 // How long to wait for modules top check in
int Verbose = 0;
// Function Prototypes
void node_discovery(void);
void send_packet(byte, int);
int get_node_number(void);
int get_UI(int);
int get_node_location(void);
int n_sensors;
int i;
int add_start = 14;
byte Node_16_add_MSB[max_Nodes], Node_16_add_LSB[max_Nodes];
int node_location[max_Nodes], node_number[max_Nodes];
byte value;
void setup()
{
Serial.begin(9600); // For serial monitor
Serial1.begin(9600); // For XBee
Serial.println("Initializing System");
node_discovery();
}
void loop()
{
while(1) {}
}
void node_discovery()
/*
From Faludi page 291:
ND is an AT command that discovers and reports all RF modules
found.
*/
{
int n_bytes;
byte discard;
int i_offset = 0;
int curr_cnt;
unsigned long bit_address, crap1, crap2, crap3, crap4;
/*
The packet below is from an example in the XBee Pro manual page
57.
*/
byte ND_packet[] =
{0x7E, 0x00, 0x04, 0x08, 0x01, 0x4E,0x44, 0x64};
Serial.println("Inside node_discovery()");
send_packet(ND_packet, sizeof(ND_packet));
delay(net_delay_time); // Delay giving nodes time to respond
n_bytes = Serial1.available(); // IS THE BUFFER BEING OVER LOADED?
if(n_bytes > 1)
{
discard = Serial1.read();
if(discard == 0x7e)
n_sensors = 1;
{
for (i = 1; i < n_bytes; i++) {
discard = Serial1.read();
if(discard == 0x7E)
{
n_sensors++;
i_offset =+ 29;
}
curr_cnt = i - i_offset;
// Get lower 32 bits of current ZigBee address
if(curr_cnt >= add_start & curr_cnt < add_start + 4)
{
if(curr_cnt == add_start)
{
crap1 = (unsigned long)discard << 0x18;
}
else if(curr_cnt == add_start + 1)
{
crap2 = (unsigned long)discard << 0x10;
}
else if(curr_cnt == add_start + 2)
{
crap3 = (unsigned long)discard << 0x08;
}
else // no if as if you are here curr_cnt == add_start + 3
{
crap4 = (unsigned long)discard;
// Serial.print("Crap4 = "); Serial.println(discard, HEX);
bit_address = crap4 | crap3 | crap2 | crap1;
// Output results
Serial.print("Node with the 32-bit address ");
Serial.print(bit_address, HEX);
Serial.println(" checked in");
}
}
if(curr_cnt > 7 & curr_cnt < 10)
{
if(curr_cnt == 8) // MSB of 16-bit address
{
Node_16_add_MSB[n_sensors] = discard;
}
else // LSB of 16-bit address
{
Node_16_add_LSB[n_sensors] = discard;
}
}
}
}
}
void send_packet(byte packet[], int n_bytes)
/*
This Routine just sends a packet out from the XBeePro Module, to send from
another radio system just change the function from XBeeSerial() to the new
radio serial port function and all is good.
*/
{
for (int ik = 0;ik < n_bytes;ik++)
{
Serial1.write(packet[ik]);
}
}