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()