-
Notifications
You must be signed in to change notification settings - Fork 22
/
SIM800L.py
429 lines (344 loc) · 16.4 KB
/
SIM800L.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# Imports
import time
import json
# Setup logging.
try:
import logging
logger = logging.getLogger(__name__)
except:
try:
import logger
except:
class Logger(object):
level = 'INFO'
@classmethod
def debug(cls, text):
if cls.level == 'DEBUG': print('DEBUG:', text)
@classmethod
def info(cls, text):
print('INFO:', text)
@classmethod
def warning(cls, text):
print('WARN:', text)
logger = Logger()
class GenericATError(Exception):
pass
class Response(object):
def __init__(self, status_code, content):
self.status_code = int(status_code)
self.content = content
class Modem(object):
def __init__(self, uart=None, MODEM_PWKEY_PIN=None, MODEM_RST_PIN=None, MODEM_POWER_ON_PIN=None, MODEM_TX_PIN=None, MODEM_RX_PIN=None):
# Pins
self.MODEM_PWKEY_PIN = MODEM_PWKEY_PIN
self.MODEM_RST_PIN = MODEM_RST_PIN
self.MODEM_POWER_ON_PIN = MODEM_POWER_ON_PIN
self.MODEM_TX_PIN = MODEM_TX_PIN
self.MODEM_RX_PIN = MODEM_RX_PIN
# Uart
self.uart = uart
self.initialized = False
self.modem_info = None
#----------------------
# Modem initializer
#----------------------
def initialize(self):
logger.debug('Initializing modem...')
if not self.uart:
from machine import UART, Pin
# Pin initialization
MODEM_PWKEY_PIN_OBJ = Pin(self.MODEM_PWKEY_PIN, Pin.OUT) if self.MODEM_PWKEY_PIN else None
MODEM_RST_PIN_OBJ = Pin(self.MODEM_RST_PIN, Pin.OUT) if self.MODEM_RST_PIN else None
MODEM_POWER_ON_PIN_OBJ = Pin(self.MODEM_POWER_ON_PIN, Pin.OUT) if self.MODEM_POWER_ON_PIN else None
#MODEM_TX_PIN_OBJ = Pin(self.MODEM_TX_PIN, Pin.OUT) # Not needed as we use MODEM_TX_PIN
#MODEM_RX_PIN_OBJ = Pin(self.MODEM_RX_PIN, Pin.IN) # Not needed as we use MODEM_RX_PIN
# Status setup
if MODEM_PWKEY_PIN_OBJ:
MODEM_PWKEY_PIN_OBJ.value(0)
if MODEM_RST_PIN_OBJ:
MODEM_RST_PIN_OBJ.value(1)
if MODEM_POWER_ON_PIN_OBJ:
MODEM_POWER_ON_PIN_OBJ.value(1)
# Setup UART
self.uart = UART(1, 9600, timeout=1000, rx=self.MODEM_TX_PIN, tx=self.MODEM_RX_PIN)
# Test AT commands
retries = 0
while True:
try:
self.modem_info = self.execute_at_command('modeminfo')
except:
retries+=1
if retries < 3:
logger.debug('Error in getting modem info, retrying.. (#{})'.format(retries))
time.sleep(3)
else:
raise
else:
break
logger.debug('Ok, modem "{}" is ready and accepting commands'.format(self.modem_info))
# Set initialized flag and support vars
self.initialized = True
# Check if SSL is supported
self.ssl_available = self.execute_at_command('checkssl') == '+CIPSSL: (0-1)'
#----------------------
# Execute AT commands
#----------------------
def execute_at_command(self, command, data=None, clean_output=True):
# Commands dictionary. Not the best approach ever, but works nicely.
commands = {
'modeminfo': {'string':'ATI', 'timeout':3, 'end': 'OK'},
'fwrevision': {'string':'AT+CGMR', 'timeout':3, 'end': 'OK'},
'battery': {'string':'AT+CBC', 'timeout':3, 'end': 'OK'},
'scan': {'string':'AT+COPS=?', 'timeout':60, 'end': 'OK'},
'network': {'string':'AT+COPS?', 'timeout':3, 'end': 'OK'},
'signal': {'string':'AT+CSQ', 'timeout':3, 'end': 'OK'},
'checkreg': {'string':'AT+CREG?', 'timeout':3, 'end': None},
'setapn': {'string':'AT+SAPBR=3,1,"APN","{}"'.format(data), 'timeout':3, 'end': 'OK'},
'setuser': {'string':'AT+SAPBR=3,1,"USER","{}"'.format(data), 'timeout':3, 'end': 'OK'},
'setpwd': {'string':'AT+SAPBR=3,1,"PWD","{}"'.format(data), 'timeout':3, 'end': 'OK'},
'initgprs': {'string':'AT+SAPBR=3,1,"Contype","GPRS"', 'timeout':3, 'end': 'OK'}, # Appeared on hologram net here or below
'opengprs': {'string':'AT+SAPBR=1,1', 'timeout':3, 'end': 'OK'},
'getbear': {'string':'AT+SAPBR=2,1', 'timeout':3, 'end': 'OK'},
'inithttp': {'string':'AT+HTTPINIT', 'timeout':3, 'end': 'OK'},
'sethttp': {'string':'AT+HTTPPARA="CID",1', 'timeout':3, 'end': 'OK'},
'checkssl': {'string':'AT+CIPSSL=?', 'timeout': 3, 'end': 'OK'},
'enablessl': {'string':'AT+HTTPSSL=1', 'timeout':3, 'end': 'OK'},
'disablessl': {'string':'AT+HTTPSSL=0', 'timeout':3, 'end': 'OK'},
'initurl': {'string':'AT+HTTPPARA="URL","{}"'.format(data), 'timeout':3, 'end': 'OK'},
'doget': {'string':'AT+HTTPACTION=0', 'timeout':3, 'end': '+HTTPACTION'},
'setcontent': {'string':'AT+HTTPPARA="CONTENT","{}"'.format(data), 'timeout':3, 'end': 'OK'},
'postlen': {'string':'AT+HTTPDATA={},5000'.format(data), 'timeout':3, 'end': 'DOWNLOAD'}, # "data" is data_lenght in this context, while 5000 is the timeout
'dumpdata': {'string':data, 'timeout':1, 'end': 'OK'},
'dopost': {'string':'AT+HTTPACTION=1', 'timeout':3, 'end': '+HTTPACTION'},
'getdata': {'string':'AT+HTTPREAD', 'timeout':3, 'end': 'OK'},
'closehttp': {'string':'AT+HTTPTERM', 'timeout':3, 'end': 'OK'},
'closebear': {'string':'AT+SAPBR=0,1', 'timeout':3, 'end': 'OK'}
}
# References:
# https://github.com/olablt/micropython-sim800/blob/4d181f0c5d678143801d191fdd8a60996211ef03/app_sim.py
# https://arduino.stackexchange.com/questions/23878/what-is-the-proper-way-to-send-data-through-http-using-sim908
# https://stackoverflow.com/questions/35781962/post-api-rest-with-at-commands-sim800
# https://arduino.stackexchange.com/questions/34901/http-post-request-in-json-format-using-sim900-module (full post example)
# Sanity checks
if command not in commands:
raise Exception('Unknown command "{}"'.format(command))
# Support vars
command_string = commands[command]['string']
excpected_end = commands[command]['end']
timeout = commands[command]['timeout']
processed_lines = 0
# Execute the AT command
command_string_for_at = "{}\r\n".format(command_string)
logger.debug('Writing AT command "{}"'.format(command_string_for_at.encode('utf-8')))
self.uart.write(command_string_for_at)
# Support vars
pre_end = True
output = ''
empty_reads = 0
while True:
line = self.uart.readline()
if not line:
time.sleep(1)
empty_reads += 1
if empty_reads > timeout:
raise Exception('Timeout for command "{}" (timeout={})'.format(command, timeout))
#logger.warning('Timeout for command "{}" (timeout={})'.format(command, timeout))
#break
else:
logger.debug('Read "{}"'.format(line))
# Convert line to string
line_str = line.decode('utf-8')
# Do we have an error?
if line_str == 'ERROR\r\n':
raise GenericATError('Got generic AT error')
# If we had a pre-end, do we have the expected end?
if line_str == '{}\r\n'.format(excpected_end):
logger.debug('Detected exact end')
break
if pre_end and line_str.startswith('{}'.format(excpected_end)):
logger.debug('Detected startwith end (and adding this line to the output too)')
output += line_str
break
# Do we have a pre-end?
if line_str == '\r\n':
pre_end = True
logger.debug('Detected pre-end')
else:
pre_end = False
# Keep track of processed lines and stop if exceeded
processed_lines+=1
# Save this line unless in particular conditions
if command == 'getdata' and line_str.startswith('+HTTPREAD:'):
pass
else:
output += line_str
# Remove the command string from the output
output = output.replace(command_string+'\r\r\n', '')
# ..and remove the last \r\n added by the AT protocol
if output.endswith('\r\n'):
output = output[:-2]
# Also, clean output if needed
if clean_output:
output = output.replace('\r', '')
output = output.replace('\n\n', '')
if output.startswith('\n'):
output = output[1:]
if output.endswith('\n'):
output = output[:-1]
logger.debug('Returning "{}"'.format(output.encode('utf8')))
# Return
return output
#----------------------
# Function commands
#----------------------
def get_info(self):
output = self.execute_at_command('modeminfo')
return output
def battery_status(self):
output = self.execute_at_command('battery')
return output
def scan_networks(self):
networks = []
output = self.execute_at_command('scan')
pieces = output.split('(', 1)[1].split(')')
for piece in pieces:
piece = piece.replace(',(','')
subpieces = piece.split(',')
if len(subpieces) != 4:
continue
networks.append({'name': json.loads(subpieces[1]), 'shortname': json.loads(subpieces[2]), 'id': json.loads(subpieces[3])})
return networks
def get_current_network(self):
output = self.execute_at_command('network')
network = output.split(',')[-1]
if network.startswith('"'):
network = network[1:]
if network.endswith('"'):
network = network[:-1]
# If after filtering we did not filter anything: there was no network
if network.startswith('+COPS'):
return None
return network
def get_signal_strength(self):
# See more at https://m2msupport.net/m2msupport/atcsq-signal-quality/
output = self.execute_at_command('signal')
signal = int(output.split(':')[1].split(',')[0])
signal_ratio = float(signal)/float(30) # 30 is the maximum value (2 is the minimum)
return signal_ratio
def get_ip_addr(self):
output = self.execute_at_command('getbear')
output = output.split('+')[-1] # Remove potential leftovers in the buffer before the "+SAPBR:" response
pieces = output.split(',')
if len(pieces) != 3:
raise Exception('Cannot parse "{}" to get an IP address'.format(output))
ip_addr = pieces[2].replace('"','')
if len(ip_addr.split('.')) != 4:
raise Exception('Cannot parse "{}" to get an IP address'.format(output))
if ip_addr == '0.0.0.0':
return None
return ip_addr
def connect(self, apn, user='', pwd=''):
if not self.initialized:
raise Exception('Modem is not initialized, cannot connect')
# Are we already connected?
if self.get_ip_addr():
logger.debug('Modem is already connected, not reconnecting.')
return
# Closing bearer if left opened from a previous connect gone wrong:
logger.debug('Trying to close the bearer in case it was left open somehow..')
try:
self.execute_at_command('closebear')
except GenericATError:
pass
# First, init gprs
logger.debug('Connect step #1 (initgprs)')
self.execute_at_command('initgprs')
# Second, set the APN
logger.debug('Connect step #2 (setapn)')
self.execute_at_command('setapn', apn)
self.execute_at_command('setuser', user)
self.execute_at_command('setpwd', pwd)
# Then, open the GPRS connection.
logger.debug('Connect step #3 (opengprs)')
self.execute_at_command('opengprs')
# Ok, now wait until we get a valid IP address
retries = 0
max_retries = 5
while True:
retries += 1
ip_addr = self.get_ip_addr()
if not ip_addr:
retries += 1
if retries > max_retries:
raise Exception('Cannot connect modem as could not get a valid IP address')
logger.debug('No valid IP address yet, retrying... (#')
time.sleep(1)
else:
break
def disconnect(self):
# Close bearer
try:
self.execute_at_command('closebear')
except GenericATError:
pass
# Check that we are actually disconnected
ip_addr = self.get_ip_addr()
if ip_addr:
raise Exception('Error, we should be disconnected but we still have an IP address ({})'.format(ip_addr))
def http_request(self, url, mode='GET', data=None, content_type='application/json'):
# Protocol check.
assert url.startswith('http'), 'Unable to handle communication protocol for URL "{}"'.format(url)
# Are we connected?
if not self.get_ip_addr():
raise Exception('Error, modem is not connected')
# Close the http context if left open somehow
logger.debug('Close the http context if left open somehow...')
try:
self.execute_at_command('closehttp')
except GenericATError:
pass
# First, init and set http
logger.debug('Http request step #1.1 (inithttp)')
self.execute_at_command('inithttp')
logger.debug('Http request step #1.2 (sethttp)')
self.execute_at_command('sethttp')
# Do we have to enable ssl as well?
if self.ssl_available:
if url.startswith('https://'):
logger.debug('Http request step #1.3 (enablessl)')
self.execute_at_command('enablessl')
elif url.startswith('http://'):
logger.debug('Http request step #1.3 (disablessl)')
self.execute_at_command('disablessl')
else:
if url.startswith('https://'):
raise NotImplementedError("SSL is only supported by firmware revisions >= R14.00")
# Second, init and execute the request
logger.debug('Http request step #2.1 (initurl)')
self.execute_at_command('initurl', data=url)
if mode == 'GET':
logger.debug('Http request step #2.2 (doget)')
output = self.execute_at_command('doget')
response_status_code = output.split(',')[1]
logger.debug('Response status code: "{}"'.format(response_status_code))
elif mode == 'POST':
logger.debug('Http request step #2.2 (setcontent)')
self.execute_at_command('setcontent', content_type)
logger.debug('Http request step #2.3 (postlen)')
self.execute_at_command('postlen', len(data))
logger.debug('Http request step #2.4 (dumpdata)')
self.execute_at_command('dumpdata', data)
logger.debug('Http request step #2.5 (dopost)')
output = self.execute_at_command('dopost')
response_status_code = output.split(',')[1]
logger.debug('Response status code: "{}"'.format(response_status_code))
else:
raise Exception('Unknown mode "{}'.format(mode))
# Third, get data
logger.debug('Http request step #4 (getdata)')
response_content = self.execute_at_command('getdata', clean_output=False)
logger.debug(response_content)
# Then, close the http context
logger.debug('Http request step #4 (closehttp)')
self.execute_at_command('closehttp')
return Response(status_code=response_status_code, content=response_content)