Skip to content

Commit ef936ba

Browse files
committed
RPI_PICO_PPP: Lint lte module.
1 parent a1be7a6 commit ef936ba

File tree

1 file changed

+27
-37
lines changed

1 file changed

+27
-37
lines changed

micropython/modules_py/lte.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import time
2+
13
from machine import UART, Pin
24
from network import PPP
3-
from micropython import const
4-
import time
55

6+
from micropython import const
67

78
DEFAULT_PIN_RST = 35
89
DEFAULT_PIN_NETLIGHT = 34
@@ -18,8 +19,8 @@
1819

1920

2021
class CellularError(Exception):
21-
def __init__(self, message=None):
22-
self.message = "CellularError: " + message
22+
def __init__(self, message=None):
23+
self.message = "CellularError: " + message
2324

2425

2526
class LTE():
@@ -30,13 +31,13 @@ def __init__(self, apn, uart=None, reset_pin=None, netlight_pin=None, netlight_l
3031
DEFAULT_UART_ID,
3132
tx=Pin(DEFAULT_PIN_TX, Pin.OUT),
3233
rx=Pin(DEFAULT_PIN_RX, Pin.OUT))
33-
34+
3435
# Set PPP timeouts and rxbuf
3536
self._uart.init(
3637
timeout=DEFAULT_UART_TIMEOUT,
3738
timeout_char=DEFAULT_UART_TIMEOUT_CHAR,
3839
rxbuf=DEFAULT_UART_RXBUF)
39-
40+
4041
if not skip_reset:
4142
self._reset.value(0)
4243
time.sleep(1.0)
@@ -46,7 +47,7 @@ def __init__(self, apn, uart=None, reset_pin=None, netlight_pin=None, netlight_l
4647
self._led = netlight_led
4748
self._netlight = netlight_pin or Pin(DEFAULT_PIN_NETLIGHT, Pin.IN)
4849
self._netlight.irq(self._netlight_irq)
49-
50+
5051
def _netlight_irq(self, pin):
5152
self._led.value(pin.value())
5253

@@ -60,23 +61,24 @@ def iccid(self):
6061
try:
6162
return self._send_at_command("AT+CICCID", 1)
6263
except CellularError:
63-
return None
64+
return None
6465

6566
def status(self):
6667
lte_status = self._send_at_command("AT+CEREG?", 1)
6768
gsm_status = self._send_at_command("AT+CGREG?", 1)
6869
return lte_status, gsm_status
69-
70+
7071
def signal_quality(self):
7172
try:
7273
response = self._send_at_command("AT+CSQ", 1)
7374
quality = int(response.split(":")[1].split(",")[0])
74-
db = -113 + (2 * quality) # conversion as per AT command set datasheet
75+
# Conversion as per AT command set datasheet
76+
db = -113 + (2 * quality)
7577
return db
7678
except CellularError:
7779
pass
7880
return None
79-
81+
8082
def stop_ppp(self):
8183
self._ppp.disconnect()
8284
self._send_at_command(f"AT+IPR={DEFAULT_UART_STARTUP_BAUD}")
@@ -99,18 +101,10 @@ def start_ppp(self, baudrate=DEFAULT_UART_BAUD, connect=True):
99101
if connect:
100102
self.connect()
101103

102-
# This will just always time out!?
103-
# try:
104-
# self._send_at_command("ATD*99#", timeout=300)
105-
# except CellularError as e:
106-
# print(e)
107-
108104
# Force PPP to use modem's default settings...
109-
#time.sleep(2.0)
110105
self._flush_uart()
111106
self._uart.write("ATD*99#\r")
112107
self._uart.flush()
113-
#time.sleep(2.0)
114108

115109
self._ppp = PPP(self._uart)
116110
self._ppp.connect()
@@ -121,29 +115,29 @@ def start_ppp(self, baudrate=DEFAULT_UART_BAUD, connect=True):
121115

122116
def connect(self, timeout=60):
123117
print(" - setting up cellular uart")
124-
# connect to and flush the uart
125-
# consume any unsolicited messages first, we don't need those
118+
# Connect to and flush the uart
119+
# Discard any unsolicited messages first, we don't need those
126120
self._flush_uart()
127121

128122
print(" - waiting for cellular module to be ready")
129123

130-
# wait for the cellular module to respond to AT commands
131-
self._wait_ready()
124+
# Wait for the cellular module to respond to AT commands
125+
self._wait_ready()
132126

133-
self._send_at_command("ATE0") # disable local echo
134-
self._send_at_command(f"AT+CGDCONT=1,\"IP\",\"{self._apn}\"") # set apn and activate pdp context
127+
self._send_at_command("ATE0") # Disable local echo
128+
self._send_at_command(f"AT+CGDCONT=1,\"IP\",\"{self._apn}\"") # Set apn and activate pdp context
135129

136-
# wait for roaming lte connection to be established
130+
# Wait for roaming lte connection to be established
137131
giveup = time.time() + timeout
138132
status = None
139133
while status != "+CEREG: 0,5" and status != "+CEREG: 0,1":
140134
status = self._send_at_command("AT+CEREG?", 1)
141135
time.sleep(0.25)
142136
if time.time() > giveup:
143-
raise CellularError("timed out getting network registration")
137+
raise CellularError("timed out getting network registration")
144138

145-
# disable server and client certification validation
146-
self._send_at_command("AT+CSSLCFG=\"authmode\",0,0")
139+
# Disable server and client certification validation
140+
self._send_at_command("AT+CSSLCFG=\"authmode\",0,0")
147141
self._send_at_command("AT+CSSLCFG=\"enableSNI\",0,1")
148142

149143
print(f" - SIM ICCID is {self.iccid()}")
@@ -153,12 +147,12 @@ def _wait_ready(self, poll_time=0.25, timeout=10):
153147
while time.time() <= giveup:
154148
try:
155149
self._send_at_command("AT")
156-
return # if __send_at_command doesn't throw an exception then we're good!
150+
return # If __send_at_command doesn't throw an exception then we're good!
157151
except CellularError as e:
158152
print(e)
159153
time.sleep(poll_time)
160154

161-
raise CellularError("timed out waiting for AT response")
155+
raise CellularError("timed out waiting for AT response")
162156

163157
def _flush_uart(self):
164158
self._uart.flush()
@@ -168,28 +162,25 @@ def _flush_uart(self):
168162
time.sleep(0.25)
169163

170164
def _send_at_command(self, command, result_lines=0, timeout=5.0):
171-
# consume any unsolicited messages first, we don't need those
165+
# Discard any unsolicited messages first, we don't need those
172166
self._flush_uart()
173167

174168
self._uart.write(command + "\r")
175-
#print(f" - tx: {command}")
176169
self._uart.flush()
177170
status, data = self._read_result(result_lines, timeout=timeout)
178171

179172
print(" -", command, status, data)
180173

181174
if status == "TIMEOUT":
182-
#print.error(" !", command, status, data)
183175
raise CellularError(f"cellular module timed out for command {command}")
184176

185177
if status not in ["OK", "DOWNLOAD"]:
186-
#print(" !", command, status, data)
187178
raise CellularError(f"non 'OK' or 'DOWNLOAD' result for command {command}")
188179

189180
if result_lines == 1:
190181
return data[0]
191182
if result_lines > 1:
192-
return data
183+
return data
193184
return None
194185

195186
def _read_result(self, result_lines, timeout=1.0):
@@ -212,4 +203,3 @@ def _read_result(self, result_lines, timeout=1.0):
212203
start = time.ticks_ms()
213204

214205
return status, result
215-

0 commit comments

Comments
 (0)