Configuring api mode 2 for xbees without x-ctu
When you get XBees from the factory, many of them are configured with api mode = 0, meaning the xbee is operating in transparent mode. If you want to use certain libraries like the xbee-python library from digi to configure, you need to use api mode = 2. You can use X-CTU to do this, but I find it’s much easier to do through the command line… it’s also a relatively simple process.
X-CTU is also not supported on arm architectures, so this might be useful for folks who are managing XBees on raspberry pi’s.
You need to do a couple of things:
- enter command mode
ATAPto set the escaping mode
- write the configuration to the XBee’s non-volatile memory
- exit command mode (not really necessary since the device will do this naturally after a while).
Here’s a python script that should do the described:
import serial ser = serial.Serial('/dev/ttyUSB0') # open serial port print(ser.name) # check which port was really used ser.write(b'+++') # write a string reply = ser.read_until(b'\r') print(reply) # Set api = 2 ser.write(b'ATAP2\r') reply = ser.read_until(b'\r') print(reply) # Commit the configuration to storage ser.write(b'ATWR\r') # write a string reply = ser.read_until(b'\r') print(reply) # Exit command mode ser.write(b'ATCN\r') reply = ser.read_until(b'\r') print(reply) ser.close() # close port
You should see output like:
/dev/ttyUSB0 b'OK\r' b'OK\r' b'OK\r' b'OK\r'
Alternatively, there are some modifications you can make to this flow:
- you can also use
ATREto reset the device to factory settings before changing whatever configuration you need to
- reboot the xbee device with
ATFRso you can check the settings will persist after a reboot