digidotcom / xbee-python

Python library to interact with Digi International's XBee radio frequency modules.
Mozilla Public License 2.0
185 stars 93 forks source link

Random timeout in xbee sender using crontab on Raspberry pi #263

Open zrion opened 3 years ago

zrion commented 3 years ago

Hello,

I am building a system where I have a remote Xbee sending packets continuously, and I'm using Raspberry Pi 4 as the host for the Xbee (connected to Xbee grove dev kit) , powered by a battery pack. I have a script that is loaded at Pi startup using crontab:

from digi.xbee.devices import XBeeDevice
import time

PORT = "/dev/ttyUSB0"
BAUD_RATE = 9600

DATA_TO_SEND = "Hello XBee! Sending packet "

def main():
    device = XBeeDevice(PORT, BAUD_RATE)

    packet_counter = 0

    try:
        device.open()

        print(device.set_sync_ops_timeout(15))
        while True:
            packet_counter += 1
            data = DATA_TO_SEND + str(packet_counter)

            print("Sending broadcast data: %s..." % data)

            device.send_data_broadcast(data)

            print("Success")
            time.sleep(0.03)

    finally:
        if device is not None and device.is_open():
            device.close()

if __name__ == '__main__':
    main()

What I observe is that the operation is experiencing random timeouts that cause the broadcasting interrupted, although sometimes it works smoothly. I tried to increase the timeout duration device.set_sync_ops_timeout(10) but the problem still persists. One thing to note is that manually running the script doesn't have this issue.

Any ideas to solve this problem is appreciated. I also want to ask a broader question: Is there any way to do something similar for the XBee without the Raspberry Pi (Xbee powered directly by a battery pack)?