Read incoming sms

How can I read the incoming SMS from the AutoPi’s Sim?

In the terminal, nothing happens with “ec2x.list_sms” or the local terminal with “autopi ec2x.list_sms” command.

It would be very interesting to be able to read them from the dashboard …

Thanks!

1 Like

It would have been nice to be able to send SMS too, then you could check the amount of data on prepaid cards.

/Mats

2 Likes

Hi @atlantisworld and @el93mon

Great ideas. At the moment we dont have a graphical interface for reading and sending SMS, but its actually already possible.

From the terminal you should be able to query any command to the modem. This is done with:

ec2x.query

This is actually what the ec2x.list_sms does, if you look in the code:

ec2x.query 'AT+CMGL="all"'

The command should return a list of messages, each with a specific index. To read the index you want you can use this command:

ec2x.query 'AT+CMGR=<index>"'

Replace <index> with a number.

So basically any command supported by the modem, can be send directly from the terminal.

Your device need to be online to do so.

best
Peter

I also see ec2x.list_sms not returning any sms’s sent -

$ ec2x.list_sms
'ERROR: {''reason'': None, ''type'': None}'

$ ec2x.query 'AT+CMGL="all"'

'ERROR: {''reason'': None, ''type'': None}'

I’m testing this by sending an sms from my phone with delivery report enabled. If the autopi is off, I don’t get the delivery report but with it on the delivery report is delivered pretty quickly … so looks like the message is getting to the autopi.

If I put the sim into a spare phone I don’t see the old messages but new messages come through.

I’m wondering if the autopi is reading / deleting all messages ?

Hi Peter

Can you try to run the following command?

ec2x.sms_format_config value=1

Afterwards, you should be able to retrieve the text messages from the modem.

We have a startup task that executes the above command that enables us to retrieve the messages, but the value is not remembered when the modem is powered off, so it needs to be executed each time it starts.
But… It is currently disabled because it fails if there is no sim card present, a fix is on our roadmap, but in the meantime, you can run the command manually, or maybe set up a job that does it on startup.

How exactly the text messages work, like where they are stored, I’m not entirely sure, but I think it’s on the modem, so that’s probably why you don’t see the previous messages when you move the SIM card to a spare phone.

Best regards
/Malte

That command also gives me an error -

peter26@Kona $ ec2x.sms_format_config value=1
'ERROR: {''reason'': None, ''type'': None}'

I’ll see what I can dig out.

It looks like I’ve got some bigger issues with 4G. Logs include -

Apr 15 17:57:46 raspberrypi qmi-manager[3578]: [ERROR] QMI network not connected
Apr 15 17:57:46 raspberrypi qmi-manager[3578]: [WARN] Connection is offline
Apr 15 17:57:46 raspberrypi qmi-manager[3578]: Loading profile at /etc/qmi-network.conf...
Apr 15 17:57:46 raspberrypi qmi-manager[3578]:     APN: three.co.uk
Apr 15 17:57:46 raspberrypi qmi-manager[3578]:     APN user: unset
Apr 15 17:57:46 raspberrypi qmi-manager[3578]:     APN password: unset
Apr 15 17:57:46 raspberrypi qmi-manager[3578]:     IP type: 4
Apr 15 17:57:46 raspberrypi qmi-manager[3578]:     qmi-proxy: no
Apr 15 17:57:46 raspberrypi qmi-manager[3578]: Network already stopped
Apr 15 17:57:46 raspberrypi qmi-manager[3578]: Clearing state at /tmp/qmi-network-state-cdc-wdm0...
Apr 15 17:57:46 raspberrypi qmi-manager[3578]: [/dev/cdc-wdm0] Operating mode set successfully
Apr 15 17:57:46 raspberrypi qmi-manager[3578]: [/dev/cdc-wdm0] Device model retrieved:
Apr 15 17:57:46 raspberrypi qmi-manager[3578]: #011Model: 'QUECTEL Mobile Broadband Module'
Apr 15 17:57:47 raspberrypi qmi-manager[3578]: [WARN] Command failed with exit code 1: qmicli --device-open-qmi --device /dev/cdc-wdm0 --uim-get-card-status | grep -q "Card state: 'present'"
Apr 15 17:57:49 raspberrypi qmi-manager[3578]: [WARN] Command failed with exit code 1: qmicli --device-open-qmi --device /dev/cdc-wdm0 --uim-get-card-status | grep -q "Card state: 'present'"
Apr 15 17:57:51 raspberrypi qmi-manager[3578]: [WARN] Command failed with exit code 1: qmicli --device-open-qmi --device /dev/cdc-wdm0 --uim-get-card-status | grep -q "Card state: 'present'"
Apr 15 17:57:53 raspberrypi qmi-manager[3578]: [WARN] Command failed with exit code 1: qmicli --device-open-qmi --device /dev/cdc-wdm0 --uim-get-card-status | grep -q "Card state: 'present'"
Apr 15 17:57:53 raspberrypi qmi-manager[3578]: [ERROR] No SIM card present

I’ve verified the SIM is insert the right way and it clicks, contacts are clean and I’ve also tried a piece of paper to put the SIM on the contacts.

This used to work ( in early testing ).

I did noticed that qmi-manager was updated on 1st March -

root@autopi-41ab498c8cb8:/var/log# ls -l /usr/bin/qmi-manager
-rwxr-xr-x 1 root root 13130 Mar  1 21:17 /usr/bin/qmi-manager

I’m not sure if this is right or not.

Anything else I can try to diagnose the situation ?

Thanks.

Yea !

I took the autopi appart, re-seated and cleaned and now looking much better :slight_smile:

peter26@Kona $ ec2x.sms_format_config value=1
_stamp: '2019-04-16T19:46:19.607093'
peter26@Kona $ ec2x.list_sms
_stamp: '2019-04-16T19:46:29.666051'
data:
  - '+CMGL: 0,"REC READ","+447867970260",,"18/10/12,18:51:24+04"'
  - '00540065007300740032'
....
2 Likes

How do i delete SMS, i now have 255 and its probably why the AutoPi stopped responding/wake up anymore?

/Mats

Guessing, to delete all -

ec2x.query 'AT+CMGD=1,4'

See http://www.developershome.com/sms/cmgdCommand.asp

1 Like

Thanks for nice support Peter, i now have delete the messages and now i can wake up the device via SMS again. :smile:

255 messages is the limit for incoming SMS on the AutoPi

/Mats

1 Like

Here is my custom code to read SMS’s on autopi, forward to telegram and delete once forwarded. My main reason for doing this is to get any “out of credit” messages from the operator.

import logging
import requests
import os
import re

log = logging.getLogger(__name__)

# Telegram tokens - see https://www.mariansauter.de/2018/01/send-telegram-notifications-to-your-mobile-from-python-opensesame/
#
bot_token = 'xxx'
bot_chatID = 'xxx'

def forward():
    """
    Poll for sms and forward to telegram

    Once sent, delete from autopi
    """

    __salt__['ec2x.query']('AT+CMGF=1')

    args = ['AT+CMGL="ALL"']
    res = __salt__['ec2x.query']('AT+CMGL="ALL"')
    if 'data' in res:
        allmessages = parse(res['data'])
        for message in allmessages:
            log.info ("SMS: "+message['message'])
            bot_sendtext('SMS From: '+message['from']+'\nDate: '+message['date']+'\n\n'+message['message'])
            __salt__['ec2x.query']('AT+CMGD='+message['index'])
            
    return {"msg": "Forward finished"}

# parse output to an array of dictionary
#
def parse(x):
    res = []
    i = 0
    while i < len(x):
        m = re.match('\+CMGL: (\d+),\"(.+)\",\"(.+)\",(.*),\"(.+)\"', x[i])
        if m:
            res.append({'index':m.group(1),'from':m.group(3),'date':m.group(5),'message':x[i+1]})
        else:
            log.info ("SMS: No match "+x[i])
        i += 2
        
    return res

# send message to telegram
#
def bot_sendtext(bot_message):
	send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
	requests.get(send_text)

5 Likes

Had to do the same to mine on second day of ownership.
That retaining system and socket don’t provide a high enough contact force, it would seem.

Wonder if it will cause issues long-term due to vibration…

Added to github here - https://github.com/plord12/autopi-tools#user-content-forward-sms

2 Likes