3

I have a ladyada xbee adapter on the computer side and an arduino xbeeshield which I am trying to communicate with over wireless. Both xbees are configured correctly in that I can receive data from the xbeeshield to the computer. However it doesn't work the other way i.e. xbeeshield does not echo a byte sent from the computer serially. Any idea what I might be doing wrong? (Note: When I connect the arduino board to the computer using USB cable, the echo program works just fine. It seems to be a problem in wireless mode only)

processing code

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

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

I am just sending keystrokes from computer and waiting for a reply. I am not getting any.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
  • Can you please post the piece of code sending the byte to XBee and the piece of code where XBee receives it? – Cassio Sep 13 '11 at 17:49
  • Have added the code that runs on arduino. From computer side I am just using the arduino software's serial monitor to send bytes across. – Danushka Bandara Sep 13 '11 at 18:53
  • Ok, just let me get this. What is supposed to happen is that Arduino must reply to the computer the same thing he has just received. Is that right? – Cassio Sep 13 '11 at 19:05
  • I'm only going to have to test and evaluate this when I get home, but in the meanwhile, could you try putting Serial.read() and Serial.print() in different lines? – Cassio Sep 13 '11 at 19:13
  • Can you connect the xbee RX and Tx together (loopback) to verify the xbees are configured and communicating properly? – Jeff Sep 14 '11 at 12:07
  • Hmm, something weird. I just tried connecting Rx and Tx of the FTDI cable which connects the computer to xbee adapter. I still didnot get the echoed keystroke on serial monitor. Could this be something wrong with the cable? – Danushka Bandara Sep 14 '11 at 21:36
  • @DanushkaBandara Did my answer help at all? – gotnull Feb 19 '12 at 23:56

2 Answers2

0

Try using softwareSerial library and connecting Tx and Rx to pin 4 and 2. Run the following sketch and tell me what happens. Change the Baudrate value to match your own

#include <SoftwareSerial.h>

uint8_t pinRx = 2 , pinTx = 4; // the pin on Arduino

long BaudRate = 57600; // Please set your Baudrate. It should match the one in XC-TU
char GotChar, getData;
// Xbee SoftwareSerial initialization
SoftwareSerial xbee(pinRx, pinTx); // RX, TX

void setup() 
{
  Serial.begin(9600);
  Serial.println( "Welcome to the XBee Communication Test" );
  Serial.print("BaudRate:");
  Serial.println(BaudRate);
  Serial.print(" Rx Pin#");
  Serial.println(pinRx,DEC);
  Serial.print(" Tx Pin#");
  Serial.println(pinTx,DEC);
  // set the data rate for the SoftwareSerial port
  xbee.begin( BaudRate );
  xbee.println("Setup Completed!");
}

void loop() 
{
  if (Serial.available()) 
  {
    GotChar = Serial.read();
    xbee.print(GotChar);
    Serial.print(GotChar);
  }
  while (xbee.available()>0)
  {  
    Serial.println("Ohohoh");
    getData = xbee.read();      
    Serial.print(" Received: );
    Serial.print(getData);
    Serial.println();

    if(getData == 'a')
    {    
      Serial.println(" sbam");
    }  
    else if(getData == 'b')
    {
      Serial.println(" sbo");
    }
  }  
}

Upload the program and open the serial monitor. Do you get the 'Setup completed' message on the computer? What happens if you send 'a' or 'b' from the Pc to the Arduino?

UserK
  • 884
  • 3
  • 17
  • 40
0

I use the code I answered to the following question in regards to sending serial bytes from PC to Xbee/Arduino. It's been working fine for months. Ensure you've configured both your Xbee modules on the PC and Arduino side. Ensure your PAN ID's are the same as well.

Arduino making decision according to a packet received from serial port

What version of the Xbee modules are you using? My code works with Series 1 but should work with newer versions as well.

Community
  • 1
  • 1
gotnull
  • 26,454
  • 22
  • 137
  • 203