From 45ee01612648f552d2bf49b9293102ce04f5c83c Mon Sep 17 00:00:00 2001 From: BlueFox Date: Sat, 30 Mar 2024 17:25:47 +0000 Subject: [PATCH] Added timeout when running as initializer and not receiving an pong answer when running as a responder, no timeout is needed - it should just sit there waiting till a request (ping) arrives - that't why the ToDo there got removed --- lora_pingpong.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lora_pingpong.py b/lora_pingpong.py index 4813c9b..5304cca 100644 --- a/lora_pingpong.py +++ b/lora_pingpong.py @@ -53,13 +53,27 @@ def as_initializer(lora, lcd, lcd_connected, interrupt_pin): ping_to_send = False if not ping_to_send: print("[LoRaPingPong] Now waiting for a response") + time_now = time() while not lora.received_packet(): sleep(0.01) if interrupted(): return # if interrupt btn is pressed, stop listening - # TODO: implement a timeout + if (time() - time_now) > 15: + # timeout if waiting too long for a response + # possible causes of triggering this could be e.g. lost connection + print("[LoRaPingPong] Timeout reached. Sending ping again.") + if lcd: + lcd.move_to(0,1) + lcd.putstr("Reached timeout!") + break + # NOW got a response! blink_led(receive_led) payload = lora.read_payload() + + if not payload: # if empty because of timeout + ping_to_send = True + continue + try: payload_dict = loads(payload) TX_POWER_REQ = payload_dict["tx_power"] @@ -116,7 +130,6 @@ def as_responder(lora, lcd, lcd_connected, interrupt_pin): while not lora.received_packet(): sleep(0.01) if interrupted(): return # if interrupt btn is pressed, stop listening - # TODO: implement a timeout # NOW got a response! blink_led(receive_led) payload = lora.read_payload()