Problems interfacing Arduino with XBee via (AT)

Hi,

I need to make a wireless network with XBee's and Arduino, I'll be using a Mesh topology network. I'm using XBee Series 1 with the new Mesh Firmware.

The problem is that I need to set an address for each XBee without having to configure each one, I mean I just want every Arduino to set a unique address to its XBee automatically everytime they are powered on.

For that I was planning to use AT commands to get the low part of the serial number of each XBee and set it as node ID. It works fine sometimes but not always. If I plug the arduino it works most of the time but many times if I reset it or just unplug and plug again it won't do it right.

I'm tired of looking at the code and trying to find the problem but I have been imposible for me to find the problem.

I appreciate it if anyone could point me in the right direcction. My guess is that the problem is in the arrays declaration or size or something like that but what I can't understand is how the same arduino with the same code works sometimes and sometimes doesn't.

Thank you!

The code:

#include <stdio.h>
#include <string.h>
char datoRX[11]= "01234567x" ;
char datoTX[15]={"0123456789012x"};

int i;
void setup(){
Serial.begin(115200);
delay(10000);
setNI();
}

void loop(){

}

void setNI(){
int i=0;
Serial.flush();
Serial.print("+++");
//Wating to get the "OK\r" response from the XBee
char thisByte = 0;
while (thisByte != '\r') {
if (Serial.available() > 0) {
thisByte = Serial.read();
}
}
thisByte='0';
i=0;
//Getting the low part of the Serial Number
Serial.flush();
Serial.print("ATSL\r");
while (thisByte != '\r') {
if (Serial.available() > 0) {
thisByte=Serial.read();
datoRX*=thisByte;*

  • i++;*

  • delay(20);*

  • }*

  • }*

  • datoRX[11]='\0';*

  • strcat(datoTX,"ATNI");*

  • strcat(datoTX,datoRX);*

  • for (i=0;i<=14;i++){ *
    _ Serial.print(datoTX*); //Configuring the SN as NI._
    _
    delay(20);_
    _
    }_
    _
    digitalWrite(13,HIGH);*_

}

Sorry didn't see your message until now. I'm a bit of a newbie with this so i can only offer some possibly incorrect advice :slight_smile:

It isn't much fun using the serial on arduino. I prefer the picaxe system in some ways although it is very limited and unuable for most projects.

Anyway..

I don't know much about the arduino but i don't think you need a long delay after calling serial begin. the hardware serial begin doesn't do anything except allocate some buffers and set some vars?

I might wrongly assume that the arduino chip does some stuff between setup and loop? You might consider calling setNI() from within the loop() and using a bit/bool to prevent calling it more than once

Q: Does this fail in standalone mode, arduino not connected to usb/serial?

remove all delays except for one second after sending +++ or any at command? def required for +++

how about checking that the value of 'i' doesn't go to high

if (Serial.available() > 0) {
thisByte=Serial.read();
datoRX*=thisByte;*

  • i++;*
  • delay(20);*
  • }*
  • }*

6)
here are you sure you will get 14 bytes back?

  • for (i=0;i<=14;i++){ *
    _ Serial.print(datoTX*); //Configuring the SN as NI._
    _
    delay(20);_
    _
    }_
    _
    digitalWrite(13*_
    7)
    debug
    can you add a few debug functions that flash the led a different number of times depending on where your code hits an unexpected problem?
    8)
    if you set your xbee to 9600 or a lower baud speed do you still get the problem? I have read articles that suggest they work best at 38k (or whatever the 30 speed is i can't recall :slight_smile: )
    Mine do work okay at 115k but i haven't tried AT commands at that speed
    Hope this helps