Baud Rate/ Serial Timing Problem?

Hello,
I have two arduinos; an ATmega1280 and Duemilanove ATmega328. Both of these arduinos have XBees with the latest firmware, AES Encryption enabled, and 9600 baud rate. I am trying to run a local send/recieve session between the arduinos using the XBees. The arduinos seem to communicate correctly; I print something to serial from one, and I see it pop out on the Serial of the other... The problem I am having, however, is that when I am printing from the ATmega1280(Client) to the ATmega328(Server) I have to send the, "command", byte twice. The Server doesn't ever seem to read the first byte. I have tried changing the baud rates to 19200, and 115200 on the XBees, the software I wrote, and the Serial Monitor. With a baud rate of 115200 everything printed on the Serial Monitor is garbled, whereas with the baud rate at 19200, only the first character becomes garbled. If I write the command twice and have the baud rate at 9600 it seems to work, whereas at any other baud rate it doesn't. I have pasted some of my code below.

Server.ino:

const unsigned int BAUD_RATE = 9600;
const int TIMEOUT = 2000;

const byte ID = B00000011;
const byte CLIENT_ID = B00000001;
const byte OPEN_ID = B00000111;

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

void loop()
{
  // Scanning for header
  if(Serial.available() > 0)
  {
    byte uID = Serial.read();
    if(uID == ID)
    {
      // Identifier found, read command.
      ReadCommand();
    }
  }
}

void ReadCommand()
{
  // 4320000000 max before overflow
  unsigned long lapse = millis() + TIMEOUT;
  while(millis() < lapse)
  {
    if(Serial.available() > 0)
    {
      byte uAc = Serial.read();
      switch (uAc)
      {
      case OPEN_ID:
        OpenDoor();
        break;
      }
    }
  }
}

void OpenDoor()
{
  SendResult(true);
}

void SendResult(boolean result)
{
  if(result)
  {
    Serial.write(SUCCESS);
  }
  else
  {
    Serial.write(FAILURE);
  }
}

Client.ino

// Constants
const unsigned int BAUD_RATE = 9600;
const int TIMEOUT = 2000;

const byte ID = B00000001;
const byte SERVER_ID = B00000011;
const byte OPEN_ID = B00000111;

// Fields
boolean toggle;

void setup()
{
  Serial.begin(BAUD_RATE);
  toggle = true;
}

void loop()
{
  if(toggle)
  {
    SendDoorOpen();
    toggle = false;
  }
}

void SendDoorOpen()
{
  // Need to write twice to work :(
  Serial.write(SERVER_ID);
  Serial.write(SERVER_ID);

  Serial.write(OPEN_ID);
  
  if(ReadResult())
  {
    Serial.println("Door succesfully opened.");
  }
  else
  {
    Serial.println("Door failed to open.");
  }
}

boolean ReadResult()
{
  // 4320000000 max before overflow
  unsigned long lapse = millis() + TIMEOUT;
  while(millis() < lapse)
  {
    if(Serial.available() > 0)
    {
      byte response = Serial.read();
      return response == SUCCESS;
    }
  }
  return false;
}

Both of these arduinos have XBees with the latest firmware, AES Encryption enabled, and 9600 baud rate.

Xbees are not generic things like 10k Ohm resistors. Which XBees? How are they configured?

XBee pro 802.15.4 on version 10ED with default firmware. The only value I changed is the AES Encryption, which i enabled and set an AES Encrption key.

The only value I changed is the AES Encryption, which i enabled and set an AES Encrption key.

So, both XBees are still using the default DL value of 0? So, everything is being broadcast to all other XBees?

That is NOT the proper way to do encrypted transmission, or any point-to-point, for that matter.

Assign DL and MY on both XBees. DL on one must match MY on the other. The exact values do not matter, except that the should not be 0.

So, both XBees are still using the default DL value of 0? So, everything is being broadcast to all other XBees?

That is NOT the proper way to do encrypted transmission, or any point-to-point, for that matter.

Assign DL and MY on both XBees. DL on one must match MY on the other. The exact values do not matter, except that the should not be 0.

I have set the DL on the client to 500 and the MY to 1000. I have set the DL on the Server to 1000 and the MY to 500. The problem still persists; the initial byte written by the client is not getting picked up by the Server.

Do you have the same problem without AES encryption?

Do you have the same problem without AES encryption?

Yes, here are my settings.

Client:

Server:

Sample code I loaded.

Client:

boolean toggle;
void setup()
{
  Serial.begin(9600);
  toggle = true;
}

void loop()
{
  if(toggle)
  {
    //delay(10);
    Serial.println("Hello");
    toggle = false;
  }
}

Server:

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

void loop()
{
  if(Serial.available())
  {
    Serial.write(Serial.read());
  }
}

Results:

Second set of results after uncommenting the delay(10); in the Client code.

As you can see, as I stated above, the Server (Or client if the code is switched around) never reads the first byte of data, or adds in some garbage byte of data!

edit: if i set the delay to 1ms i get garbage on the first byte, if i set it to .99 i get no byte.

edit 2: ran a range test to see if the problem was interference, but I got clean results...

falven:
edit: if i set the delay to 1ms i get garbage on the first byte, if i set it to .99 i get no byte.

The delay() function takes an integer argument, so 0.99 is the same as zero, that is, no delay.

It isn't particularly unusual for serial comms to need a brief "settling" period. After all, the clock is an implied clock (implied by the baud rate). I wouldn't get too excited if the first byte got lost.

It isn't particularly unusual for serial comms to need a brief "settling" period. After all, the clock is an implied clock (implied by the baud rate). I wouldn't get too excited if the first byte got lost.

Thank you for your input.

I, however, believe that this issue stems deeper than just a "settling period", because I have not been able to make the Xbee and Arduino work at any Baud rate higher than 19200. (Setting the baud rate of both the XBee and Arduino to the same values...)

At any pre-set baud rate > 19200, i get no data.

Any ideas?

Edit: Here's the output for data at a baud rate of 19200

Edit 2:
When I set the baud rate on the Xbees to 38400 and in Serial.begin(38400), I see no output in the Serial Monitor, at all. however, If I change the baud rate on the program to say 40000, I can see garbled data. Does this mean the XBees are running at a different baud rate than the one I wrote to them?But that wouldn't make sense because X-CTU can Test / Query them at the Baud Rate I wrote to them (38400)?

There are a total of 6 baud rates involved - the rate that the PC talks to the Arduino with the 1st XBee, the rate that that Arduino talks to the PC/XBee, the rate that the first XBee talks to the other XBee, the rate that the second XBee talks to the other XBee, the rate that the 2nd Arduino talks to the XBee, the rate that the 2nd Arduino talks to the PC, and the rate that the PC talks to the 2nd Arduino.

Obviously, the 1st two need to match, so the Arduino and PC can understand each other. The 2nd pair need to match, so that the XBees can understand each other. The third pair needs to match so that the 2nd Arduino and PC can understand each other.

What might not be so obvious is that the 6 rates all need to be the same, so that the Arduino and XBee can communicate, since you are using the same port to talk to the PC and the XBee.

Typically, this is not a good idea. Depending on the XBee shield you have, you may be able to select a different set of pins for the Arduino and XBee to talk to each other on, using NewSoftSerial (0023-) or SoftwareSerial (1.0+). Then, you can use different speeds, if needed. Standard speeds, of course. 40000 is not a standard value.

If you can't do that, add a small delay() to setup() (maybe half a second), to give the XBees time to boot up, too.

Even with the XBees on different pins, the small delay in setup() may be required.

PaulS:
... the rate that the first XBee talks to the other XBee, the rate that the second XBee talks to the other XBee ...

I'm not aware that these are observable parameters, let alone configurable. My understanding is that how the XBees communicate to each other is a constant, and is independent of the settings for their local serial port, even to the extent of AT vs. API mode.

What might not be so obvious is that the 6 rates all need to be the same, so that the Arduino and XBee can communicate, since you are using the same port to talk to the PC and the XBee.

I'd be very surprised if I could not configure two XBees for different baud rates and still have them communicate perfectly. In fact, I'm pretty sure I've done this with the S2 modules. falven is using S1? Is there something about the S1 modules that is different from the S2 in this regard?

If it were me, at this point, I'd get the Arduinos out of the loop, and just try to make the XBees talk between each other, either with two terminal programs running on one PC, or one terminal program running on each of two PCs.

Well? I still don't have an inkling as to how to fix this problem... This is pretty serious too, wait 'till you guys see this gem...

I am really surprised to see that nobody else has had this problem, at least I haven't been able to find a thread similar to this for the past couple of days...

falven:
Well? I still don't have an inkling as to how to fix this problem... This is pretty serious too, wait 'till you guys see this gem...

I am really surprised to see that nobody else has had this problem, at least I haven't been able to find a thread similar to this for the past couple of days...

That is a gem all right. Darn things behave pretty well for me. Any chance of trying the XBee-to-XBee thing without the Arduinos? I'm not entirely clear yet, these are Series 1 Pro models? (A link or exact model number would be great) What are you using for adapters and how are they powered?

That is a gem all right. Darn things behave pretty well for me. Any chance of trying the XBee-to-XBee thing without the Arduinos? I'm not entirely clear yet, these are Series 1 Pro models? (A link or exact model number would be great) What are you using for adapters and how are they powered?

From left to right:

  1. Arduino Duemilanove http://arduino.cc/en/Main/ArduinoBoardDuemilanove (Atmega328 edition) with XBee Pro s1 http://www.sparkfun.com/products/8742
  2. Arduino Mega http://arduino.cc/en/Main/ArduinoBoardMega (Atmega1280 edition) width XBee Pro s1 http://www.sparkfun.com/products/8742
  3. XBee Explorer USB http://www.sparkfun.com/products/8687

Again, configuration and code is in Reply #6.

I only have one XBee Explorer to connect to the computer. Unless there is another way to connect an XBee... I might have an FTDI cable laying around, I will have a look.

Thanks, the pic helps! Where do the shields come from?

Thanks, the pic helps! Where do the shields come from?

They are the original Arduino XBee Shields, I think I got them from Amazon (New, I don't buy used parts).

Yep just found that, thanks. You could use the explorer for one XBee and do this for the other:

With the jumpers in the USB position (i.e. on the two pins nearest the edge of the board), the DOUT pin the Xbee module is connected to the RX pin of the FTDI chip, and DIN on the Xbee module is connected to the TX pin of the FTDI chip. This means that the Xbee module can communicate directly with the computer - however, this only works if the microcontroller has been removed from the Arduino board. If the microcontroller is left in the Arduino board, it will be able to talk to the computer normally via USB, but neither the computer nor the microcontroller will be able to talk to the Xbee module.

Is there any way to pull out the microcontroller with lowest riskof breaking it :o just tried pulling it a little, it's really in there...

falven:
Is there any way to pull out the microcontroller with lowest riskof breaking it :o just tried pulling it a little, it's really in there...

They can be tough to get started. I use a very small flat-blade screwdriver, work from one end then the other, little by little. I think there's really little chance of breaking it.

The other thing to try is to take the shields off, wire the TX from one Arduino to RX on the other and vice versa, see if the little test sketch works that way. Just trying to eliminate some variables. But if that works OK, then I'd still want to test the XBees standalone.

Ok, I got it off :slight_smile: any specific operations you think I should perform?