Simple XBee hookup

Hi,

I’m working through the tutorials that use the xbee library and NewSoftSerial. The disconnect in understanding here is where do you define which pin the XBee object actually listens for the data coming in? I know the data is coming in, as I can check it by hooking up an LCD to the TX pin of the XBee. However, it isn’t clear to me how the XBee object knows which pin to listen on for data.

I ran the each of the xbee series 1 examples and hooked up the lines to the pins specified in the example source. However, my code never enters the ‘meat’ of the logic (i.e. never gets true for xbee.getResponse().isAvailable()). This makes me think that the xbee object isn’t listening to the data or I have a configuration problem of some kind.

//attempt to read a packet    
  xbee.readPacket();

  if (xbee.getResponse().isAvailable()) {
    // got something
    Serial.println("Got something!");
    nss.println("Got something!");

    if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {
      xbee.getResponse().getRx16IoSampleResponse(ioSample);

      nss.print("Received I/O Sample from: ");
      nss.println(ioSample.getRemoteAddress16(), HEX);  

      nss.print("Sample size is ");
      nss.println(ioSample.getSampleSize(), DEC);

      if (ioSample.containsAnalog()) {
        nss.println("Sample contains analog data");
      }

      if (ioSample.containsDigital()) {
        nss.println("Sample contains digtal data");
      }

      for (int k = 0; k < ioSample.getSampleSize(); k++) {
        nss.print("Sample "); 
        nss.print(k + 1, DEC);   
        nss.println(":");    

        for (int i = 0; i <= 5; i++) {
          if (ioSample.isAnalogEnabled(i)) {
            nss.print("Analog (AI");
            nss.print(i, DEC);
            nss.print(") is ");
            nss.println(ioSample.getAnalog(i, k));  
          }
        }

        for (int i = 0; i <= 8; i++) {
          if (ioSample.isDigitalEnabled(i)) {
            nss.print("Digtal (DI");
            nss.print(i, DEC);
            nss.print(") is ");
            nss.println(ioSample.isDigitalOn(i, k));
          }
        }
      }
    } 
    else {
      nss.print("Expected I/O Sample, but got ");
      nss.print(xbee.getResponse().getApiId(), HEX);
    }    
  } 
  else if (xbee.getResponse().isError()) {
    nss.print("Error reading packet.  Error code: ");  
    nss.println(xbee.getResponse().getErrorCode());
  }

Which Arduino pins are the XBee TX and XBee RX connected to? In general, how is the XBee connected to the Arduino?

I’ll just post the entire pde, so you can get an idea of where I’m coming from.

#include <XBee.h>
#include <NewSoftSerial.h>

// Connect pinRx to the DO of XBee(Pin2)
// Connect pinTx to the buffer of DIn of XBee(Pin3)
uint8_t pinRx = 2 , pinTx = 14;
long BaudRate = 9600 , sysTick = 0;
char GotChar;
NewSoftSerial nss( pinRx , pinTx );
XBee xbee = XBee();

Rx16IoSampleResponse ioSample = Rx16IoSampleResponse();
// 64-bit response is same except api id equals RX_64_IO_RESPONSE and returns a 64-bit address
//Rx64IoSampleResponse ioSample = Rx64IoSampleResponse();

Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();

int progressTick = 0;
long lastProgressTick = 0;
unsigned long time;

void setup()  
{
  Serial.begin(BaudRate);
  Serial.println("XBee Communication Test Start !");
  Serial.print("BaudRate:");
  Serial.println(BaudRate);
  Serial.print("NewSoftSerial Rx Pin#");
  Serial.println(pinRx,DEC);
  Serial.print("NewSoftSerial Tx Pin#");
  Serial.println(pinTx,DEC);

  // set the Baud Rate for the NewSoftSerial port
  nss.begin(BaudRate);

  nss.print("?G420");   // set display geometry,  4 x 20 characters in this case
  delay(500);                // pause to allow LCD EEPROM to program

  nss.print("?Bff");    // set backlight to ff hex, maximum brightness
  delay(1000);                // pause to allow LCD EEPROM to program

  nss.print("?s6");     // set tabs to six spaces
  delay(1000);               // pause to allow LCD EEPROM to program

  nss.print("?D00000000000000000");       // define special characters
  delay(300);                                  // delay to allow write to EEPROM
  
  // see moderndevice.com for a handy custom char generator (software app)
  nss.print("?f");                   // clear the LCD
  delay(10);                                              
  nss.print("...");   

  //crashes LCD without delay
  nss.print("?D11010101010101010");
  delay(300);

  nss.print("?D21818181818181818");
  delay(300);

  nss.print("?D31c1c1c1c1c1c1c1c");
  delay(300);

  nss.print("?D41e1e1e1e1e1e1e1e");
  delay(300);

  nss.print("?D51f1f1f1f1f1f1f1f");
  delay(300);

  nss.print("?D60000000000040E1F");
  delay(300);

  nss.print("?D70000000103070F1F");
  delay(300);

  nss.print("?c0");                  // turn cursor off
  delay(300);

  xbee.begin(BaudRate);
}

void loop()                    
{
  sysTick++ ;
  /*
  Serial.print("Xbee Timer :");
   Serial.println(sysTick);
   nss.print("Xbee Timer :");
   nss.println(sysTick);
   */
  /*
  // Monitor Rx from PC
   if (Serial.available()) {
   GotChar = Serial.read();
   nss.print(GotChar);
   }
   // Monitor data from NewSoftSerial pinRx
   if (nss.available()) {
   GotChar = nss.read();
   Serial.print(GotChar,BYTE);
   }
   */
   
 // Serial.print("Time: ");
  //time = millis();
  //prints time since program started
  //Serial.println(time);
  //Serial.println(millis() - lastProgressTick);


  if ((millis() - lastProgressTick) > 1000) {
    Serial.print("ProgressTick: ");
    Serial.println(progressTick);
    
    if (progressTick == 0) {
      nss.print("?f");                   // clear the LCD
      nss.print("?x00?y0");              // move cursor to beginning of line 0
      nss.println("Waiting for");
      nss.print("?x00?y1");
      nss.print("packet");
    }
    
    if (progressTick < 3) {
      nss.print(".");
      progressTick++;
      lastProgressTick = millis();    
    } else {
      progressTick = 0;
    }
  }
  //attempt to read a packet    
  xbee.readPacket();

  if (xbee.getResponse().isAvailable()) {
    // got something
    Serial.println("Got something!");
    nss.println("Got something!");

    if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {
      xbee.getResponse().getRx16IoSampleResponse(ioSample);

      nss.print("Received I/O Sample from: ");
      nss.println(ioSample.getRemoteAddress16(), HEX);  

      nss.print("Sample size is ");
      nss.println(ioSample.getSampleSize(), DEC);

      if (ioSample.containsAnalog()) {
        nss.println("Sample contains analog data");
      }

      if (ioSample.containsDigital()) {
        nss.println("Sample contains digtal data");
      }

      for (int k = 0; k < ioSample.getSampleSize(); k++) {
        nss.print("Sample "); 
        nss.print(k + 1, DEC);   
        nss.println(":");    

        for (int i = 0; i <= 5; i++) {
          if (ioSample.isAnalogEnabled(i)) {
            nss.print("Analog (AI");
            nss.print(i, DEC);
            nss.print(") is ");
            nss.println(ioSample.getAnalog(i, k));  
          }
        }

        for (int i = 0; i <= 8; i++) {
          if (ioSample.isDigitalEnabled(i)) {
            nss.print("Digtal (DI");
            nss.print(i, DEC);
            nss.print(") is ");
            nss.println(ioSample.isDigitalOn(i, k));
          }
        }
      }
    } 
    else {
      nss.print("Expected I/O Sample, but got ");
      nss.print(xbee.getResponse().getApiId(), HEX);
    }    
  } 
  else if (xbee.getResponse().isError()) {
    nss.print("Error reading packet.  Error code: ");  
    nss.println(xbee.getResponse().getErrorCode());
  }
}

I have the XBee’s RX pin connected to pin 14 of the arduino and its TX pin connected to pin 2 of the arduino.

I’m using Adafruit’s Xbee adapter so I can easily connect it to a breadboard. The activity light is active after I send a packet plus, as I mentioned, if I connect my LCD data line to pin 14 of the XBee, I can see the data I send. However, the XBee library doesn’t pick up the data for some reason.

I’m hoping this is a simple configuration issue or something. However, it’s not clear to me how the XBee object even knows which Arduino pin to monitor for data.

I'm not sure that the NewSoftSerial library works for pins higher than 13 (on the Duemilanove). Have you tried a digital pin to receive the data?

The code I submitted there is not very clear. It has some 'in progress' tests, incorrect comments and unused variables.

I have some ideas to try tonight and I'll update this thread to let you know the result.

I think I'm overthinking this. I think the XBee TX needs to be connected to the RX of the Arduino and the XBee RX to the Arduino TX. This is how the XBee library 'knows' where to get data from...I think.

Anyways, I tried everything BUT this. Perhaps it was just common knowledge that went over my head with the intimidation factor of diving into this stuff. I know software, but am new to hardware.

So, removing the NewSoftSerial stuff, the XBee should be connected to the TX and RX pins of the Arduino? I'm going to simplify things and build on that.

The TX of the XBee connects to the RX of the Arduino, and the RX of the XBee attaches to the TX of the Arduino.

I presumed that you were connecting the XBee RX/TX to the pins that you identified as the RX/TX pins for the NewSoftSerial instance. Apparently not.