Xbee使用教程:软件配置及通信python代码

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

在室外通信会常用 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。

如下图所示:

图1 XBEE-B配置
图2 XBEE-A配置

多个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模式。
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的使用进行简单的总结,后续会继续补充完善。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰子可可派_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值