def send_backend_data(sim: ubirch.SimProtocol, modem: Modem, conn: Connection, api_function, uuid, data) -> (int, bytes): MAX_MODEM_RESETS = 1 # number of retries with modem reset before giving up MAX_RECONNECTS = 1 # number of retries with reconnect before trying a modem reset for reset_attempts in range(MAX_MODEM_RESETS + 1): # check if this is a retry for reset_attempts if reset_attempts > 0: print("\tretrying with modem reset") sim.deinit() modem.reset( ) # TODO: should probably be connection.reset_hardware() conn.connect() # try to send multiple times (with reconnect) try: for send_attempts in range(MAX_RECONNECTS + 1): # check if this is a retry for send_attempts if send_attempts > 0: print("\tretrying with disconnect/reconnect") conn.disconnect() conn.connect() try: print("\tsending...") return api_function(uuid, data) except Exception as e: # TODO: log/print exception? print("\tsending failed: {}".format(e)) # (continues to top of send_attempts loop) else: # all send attempts used up raise Exception("all send attempts failed") except Exception as e: print(repr(e)) # (continues to top of reset_attempts loop) else: # all modem resets used up raise Exception("could not establish connection to backend")
max_file_size_kb=max_file_size_kb, sd_card=SD_CARD_MOUNTED) try: # get system information print("pycom firmware: ", uname()) # print("modem firmware: ", sqnsupgrade.info()) # initialize modem lte = LTE() modem = Modem(lte, error_handler) try: # reset modem on any non-normal loop (modem might be in a strange state) if not COMING_FROM_DEEPSLEEP: print("++ not coming from sleep, resetting modem") modem.reset() print("++ getting IMSI") imsi = modem.get_imsi() print("IMSI: " + imsi) except Exception as e: print("\tERROR setting up modem") error_handler.log(e, COLOR_MODEM_FAIL) while True: machine.idle() set_led(LED_TURQUOISE) # load configuration, blocks in case of failure print("++ loading config") try: