提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
在室外通信会常用 Xbee,本文介绍了 Xbee 的基本配置和通信例程。
配置参数软件在 Windows,例程运行在 Nano 上。
一、Xbee 配置
配置软件 X-CTU 可以从官网的下载:配置软件 X-CTU 下载地址。
在 X-CTU 中对组网模式设置为 Router:
点对点通信设置:
1、A和B两个模块的TO都设置为40;
2、A的DH设置为B的SH,A的DL设置为B的SL;
3、B的DH设置为A的SH,B的DL设置为A的SL。
如下图所示:
![]() |
![]() |
多个Xbee组网后面会再添加。
二、使用步骤
1.准备工作
安装 python3 中 digi.xbee, xbee 功能包
pip3 install digi-xbee
pip3 install xbee
打开USB串口权限
sudo usermod -a -G dialout <username>
username是终端“A@B:”中的A。添加权限之后还需“先登出,再登入”。
为了能够使用程序发送数据,需要设置为API模式。
2.发送数据例程
transfer.py:
#! /usr/bin/python
from digi.xbee.devices import XBeeDevice
# TODO: Replace with the serial port where your local module is connected to.
PORT = "/dev/ttyUSB0"
# TODO: Replace with the baud rate of your local module.
BAUD_RATE = 9600
DATA_TO_SEND = "BUAA"
REMOTE_NODE_ID = "XBEE_A" # transfer to XBEE_A.
def main():
print(" +--------------------------------------+")
print(" | XBee Python Library Send Data Sample |")
print(" +--------------------------------------+\n")
device = XBeeDevice(PORT, BAUD_RATE)
try:
device.open()
# Obtain the remote XBee device from the XBee network.
xbee_network = device.get_network()
remote_device = xbee_network.discover_device(REMOTE_NODE_ID)
if remote_device is None:
print("Could not find the remote device")
exit(1)
print("Sending data to %s >> %s..." % (remote_device.get_64bit_addr(), DATA_TO_SEND))
device.send_data(remote_device, DATA_TO_SEND)
print("Success")
finally:
if device is not None and device.is_open():
device.close()
if __name__ == '__main__':
main()
3.接收数据例程
receiver.py:
#! /usr/bin/python
from digi.xbee.devices import XBeeDevice
from xbee import XBee, ZigBee
import serial
# TODO: Replace with the serial port where your local module is connected to.
PORT = "/dev/ttyUSB0"
# TODO: Replace with the baud rate of your local module.
BAUD_RATE = 9600
def main():
print(" +-----------------------------------------+")
print(" | XBee Python Library Receive Data Sample |")
print(" +-----------------------------------------+\n")
device = XBeeDevice(PORT, BAUD_RATE)
try:
device.open()
def data_receive_callback(xbee_message):
print("From %s >> %s" % (xbee_message.remote_device.get_64bit_addr(),
xbee_message.data.decode()))
device.add_data_received_callback(data_receive_callback)
print("Waiting for data...\n")
input()
finally:
if device is not None and device.is_open():
device.close()
if __name__ == '__main__':
main()
总结
对Xbee的使用进行简单的总结,后续会继续补充完善。