Skip to content

Instantly share code, notes, and snippets.

@cpbotha
Last active March 15, 2018 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpbotha/8d47ef99a76516086a5a3cfa18ae9d9b to your computer and use it in GitHub Desktop.
Save cpbotha/8d47ef99a76516086a5a3cfa18ae9d9b to your computer and use it in GitHub Desktop.
/**
* Running this sketch on the RobotDyn Arduino M0 (SAMD21) with the itead xbee shield 1.1 I see the following output:
*
* waiting for any serial data to become available from the xbee
* 2795 MODEM_STATUS_RESPONSE: 0
* waiting for any serial data to become available from the xbee
* No packet available.
* waiting for any serial data to become available from the xbee
* 7974 MODEM_STATUS_RESPONSE: 0
*
* In other words, the xbee hardware resets more or less every 5 seconds.
*
* The DIN LED on the board remains softly on, which is also strange.
*
* xbee s2c with 802.15.4 function set, firmware 2001, settings at default except for API Mode 2, and RTS flow control.
*
* I would be super grateful for any hints you can give me to find out why this does not work!
*
* -- cpbotha@vxlabs.com
*
*/
#include <XBee.h>
#include <Printers.h>
// On the Arduino M0, Serial5 is the hardware serial on pins 0 and 1,
// see https://store.arduino.cc/arduino-m0
#define SERIAL_XBEE Serial5
// on the M0 we use this to output to the serial monitor on the connected
// host which is connected via the USB iface of the chip
#define SERIAL_MON SerialUSB
// the itead shield connects A0 to xbee RTS
// we have configured XBEE (via XCTU) with RTS activated for flow control
#define PIN_RTS A0
// construct XBee object to be use as API to the xbee module
XBee xbee = XBee();
// sets the command to get the MY address
uint8_t myCmd[] = {'M', 'Y'};
// Sets the command to exit AT mode.
uint8_t cnCmd[] = {'C','N'};
// Initialises the AT Request and Response.
AtCommandRequest atRequest = AtCommandRequest();
AtCommandResponse atResponse = AtCommandResponse();
void setup() {
// to keep it simple, tell xbee it is free to talk to us
pinMode(PIN_RTS, OUTPUT);
digitalWrite(PIN_RTS, LOW);
SERIAL_MON.begin(9600);
// have to wait for the serial port to connect
while (!SERIAL_MON) {
;
}
SERIAL_XBEE.begin(9600);
xbee.begin(SERIAL_XBEE);
addressRead();
}
void loop() {
// put your main code here, to run repeatedly:
}
void addressRead()
{
// Sets the AT Command to Serial Low Read.
atRequest.setCommand(myCmd);
// Sends the AT Command.
xbee.send(atRequest);
auto got_at_response = false;
while (!got_at_response) {
SERIAL_MON.println("waiting for any serial data to become available from the xbee");
while (!SERIAL_XBEE.available()) {
;
}
if (xbee.readPacket(1000))
{
if(xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE)
{
got_at_response = true;
xbee.getResponse().getAtCommandResponse(atResponse);
if(atResponse.isOk())
{
// Reads the entire response and outputs
for (int i = 0; i < atResponse.getValueLength(); i++)
{
SERIAL_MON.print(atResponse.getValue()[i], HEX);
SERIAL_MON.print(" ");
}
SERIAL_MON.println("SUCCESSFULLY READ FROM XBEE");
}
}
else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
auto msr = ModemStatusResponse();
xbee.getResponse().getModemStatusResponse(msr);
SERIAL_MON.print(millis());
SERIAL_MON.print(" MODEM_STATUS_RESPONSE: ");
SERIAL_MON.println(msr.getStatus());
// 0: HARDWARE_RESET
// for the rest see https://github.com/andrewrapp/xbee-arduino/blob/4e839822724eadc58d4626e36baed235454a0b9d/XBee.h#L129
}
else {
// could easily be RX_16_RESPONSE when other arduinos are transmitting
SERIAL_MON.print("Unexpected ApiId :: ");
SERIAL_MON.println(xbee.getResponse().getApiId(), HEX);
}
}
else {
SERIAL_MON.println("No packet available.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment