From 8869e674c54ec7f7f2e710c01b78f910f58a99f8 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Tue, 18 Feb 2020 23:27:35 +0100 Subject: [PATCH 1/2] Put SIM800L usage example into a separate file In order to lower the memory footprint of this module, let's deliberately move the usage example code into a separate file. --- SIM800L.py | 47 ---------------------------------------------- example_SIM800L.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 47 deletions(-) create mode 100644 example_SIM800L.py diff --git a/SIM800L.py b/SIM800L.py index a0fdad3..0d8a392 100644 --- a/SIM800L.py +++ b/SIM800L.py @@ -397,50 +397,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() diff --git a/example_SIM800L.py b/example_SIM800L.py new file mode 100644 index 0000000..e71e0c1 --- /dev/null +++ b/example_SIM800L.py @@ -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() From 7ee6256e78aabc85d645479185c650a331708088 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Tue, 18 Feb 2020 23:29:20 +0100 Subject: [PATCH 2/2] Prefer vanilla logging before ramping up a custom logger Before defining a custom logger class which always will print to STDOUT/UART, let's try to use Python's native logging module. --- SIM800L.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/SIM800L.py b/SIM800L.py index 0d8a392..a4f54d9 100644 --- a/SIM800L.py +++ b/SIM800L.py @@ -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):