Xbee Series 1 mxing AT commands with readpacket()

I have two Xbee Series 1 and I’m using xbee.h library. The local xbee is attached to an arduino via Sparkfun shield. The remote xbee is stand alone and it’s setup to read analog sensor inputs, but one I/O is configured for digital output. The xbee AP setting = 2

Using the xbee.readPacket() and the examples that came with it I figured how to read all the I/O values on the remote xbee. Next I used the RemoteAtCommandRequest to send an ATD6 command to the remote Xbee to either turn DIO 6 on or off. But I can’t get these two functions to work together. The xbee.readPacket() is at the start of my sketch and it runs fine the first loop, then the RemoteAtCommandRequest runs, and it works fine, and keeps working. But when the program loops back to the beginning and gets to xbee.getResponse().isAvailable() it returns False. When this happens the sketch checks for errors: xbee.getResponse().isError() but this is False. So the Xbee is not available but there are no errors. Next the sketch moves on to the The sendRemoteATCommand() and that works fine. I have an LED on DIO6 of the remote xbee and it keeps blinking on and off every second.

Also, right after the xbee.send(remoteAtRequest); command, the Arduino is returning some garbled characters to the serial monitor. I converted them to hex and I get: 7E 00 10 17 01 00 7D 33 A2 00 40 5D BD 46 FF. This looks like Xbee packet data, but I can’t figure out why it’s showing up in the serial monitor since I’m not using a Serial.print command to display it.

This is my first shot at using Xbees. I’d appreciate any help.

#include <XBee.h>  
XBee xbee = XBee();

/*-- XBee AT command and parameters --*/
uint8_t atDIO6cmd[] = {'D', '6'};     // Set DIO6
uint8_t cmdValOff[] = { 0x4 };       // Set DIO low
uint8_t cmdValOn[] = { 0x5 };        // Set DIO High

// SH + SL of Tx Xbee radio
XBeeAddress64 remoteAddress = XBeeAddress64(0x13A200, 0x405DBD46);

// Create a remote AT request with the DIO 6 command
RemoteAtCommandRequest remoteAtRequest = RemoteAtCommandRequest(remoteAddress, atDIO6cmd, cmdValOff, sizeof(cmdValOff));
RemoteAtCommandResponse remoteAtResponse = RemoteAtCommandResponse();
Rx16IoSampleResponse ioSample = Rx16IoSampleResponse();

void setup() {
  xbee.begin(9600);
  delay(5000);  //Give Xbees time to associate
}  

void loop() {
  
  xbee.readPacket();

  if (xbee.getResponse().isAvailable()) {
    if (xbee.getResponse().getApiId() == RX_16_IO_RESPONSE) {
      xbee.getResponse().getRx16IoSampleResponse(ioSample);

      Serial.print("Received I/O Sample from: ");
      Serial.println(ioSample.getRemoteAddress16(), HEX);
      Serial.print("Sample size is ");
      Serial.println(ioSample.getSampleSize(), DEC);

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

      if (ioSample.containsDigital()) {
        Serial.println("Sample contains digital data");
      }

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

        for (int i = 0; i <= 5; i++) {
          if (ioSample.isAnalogEnabled(i)) {
            Serial.print("ioSample.getAnalog(");
            Serial.print(i);
            Serial.print(",");
            Serial.print(k);
            Serial.print(") = ");
            Serial.println(ioSample.getAnalog(i, k));
          }
        } // next i (analog)

        for (int i = 0; i <= 8; i++) {
          if (ioSample.isDigitalEnabled(i)) {
            Serial.print("ioSample.isDigitalOn(");
            Serial.print(i);
            Serial.print(",");
            Serial.print(k);
            Serial.print(") = ");
            Serial.println(ioSample.isDigitalOn(i, k));
          }
        } 
      } 
    } 
    else {
      Serial.print("Expected I/O Sample, but got ");
      Serial.println(xbee.getResponse().getApiId(), HEX);
    } 
  } 
  else if (xbee.getResponse().isError()) {
    Serial.print("Error reading packet. Error code: ");
    Serial.println(xbee.getResponse().getErrorCode());
  } 
  else { 
    Serial.println("Xbee not available, no errors");  // There is no error but xbee is not available either
  }

  // Turn DIO 6 off
  remoteAtRequest.setCommand(atDIO6cmd);
  remoteAtRequest.setCommandValue(cmdValOff);
  remoteAtRequest.setCommandValueLength(sizeof(cmdValOff));
  sendRemoteATCommand();
  delay(1000);
  
  // Turn DIO 6 On
  remoteAtRequest.setCommand(atDIO6cmd);
  remoteAtRequest.setCommandValue(cmdValOn);
  remoteAtRequest.setCommandValueLength(sizeof(cmdValOn));
  sendRemoteATCommand();
  delay(1000);

  // clear the set value so that the object can be reused 
  remoteAtRequest.clearCommandValue();
  
} // loop() 


void sendRemoteATCommand() {
  Serial.println("Sending AT command to the XBee");

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

  Serial.println("");

  // 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() == REMOTE_AT_COMMAND_RESPONSE) {
      xbee.getResponse().getRemoteAtCommandResponse(remoteAtResponse);

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

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

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

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

Without even looking at the code, I have to ask a question.

The xbee.readPacket() is at the start of my sketch and it runs fine the first loop, then the RemoteAtCommandRequest runs, and it works fine, and keeps working. But when the program loops back to the beginning and gets to xbee.getResponse().isAvailable() it returns False. When this happens the sketch checks for errors: xbee.getResponse().isError() but this is False.

You appear to be expecting a response from the RemoteAtCommandRequest. Is that a valid expectation? I've never used the XBee library, so I don't know, but I wouldn't expect a remote AT command to return a reply.

PaulS:
Without even looking at the code, I have to ask a question.
You appear to be expecting a response from the RemoteAtCommandRequest. Is that a valid expectation? I’ve never used the XBee library, so I don’t know, but I wouldn’t expect a remote AT command to return a reply.

I’m not sure, I think you might be able to get a response from RemoteAtCommandRequest, but I’m not trying to do that. I’m just using RemoteAtCommandRequest to control digital output 6 - which works fine. I can read all the xbee inputs and outputs just fine with xbee.readPacket, but my problem is the xbee.readPacket only works on the first loop in the sketch; the RemoteAtCommandRequest works all the time.

I can read all the xbee inputs and outputs just fine with xbee.readPacket, but my problem is the xbee.readPacket only works on the first loop in the sketch

Is this true even if you don't call the remore AT command function?

If you do call the remote AT command function, do the transmit and receive lights on the shield blink when the remote XBee sends data?

PaulS:
Is this true even if you don’t call the remove AT command function?

If you do call the remote AT command function, do the transmit and receive lights on the shield blink when the remote XBee sends data?

Yes, I actually had this part working first. I just added the AT Command code today. Before that I was reading the inputs with the readpacket().

After the remote AT command is sent, does the XBee stay in command mode? Perhaps it is still expecting commands? Is there a way to tell it to exit command mode?

I don't how to tell when it's in command mode. So I added the command ATCN at the end of Loop() which is supposed to make it exit command mode. But this didn't help. I still have the same problem.