1

enter image description hereenter image description here

TX code:

#include <SoftwareSerial.h>

const int MAX_LENGTH = 2;
char buffer[MAX_LENGTH];
char lineFeed = 2;

SoftwareSerial   Xbee(10, 11);

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

void loop() {
  int length = Serial.readBytesUntil(lineFeed, buffer, MAX_LENGTH);
  int i = 0;
  while( i < length )
  { 
    Serial.print(buffer[i]-48); 
    Xbee.print(buffer[i]-48);  
    i++;
  }        
}

RX code:

const int MAX_LENGTH = 2;
char buffer[MAX_LENGTH];
char lineFeed = 2;

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

void loop() {
  int length = Serial.readBytesUntil(lineFeed, buffer, MAX_LENGTH);
  int i = 0;

  while( i < length )
  { 
    Serial.print(buffer[i]-48);
    i++;
  } 
}

NOTE :

1- The Rx xbee is receiving data even if am not sending and even if the TX xbee is powered off

2- When i send data from the TX xbee id didn't get revived by the RX xbee

I need to solve both problems.

*** What i need exactly is to send serial data from my pc to arduino and send it wireless through xbee and then receive to the other arduino connected to my circuit

2

The reason that it is printing characters with no input is that the readBytesUntil function has a timeout. If no new characters are received for a certain amount of time, "Serail.setTimeout()" seconds, it will just continue with the code. the default timeout is 1 second.

The reason you are not receiving what you transmit (or at least a reason) is that the RX code does not read from an xbee, but reads from regular "serial".

I recommend you try something simple first to test the xbee's configuration, like having TX run this:

void loop(){
    xbee.println("Hello");
    delay(1000);
}

and RX run something to echo serial like this:

 void loop(){
     if(xbee.available()){
         Serial.print((char)xbee.read());
     }
}

And make sure you set up an xbee object an the receiving side first.

| improve this answer | |
  • i already make a simple code for both TX and RX and it works fine i appreciate your answer but i need to know how to differentiate between regular Serial and Xbee because i am connecting the xbee to the hardware serial 0 and 1 of the arduino so in this case regular serial and xbee are the same , – Abdelrahman Elshafiey Sep 16 '14 at 11:57
  • 1
    You can't differentiate between the PC and an Xbee if they are both using hardware serial. The standard serial can only go between two devices. If it worked with all three at all, its only because there was only one transmitter at a time. The random characters could actually be from hooking 3 serial devices together. – BrettAM Sep 16 '14 at 15:54
  • so why did they make a software-serial if its not gonna work for three devices at a time ? – Abdelrahman Elshafiey Sep 16 '14 at 15:59
  • can i add a diagram of what i need exactly so you can understand my problem ? – Abdelrahman Elshafiey Sep 16 '14 at 16:01
  • Using "Serial" for the receiver will work for a circuit as you have diagrammed because the second arduino does not connect to a PC; However, your diagram does not match the code you put up. The code (Both arduino's connect to PC) requires the use of multiple serial ports on each arduino, which means using software serial on Uno's. – BrettAM Sep 16 '14 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.