Using Xbee API library with Xbees

Hello there.. I am a new member here and I am seeking help for my WSN project.

I am using Xbee Library in C by Andrew Rapp, but I am having a problem with using the provided Examples that came with the Library.

What I am trying to accomplish is to send Sensor data connected to Analog Pins of one Xbee connected onto a Seeed Groove to another Xbee which is connected to the Arudino

Transmitter : ZIGBEE ROUTER AT with IR = 64, AD0/DIO0 = ADC and all other pins disabled. Receiver Xbee DL and DH are provided
Receiver : : ZIGBEE COORDINATOR API (Arduino Uno)

I have checked using X-CTU and it seems the receiver Xbee receives frames with 0x10 (Transmit Request) even though 0x92 (RX IO Sample)was expected. But actually this is not the problem,

I have uploaded the "Series2_Rx_Nss" sketch to the Arduino Uno. I have connected Pin 8 on Arduino to TX of Xbee (Pin 2) and Pin 9 on Arduino to RX of Xbee (Pin 3), but all I am getting is garbage on the screen. Not even one message of what is in the code.

N.B: I have changed the SoftwareSerial BaudRate from 9600 to 19200.

#include <XBee.h>
#include <SoftwareSerial.h>

/*
This example is for Series 2 (ZigBee) XBee Radios only
Receives I/O samples from a remote radio.
The remote radio must have IR > 0 and at least one digital or analog input enabled.
The XBee coordinator should be connected to the Arduino.
 
This example uses the SoftSerial library to view the XBee communication.  I am using a 
Modern Device USB BUB board (http://moderndevice.com/connect) and viewing the output
with the Arduino Serial Monitor.
*/

// Define NewSoftSerial TX/RX pins
// Connect Arduino pin 8 to TX of usb-serial device
uint8_t ssRX = 8;
// Connect Arduino pin 9 to RX of usb-serial device
uint8_t ssTX = 9;
// Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
SoftwareSerial nss(ssRX, ssTX);

XBee xbee = XBee();

ZBRxIoSampleResponse ioSample = ZBRxIoSampleResponse();

XBeeAddress64 test = XBeeAddress64();

void setup() { 
  Serial.begin(9600);
  xbee.setSerial(Serial);
  // start soft serial
  nss.begin(19200);
}

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

  if (xbee.getResponse().isAvailable()) {
    // got something

    if (xbee.getResponse().getApiId() == ZB_IO_SAMPLE_RESPONSE) {
      xbee.getResponse().getZBRxIoSampleResponse(ioSample);

      nss.print("Received I/O Sample from: ");
      
      nss.print(ioSample.getRemoteAddress64().getMsb(), HEX);  
      nss.print(ioSample.getRemoteAddress64().getLsb(), HEX);  
      nss.println("");
      
      if (ioSample.containsAnalog()) {
        nss.println("Sample contains analog data");
      }

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

      // read analog inputs
      for (int i = 0; i <= 4; i++) {
        if (ioSample.isAnalogEnabled(i)) {
          nss.print("Analog (AI");
          nss.print(i, DEC);
          nss.print(") is ");
          nss.println(ioSample.getAnalog(i), DEC);
        }
      }

      // check digital inputs
      for (int i = 0; i <= 12; i++) {
        if (ioSample.isDigitalEnabled(i)) {
          nss.print("Digital (DI");
          nss.print(i, DEC);
          nss.print(") is ");
          nss.println(ioSample.isDigitalOn(i), DEC);
        }
      }
      
      // method for printing the entire frame data
      //for (int i = 0; i < xbee.getResponse().getFrameDataLength(); i++) {
      //  nss.print("byte [");
      //  nss.print(i, DEC);
      //  nss.print("] is ");
      //  nss.println(xbee.getResponse().getFrameData()[i], HEX);
      //}
    } 
    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());
  }
}

What am I doing wrong?

Thanks in advance!

N.B: I have changed the SoftwareSerial BaudRate from 9600 to 19200.

Why? Did you also change the baud rate of the XBees?

uhmm, No I didn't. Well OK... I changed it back to 9600.. Now All Xbees Baud Rate is at 9600 as well as in the code and nothing shows up on the Serial Monitor.. why???

OK, I made some changes, Changed IR to 1388 (5000 ms). and now it seems to receive 0x92 packets. but I am getting this??

I have uploaded the "Series2_Rx_Nss" sketch to the Arduino Uno. I have connected Pin 8 on Arduino to TX of Xbee (Pin 2) and Pin 9 on Arduino to RX of Xbee (Pin 3)

So, explain this:

  xbee.setSerial(Serial);

You've told the XBee library that the XBee is connected to the Serial instance, not the nss instance.

You are then assuming that the PC is connected to the nss instance and the XBee is connected to the Serial instance. Those do not appear to be valid assumptions.

Uh, Yea. I guess.

xbee.setSerial(Serial);
means that the Xbee is using the HardwareSerial not the SoftwareSerial, but at the same time the Arudino is connected to the PC using the HardwareSerial and I can't change that. So I think I have to change the code to

xbee.setSerial(nss);, but I am not sure how can I still print to the serial monitor? DO I change all the printing instances to serial instead of nss?

I mean if it is still possible to print all these messages via SoftwareSerial while Xbee is communicating with the Arudino via HardwareSerial, what should I change in the setup part or how should I change my wiring?

It isn't possible. The only means of communication between the Arduino and the PC is via the HardwareSerial port/instance.

Since the XBee can communicate with the Arduino on any pair of pins, THAT is the change to make.

Uh, Yea. I guess.

xbee.setSerial(Serial);
means that the Xbee is using the HardwareSerial not the SoftwareSerial, but at the same time the Arudino is connected to the PC using the HardwareSerial and I can't change that. So I think I have to change the code to

xbee.setSerial(nss);, but I am not sure how can I still print to the serial monitor? DO I change all the printing instances to serial instead of nss?

/**
 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
 *
 * This file is part of XBee-Arduino.
 *
 * XBee-Arduino is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XBee-Arduino is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XBee-Arduino.  If not, see <http://www.gnu.org/licenses/>.
 */
 
#include <XBee.h>
#include <SoftwareSerial.h>

/*
This example is for Series 2 XBee
Receives a ZB RX packet and prints the packet to softserial
*/

XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle 
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();

// Define NewSoftSerial TX/RX pins
// Connect Arduino pin 8 to TX of usb-serial device
uint8_t ssRX = 8;
// Connect Arduino pin 9 to RX of usb-serial device
uint8_t ssTX = 9;
// Remember to connect all devices to a common Ground: XBee, Arduino and USB-Serial device
SoftwareSerial nss(ssRX, ssTX);


void setup() {  
  // start serial
  Serial.begin(9600);
  nss.begin(9600);
  xbee.setSerial(nss);
  
  Serial.println("Starting up!");
}

// continuously reads packets, looking for ZB Receive or Modem Status
void loop() {
   
    xbee.readPacket(100);
    
    if (xbee.getResponse().isAvailable()) {
      // got something
           
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        // got a zb rx packet
        
        // now fill our zb rx class
        xbee.getResponse().getZBRxResponse(rx);
      
        Serial.println("Got an rx packet!");
            
        if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
            // the sender got an ACK
            Serial.println("packet acknowledged");
        } else {
          Serial.println("packet not acknowledged");
        }
        
        Serial.print("checksum is ");
        Serial.println(rx.getChecksum(), HEX);

        Serial.print("packet length is ");
        Serial.println(rx.getPacketLength(), DEC);
        
         for (int i = 0; i < rx.getDataLength(); i++) {
          Serial.print("payload [");
          Serial.print(i, DEC);
          Serial.print("] is ");
          Serial.println(rx.getData()[i], HEX);
        }
        
       for (int i = 0; i < xbee.getResponse().getFrameDataLength(); i++) {
        Serial.print("frame data [");
        Serial.print(i, DEC);
        Serial.print("] is ");
        Serial.println(xbee.getResponse().getFrameData()[i], HEX);
      }
      }
    } else if (xbee.getResponse().isError()) {
      Serial.print("oh no!!! error code:");
      Serial.println(xbee.getResponse().getErrorCode());
    }
	
}

I only get "Starting up" and nothing more on the screen.. I am not sure if what I am doing is correct or not!

DO I change all the printing instances to serial instead of nss?

Serial, not serial. Otherwise, yes.

Yup, Serial for sure.But, I still can't determine the problem. Now the Xbee is using SoftwareSerial, while Arduino to PC is using HardwareSerial, but still I am not getting anything on Serial Monitor except "Starting Up".. Nothing of the other messages.. Why?

I think I made the proper changes into my previous post.. Am I missing something?

How is the XBee connected to the Arduino? All the XBee shields I've seen have blinky lights showing when the XBee is transmitting and receiving. If you have them, and they aren't, the connections (TX and RX) are reversed.

I am using Xbee Shield by Funduino.. The only LED blinking there is the "ASSOCIATE" Led which blinks all the time if the Xbee is connected.

The Trasmitting Xbee is attached to Seeed Grove Xbee Carrier using on-board FT232RL. Both TX and RX Leds are not blinking, however when the Xbee Shield on the Receiving Xbee is switched to "USB Mode" and the Arduino Chip is onbypassed (Reset Connected to Ground),
I am receiving this on the X-CTU Terminal. Seems to be API packets of IO Sampling type. The TX LED on the Arduino blinks when I receive a packet (every 1000ms)


From image, I can conclude that there is a communication between the Transmitting and Receiving Xbees, but the Receiving Xbee is not able to communicate with the Arudino

Now, Switch to Xbee Mode on the shield and TX Led starts to blink so quickly, wether the other Transmitting Xbee is on or off.

I have no clue what is happening honestly!