XBee successfully transmits four times then fails

I’m running the standard sample code below modified with a for loop. The result is four successful transmissions, and five unsuccessful transmissions. What am I missing here? Thank you!

import sys
import xbee

TODO: replace with the node identifier of your target device.

TARGET_NODE_ID = “Router”
MESSAGE = “Hello XBee!”

def find_device(node_id):

for dev in xbee.discover():
    if dev['node_id'] == node_id:
        return dev
return None

print(" ±-------------------------------------------+“)
print(” | XBee MicroPython Transmit Data (NI) Sample |“)
print(” ±-------------------------------------------+
")

Find the device with the configure node identifier.

device = find_device(TARGET_NODE_ID)
if not device:
print(“Could not find the device with node identifier ‘%s’” % TARGET_NODE_ID)
sys.exit(-1)

addr16 = device[‘sender_nwk’]
addr64 = device[‘sender_eui64’]
for x in range (0,9):
print(“Sending data to %s >> %s” % (TARGET_NODE_ID, MESSAGE))
try:
# Some protocols do not have 16-bit address. In those cases, use the 64-bit one.
xbee.transmit(addr16 if addr16 else addr64, MESSAGE)
print(“Data sent successfully”)
except Exception as e:
print(“Transmit failure: %s” % str(e))
x += 1

Results:
Sending data to Router >> Hello XBee!
Data sent successfully
Sending data to Router >> Hello XBee!
Data sent successfully
Sending data to Router >> Hello XBee!
Data sent successfully
Sending data to Router >> Hello XBee!
Data sent successfully
Sending data to Router >> Hello XBee!
Transmit failure: [Errno 7107] ENOTCONN
Sending data to Router >> Hello XBee!
Transmit failure: [Errno 7107] ENOTCONN
Sending data to Router >> Hello XBee!
Transmit failure: [Errno 7107] ENOTCONN
Sending data to Router >> Hello XBee!
Transmit failure: [Errno 7107] ENOTCONN
Sending data to Router >> Hello XBee!
Transmit failure: [Errno 7107] ENOTCONN

Try importing time and then add a time.sleep.s(1) to your code.

Where would you put it? No matter where I put it, I get 9 transmission failures.

I would put the time.sleep just after the x+1

That was my initial thought as well. 9 transmission failures, then I had to power cycle the units to get them back to communicating at all.

This is interesting. Could you update your program to report the value of ATAI (using xbee.atcmd(‘AI’)) on transmit failure? What if you have an infinite loop with a 1 second delay between each send?

The ENOTCONN should only come up when ATAI is non-zero, indicating that you aren’t on a network.

Sending data to Router >> Hello XBee!
Data sent successfully
Sending data to Router >> Hello XBee!
Data sent successfully
Sending data to Router >> Hello XBee!
Data sent successfully
Sending data to Router >> Hello XBee!
Data sent successfully
Sending data to Router >> Hello XBee!
0
Transmit failure: [Errno 7107] ENOTCONN
Sending data to Router >> Hello XBee!
0
Transmit failure: [Errno 7107] ENOTCONN
Sending data to Router >> Hello XBee!
0
Transmit failure: [Errno 7107] ENOTCONN
Sending data to Router >> Hello XBee!
0
Transmit failure: [Errno 7107] ENOTCONN
Sending data to Router >> Hello XBee!
0
Transmit failure: [Errno 7107] ENOTCONN

Updated to report ATAI - I’ll tackle the infinite loop next. I appreciate the thoughts.