Sending & Recieving SMS with a 3G device in linux

26 08 2009

Hi all!

Got something quite interesting (or at least I thought so!) for you today: Sending and recieving SMS with a 3G USB dongle and I also assume this will work with a PCMCIA/Express card, if possibly in a slightly different manner. If I get the chance to try it out I’ll update this post with info for them too.

It’s actually pretty simple. The only prerequisites are that you have the correct driver for your 3G device loaded, ie you have access to your 3G dongle via /dev entries, and that you have the ppp package installed, as this contains the chat program. I’d imagine if you’re already successfully using the 3G dongle, then you’ve already got this installed, though.

When you plug your 3G device into a USB slot, use the command dmesg to find out which /dev/ttyUSB slots have been used for the device. This information can be seen on lines which will read something along the lines of:

usb 3-2: GSM modem (1-port) converter now attached to ttyUSB2

Then you need to work out which of the ports is the command port. You can do this by using the command cat /dev/tts/USBn, where n is the number of the port you want to check. On the command port, but not the other(s), you will see messages coming through on regular intervals. They will read something like:

^BOOT:28385139,0,0,0,72

You may as well leave the terminal open watching the USB device as you attempt communication, as you will be able to see the data scroll past as you pass it to the device this way.

Something important to at least know, if not understand regarding SMS itself, is the difference between PDU and text mode, as this article will not go into the depth of all the encoding issues you’ll come across trying to deal with SMS in PDU mode.

The messages you will send to the device will all be in the same format. They are just basically a modem chat script all filled out into one command line. They will follow the format:

chat TIMEOUT x "" "Info to send to device" "OK" > /dev/tts/USBn

The USB device is referred to as /dev/tts/USBn, as /dev/ttyUSBn is just a symlink to /dev/tts/USBn.

For the ins and outs of the chat command, check out the man page, as it’s not particularly relevant to this article’s sphere of interest. I’m concentrating more on the specific AT commands used to control the device, as that’s what’s important, not the method of communication. You could just as well use a terminal emulator like minicom to achieve exactly the same result.

The only difference may be replacing x with a timeout value, n with the USB device number and the info to send to the device. For this example, I will use a standard TIMEOUT of 1 and I will assume your USB device is USB2.

Sending SMS

First of all, the simplest option is to put the device into text mode. This is done with the following command:
chat TIMEOUT 1 "" "AT+CMGF=1" "OK" > /dev/tts/USB2
The terminal on which you are running cat should come back wtih the result:

AT+CMGF=1
OK

If it doesn’t, then either you are not watching the correct device, or your USB dongle’s firmware does not support SMS.

The next step is to tell the modem what number to send the text message to. Note the escaped quotes in the following command. When nesting similar quotes in such a manner, we must tell the terminal to treat the inner quotes as plain text to avoid the command trying to process them as part of the command line. Replace the xxxxxxxxxxx with the telephone number you wish to send the SMS to.
chat TIMEOUT 1 "" "AT+CMGS=\"xxxxxxxxxxx\"" "OK" > /dev/tts/USB2

On your second terminal, you should see the response:

AT+CMGS="xxxxxxxxxxx"
>

This is a prompt to enter the SMS’s text in plaintext format. The command is simply:

chat TIMEOUT 1 "" "Message text goes here" "OK" > /dev/tts/USB2

To insert a new line of text, just repeat this command.

To finalise the message, normally you would press . To emulate this, we use the ^ character. This will cause the terminal to treat it as if you have pressed the CTRL key followed by the next character. The command reads as follows:

chat TIMEOUT 1 "" "^Z" "OK" > /dev/tts/USB2

After a couple of seconds, your watching terminal should respond with:

+CMGS: n

n will be a number corresponding to the number of sent messages from the device.

You should now have the text message arrive at the destination telephone number.

Simples.

Receiving SMS

First step, as with sending SMS, is to put the device into text mode. This is exactly the same command used before:

chat TIMEOUT 1 "" "AT+CMGF=1" "OK" > /dev/tts/USB2

As an optional step, you can list all of the SMS stored on the SIM like so:

chat TIMEOUT 1 "" "AT+CMGL" "OK" > /dev/tts/USB2

The only real requirement for reading SMS, however, is to choose the index number of the SMS you wish to read. This is done with this command:

chat TIMEOUT 1 "" "AT+CMGR=n" "OK" > /dev/tts/USB2

In order to delete a SMS, should you so wish, it is done via a very similar command:

chat TIMEOUT 1 "" "AT+CMGD=n" "OK" > /dev/tts/USB2

That’s all I’m gonna talk you through, but there are countless resources online for finding out what some other AT commands are, and it’s quite interesting just firing them at the device to see what the response is, and I encourage you to have a mess about… Be careful when messing around with the SIM PIN codes, though, and it’s not my fault if you lock yourself out of your kit!

Hope I’ve been of use!

As always… Any questions/mistakes I’ve made, just leave a comment.

n00b

Advertisements

Actions

Information

6 responses

25 05 2011
liam Donaghy

Hi,

Really good article, works perfect, thanks for sharing 🙂

26 05 2011
n00bsys0p

Glad to be able to help, mate!

n00b

13 02 2012
thanhnv

this is a good article. i need it.

13 02 2012
n00bsys0p

Thanks, thanhnv!

If you liked this, please check out my new blog at http://n00bsys0p.co.uk, as this one has been discontinued.

n00b

12 02 2013
Andrej Rusinov

Good article, thanks a lot.

I my case I am reading SMS straight from Asterisk, thanks to you:
dongle cmd datacard2 AT+CMGL
dongle cmd datacard2 AT+CMGR=0

I don’t use SMS via Asterisk at all, so I never configured chan_dongle to manage them. But once in a while you may have to read incoming SMS.

12 02 2013
n00bsys0p

Hi Andrej,

Glad my article is still of use after all these years!

n00b

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: