Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions SIM800L.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def __init__(self, MODEM_PWKEY_PIN, MODEM_RST_PIN, MODEM_POWER_ON_PIN, MODEM_TX_
self.MODEM_TX_PIN = MODEM_TX_PIN
self.MODEM_RX_PIN = MODEM_RX_PIN

self.initialized = False
self.modem_info = None


#----------------------
# Modem initializer
Expand Down Expand Up @@ -77,7 +80,7 @@ def initialize(self):
retries = 0
while True:
try:
modem_info = self.execute_at_command('modeminfo')
self.modem_info = self.execute_at_command('modeminfo')
except:
retries+=1
if retries < 3:
Expand All @@ -88,7 +91,7 @@ def initialize(self):
else:
break

logger.debug('Ok, modem "{}" is ready and accepting commands'.format(modem_info))
logger.debug('Ok, modem "{}" is ready and accepting commands'.format(self.modem_info))

# Set initialized flag and support vars
self.initialized = True
Expand All @@ -102,6 +105,7 @@ 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'},
Expand Down Expand Up @@ -334,6 +338,9 @@ def disconnect(self):

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')
Expand All @@ -351,15 +358,18 @@ def http_request(self, url, mode='GET', data=None, content_type='application/jso
logger.debug('Http request step #1.2 (sethttp)')
self.execute_at_command('sethttp')

# ..Do we have to enable ssl as well?
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')
# Do we have to enable ssl as well?
ssl_available = self.modem_info >= 'SIM800 R14.00'
if 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:
raise Exception('I have no idea how to handle communication protocol for URL "{}"'.format(url))
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)')
Expand Down