Search code examples
pythongsmat-command

python-gsmmodem read old sms


I try to read SMS received while python-gsmmodem was not running but not able to make it work...

== MODEM INFORMATION ==

Manufacturer: huawei Model: E3531 Revision: 22.521.31.00.400

Here is my python code:

#!/usr/bin/env python

from __future__ import print_function
from datetime import datetime
from gsmmodem.modem import GsmModem, Sms

import logging

PORT = '/dev/ttyUSB0'
BAUDRATE = 115200
PIN = None  # SIM card PIN (if any)


def handleSms(sms):
    print(u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time, sms.text))


def main():
    print('Initializing modem...')
    # Uncomment the following line to see what the modem is doing:
    logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
    modem = GsmModem(PORT, BAUDRATE, smsReceivedCallbackFunc=handleSms)
    modem.smsTextMode = False
    modem.connect(PIN)
    #modem.processStoredSms(False)
    smss = modem.listStoredSms(status=Sms.STATUS_ALL, delete=True);
    print(str(len(smss)) + ' messages found')
    for i in range(len(smss)):
        sms = smss[i]
        handleSms(sms)
    print('Waiting for new SMS message...')    
    try:    
        modem.rxThread.join(2**31) # Specify a (huge) timeout so that it essentially blocks indefinitely, but still receives CTRL+C interrupt signal
    finally:
        modem.close();

if __name__ == '__main__':
    main()

Here are the logs :

Initializing modem...
INFO: Connecting to modem on port /dev/ttyUSB0 at 115200bps
DEBUG: write: ATZ
DEBUG: response: [u'OK']
DEBUG: write: ATE0
DEBUG: response: [u'OK']
DEBUG: write: AT+CFUN?
DEBUG: response: [u'+CFUN: 1', u'OK']
DEBUG: write: AT+CMEE=1
DEBUG: response: [u'OK']
DEBUG: write: AT+CPIN?
DEBUG: response: [u'+CPIN: READY', u'OK']
DEBUG: write: AT+CLAC
DEBUG: write: AT
DEBUG: response: [u'OK']
DEBUG: write: AT^CVOICE=?
DEBUG: response: [u'^CVOICE: (0)', u'OK']
DEBUG: write: AT+VTS=?
DEBUG: response: [u'+VTS: (0-9,A-D,*,#)', u'OK']
DEBUG: write: AT^DTMF=?
DEBUG: response: [u'ERROR']
DEBUG: write: AT^USSDMODE=?
DEBUG: response: [u'^USSDMODE: (0-1)', u'OK']
DEBUG: write: AT+WIND=?
DEBUG: response: [u'ERROR']
DEBUG: write: AT+ZPAS=?
DEBUG: response: [u'ERROR']
DEBUG: write: AT+CSCS=?
DEBUG: response: [u'+CSCS: ("IRA","UCS2","GSM")', u'OK']
DEBUG: write: AT+CNUM=?
DEBUG: response: [u'OK']
DEBUG: write: AT^CVOICE=0
DEBUG: response: [u'ERROR']
DEBUG: write: AT^USSDMODE=0
DEBUG: response: [u'OK']
DEBUG: write: AT+CGMI
DEBUG: response: [u'huawei', u'OK']
DEBUG: write: AT+CGMI
DEBUG: response: [u'huawei', u'OK']
INFO: Loading Huawei call state update table
DEBUG: write: AT+COPS=3,0
DEBUG: response: [u'OK']
DEBUG: write: AT+CMGF=0
DEBUG: response: [u'OK']
DEBUG: write: AT+CSCA?
DEBUG: response: [u'+CSCA: "002B00330033003600390035003000300030003600390035",145', u'OK']
DEBUG: write: AT+CSMP=49,167,0,0
DEBUG: response: [u'OK']
DEBUG: write: AT+CSCA?
DEBUG: response: [u'+CSCA: "002B00330033003600390035003000300030003600390035",145', u'OK']
DEBUG: write: AT+CPMS=?
DEBUG: response: [u'+CPMS: ("SM","ME"),("SM","ME"),("SM","ME")', u'OK']
DEBUG: write: AT+CPMS="ME","ME","ME"
DEBUG: response: [u'+CPMS: 0,20,0,20,0,20', u'OK']
DEBUG: write: AT+CNMI=2,1,0,2
DEBUG: response: [u'OK']
DEBUG: write: AT+CLIP=1
DEBUG: response: [u'OK']
DEBUG: write: AT+CRC=1
DEBUG: response: [u'OK']
DEBUG: write: AT+CVHU=0
DEBUG: response: [u'ERROR']
DEBUG: write: AT+CMGL=4
DEBUG: response: [u'OK']
0 messages found
Waiting for new SMS message...

I only receive message when python-gsmmodem is running and I'm not able to read messages that I received before... I also tried processStoredSms() but nothing appends.

Thanks


Solution

  • Self answer : I created this script that can read all long and short sms and also read old sms in memory.

    More info in the README : https://gist.github.com/stevecohenfr/8d3908da4ed39169992e407261f4c0e6