user:kluong:feature_idea_-_python_script_to_configure_xbees

Feature Idea - Python script to program XBees

Instead of configuring the XBees using X-CTU, it should be possible to automate this process a little more by using a python script. This would make it a bit easier for anyone to configure new xbees that work with the production network, and you can also be sure that there are no human errors as well.

The key is being able to issue AT commands to the XBees, as described in this page: https://www.digi.com/resources/documentation/Digidocs/90001456-13/concepts/c_at_commands.htm?TocPath=XBee%20transparent%20mode%7CCommand%20mode%7C_____1_change_attached_xbee_s_panid

A script would look something like this:

import serial
ser = serial.Serial('/dev/ttyUSB0')  # open serial port, TODO ask the user what port
print(ser.name)         # check which port was really used
ser.write(b'+++')     # write a string
reply =  ser.read_until(b'\r')
print(reply)
 
# Reset the xbee to factory defaults:
ser.write(b'ATRE\r')
reply =  ser.read_until(b'\r')
print(reply)
 
# Set api mode = 2. This is required for the xbee to work with certain libraries, like the arduino-xbee or python xbee libraries for example.
ser.write(b'ATAP 2\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

The output would look like:

/dev/ttyUSB0
b'OK\r'
b'OK\r'
b'OK\r'
b'OK\r'

Authors

Contributing authors:

kluong

Created by kluong on 2021/03/24 00:09.

  • user/kluong/feature_idea_-_python_script_to_configure_xbees.txt
  • Last modified: 2021/09/19 21:59
  • (external edit)