Pages: [1]   Go Down
Author Topic: (Seemingly) Simple Xbee Communication handling problem  (Read 1115 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok,

Background:
I have successfully gotten direct communication between 2 Unos using Software Serial working.
My sending station sends the numbers 1 to 100 repeating to the receiving station which outputs it to the serial monitor. 

Here's my next challenge: I want to replace this direct connection with 2 Xbees.

In each case I have the Xbees and Uno's connected with 3 wires.
The sending station has 1 Gnd to Gnd, 1 5V to 5V, and 1 from Pin 3 to Rx on the Xbee.
The green light is flashing.

The receiving station has 1Gnd to Gnd, 1 5v to 5V and 1 from Pin 2 to Tx on the Xbee.
The green light is flashing and the red light is lit constantly.

My code for both stations is this:
SENDING
Code:
#include <SoftwareSerial.h>

SoftwareSerial Xbee(2,3);

int i;

void setup()
{
  Serial.begin(57600);
  Xbee.begin(57600); 
} // Setup

void loop()
{
 
   
  for ( i = 1; i <= 100; i++)
  {
   
   Xbee.print(i);
   Xbee.print('T');
   Serial.println(i);
   
   delay(500);
   
  }
 
} // Loop

RECEIVING
Code:
#include <SoftwareSerial.h>

SoftwareSerial Xbee(2,3);

int RecVar;
int input;

void setup()
{
  Serial.begin(57600);
  Xbee.begin(57600); 
} // Setup

void loop()
{
//  delay(50);
   
  if(Xbee.available())
  {
   
    RecVar = Xbee.read();
    //Serial.println(RecVar); //Troubleshooting step
    if(isDigit(RecVar))
     {
       input = (input * 10) + (RecVar - 48);
     }
    else
    {
      Serial.println(input);
      input = 0;
    }
   
  }
 
} // Loop

So when Directly Connected (1wire pin 3 to pin 2, 1 wire GND to GND) I get this:
Quote
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...

But when put through the Xbees I get:
Quote
2
3
4
5
6
8
9
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
2
0
2
0
2
0

What's the Xbee Doing?

Thanks for any help
Logged

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

Code:
    //Serial.println(RecVar); //Troubleshooting step
Why is this commented out? Invaluable data is being suppressed.
Logged

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

Man this seems to be stumping everyone...
Logged

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

You have a problem with your setup. You haven't provided enough information to enable anyone to help you. The one piece of code that would provide important information is commented out. I thought maybe you'd take the hint and uncomment it. I guess not. So, I'll tell you flat out. Uncomment that line and tell us what you get.
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 928
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Aluman, there's something missing here.  In the sending loop, you're sending a number and the letter T.   Then in the receive you're printing the number when you get the T.  Is that what you intended, or did I miss something?  Just guessing, but it looks like you're using broadcast and are not in API mode, so the packet size that is used is up to the XBee's timing.  At 57600, it'll probably fill up the softwareserial buffer and start losing stuff because it might be forming the packets larger than software serial can handle them and stuff is getting dropped or over written.

Also, in my experience, you can't send stuff to the XBee without giving it a chance to actually transmit it.  You reach a point where the XBee has to send the data and it will stop listening to you while it gets rid of what its already trying to handle.

Try putting a delay of about a second in the transmit loop just to get a feel for what is going on, and then play with it some.

Also, tell us which series of XBee you're using (1 or 2) and some information on how you've configured the devices.  Even something like, "Series 1, right out of the box with no changes," will help us understand what you've got going on.

Logged

Trying to keep my house under control http://www.desert-home.com/

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

"Series 1, right out of the box with no changes,"
This is exactly right.  With the antenna no less, not the chip.

I am transmitting 'T' just because my receiving code needs to see a non-numeric in order to know when one number stops and the next starts.
This seems to be a creation of the Arduino in that it doesn't send any message indicating string termination.
I had to add it in order for the code to work when direct connected.

I have had delays change things in interesting ways before, maybe it will work here.
I wish I knew the buffer sizes so that I could plan for this stuff...

I'll give this a try and I'll uncomment the code as well for grumpy PaulS.

Thanks for the help, both of you.
(PS finals are coming up)
Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 19
Posts: 928
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There's way too many articles out there that give the impression that all you have to do is hook them up and they just work.  Not entirely true; sometimes you have to do some special stuff.  But, once you get it talking somewhat, you'll get the hang of it.

I don't know where the user manual is on the Series 1 XBee, I only play with series 2.  Maybe someone will pop up with a link.  You can google it though and prowl through the million links; it's out there.

Logged

Trying to keep my house under control http://www.desert-home.com/

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

Ok, went home, changed the baud to 4800 for both modems and code and it works perfectly.

Even without any delays in either system.

Thanks so much for the help.
Logged

Pages: [1]   Go Up
Jump to: