Hi,
I am trying to send data from an ESP-32 running Circuitpython over a mesh network of XBEE PRO 900 S3Bs and then receive it on a PC connected to another XBEE PRO 900. Normal Python the PC side and I can certainly receive packets and interpet data when generating packets on the remote XBEE using the frame generator.
The issue is that I can’t completely seem to figure out the circuitpython to make the packets completely correct. The XBEE python library doesn’t have a circuit python port. I only ever need to send data from the ESP-32 and it is simple numerical values so… real simple.
Here’s my circuit python:
code START
import busio
import board
uart = busio.UART(board.TX, board.RX, baudrate=9600)
def calculate_checksum(frame_data):
“”“Calculate the checksum for a given frame.”“”
return 0xFF - (sum(frame_data) & 0xFF)
def escape_byte(byte):
“”“Escape a byte if necessary.”“”
escape_bytes = [0x7E, 0x7D, 0x11, 0x13]
if byte in escape_bytes:
return bytes([0x7D, byte ^ 0x20])
else:
return bytes([byte])
def escape_frame(frame_data):
“”“Escape special bytes in the frame data.”“”
escaped_frame = bytearray()
for byte in frame_data:
escaped_frame.extend(escape_byte(byte))
return escaped_frame
def send_message(message, dest_address):
if len(dest_address) != 16:
raise ValueError("Destination address must be a 16-character hexadecimal string")
# Frame type 0x10 (Transmit Request), Frame ID 0x01 (to receive ACK)
frame_data = bytearray([0x10, 0x01])
# 64-bit dest_address
frame_data += bytes.fromhex(dest_address)
# 16-bit network address (unknown or broadcast), broadcast radius, options
frame_data += bytes([0xFF, 0xFE, 0x00, 0x00])
# RF data (payload)
frame_data += message.encode()
# Calculate checksum
checksum = calculate_checksum(frame_data)
# Append checksum to get the complete frame data before escaping
complete_frame_data = frame_data + bytes([checksum])
# Escape the frame data
escaped_frame = escape_frame(complete_frame_data)
# Frame delimiter and length (excluding delimiter, length, and checksum bytes)
final_frame = bytes([0x7E]) + len(frame_data).to_bytes(2, 'big') + escaped_frame
print(len(frame_data).to_bytes(2, 'big'))
# Send the frame
uart.write(final_frame)
print(final_frame)
dest_address = “000000000000FFFF” # Broadcast address for example
send_message(“hello”, dest_address)
code END
If I print the final frame I get : b’~\x00\x13\x10\x01\x00\x00\x00\x00\x00\x00\xff\xff\xff\xfe\x00\x00hello\xdf’
But on my receive SBEE side I receive " 7E 00 23 90 00 7D 33 A2 00 42 3F 49 F6 FF FE C2 7D 5E 00 7D 33 10 01 00 00 00 00 00 00 FF FF FF FE 00 00 68 65 6C 6C 6F DF AB" and a little extra ascii… ~ÿÿÿþhelloß
Any ideas?