I have been trying to send API commands from arduino to XBee Series 1 (the coordinator device) recently and have been unable to do so. I have tried it both with plain Arduino code (sending 7E 00 10 17 etc etc using Serial.print with SoftwareSerial.h ) and using the Arduino XBee API library.
I have been unsuccessful with both, would be great if someone could help.
I have been successful with the following scenarios so far:
Using X-CTU, I can send AT commands or API commands to the Xbee and the Xbee responds as expected (I am changing D0 to HIGH)
I can have a Xbee end device periodically send data I/O values to Xbee coordinator which is read by the arduino through serial (Data is coming as 7E 00 etc etc)
So, the Xbeess are generally working. My only problem is with sending AT command or remote AT Command from Arduino to XBee coordinator device (API Enable is set to 2)
When I send the AT command from Arduino to XBee Coordinator, I get the “No response from radio”. It almost seems like the at command is not being sent and there is a problem with the connection from arduino to XBee, however I have not been able to figure it out.
The code is below:
#include <SPI.h>
#include <SoftwareSerial.h>
#include <XBee.h>
SoftwareSerial mySerial(0, 1);
XBee xbee = XBee();
uint8_t shCmd[] = {'D','0'};
uint8_t shCmd_param[] = { 0x5 };
uint8_t slCmd[] = {'D','0'};
uint8_t slCmd_param[] = { 0x4 };
AtCommandRequest atRequest = AtCommandRequest(shCmd,shCmd_param,sizeof(shCmd_param));
AtCommandResponse atResponse = AtCommandResponse();
void setup() {
Serial.begin(9600);
xbee.begin(9600);
delay(5000);
}
void loop() {
sendAtCommand();
delay(5000);
atRequest.setCommand(slCmd);
atRequest.setCommandValue(slCmd_param);
atRequest.setCommandValueLength(sizeof(slCmd_param));
sendAtCommand();
delay(5000);
}
void sendAtCommand() {
Serial.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()) {
Serial.print("Command [");
Serial.print(atResponse.getCommand()[0]);
Serial.print(atResponse.getCommand()[1]);
Serial.println("] was successful!");
if (atResponse.getValueLength() > 0) {
Serial.print("Command value length is ");
Serial.println(atResponse.getValueLength(), DEC);
Serial.print("Command value: ");
for (int i = 0; i < atResponse.getValueLength(); i++) {
Serial.print(atResponse.getValue()[i], HEX);
Serial.print(" ");
}
Serial.println("");
}
}
else {
Serial.print("Command return error code: ");
Serial.println(atResponse.getStatus(), HEX);
}
} else {
Serial.print("Expected AT response but got ");
Serial.print(xbee.getResponse().getApiId(), HEX);
}
} else {
// 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");
}
}
}
I have tried it both with plain Arduino code (sending 7E 00 10 17 etc etc using Serial.print with SoftwareSerial.h )
You can't send binary data using the print() method. Are you using Serial or an instance of SoftwareSerial? Not that it matters, because the print() method is not for sending binary data using either one.
I can have a Xbee end device periodically send data I/O values to Xbee coordinator
End devices and coordinators are Series 2/mesh network terms, not Series 1/point to point terms.
The code is below:
is posted improperly. Fix it, if you want is to read it.
Thank you for the reply, I fixed the way code appears in the forum for everyone’s review.
Replies to your questions:
Of course, I did Serial.write for the API packet sending to the XBee
When I used plain API Packet sending with mySerial.write, I used an instance of SoftwareSerial to send data to Xbee through RX / TX (ports 0 and 1) and used Serial.println to write debug messages to Arduino IDE Serial Monitor
When I used the XBee library (which is the code I pasted in the initial post), I used Serial.println to write debug message to Arduino IDE Serial Monitor again and xbee.begin to send messages to xbee
My guess is that there is something wrong with writing to the serial port for Xbee, however I have not been able to figure it out. It seems to be something very fundamental!
I made the changes you mentioned, however, I am still getting the same response ("No response from radio").
When I instantiate an XBee class, which Rx-Tx ports does it use automatically to transmit/receive data for my XBee? (No pins are specified during class instantiation or later on, it is done this way in the example codes as well so I am wondering)
I believe there could be something wrong with the way I am using the serial connection to the Xbee. Currently, when I take out the SoftwareSerial.h and XBee.h, and I do a Serial.write in the code, the data in Serial.write() is supposed to be transmitted to the XBee correct? Currently, it is being written to my Arduino IDE Serial Monitor. Is this correct behaviour?
When I instantiate an XBee class, which Rx-Tx ports does it use automatically to transmit/receive data for my XBee?
If the setSerial() method is used, the pins associated with the specified hardware or software instance are used. If the method is not used, the default is (mostly) Serial, which is tied to pins 0 and 1. For the Leonardo and other boards that use Serial1 to talk to pins 0 and 1, that is the default instance for the XBee instance.
Currently, when I take out the SoftwareSerial.h and XBee.h, and I do a Serial.write in the code, the data in Serial.write() is supposed to be transmitted to the XBee correct? Currently, it is being written to my Arduino IDE Serial Monitor. Is this correct behaviour?
Yes. The XBee, if properly configured, should also be transmitting the data. Do it's (shield's) lights flash?
When I connect the XBee directly to my computer through the USB-FTDI cable and use X-CTU, only the PWR light is on for the shield. In this case, sending a command (AT or API) through X-CTU terminal works perfectly.
However, when I have Arduino power the Xbee through 3V3 and GND, then both PWR and DIN lights are on all the time (I have not even connected RX and TX yet at this point). I had seen this before however did not pay attention to it. This means that XBee is somehow getting data (I have no clue how), therefore it cannot get any more data through the serial connection (which is my problem).
I am trying to figure this out as at the same time. Do you have any ideas - why would DIN be on all the time without establishing a connection between Arduino and Xbee through serial connection?
Apparently, one needs to use the DIN pin on the side of the breakout board instead of the DIN pin below the breakout board - I have no clue why. Once I started to use the DIN pin on the side, I was able to send data from Arduino to XBee.