XBee API Interface for Arduino
**** This code hasn’t been maintained in some time, and there are some definite bugs. I recommend using the library at http://code.google.com/p/xbee-arduino/ — if for some reason you really prefer to use this library, drop me a line and I’ll try to pull together my current, updated XBee code into something useful.
This is a library I’ve been working on (and off and on and off) for a while now, for using XBees in API mode on the Arduino platform. This is totally alpha code right now, and the API WILL CHANGE. Still, it’s probably useful enough for some projects.
*** Updated — API changed to be closer to Processing API by Rob Faludi / Daniel Shiffman ***
New Code here
Original Code here
Right now it’s made to work with the Series 1 (802.15.4) radios; at some point I should get the bits in to work with the Zigbee style packets.
New Sample Code — this will print out IO packets sent by an XBee node (broadcast or sent directly to the local XBee)… I’ve been testing with settings on the remote node of ATD02 ATD12 ATD22 ATD32 ATIR50 for analog, and ATD03 ATD13 ATD23 ATD33 ATIR50 for digital)
I’m getting glitchy output from the swserial package (but still good enough to be useful for testing…), I’ve been testing using an XBee breakout board for the XBee interface, and an FTDI TTL232R cable connected to the digital pins on the Arduino that the breakout board doesn’t cover (black goes to GND, brown to D13 and so on down to GRN at D9). I pulled the +5v pin from the connecting set of pins just to make sure if I do something with D12 it won’t burn the pin. I could have just wired up GND and RX and that would work just as well for this.
#include <SoftwareSerial.h>
#include <XBeeReader.h>
// Associate the xbee with the standard serial port.
XBeeReader xbee(Serial);
SoftwareSerial swserial = SoftwareSerial(11, 10);
void setup()
{
int r;
digitalWrite(13, HIGH);
pinMode(13, OUTPUT);
pinMode(10, OUTPUT);
// Open the xbee at 57600 baud (optional second parameter, “force API mode”, defaults to true,
// causing an ATAP1 to be sent. This takes a few seconds to accomplish.
// Default baud rate if none given is 9600.
r = xbee.begin(57600);
if (r < 0) {
// If there was an error on startup, flash the LED forever.
while(1) {
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
}
}
digitalWrite(13, LOW);
swserial.begin(2400);
swserial.println(“SWSERIAL started”);
}
void loop()
{
byte addr[8];
// Should be called every time through loop to operate the xbee data pump
xbee.poll();
// Default address is 0xffff (broadcast). Probably should split into 2 functions instead of
// weirdly putting address, etc, after data to go out.
//
// if you do this, i recommend putting a delay to slow things down to manageable.
//xbee.send(“hi”, 2);
// If there’s a packet available coming in, process it…
if (xbee.available()) {
XBeeDataFrame f;
int digitalVals[9];
int analogVals[6];
int r;
// Get packet reading into f
xbee.getXBeeReading(f);
swserial.print(“pkt type: “);
swserial.println(f.getApiID(), HEX);
if (f.getAddress16(addr) >= 0) {
swserial.print(” addr16: “);
swserial.print(addr[0], HEX);
swserial.println(addr[1], HEX);
}
r = f.getDigital(digitalVals);
if (r >= 0) {
swserial.print(” Digital lines (“);
swserial.print(r);
swserial.print(“): “);
for (int i = 0; i < 9; i++) {
if (digitalVals[i] == -1) {
swserial.print(“?”);
} else {
swserial.print(digitalVals[i], HEX);
}
}
}
r = f.getAnalog(analogVals);
if (r >= 0) {
swserial.print(” Analog lines (“);
swserial.print(r);
swserial.print(“): “);
for (int i = 0; i < 6; i++) {
swserial.print(analogVals[i]);
swserial.print(” “);
}
swserial.println();
}
swserial.println();
}
}
content rss

Recent Comments