This section provides various code snippets that demonstrate the practical applications of XBee devices in Python to utilize to the best extent the capabilities of the XBee mesh network. The code snippets cover different functionalities such as obtaining mesh network device information, broadcasting/receiving a test ping signal, testing signal strength, sending files, time updates, and obtaining information and status of specified nodes.
The Common Setup section provides a shared step for each application and usage that needs to be included before using other codes for the applications. The goal is to aggregate all of the modules needed to be imported into one singular code block below for the sake of code organization.
The Specific Applications section contains the code for specific applications such as obtaining mesh network device information, broadcasting/receiving a test ping signal, testing signal strength, sending files, time updates, and obtaining information and status of specified nodes. The code snippets provide detailed instructions and notes for executing the code on different nodes and devices. Please copy and paste the Common Setup code, along with the specific snippets into a new Python code into a new Python file on the correct Raspberry Pi device type specified in the header in front of each snippet.
The code snippets provided can be easily modified to suit specific project needs and requirements.
<aside> 💡 Tip: Click on the corresponding section header in the wiki to be automatically redirected to a desired section.
</aside>
This is a shared step for each application and usage that would need to be included before using other codes for the said applications. The goal is to aggregate all of the modules needed to be imported into one singular code block below for the sake of code organization.
import numpy as np
import os
import datetime
import time
import serial
from digi.xbee.devicese import XBeeDevice
from xbee import XBee
from digi.xbee import serial
from digi.xbee.util import utils
from abc import ABCMeta, abstractmethod
from enum import enum, unique
from functools import wraps
from ipadress import IPv4Address
from queue import Queue, Empty
from digi.xbee.serial import FlowControl, XBeeSerialPort
device = XBeeDevice("COM1", 9600) # NOTE: Change "COM1" to the according serial port
# that the XBee is plugged into. COM1 is placed here as a placeholder assuming the
# XBee device is plugged into "USB-A Port 1".
try:
device.open()
# Open device connection.
local = device.get_local_xbee_device()
# Obtain the local nodes.
print("Discovered local nodes:")
for node in local:
print(" - %s" % node)
# Print the output log discovered local nodes.
local_add = local.get_64bit_addr()
# Obtain the MAC Address of the local node.
print("Discovered local node addresses:")
for addr in local_add:
print(" - %s" % addr)
# Print the output log discovered local nodes.
remote = device.get_network().discover_devices()
# Print the output log discovered remote nodes.
print("Discovered remote nodes:")
for node in remote:
print(" - %s" % node)
finally:
if device is not None and device.is_open():
device.close()
# Instantiate the boardcasting XBee device object.
device = XBeeDevice("COM1", 9600) # NOTE: Change "COM1" to the according serial port
# that the XBee is plugged into. COM1 is placed here as a placeholder assuming the
# XBee device is plugged into "USB-A Port 1".
try:
# Open the device.
device.open()
device.send_data_broadcast("Test Thingity Test!")
# Broadcast the test signal.
finally:
# Close the device connection.
if device is not None and device.is_open():
device.close()