currently I’am using the pi-i2c-shield-w-xbee-communication shield on mi raspberry pi4b. I have everything set up I am receiving the data that I’m looking for,but what’s happening is if I reboot my pi or it looses power the xbee does not respond or reconnects. I have to physically remove the xbee module from the shield re-insert it and restart my script and it connects. on startup I have my python script running as a service so it starts on boot up and in my script i give the xbee module 3 tries to establish comms. If it doesn’t it exits. I also tested this theory by not running my script on start up and same issue if I manually started it. Again the only way I got it to communicate was physically remove it and re-insert it on the shield and then start the script.I’m not sure why it’s doing this? I don’t know if it’s a hardware issue or what? Any help on this matter is greatly appreciated. Below is my script if it helps. But like I said the script is running fine.
import time
import threading
from digi.xbee.devices import XBeeDevice, RemoteXBeeDevice
from digi.xbee.models.address import XBee64BitAddress
from digi.xbee.exception import TimeoutException, TransmitException
import RPi.GPIO as GPIO
Constants for retry mechanism
MAX_RETRIES = 3
DELAY_BETWEEN_RETRIES = 5 # 5 seconds delay
Configure GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
Instantiate XBee device
xbee = XBeeDevice(“/dev/serial1”, 9600)
remote_address = “0013A2004230AED2”
remote_device = RemoteXBeeDevice(xbee, XBee64BitAddress.from_hex_string(remote_address))
Global timer variable
timer = None
def send_data_with_retry(xbee_device, remote_device, data, max_retries, delay):
for attempt in range(1, max_retries + 1):
try:
xbee_device.send_data(remote_device, data)
print(f"Sent ‘{data}’ command to remote XBee.“)
return True
except (TimeoutException, TransmitException) as e:
if attempt < max_retries:
print(f"Attempt {attempt} to send data ‘{data}’ failed. Retrying in {delay} seconds…”)
time.sleep(delay)
else:
print(f"Attempt {attempt} to send data ‘{data}’ failed. No more retries.“)
return False
except Exception as e:
print(f"Unexpected error while sending ‘{data}’: {str(e)}”)
return False
def start_timer(channel):
print(“start_timer function executed”)
global timer
if timer:
timer.cancel()
# Send command to turn on light with retries
send_data_with_retry(xbee, remote_device, "light on", MAX_RETRIES, DELAY_BETWEEN_RETRIES)
# Start a new 60 second timer
timer = threading.Timer(60.0, timer_function)
timer.start()
def timer_function():
if GPIO.input(5) == GPIO.LOW:
# Send command to turn off light with retries
send_data_with_retry(xbee, remote_device, “light off”, MAX_RETRIES, DELAY_BETWEEN_RETRIES)
def open_xbee_with_retry(xbee_device, max_retries, delay):
for attempt in range(1, max_retries + 1):
try:
xbee_device.open()
print(“Successfully opened XBee device on attempt”, attempt)
return True
except Exception as e:
if attempt < max_retries:
print(f"Attempt {attempt} to open XBee device failed. Retrying in {delay} seconds…“)
time.sleep(delay)
else:
print(f"Attempt {attempt} to open XBee device failed. No more retries.”)
return False
def main():
try:
# Initial delay before first attempt
print(“Waiting a bit for XBee module to be ready…”)
time.sleep(2)
# Try to open the XBee device with retries
if not open_xbee_with_retry(xbee, MAX_RETRIES, DELAY_BETWEEN_RETRIES):
print("Failed to open XBee device after multiple attempts. Exiting.")
return
# Print GPIO Pin Status
print("GPIO Pin Status:", GPIO.input(5))
# Setup GPIO interrupt
GPIO.add_event_detect(5, GPIO.BOTH, callback=start_timer, bouncetime=200)
# Run forever
while True:
time.sleep(0.1)
except KeyboardInterrupt:
pass
except Exception as e:
print("An error occurred: {}".format(str(e)))
finally:
# Cleanup
if xbee and xbee.is_open():
xbee.close()
GPIO.cleanup()
if name == “main”:
main()