1
+ import time
2
+
1
3
from machine import UART , Pin
2
4
from network import PPP
3
- from micropython import const
4
- import time
5
5
6
+ from micropython import const
6
7
7
8
DEFAULT_PIN_RST = 35
8
9
DEFAULT_PIN_NETLIGHT = 34
18
19
19
20
20
21
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
23
24
24
25
25
26
class LTE ():
@@ -30,13 +31,13 @@ def __init__(self, apn, uart=None, reset_pin=None, netlight_pin=None, netlight_l
30
31
DEFAULT_UART_ID ,
31
32
tx = Pin (DEFAULT_PIN_TX , Pin .OUT ),
32
33
rx = Pin (DEFAULT_PIN_RX , Pin .OUT ))
33
-
34
+
34
35
# Set PPP timeouts and rxbuf
35
36
self ._uart .init (
36
37
timeout = DEFAULT_UART_TIMEOUT ,
37
38
timeout_char = DEFAULT_UART_TIMEOUT_CHAR ,
38
39
rxbuf = DEFAULT_UART_RXBUF )
39
-
40
+
40
41
if not skip_reset :
41
42
self ._reset .value (0 )
42
43
time .sleep (1.0 )
@@ -46,7 +47,7 @@ def __init__(self, apn, uart=None, reset_pin=None, netlight_pin=None, netlight_l
46
47
self ._led = netlight_led
47
48
self ._netlight = netlight_pin or Pin (DEFAULT_PIN_NETLIGHT , Pin .IN )
48
49
self ._netlight .irq (self ._netlight_irq )
49
-
50
+
50
51
def _netlight_irq (self , pin ):
51
52
self ._led .value (pin .value ())
52
53
@@ -60,23 +61,24 @@ def iccid(self):
60
61
try :
61
62
return self ._send_at_command ("AT+CICCID" , 1 )
62
63
except CellularError :
63
- return None
64
+ return None
64
65
65
66
def status (self ):
66
67
lte_status = self ._send_at_command ("AT+CEREG?" , 1 )
67
68
gsm_status = self ._send_at_command ("AT+CGREG?" , 1 )
68
69
return lte_status , gsm_status
69
-
70
+
70
71
def signal_quality (self ):
71
72
try :
72
73
response = self ._send_at_command ("AT+CSQ" , 1 )
73
74
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 )
75
77
return db
76
78
except CellularError :
77
79
pass
78
80
return None
79
-
81
+
80
82
def stop_ppp (self ):
81
83
self ._ppp .disconnect ()
82
84
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):
99
101
if connect :
100
102
self .connect ()
101
103
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
-
108
104
# Force PPP to use modem's default settings...
109
- #time.sleep(2.0)
110
105
self ._flush_uart ()
111
106
self ._uart .write ("ATD*99#\r " )
112
107
self ._uart .flush ()
113
- #time.sleep(2.0)
114
108
115
109
self ._ppp = PPP (self ._uart )
116
110
self ._ppp .connect ()
@@ -121,29 +115,29 @@ def start_ppp(self, baudrate=DEFAULT_UART_BAUD, connect=True):
121
115
122
116
def connect (self , timeout = 60 ):
123
117
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
126
120
self ._flush_uart ()
127
121
128
122
print (" - waiting for cellular module to be ready" )
129
123
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 ()
132
126
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
135
129
136
- # wait for roaming lte connection to be established
130
+ # Wait for roaming lte connection to be established
137
131
giveup = time .time () + timeout
138
132
status = None
139
133
while status != "+CEREG: 0,5" and status != "+CEREG: 0,1" :
140
134
status = self ._send_at_command ("AT+CEREG?" , 1 )
141
135
time .sleep (0.25 )
142
136
if time .time () > giveup :
143
- raise CellularError ("timed out getting network registration" )
137
+ raise CellularError ("timed out getting network registration" )
144
138
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" )
147
141
self ._send_at_command ("AT+CSSLCFG=\" enableSNI\" ,0,1" )
148
142
149
143
print (f" - SIM ICCID is { self .iccid ()} " )
@@ -153,12 +147,12 @@ def _wait_ready(self, poll_time=0.25, timeout=10):
153
147
while time .time () <= giveup :
154
148
try :
155
149
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!
157
151
except CellularError as e :
158
152
print (e )
159
153
time .sleep (poll_time )
160
154
161
- raise CellularError ("timed out waiting for AT response" )
155
+ raise CellularError ("timed out waiting for AT response" )
162
156
163
157
def _flush_uart (self ):
164
158
self ._uart .flush ()
@@ -168,28 +162,25 @@ def _flush_uart(self):
168
162
time .sleep (0.25 )
169
163
170
164
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
172
166
self ._flush_uart ()
173
167
174
168
self ._uart .write (command + "\r " )
175
- #print(f" - tx: {command}")
176
169
self ._uart .flush ()
177
170
status , data = self ._read_result (result_lines , timeout = timeout )
178
171
179
172
print (" -" , command , status , data )
180
173
181
174
if status == "TIMEOUT" :
182
- #print.error(" !", command, status, data)
183
175
raise CellularError (f"cellular module timed out for command { command } " )
184
176
185
177
if status not in ["OK" , "DOWNLOAD" ]:
186
- #print(" !", command, status, data)
187
178
raise CellularError (f"non 'OK' or 'DOWNLOAD' result for command { command } " )
188
179
189
180
if result_lines == 1 :
190
181
return data [0 ]
191
182
if result_lines > 1 :
192
- return data
183
+ return data
193
184
return None
194
185
195
186
def _read_result (self , result_lines , timeout = 1.0 ):
@@ -212,4 +203,3 @@ def _read_result(self, result_lines, timeout=1.0):
212
203
start = time .ticks_ms ()
213
204
214
205
return status , result
215
-
0 commit comments