Home/Support/Support Forum/AtCommandRequest example for Arduino not working- Can't change D3 pin value

AtCommandRequest example for Arduino not working- Can't change D3 pin value

0 votes
I'm using the arduino API library and trying to perform AT commands. I've had plenty of luck before using remote AT commands, but for some reason this nearly identical process won't work.

I'm not actually using software serial. I'm loading the program to the board, running it with the Xbee attached, and then checking it in XCTU afterward to see if the parameter has changed. There should be no conflicts with serial ports.

All I get with this code is a '~' (7E, the start delimiter byte) character that shows up in the serial port. Then the script progresses no further and the command is not executed.

I thought it had something to do with my Arduino so I trued a Nano (I had one lying around) instead of an Uno.This time, It looks like I got the whole string out, but the radio still does not respond or change values.

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

// Define SoftSerial 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();

// serial high
uint8_t D3Cmd[] = { 'D', '3' }; //D3
uint8_t low[] = { 0x04 };////for DI03 pin parameter

AtCommandRequest atRequest = AtCommandRequest(D3Cmd, low, 1);

AtCommandResponse atResponse = AtCommandResponse();

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

// Startup delay to wait for XBee radio to initialize.
// you may need to increase this value if you are not getting a response
delay(10000);
}

void loop() {


atRequest.setCommand(D3Cmd);

atRequest.setCommandValue(low);

atRequest.setCommandValueLength(sizeof(low));

sendAtCommand();

while (1) {};
}

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(), 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");
}
}
}
asked Sep 14, 2017 in IEEE 802.15.4 by D_MK New to the Community (2 points)

Please log in or register to answer this question.

1 Answer

0 votes
You can't monitor the serial port that the radio is on and have the processor talk to the radio. You can do one or the other.
answered Sep 14, 2017 by mvut Veteran of the Digi Community (13,488 points)
I'm not doing them at the same time. Like I said, I'm flashing the board, then connecting the Xbee afterward. I'm only monitoring the serial pane with the Xbee disconnected. That's when I can see that I'm only getting the first character out of the Arduino.

Right now I'm investigating a possible issue with the Arduino itself. It appears I can only print the first character of an array when simply printing to the serial pane with the following code:

void setup()
{
    Serial.begin(115200);
    char stringy[9] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x44, 0x33, 0x04, 0x7B };
    for (int i = 0; i < 9; i++) {
        Serial.print(stringy[i]);
    }
    Serial.println("");
    Serial.print("done");
}
void loop()
{
    while (1) {  }
}
What if you just do:

void setup()
{
 Serial.begin(115200);
    Serial.print(0x7E, 0x00, 0x05, 0x08, 0x01, 0x44, 0x33, 0x04, 0x7B);
    Serial.Print("")
    Serial.Print(Done)
}
void loop()
Unfortunately, it takes a for loop to print the individual elements of an array in C++.   Serial.print(array[0], array[1]...); will not compile.


But anyway, it's a separate issue. I tried the code on my Nano and the whole frame seems to be generated perfectly. Here is my code as of now: I'm flashing it to the board in release mode, unplugging it, installing the Xbee onto the shield, and then powering the board to let the code run. I can see the Din light flash while the command is being sent, but there is no Dout flash indicating a response, and the parameters remain unchanged when I check them in XCTU. I can generate an AT command in XCTU and execute it without any problems. My Xbee is set to default settings, except API is enabled and the baud rate is set to 115200. I’m not sure what’s going wrong, and I’ve been at it for two days. Any advice would be greatly appreciated.


#include <XBee.h>  


XBee xbee;

//for local control of wake pin
uint8_t D3[2] = { 'D', '3' };////for DI03 pin parameter
uint8_t SH[2] = { 'S', 'H' };////for DI03 pin parameter
uint8_t NI[2] = { 'N', 'I' };////for DI03 pin parameter
uint8_t PP[] = { 'P', 'P', 'P' };////for DI03 pin parameter
uint8_t high[] = { 0x05 };/////for DI03 pin parameter
uint8_t low[] = { 0x04};////for DI03 pin parameter

AtCommandRequest AtRequest = AtCommandRequest();
AtCommandResponse atResponse = AtCommandResponse();

void setup()
{
    Serial.begin(115200);
    xbee.setSerial(Serial);
    Serial.print("10sec...");
    delay(10000);
    xbee.readPacket(100); //to consume any modem status responses that sometimes show up first in buffer

    AtRequest.setCommand(D3);
    AtRequest.setCommandValue(low);
    AtRequest.setCommandValueLength(sizeof(low));
   
    xbee.send(AtRequest)
   
    delay(3000);
    AtRequest.setCommand(NI);
    AtRequest.setCommandValue(PP);
    AtRequest.setCommandValueLength(sizeof(PP));
   
    xbee.send(AtRequest)
   

}

void loop()
{

}
You might want to post this to a Arduino Coding forum instead of an XBee forum.
I'm afraid I've confused you by mentioning what's going on in my serial pane. And anyway, you're wrong about not being able to monitor serial output while also transmitting. I've been doing just this for over a year. This has nothing to do with Arduino coding. Like I just said, the frame is being generated perfectly on my Nano. The Tx example is working perfectly, and I'm also able to issue remote AT commands. All of these functions are nearly identical, so why are AT commands not working?
If the commands work in XCTU and the Code works on a different processor with the same module, then the issue has to be with the processor and how it interacts with the module. Possibly a voltage issue? Or a configuration issue with the processor interface. IE the hardware.
Its the same Nano. Tx works, Remote AT commands work, but AT commands do not work.
What is the API frame you are sending over that is not working?
It should be 7E 00 05 08 01 44 33 04 7B

But I have no view of what the API library is sending except for the gibberish I see in my serial pane when not connected to Xbee. It looks like ~(strange character)(strange character)(null)D3(strange character){.

Here s that code again, this time simplified even further:

#include <XBee.h>  

XBee xbee = XBee();

uint8_t D3[] = { 'D', '3' };
uint8_t high[] = { 0x05 };/////for DI03 pin parameter
uint8_t low[] = { 0x04};////for DI03 pin parameter

AtCommandRequest AtRequest = AtCommandRequest();
AtCommandResponse atResponse = AtCommandResponse();

void setup()
{
    Serial.begin(9600);
    xbee.setSerial(Serial);
    delay(10000);

    AtRequest.setCommand(D3);
    AtRequest.setCommandValue(low);
    AtRequest.setCommandValueLength(sizeof(low));
    xbee.send(AtRequest);

}

void loop()
{
The gibberish you are seeing is the ASCII form of the hex data.  Try using a terminal that has a Hex viewer and you will see it.

Note that sending code snippets without having any context as to what a function is doing does not really help as it does not tell you what is being sent.
I know what the gibberish is. I don't have to see it. It only needs to be read by the Xbee.

"Note that sending code snippets without having any context as to what a function is doing does not really help as it does not tell you what is being sent."

Are you serious? I'm using THE Xbee API library for Arduino. We all know what it's supposed to be doing and how it's supposed to work.

Copying the frame generated in the XCTU frame generator in hex and converting it to ascii, it's exactly the same frame as what the library is putting out. Just like I said five times, the frame is being generated perfectly. So why won't the Xbee respond? If you don't have an answer, then why bother responding if not just to try and make yourself look smart?
...