Skip to content
Merged
Show file tree
Hide file tree
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
79 changes: 19 additions & 60 deletions SIM800L.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
import time
import json

# Logger
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()
# Setup logging.
try:
import logging
logger = logging.getLogger(__name__)

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):
Expand Down Expand Up @@ -397,50 +403,3 @@ def http_request(self, url, mode='GET', data=None, content_type='application/jso
self.execute_at_command('closehttp')

return Response(status_code=response_status_code, content=response_content)



#-----------------------
# Example usage
#----------------------

def example_usage():
print('Starting up...')

# Create new modem object on the right Pins
modem = Modem(MODEM_PWKEY_PIN = 4,
MODEM_RST_PIN = 5,
MODEM_POWER_ON_PIN = 23,
MODEM_TX_PIN = 26,
MODEM_RX_PIN = 27)

# Initialize the modem
modem.initialize()

# Run some optional diagnostics
#print('Modem info: "{}"'.format(modem.get_info()))
#print('Network scan: "{}"'.format(modem.scan_networks()))
#print('Current network: "{}"'.format(modem.get_current_network()))
#print('Signal strength: "{}%"'.format(modem.get_signal_strength()*100))

# Connect the modem
modem.connect(apn='web.omnitel.it')
print('\nModem IP address: "{}"'.format(modem.get_ip_addr()))

# Example GET
print('\nNow running demo http GET...')
url = 'http://checkip.dyn.com/'
response = modem.http_request(url, 'GET')
print('Response status code:', response.status_code)
print('Response content:', response.content)

# Example POST
print('Now running demo https POST...')
url = 'https://postman-echo.com/post'
data = json.dumps({'myparameter': 42})
response = modem.http_request(url, 'POST', data, 'application/json')
print('Response status code:', response.status_code)
print('Response content:', response.content)

# Disconnect Modem
modem.disconnect()
46 changes: 46 additions & 0 deletions example_SIM800L.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ---------------------
# SIM800L example usage
# ---------------------
from SIM800L import Modem


def example_usage():
print('Starting up...')

# Create new modem object on the right Pins
modem = Modem(MODEM_PWKEY_PIN = 4,
MODEM_RST_PIN = 5,
MODEM_POWER_ON_PIN = 23,
MODEM_TX_PIN = 26,
MODEM_RX_PIN = 27)

# Initialize the modem
modem.initialize()

# Run some optional diagnostics
#print('Modem info: "{}"'.format(modem.get_info()))
#print('Network scan: "{}"'.format(modem.scan_networks()))
#print('Current network: "{}"'.format(modem.get_current_network()))
#print('Signal strength: "{}%"'.format(modem.get_signal_strength()*100))

# Connect the modem
modem.connect(apn='web.omnitel.it')
print('\nModem IP address: "{}"'.format(modem.get_ip_addr()))

# Example GET
print('\nNow running demo http GET...')
url = 'http://checkip.dyn.com/'
response = modem.http_request(url, 'GET')
print('Response status code:', response.status_code)
print('Response content:', response.content)

# Example POST
print('Now running demo https POST...')
url = 'https://postman-echo.com/post'
data = json.dumps({'myparameter': 42})
response = modem.http_request(url, 'POST', data, 'application/json')
print('Response status code:', response.status_code)
print('Response content:', response.content)

# Disconnect Modem
modem.disconnect()