Trouble Sending AT Commands to XBee Pro S1 over Software Serial

Hello! I can't seem to get AT Commands to properly send to XBee over software serial.

Here is my harware:
Arduino Uno
XBee Pro S1
Wireless SD Shield (Link here for reference)

Here is my config:
Followed pinout for XBee from the data sheet

  • XBee pin 1 (VCC) -> Arduino 3.3V
  • XBee pin 2 (DOUT) -> Arduino pin 9
  • XBee pin 3 (DIN) -> Arduino pin 8
  • XBee pin 9 (SLEEP) -> Arduino pin 10
  • XBee pin 10 (GND) -> Arduino GND
  • Picture for reference
  • I have the Serial Select switch on the board set to USB for upload then Micro for code execution. If I set it to USB during execution, nothing prints to the Serial monitor.

Here is my code:

/************************************************************************
* Module for testing ability of Arduino to sent AT Commands to XBee     *
************************************************************************/

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

//Define SoftwareSerial TX/RX pins
uint8_t ssRX = 8; //Connect pin 8 to TX of usb-serial device
uint8_t ssTX = 9; //Connect pin 9 to RX of usb-serial device
SoftwareSerial nss(ssRX, ssTX);

XBee xbee = XBee();

uint8_t shCmd[] = {'S', 'H'}; //Serial high
uint8_t slCmd[] = {'S', 'L'}; //Serial low
uint8_t assocCmd[] = {'A', 'I'}; //Association status

AtCommandRequest atRequest = AtCommandRequest(shCmd);
AtCommandResponse atResponse = AtCommandResponse();

void setup()
{
  Serial.begin(9600);
  xbee.begin(Serial);
  nss.begin(9600);
  nss.println("Hello word!");
}

void loop()
{
  //Send SH
  atRequest.setCommand(shCmd);
  sendAtCommand();

  //Set command to SL
  atRequest.setCommand(slCmd);
  sendAtCommand();

  //Set command to AI
  atRequest.setCommand(assocCmd);
  sendAtCommand();

  delay(1000); //Wait a second and start over
}

void sendAtCommand() {
  nss.println("Sending command to the XBee");

  // send the command
  xbee.send(atRequest);

  // wait up to 5 seconds for the status response
  if (xbee.readPacket(5000)) {
    // got a response!

    // should be an AT command response
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(atResponse);

      if (atResponse.isOk()) {
        nss.print("Command [");
        nss.print(atResponse.getCommand()[0]);
        nss.print(atResponse.getCommand()[1]);
        nss.println("] was successful!");

        if (atResponse.getValueLength() > 0) {
          nss.print("Command value length is ");
          nss.println(atResponse.getValueLength(), DEC);

          nss.print("Command value: ");
          
          for (int i = 0; i < atResponse.getValueLength(); i++) {
            nss.print(atResponse.getValue()[i], HEX);
            nss.print(" ");
          }

          nss.println("");
        }
      } 
      else {
        nss.print("Command return error code: ");
        nss.println(atResponse.getStatus(), HEX);
      }
    } else {
      nss.print("Expected AT response but got ");
      nss.print(xbee.getResponse().getApiId(), HEX);
    }   
  } else {
    // at command failed
    if (xbee.getResponse().isError()) {
      nss.print("Error reading packet.  Error code: ");  
      nss.println(xbee.getResponse().getErrorCode());
    } 
    else {
      nss.print("No response from radio");  
    }
  }
}

Here is what I get when using the Arduino Serial console:

It just repeats over and over.

It is supposed to print a command successful message after each command sent.

What I'm trying to do:
I am using an Arduino to record values from a temperature sensor, then writing them to an SD card and sending them to a Raspberry Pi hub using XBee. The setup is running off a battery pack and only records data every 15 minutes. Because of this I want the Arduino and XBee to sleep while not in use.
I followed this guide which said I have to send AT Commands D7 0 and SM 1 to disconnect XBee pin 7 from Arduino reset, and set XBee sleep mode to 1 (controllable by Arduino). Upon trying the example sketch I got errors relating to AT Commands and decided to try this example sketch from the XBee library first.
I can post the code for sleeping if that will be of use.

I'm at a loss of what to try next. Any help would be greatly appreciated, thanks!

For reference here is the code I am trying to use to put the Arduino to sleep:

/********************************************************************
* Module for testing sleep functionality of Arduino (With XBee)     *
********************************************************************/

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

#define LEDPIN 13
#define XBEE_SLEEP 10

//Define SoftwareSerial TX/RX pins
uint8_t ssRX = 7; //Connect pin 8 to TX of usb-serial device
uint8_t ssTX = 8; //Connect pin 9 to RX of usb-serial device
SoftwareSerial nss(ssRX, ssTX);

uint8_t psCmd[] = {'D', '7'}; //Pin control
uint8_t smCmd[] = {'S', 'M'}; //Sleep mode

unsigned long sleepTime; //Amount of time in MS Arduino should sleep

Sleep sleep;
XBee xbee = XBee();

AtCommandRequest atRequest = AtCommandRequest(psCmd);
AtCommandResponse atResponse = AtCommandResponse();

void setup()
{
   sleepTime = 30000; //Sleep for 30 seconds
   pinMode(9, OUTPUT);
   pinMode(LEDPIN, OUTPUT);
   digitalWrite(XBEE_SLEEP, LOW); //Keep XBee awake

   Serial.begin(9600);
   xbee.begin(Serial);
   nss.begin(9600);
   
   Serial.println("XBee + Arduino Sleep module sketch starting...");

   //Send XBee pin setup
   sendAtCommand();

   //Set command to sleep mode
   atRequest.setCommand(smCmd);
   sendAtCommand();
}

void loop()
{
   digitalWrite(XBEE_SLEEP, LOW); //Wake XBee up
   delay(100); //Delay for Serial to resume after sleeping
   Serial.println("Executing code routine...");

   //Blink LED 5 times
   for (int i = 0; i < 6; i++)
   {
      digitalWrite(LEDPIN, HIGH);
      delay(500);
      digitalWrite(LEDPIN, LOW);
      delay(500);
   }

   //Go to sleep
   Serial.print("Sleeping for ");
   Serial.print(sleepTime / 1000);
   Serial.println(" seconds...");
   delay(100); //Ensure print completes before sleeping

   //Power off XBee
   digitalWrite(XBEE_SLEEP, HIGH);
   //Power off Arduino
   sleep.pwrDownMode(); //Set sleep mode
   sleep.sleepDelay(sleepTime); //Sleep for specified time
}

void sendAtCommand() {
  nss.println("Sending command to the XBee");

  // send the command
  xbee.send(atRequest);

  // wait up to 5 seconds for the status response
  if (xbee.readPacket(5000)) {
    // got a response!

    // should be an AT command response
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(atResponse);

      if (atResponse.isOk()) {
        nss.print("Command [");
        nss.print(atResponse.getCommand()[0]);
        nss.print(atResponse.getCommand()[1]);
        nss.println("] was successful!");

        if (atResponse.getValueLength() > 0) {
          nss.print("Command value length is ");
          nss.println(atResponse.getValueLength(), DEC);

          nss.print("Command value: ");
          
          for (int i = 0; i < atResponse.getValueLength(); i++) {
            nss.print(atResponse.getValue()[i], HEX);
            nss.print(" ");
          }

          nss.println("");
        }
      } 
      else {
        nss.print("Command return error code: ");
        nss.println(atResponse.getStatus(), HEX);
      }
    } else {
      nss.print("Expected AT response but got ");
      nss.print(xbee.getResponse().getApiId(), HEX);
    }   
  } else {
    // at command failed
    if (xbee.getResponse().isError()) {
      nss.print("Error reading packet.  Error code: ");  
      nss.println(xbee.getResponse().getErrorCode());
    } 
    else {
      nss.print("No response from radio");  
    }
  }
}

It is supposed to flash the LED 5 times then put the XBee and Arduino to sleep for 30s (just to test).
Still has the problem of printing strange characters.

Here is what I get when using the Arduino serial monitor:

(The sketch restarting is when I switch the board's serial select to Micro)

The Arduino appears to sleep but I'm not sure how to tell if the XBee is.
I will get out a multi meter tomorrow and check if the power consumption changes when it's running the sleep routine. According to the XBee datasheet it should consume <10uA when sleeping.

Here are the links to the libraries I'm using:

Thanks again for any help!

In your test program you don't seem to have any Serial.println() instructions that would print debug messages to the Serial Monitor and allow you to see what is happening?

...R

Here is the example I'm using.

The sendAtCommand function (which is called from the main loop) has print statements in it, but the example uses software serial instead of hardware serial for its print statements.

I tried changing the print statements from software serial to hardware serial but got "No response from radio" back.

Should I switch to hardware serial and change my wiring so XBee DIN and DOUT are connected to Arduino pins 0 and 1?
The program seems to work as expected except printing ~[][][] to the serial console.
Also, where should I try adding serial print statements?

Thanks!

I don't have XBees (can't afford them) and I have no experience of them and no familiarity with the XBee library

It seems from your program (but I could be wrong) that the XBee is expected to be connected to Serial (pins 0 and 1) and SoftwareSerial (nss) is used for outputting the results. But that is not much use unless the SoftwareSerial pins (8,9) are connected to something that can display their output.

If it was my project I would connect the Xbee to SoftwareSerial and use Serial for the output so I could see it on the Serial Monitor. I THINK that means swapping "Serial" and "nss" througout the program except on lines 54 and 57.

There must be hundreds of XBee threads in this Forum - have you tried Googling "xbee site:arduino.cc"

Hopefully someone with more Xbee knowledge will come along.

...R