Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino, sim900 fixes #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 12 additions & 5 deletions gsmmodem/modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import sys, re, logging, weakref, time, threading, abc, codecs
from datetime import datetime
from time import sleep

from .serial_comms import SerialComms
from .exceptions import CommandError, InvalidStateException, CmeError, CmsError, InterruptedException, TimeoutException, PinRequiredError, IncorrectPinError, SmscNumberUnknownError
Expand Down Expand Up @@ -130,11 +131,12 @@ class GsmModem(SerialComms):
# Used for parsing SMS status reports
CDSI_REGEX = re.compile(r'\+CDSI:\s*"([^"]+)",(\d+)$')

def __init__(self, port, baudrate=115200, incomingCallCallbackFunc=None, smsReceivedCallbackFunc=None, smsStatusReportCallback=None):
def __init__(self, port, baudrate=115200, incomingCallCallbackFunc=None, smsReceivedCallbackFunc=None, smsStatusReportCallback=None, AT_CNMI=""):
super(GsmModem, self).__init__(port, baudrate, notifyCallbackFunc=self._handleModemNotification)
self.incomingCallCallback = incomingCallCallbackFunc or self._placeholderCallback
self.smsReceivedCallback = smsReceivedCallbackFunc or self._placeholderCallback
self.smsStatusReportCallback = smsStatusReportCallback or self._placeholderCallback
self.AT_CNMI = AT_CNMI or "2,1,0,2"
# Flag indicating whether caller ID for incoming call notification has been set up
self._callingLineIdentification = False
# Flag indicating whether incoming call notifications have extended information
Expand All @@ -161,7 +163,7 @@ def __init__(self, port, baudrate=115200, incomingCallCallbackFunc=None, smsRece
self._smsMemWrite = None # Preferred message storage memory for writes (<mem2> parameter used for +CPMS)
self._smsReadSupported = True # Whether or not reading SMS messages is supported via AT commands

def connect(self, pin=None):
def connect(self, pin=None, waitingForModemToStartInSeconds=0):
""" Opens the port and initializes the modem and SIM card

:param pin: The SIM card PIN code, if any
Expand All @@ -170,8 +172,13 @@ def connect(self, pin=None):
:raise PinRequiredError: if the SIM card requires a PIN but none was provided
:raise IncorrectPinError: if the specified PIN is incorrect
"""
self.log.info('Connecting to modem on port %s at %dbps', self.port, self.baudrate)

self.log.info('Connecting to modem on port %s at %dbps', self.port, self.baudrate)
super(GsmModem, self).connect()

if waitingForModemToStartInSeconds > 0:
sleep(waitingForModemToStartInSeconds)

# Send some initialization commands to the modem
try:
self.write('ATZ') # reset configuration
Expand Down Expand Up @@ -338,7 +345,7 @@ def connect(self, pin=None):

if self._smsReadSupported:
try:
self.write('AT+CNMI=2,1,0,2') # Set message notifications
self.write('AT+CNMI=' + self.AT_CNMI) # Set message notifications
except CommandError:
# Message notifications not supported
self._smsReadSupported = False
Expand Down Expand Up @@ -384,7 +391,7 @@ def _unlockSim(self, pin):
else:
raise PinRequiredError('AT+CPIN')

def write(self, data, waitForResponse=True, timeout=5, parseError=True, writeTerm='\r', expectedResponseTermSeq=None):
def write(self, data, waitForResponse=True, timeout=10, parseError=True, writeTerm='\r', expectedResponseTermSeq=None):
""" Write data to the modem.

This method adds the ``\\r\\n`` end-of-line sequence to the data parameter, and
Expand Down
6 changes: 4 additions & 2 deletions tools/identify-modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def parseArgs():
parser.add_argument('port', metavar='PORT', help='port to which the GSM modem is connected; a number or a device name.')
parser.add_argument('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate')
parser.add_argument('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN')
parser.add_argument('-d', '--debug', action='store_true', help='dump modem debug information (for python-gsmmodem development)')
parser.add_argument('-d', '--debug', action='store_true', help='dump modem debug information (for python-gsmmodem development)')
parser.add_argument('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds')
return parser.parse_args()

def parseArgsPy26():
Expand All @@ -32,6 +33,7 @@ def parseArgsPy26():
parser.add_option('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate')
parser.add_option('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN')
parser.add_option('-d', '--debug', action='store_true', help='dump modem debug information (for python-gsmmodem development)')
parser.add_option('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds')
options, args = parser.parse_args()
if len(args) != 1:
parser.error('Incorrect number of arguments - please specify a PORT to connect to, e.g. {0} /dev/ttyUSB0'.format(sys.argv[0]))
Expand All @@ -46,7 +48,7 @@ def main():

print('Connecting to GSM modem on {0}...'.format(args.port))
try:
modem.connect(args.pin)
modem.connect(args.pin, waitingForModemToStartInSeconds=args.wait)
except PinRequiredError:
sys.stderr.write('Error: SIM card PIN required. Please specify a PIN with the -p argument.\n')
sys.exit(1)
Expand Down
14 changes: 9 additions & 5 deletions tools/sendsms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ def parseArgs():
parser.add_argument('-i', '--port', metavar='PORT', help='port to which the GSM modem is connected; a number or a device name.')
parser.add_argument('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate')
parser.add_argument('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN')
parser.add_argument('-d', '--deliver', action='store_true', help='wait for SMS delivery report')
parser.add_argument('destination', metavar='DESTINATION', help='destination mobile number')
parser.add_argument('-d', '--deliver', action='store_true', help='wait for SMS delivery report')
parser.add_argument('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds')
parser.add_argument('--CNMI', default='', help='Set the CNMI of the modem, used for message notifications')
parser.add_argument('destination', metavar='DESTINATION', help='destination mobile number')
return parser.parse_args()

def parseArgsPy26():
Expand All @@ -30,7 +32,9 @@ def parseArgsPy26():
parser.add_option('-i', '--port', metavar='PORT', help='port to which the GSM modem is connected; a number or a device name.')
parser.add_option('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate')
parser.add_option('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN')
parser.add_option('-d', '--deliver', action='store_true', help='wait for SMS delivery report')
parser.add_option('-d', '--deliver', action='store_true', help='wait for SMS delivery report')
parser.add_option('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds')
parser.add_option('--CNMI', default='', help='Set the CNMI of the modem, used for message notifications')
parser.add_positional_argument(Option('--destination', metavar='DESTINATION', help='destination mobile number'))
options, args = parser.parse_args()
if len(args) != 1:
Expand All @@ -44,13 +48,13 @@ def main():
if args.port == None:
sys.stderr.write('Error: No port specified. Please specify the port to which the GSM modem is connected using the -i argument.\n')
sys.exit(1)
modem = GsmModem(args.port, args.baud)
modem = GsmModem(args.port, args.baud, AT_CNMI=args.CNMI)
# Uncomment the following line to see what the modem is doing:
#logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)

print('Connecting to GSM modem on {0}...'.format(args.port))
try:
modem.connect(args.pin)
modem.connect(args.pin, waitingForModemToStartInSeconds=args.wait)
except PinRequiredError:
sys.stderr.write('Error: SIM card PIN required. Please specify a PIN with the -p argument.\n')
sys.exit(1)
Expand Down