Pages: [1]   Go Down
Author Topic: Xbee Communication between 2 arduinos  (Read 1095 times)
0 Members and 1 Guest are viewing this topic.
Barcelona
Offline Offline
Newbie
*
Karma: 0
Posts: 19
The Dude abides
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all!
I am trying to connect 2 arduinos on a PC and have one sending wirelessly data (through a Xbee module) to the PC and passing them on to the other arduino through another Xbee module.
So the setup is that I have one arduino with some sensors (4 in total) which is wireless with a xbee module, one receiving end connected to the PC with another Xbee module, a third arduino with an Xbee connected to the PC to act as the transmitting module, and finally a remote (wireless) arduino with an Xbee module with some lights.
I want whenever one sensor is activated on the first described arduino, to trigger one specific output (light), on the last described arduino.
What would be the easiest way to achieve this?

thanks in advance.

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What would be the easiest way to achieve this?
I got lost reading what you are trying to do. A picture is worth a thousand words.

Quote
I am trying to connect 2 arduinos on a PC and have one sending wirelessly data (through a Xbee module) to the PC and passing them on to the other arduino through another Xbee module.
You want to two Arduinos that are wired to the PC to share data wirelessly. Why?

I can't figure out whether you have two Arduinos or three.
Logged

Barcelona
Offline Offline
Newbie
*
Karma: 0
Posts: 19
The Dude abides
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, sorry about the confusion.

I just have some pressure sensors with which i want to control some lights. The sensors i want to control wirelessly, and the lights will also be wireless. All through Xbee.
All that i have connected on the pc is one arduino to receive the data (through Xbee) of the sensors, and a second arduino to transmit the data (via Xbee again) to the lights.

In total i am using 4 arduinos with Xbees:
- one for the sensors
- one for receiveing the sensor data at the PC
- one for transmitting the data from the PC
- one for the lights
Logged

Barcelona
Offline Offline
Newbie
*
Karma: 0
Posts: 19
The Dude abides
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just to clarify even further, the idea is that i want to have two self powered modules: one for sending the data and one for receiving them, and I would like to know how I could coordinate this.

Any help is welcome..

thanks.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, what kind of XBees do you have? If they are Series 1, and are configured correctly, there will be no interference. You'll just have a series of discrete events that the other Arduinos/XBees are not even aware of.

Ard1 with sensors will send data only to/receive data only from Ard2.

Ard2 will be connected to the PC, on one COM port.

Ard3 will be connected to the PC, on another COM port.

Ard4 will be connected to the lights.

Ard1 will talk to Ard2, only.
Ard2 will talk to the PC and Ard1.
Ard3 will talk to the PC and Ard4.
Ard4 will talk to Ard3, only.

The communication involving Ard2 and Ard3 will need to include some ID, so the PC and other Arduino will be able to distinguish messages for themselves vs. messages for the other device.

So, Ard2 might do Serial.print("PC: do this") or Serial.print("A2: do that"). The PC and Ard1 would ignore messages for the other recipient.

The same would hold true for Ard3.

Otherwise, the plan is fairly straight-forward.

What are Ard2 and Ard3 actually doing? Just passing serial data from the XBee to the PC and from the PC to the the XBee? If so, a USB explorer would be cheaper.
Logged

Barcelona
Offline Offline
Newbie
*
Karma: 0
Posts: 19
The Dude abides
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So sorry about the late reply, but I finally got the setup working. Thanks for all your replies.
Just to clarify:
I had one battery-powered arduino UNO with an Xbee shield (1) which had some lights to be controlled connected on it's PWM outputs, communicating with an arduino UNO (without the microcontroller) with an Xbee shield (2) which was connected to the PC.
Then I had one battery powered Arduino Diecimila with an Xbee Shield (3) which had the sensors on its analog inputs, communicating with an arduino Diecimila (without the microcontroller) with an Xbee shield (4) which was connected to the PC.
What I wanted to do is to trigger the different sensors in '4' and these would in turn trigger and controll their respective lights in '1'. In order to do this I wrote a very simple program in Processing for the communication between the two modules. What I figured out on the way, and this might be of use to anyone who might have the same issues in the future, is that I had to configure the two pairs of Xbees in different baud rates for efficient communication. So I set '1' and '2' @ 115200 baud, while '3' and '4' @ 57600. I chose the highest ends to ensure that I had minimal latency in the signal transmission.
Still one thing that I cannot figure out is the following. Although everything works pretty smoothly, I don't understand one piece of code which was by accident. Let me elaborate:
This is the code  that I wrote for the receiving end (1):
Code:
int light1=3;
int light2=5;
int light3=9;
int light4=11;

int val1;
int val2;
int val3;
int val4;
int val5;

void setup()
{
    Serial.begin(115200);  // the receiving end @ 115200
    pinMode(light1, OUTPUT);  // set light pins as outputs
    pinMode(light2, OUTPUT); 
    pinMode(light3, OUTPUT); 
    pinMode(light4, OUTPUT);       
}

void loop(){
   
    if (Serial.available()>0){
          val1=Serial.read(); // read the input from the Serial port (from the sensors)
          if (val1<10 || val1>255){ // shield the outputs from noise
            val1 = LOW;
          }
          analogWrite(light1,val1); //write the first value to the first light and so on..
          val2=Serial.read();
          if (val2<10 || val2>255){
            val2 = LOW;
          }
          analogWrite(light2,val2);
          val3=Serial.read();
          if (val3<10 || val3>255){
            val3 = LOW;
          }
          analogWrite(light3,val3);
          val4=Serial.read();
          if (val4<10 || val4>255){
            val4 = LOW;
          }
          analogWrite(light4,val4);
          val5=Serial.read();
    }
               
       //NO IDEA WHY THIS IS NEEDED ?!?!?!
        Serial.print("val1: ");
        Serial.print(val1);
        Serial.print(", val2: ");
        Serial.print(val2);
        Serial.print(", val3: ");
        Serial.print(val3);
        Serial.print(", val4: ");
        Serial.println(val4);
       
     
}   

Note that the sending arduino '3' sent in every cycle 4 values (from each of the four sensors) plus one '/' to indicate the end of transmission per cycle. What I do not understand is that while I initially had the Serial.print lines just for debugging purposes and to monitor what was coming in, when I removed those the whole thing didn't work and the output pins were all set to HIGH apparently. Furthermore, even when I just left the Serial.print(val#) lines and commented the Serial.print(", val#: ") the thing still didn't work!!
I am happy that the whole thing worked in the end, but I am still baffled by this. Can someone help me understand the reason for this?
This is just for my own peace of mind..
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I am still baffled by this. Can someone help me understand the reason for this?
Pretty simple really.

Code:
    if (Serial.available()>0){
          val1=Serial.read(); // read the input from the Serial port (from the sensors)
          val2=Serial.read();
          val3=Serial.read();
          val4=Serial.read();
          val5=Serial.read();
    }
If there is at least one byte to read, read all 5 of them. Usually, I'd ask a smart-ass question like "How's that working out for you?", but I think we already know the answer to that question.

By adding a bunch of time-wasting output, you allow for more data to arrive, so that even though you are only checking for at least one character, there has been enough time elapsed for more all the data in a packet to arrive.

You really shouldn't be assuming that there will never be a data loss, and you should not be reading more data than is present in the serial buffer.
Logged

Barcelona
Offline Offline
Newbie
*
Karma: 0
Posts: 19
The Dude abides
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks man!

That really puts things into place now! So you mean that if I introduced a longer delay (currently its set to 10 ms) at the transmitting end (say like 50ms) this would resolve the issue and I could remove the 'Serial.print's?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So you mean that if I introduced a longer delay (currently its set to 10 ms) at the transmitting end (say like 50ms) this would resolve the issue and I could remove the 'Serial.print's?
You don't need to change the delay on the sending in, and you can remove the Serial.print()s.

You do need to re-write the code to read the data to wait for the correct number of bytes (if(Serial.available() >= 5)) or read whatever is available, storing it in a buffer, until the end-of-packet marker arrives (preferred).
Logged

Barcelona
Offline Offline
Newbie
*
Karma: 0
Posts: 19
The Dude abides
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cheers Paul.
I learned something today.
that is good.
Logged

Pages: [1]   Go Up
Jump to: