PPP: Add send_text_message for #14.

Also a quick cleanup of the lte module code.
This commit is contained in:
Phil Howard 2024-10-04 15:41:00 +01:00
parent 6c6a839903
commit bfcb181cdf

View File

@ -19,7 +19,7 @@ DEFAULT_UART_BAUD = const(460800)
class CellularError(Exception):
def __init__(self, message=None):
self.message = "CellularError: " + message
self.message = f"CellularError: {message}"
class LTE():
@ -106,11 +106,9 @@ class LTE():
# print(e)
# Force PPP to use modem's default settings...
#time.sleep(2.0)
self._flush_uart()
self._uart.write("ATD*99#\r")
self._uart.flush()
#time.sleep(2.0)
self._ppp = PPP(self._uart)
self._ppp.connect()
@ -152,8 +150,9 @@ class LTE():
giveup = time.time() + timeout
while time.time() <= giveup:
try:
# if __send_at_command doesn't throw an exception then we're good!
self._send_at_command("AT")
return # if __send_at_command doesn't throw an exception then we're good!
return
except CellularError as e:
print(e)
time.sleep(poll_time)
@ -172,18 +171,12 @@ class LTE():
self._flush_uart()
self._uart.write(command + "\r")
#print(f" - tx: {command}")
self._uart.flush()
status, data = self._read_result(result_lines, timeout=timeout)
print(" -", command, status, data)
if status == "TIMEOUT":
#print.error(" !", command, status, data)
raise CellularError(f"cellular module timed out for command {command}")
if status not in ["OK", "DOWNLOAD"]:
#print(" !", command, status, data)
raise CellularError(f"non 'OK' or 'DOWNLOAD' result for command {command}")
if result_lines == 1:
@ -199,7 +192,7 @@ class LTE():
timeout *= 1000
while len(result) < result_lines or status is None:
if (time.ticks_ms() - start) > timeout:
return "TIMEOUT", []
raise CellularError("cellular module timed out")
line = self._uart.readline()
@ -213,3 +206,15 @@ class LTE():
return status, result
def send_text_message(self, recipient, body, timeout=5.0):
self._uart.write("AT+CMGS=\"")
self._uart.write(recipient.encode("ascii"))
self._uart.write(b"\"\r")
time.sleep(0.5)
self._uart.write(body.encode("ascii"))
self._uart.write(b"\x1a\r")
self._uart.flush()
status, _ = self._read_result(1, timeout=timeout)
return status == "OK"