1

The Xbee API library is working fine for TxRequests on my Arduino, but AT commands are failing every time. I've been at this for three days.

It seems that there is some sort of conflict happening with my hardware serial port, because if I try to repeat the command a second time (after a 3sec delay), I see that the 2nd transmission (which should trigger the "D_in" light on my Xbee shield to flash) never gets sent. Then if I try to load and execute the sketch again (this shield on the nano allows me to do so with the Xbee still plugged in), I get an avrdude error suggesting that my arduino serial is busy. My PC should be out of the picture here, since I have no Serial.print() functions and the cable is plugged into a wall adapter with power only.

Basically I get no response and the parameter I'm trying to change remains unchanged. I'm at a total loss for why this is not working. Heres my code:

#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);

    delay(3000);

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


}

void loop()
{

}

And in addition, here's the code for the Tx example that works just fine:

/**
* 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>

/*
This example is for Series 1 XBee
Sends a TX16 or TX64 request and checks the status response for success
Note: In my testing it took about 15 seconds for the XBee to start reporting success, so I've added a startup delay
*/

XBee xbee = XBee();

unsigned long start = millis();

// allocate two bytes for to hold a 10-bit analog reading
uint8_t payload[5] = { 0x41, 0x42, 0x43, 0x44, 0x45 };

// with Series 1 you can use either 16-bit or 64-bit addressing

// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0x03, payload, sizeof(payload));

// 64-bit addressing: This is the SH + SL address of remote XBee
//XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x4105B41E);
// unless you have MY on the receiving radio set to FFFF, this will be received as a RX16 packet
//Tx64Request tx = Tx64Request(addr64, payload, sizeof(payload));

TxStatusResponse txStatus = TxStatusResponse();

int statusLed = 11;
int errorLed = 12;

void flashLed(int pin, int times, int wait) {

    for (int i = 0; i < times; i++) {
        digitalWrite(pin, HIGH);
        delay(wait);
        digitalWrite(pin, LOW);

        if (i + 1 < times) {
            delay(wait);
        }
    }
}

void setup() {
    pinMode(statusLed, OUTPUT);
    pinMode(errorLed, OUTPUT);
    Serial.begin(9600);
    xbee.setSerial(Serial);
}

void loop() {

    // start transmitting after a startup delay.  Note: this will rollover to 0 eventually so not best way to handle
    if (millis() - start > 10000) {


        xbee.send(tx);

        // flash TX indicator
        flashLed(statusLed, 1, 100);
    }

    // after sending a tx request, we expect a status response
    // wait up to 5 seconds for the status response
    if (xbee.readPacket(5000)) {
        // got a response!

        // should be a znet tx status               
        if (xbee.getResponse().getApiId() == TX_STATUS_RESPONSE) {
            xbee.getResponse().getTxStatusResponse(txStatus);

            // get the delivery status, the fifth byte
            if (txStatus.getStatus() == SUCCESS) {
                // success.  time to celebrate
                flashLed(statusLed, 5, 50);
            }
            else {
                // the remote XBee did not receive our packet. is it powered on?
                flashLed(errorLed, 3, 500);
            }
        }
    }
    else if (xbee.getResponse().isError()) {
        //nss.print("Error reading packet.  Error code: ");  
        //nss.println(xbee.getResponse().getErrorCode());
        // or flash error led
    }
    else {
        // local XBee did not provide a timely TX Status Response.  Radio is not configured properly or connected
        flashLed(errorLed, 2, 50);
    }

    delay(1000);

}
0

Just a thought from a point of ignorance.. When I use serial comm, I always use an interrupt watching loop. When data hits UART, a data available pin throws an interrupt that causes the loop to read the input register.

  • That doesn't really have anything to do with Xbee AT commands, but I do like that idea for the rest of my project. – LegitimateWorkUser Sep 27 '17 at 15:02
0

So it turns out that it was working the whole time. The problem was that that the values were not being written to the xbee, only changed. Hence, hen I would check the radio in XCTU, the values would have reverted to previous. I had no idea I needed to be calling a "write" function to save the parameters to the radio.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.