Go Down

Topic: XBee Arduino - Program PAN ID (Read 205 times) previous topic - next topic

jsxman

I am trying to program the "ID" of the XBee.

I am using the XBee Arduino Master library

I have an Arduino nano with a XBee shield - Rx to pin 0 and Tx to pin 1.

I delay 10 seconds, send the command in and read the response.
The base example code was looking for an AT response - I got a response 90, which is the ZB_RX_RESPONSE.

This might mean something of significance, I can't tell and, ergo, is where I am stuck.

Anyone have some help on how to get this to work?

Thanks

With this code shown here, I get an error code of "1".
Code: [Select]

/**
 * 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 1 (10C8 or later firmware) or Series 2 XBee
Sends two Remote AT commands to configure the remote radio for I/O line monitoring

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



int tmpval=0;

XBee xbee = XBee();


// my code
uint8_t idCmd[] = {'I','D'};
uint8_t idValue[] = {0x01, 0x10}; // some made up value for testing purposes
// end my code

// SH + SL of your remote radio
//XBeeAddress64 remoteAddress = XBeeAddress64(0x0013a200, 0x400a3e02);
XBeeAddress64 remoteAddress = XBeeAddress64(0x0013a200, 0x40BDB1FB);
// Create a remote AT request with the IR command
//RemoteAtCommandRequest remoteAtRequest = RemoteAtCommandRequest(remoteAddress, irCmd, irValue, sizeof(irValue));
// was line above - changed to line below
RemoteAtCommandRequest remoteAtRequest = RemoteAtCommandRequest(remoteAddress, idCmd, idValue, sizeof(idValue));
 
// Create a Remote AT response object
RemoteAtCommandResponse remoteAtResponse = RemoteAtCommandResponse();

void setup() {
  Serial.begin(9600);
  xbee.begin(Serial);
  Serial.println("Power On.");
 
 // When powered on, XBee radios require a few seconds to start up
 // and join the network.
 // During this time, any packets sent to the radio are ignored.
 // Series 2 radios send a modem status packet on startup.
 
 // it took about 4 seconds for mine to return modem status.
 // In my experience, series 1 radios take a bit longer to associate.
 // Of course if the radio has been powered on for some time before the sketch runs,
 // you can safely remove this delay.
 // Or if you both commands are not successful, try increasing the delay.
 Serial.println("Waiting 10 seconds.");
 delay(10000);
}

void loop() {
  sendRemoteAtCommand();
 
  // now reuse same object for DIO0 command
/* commented out start
  remoteAtRequest.setCommand(d0Cmd);
  remoteAtRequest.setCommandValue(d0Value);
  remoteAtRequest.setCommandValueLength(sizeof(d0Value));
  sendRemoteAtCommand();
commented out end */
 
  // it's a good idea to clear the set value so that the object can be reused for a query
  remoteAtRequest.clearCommandValue();
  Serial.println("Done---Going into an infinite loop...");
  Serial.print(tmpval);
  Serial.println("---");
  // we're done
  while (1) {};
}


void sendRemoteAtCommand() {
  // send the command
  xbee.send(remoteAtRequest);

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

    // should be an AT command response
//    if (xbee.getResponse().getApiId() == REMOTE_AT_COMMAND_RESPONSE) {
    if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
      xbee.getResponse().getRemoteAtCommandResponse(remoteAtResponse);
    tmpval+=3;

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

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

          Serial.print("Command value: ");
         
    tmpval+=7;
          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);
    tmpval+=11;
      }
    } else {
      Serial.print("Expected Remote AT response but got ");
      Serial.print(xbee.getResponse().getApiId(), HEX);
    tmpval+=13;
    }   
  } else if (xbee.getResponse().isError()) {
    Serial.print("Error reading packet.  Error code: "); 
    Serial.println(xbee.getResponse().getErrorCode());
    tmpval+=17;
  } else {
    Serial.print("No response from radio"); 
    tmpval+=19;
  }
}


This is the output log
Quote
Power On.
Waiting 10 seconds.
...non readable characters...
Command returned error code: 1
Done---Going into an infinite loop...
15---

PaulS

Two XBees that are not using the same PAN ID can not talk to each other. It makes little sense to try to programmatically configure an XBee when the code you are using needs to know everything about the current configuration of the remote XBee.

So, the first question is why are you trying to programmatically change the PAN ID of the remote XBee? If it is not in the correct PAN, you can't talk to it. If it is, moving it to another doesn't make sense.

jsxman

Yes I am aware.  This design is on purpose.

I am configuring an Arduino to read DIP switches to set the PAN ID of the connected XBee.
Doing this on two Arduinos will allow them to communicate.

The question remains:
- How can I get this code to work?

Thank you


PaulS

Quote
The question remains:
- How can I get this code to work?
The XBee library you are using uses API mode to have one XBee communicate with another.

That code APPEARS to be trying to have one XBee tell the other to change its PAN ID. Nothing you do is going to make that work.

You might be able to have an Arduino communicate with its XBee, using AT commands, and have its XBee change its PAN ID.

jsxman

Thank you.

I have altered the code to work with an XBee directly connected to the Arduino.

Code: [Select]

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


uint8_t ssRX = 0;
uint8_t ssTX = 1;
int tmpval=0;
XBee xbee = XBee();


uint8_t idCmd[] = {'I','D'};
//uint8_t idValue[] = {0x01, 0x10}; // some made up value for testing purposes
uint8_t idValue[] = {0x02}; // some made up value for testing purposes

AtCommandRequest request = AtCommandRequest(idCmd, idValue, sizeof(idValue));
AtCommandResponse response = AtCommandResponse();

void setup() {
  Serial.begin(9600);
  delay(5000);
}

void loop() {
  sendAtCommand();
  request.clearCommandValue();
  Serial.println("Done---Going into an infinite loop...");
  Serial.print(tmpval);
  Serial.println("---");
  while (1) {};
}


void sendAtCommand() {
  xbee.send(request);
  if (xbee.readPacket(5000)) {
    tmpval+=1;
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(response);
      tmpval+=3;

      if (response.isOk()) {
        Serial.print("Command [");
        Serial.print(response.getCommand()[0]);
        Serial.print(response.getCommand()[1]);
        Serial.println("] was successful!");
        tmpval+=5;
        if (response.getValueLength() > 0) {
          Serial.print("Command value length is ");
          Serial.println(response.getValueLength(), DEC);
          Serial.print("Command value: ");
          tmpval+=7;
          for (int i = 0; i < response.getValueLength(); i++) {
            Serial.print(response.getValue()[i], HEX);
            Serial.print(" ");
          }
          Serial.println("");
        }
      } else {
        Serial.print("Command returned error code: ");
        Serial.println(response.getStatus(), HEX);
        tmpval+=11;
      }
    } else {
      Serial.print("Expected AT COMMAND response but got ");
      Serial.print(xbee.getResponse().getApiId(), HEX);
      tmpval+=13;
    }   
  } else if (xbee.getResponse().isError()) {
    Serial.print("Error reading packet.  Error code: "); 
    Serial.println(xbee.getResponse().getErrorCode());
    tmpval+=17;
  } else {
    Serial.print("No response from radio"); 
    tmpval+=19;
  }
}


This code sequence has the following output:
Quote
Command [7368] was successful!
Done---Going into an infinite loop...
9---
However, when I use XCTU to look at the XBee module, the PAN ID is never updated.

I set the PAN ID to 23 with an XCTU update - XBee connected through a USB shield.
Moved the XBee onto the Arduino shield, and ran the above code. Saw the output of success!, and then moved the XBee back to the USB shield and ran XCTU to see nothing was changed - the PAN ID remained 23.

So it appears the command is successful but it isn't.

Any ideas?

Thank you so much

PaulS

When you make changes with X-CTU, it is a three step process. 1) Read the current configuration data. 2) Make changes. 3) Save the modified configuration data.

I suspect that you are successfully completing step 2 but not performing step 3. Step 1 can be skipped if you don't care what the configuration currently is.

jsxman

thank you.

i was missing the write to EEPROM step as you said.

here is the full code that works:

Code: [Select]

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


uint8_t ssRX = 0;
uint8_t ssTX = 1;
int tmpval=0;
XBee xbee = XBee();


uint8_t idCmd[] = {'I','D'};
//uint8_t idValue[] = {0x01, 0x10}; // some made up value for testing purposes
uint8_t idValue[] = {0x02}; // some made up value for testing purposes

uint8_t idCmd2[] = {'W','R'}; // write to EEPROM


AtCommandRequest request = AtCommandRequest(idCmd, idValue, sizeof(idValue));
AtCommandResponse response = AtCommandResponse();

void setup() {
  Serial.begin(9600);
  delay(5000);
}

void loop() {
  sendAtCommand();
  request.clearCommandValue();
  request.setCommand(idCmd2);
  sendAtCommand();
  request.clearCommandValue();
  Serial.println("Done---Going into an infinite loop...");
  Serial.print(tmpval);
  Serial.println("---");
  while (1) {};
}


void sendAtCommand() {
  xbee.send(request);
  if (xbee.readPacket(5000)) {
    tmpval+=1;
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(response);
      tmpval+=3;

      if (response.isOk()) {
        Serial.print("Command [");
        Serial.print(response.getCommand()[0]);
        Serial.print(response.getCommand()[1]);
        Serial.println("] was successful!");
        tmpval+=5;
        if (response.getValueLength() > 0) {
          Serial.print("Command value length is ");
          Serial.println(response.getValueLength(), DEC);
          Serial.print("Command value: ");
          tmpval+=7;
          for (int i = 0; i < response.getValueLength(); i++) {
            Serial.print(response.getValue()[i], HEX);
            Serial.print(" ");
          }
          Serial.println("");
        }
      } else {
        Serial.print("Command returned error code: ");
        Serial.println(response.getStatus(), HEX);
        tmpval+=11;
      }
    } else {
      Serial.print("Expected AT COMMAND response but got ");
      Serial.print(xbee.getResponse().getApiId(), HEX);
      tmpval+=13;
    }   
  } else if (xbee.getResponse().isError()) {
    Serial.print("Error reading packet.  Error code: "); 
    Serial.println(xbee.getResponse().getErrorCode());
    tmpval+=17;
  } else {
    Serial.print("No response from radio"); 
    tmpval+=19;
  }
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy