Jan 222010
 
XBee - API Mode

After playing a bit in the transparent (AKA AT) mode, it became apparent that it’s quite limited if you plan to do more than just using 2 XBee’s as a wireless serial connection. E.g. it’s not possible to receive the state of the digital and analog inputs from a remote XBee endpoint. The biggest drawback of the API mode is that it’s quite difficult to interactively use it. It’s made for programs to use, not for humans.

After looking for a usable API library, I found this blog which not only pointed me to the right direction, but also to a Python library which is easy enough to use. Easy enough for me who’s not used to Python.

Here a short program included in the library and meant as a sample, which I I slightly modified to make D4 of the endpoint blink, and it sends back the status of D1 and D2 once in 0xf000ms. That part does not work though. I always seem to get the same values back regardless of the I/O status.

from xbee_api import *

class myXbeeApi(XbeeApi):
def onData(self,pkg):
#get header for IO data
if pkg["code"]==0x92:
  print "Samples %s-> D:%s/%s" % (pkg["data"]["mac"],pkg["data"]["dmask"],pkg["data"]["dsamples"])

api=myXbeeApi("/dev/ttyUSB1",9600)

device={
 "CO3":[0x00,0x13,0xa2,0x00,0x40,0x52,0x8d,0x8a],
  "EP1":[0x00,0x13,0xa2,0x00,0x40,0x4a,0x61,0x84]
}

led=False

api.sendRemoteAT(device["EP1"],"D2",[0x02])
api.sendRemoteAT(device["EP1"],"D1",[0x03])
api.sendRemoteAT(device["EP1"],"IR",[0xf0,0x0])
api.sendRemoteAT(device["EP1"],"IC",[0x00])

while 1:

 #set led status
 led=not led
 if led:
  api.sendRemoteAT(device["EP1"],"D4",[0x04])
 else:
  api.sendRemoteAT(device["EP1"],"D4",[0x05])
 time.sleep(0.5)

Several things I learned while using those XBee radios:

  1. When flashing new firmware on the XBee using the X-CTU program, keep 9600 baud. It makes your life easier as it will always default back to 9600 which is when you will lose your connection to it again and again. When everything is set and working, change baud rate to what you like.
  2. Digi’s documentation is not bad, but more examples would make life easier. Luckily those XBees are popular enough that there are lots of examples in the wild.
  3. I still have no idea what the firmwares for digital and analog endpoints do. The endpoint with API interface seems to be able to handle everything.
  4. Watch out for sleep mode. For endpoints, you cannot turn off sleep mode. Instead use Pin 9 and set it to GND to disable sleep (and SM=1).
  5. Connect the RSSI and Associate pin to LEDs. Helps a lot while debugging.
  6. Have the Commission pin connected. It turns the radio on for 30s so sleep mode is not an issue for 30 seconds.