adjust some cell things

This commit is contained in:
Cyberes 2024-06-29 22:56:22 -06:00
parent 0593a52501
commit 6bc9f9c36c
2 changed files with 28 additions and 20 deletions

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import gc
import time import time
import machine import machine
@ -6,7 +7,7 @@ from machine import Pin
from config import CELLULAR_APN, CELLULAR_STARTUP_TIMEOUT from config import CELLULAR_APN, CELLULAR_STARTUP_TIMEOUT
from lib.logging import logger, LogLevel 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_POWER = 27
PIN_BEE_UART_RXD = 35 PIN_BEE_UART_RXD = 35
@ -155,24 +156,6 @@ async def cellular_ip():
return 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(): async def cellular_check_service_ready():
resp = await cellular.send_command('AT+CGATT?') resp = await cellular.send_command('AT+CGATT?')
if len(resp) == 2 and resp[0].startswith('+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 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) bee_power_pin.value(0)
time.sleep(0.5) time.sleep(0.5)
bee_power_pin.value(1) 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) creg = await run_with_timeout(func=cellular_wait_creg, timeout_sec=1)
if creg.failure: 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]: elif not creg.result[0]:
logger(f'Bad AT+CREG response: {creg.result[1]}', source='CELL', level=LogLevel.error) logger(f'Bad AT+CREG response: {creg.result[1]}', source='CELL', level=LogLevel.error)
await restart_modem() await restart_modem()
@ -265,8 +253,10 @@ async def start_modem_task():
logger('Initalizing modem', source='CELL') logger('Initalizing modem', source='CELL')
while True: while True:
try: try:
gc.collect()
await start_modem() await start_modem()
break break
except uTimeoutError: except uTimeoutError:
await asyncio.sleep(10) await asyncio.sleep(10)
gc.collect()
# TODO: loop to reconnect modem # TODO: loop to reconnect modem

View File

@ -26,3 +26,21 @@ def timeout(seconds):
def reboot(): def reboot():
import machine import machine
machine.reset() 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)