diff --git a/src/lib/networking/cell_modem.py b/src/lib/networking/cell_modem.py index bb642bc..b294235 100644 --- a/src/lib/networking/cell_modem.py +++ b/src/lib/networking/cell_modem.py @@ -1,4 +1,5 @@ import asyncio +import gc import time import machine @@ -6,7 +7,7 @@ from machine import Pin from config import CELLULAR_APN, CELLULAR_STARTUP_TIMEOUT from lib.logging import logger, LogLevel -from lib.runtime import timeout, uTimeoutError +from lib.runtime import timeout, uTimeoutError, run_with_timeout PIN_BEE_POWER = 27 PIN_BEE_UART_RXD = 35 @@ -155,24 +156,6 @@ async def cellular_ip(): return ip -class RunWithtimeoutResult: - def __init__(self, failure: bool, result: any): - self.failure = failure - self.result = result - - -async def run_with_timeout(timeout_sec: int, func, *args, **kwargs) -> RunWithtimeoutResult: - @timeout(timeout_sec) - async def inner(): - return await func(*args, **kwargs) - - try: - result = await inner() - return RunWithtimeoutResult(False, result) - except uTimeoutError: - return RunWithtimeoutResult(True, None) - - async def cellular_check_service_ready(): resp = await cellular.send_command('AT+CGATT?') if len(resp) == 2 and resp[0].startswith('+CGATT: '): @@ -182,6 +165,11 @@ async def cellular_check_service_ready(): async def restart_modem(): + async def reset(): + await cellular.send_command('ATZ', require_ok=True) + + await run_with_timeout(func=reset, timeout_sec=2) + await asyncio.sleep(0.5) bee_power_pin.value(0) time.sleep(0.5) bee_power_pin.value(1) @@ -245,7 +233,7 @@ async def start_modem(): creg = await run_with_timeout(func=cellular_wait_creg, timeout_sec=1) if creg.failure: - logger('AT+CREG timeout, network offline?', source='CELL', level=LogLevel.warning) + logger('AT+CREG timeout, no signal?', source='CELL', level=LogLevel.warning) elif not creg.result[0]: logger(f'Bad AT+CREG response: {creg.result[1]}', source='CELL', level=LogLevel.error) await restart_modem() @@ -265,8 +253,10 @@ async def start_modem_task(): logger('Initalizing modem', source='CELL') while True: try: + gc.collect() await start_modem() break except uTimeoutError: await asyncio.sleep(10) + gc.collect() # TODO: loop to reconnect modem diff --git a/src/lib/runtime.py b/src/lib/runtime.py index fa5c8ce..b67972d 100644 --- a/src/lib/runtime.py +++ b/src/lib/runtime.py @@ -26,3 +26,21 @@ def timeout(seconds): def reboot(): import machine machine.reset() + + +class RunWithtimeoutResult: + def __init__(self, failure: bool, result: any): + self.failure = failure + self.result = result + + +async def run_with_timeout(func, timeout_sec: int, *args, **kwargs) -> RunWithtimeoutResult: + @timeout(timeout_sec) + async def inner(): + return await func(*args, **kwargs) + + try: + result = await inner() + return RunWithtimeoutResult(False, result) + except uTimeoutError: + return RunWithtimeoutResult(True, None)