Message Boards Message Boards

1 Reply
11 Total Likes
View groups...
Share this post:

Controlling a GSM module with the Wolfram Language

In this post I show how to send and receive SMS messages using a GSM module connected to a Raspberry Pi computer. This enables the development of applications in the R-Pi which can be controlled remotely with a cell phone or any other device connected to a cellular network.

Material needed:

  1. The R-Pi computer (I used R-Pi model B+)

  2. SIM908 GSM module (see the attached notebook for a complete description of the module and how it is connected to the R-Pi)

enter image description here

The way in which the module works is by sending commands as text strings over the serial port. The module responds to these commands in a way which we can read by retrieving the buffer data of the serial port, as we illustrate below. The structure of the comand is "AT+some_instruction". The complete list of commands and their meanings can be found in the module manual. In this example we shall only explain the commands which we need.

The module is attached to the serial port of the R-Pi computer. We use DeviceOpen to be able to write and read data on this port:

serial = DeviceOpen["Serial", {"/dev/ttyAMA0", "BaudRate" -> 115200}]

Commands are sent by means of strings ended with the character \r is which is added to indicate the "carriage return" (if the commands were typeset with a computer keyboard then this is just the enter key). For example the simplest command is the string "AT \r" which we send with DeviceWrite.

DeviceWrite[serial, "AT\r"]

The answer of the module is stored in binary form in the device buffer and we use DeviceReadBuffer as shown:


The structure of this answer is similar for all commands: the module returns the command entered and, if the command was successfully executed, the module response followed by the value "OK". If the command cannot be executed (for example due to a syntax error) the module returns "ERROR" instead of "OK".

Next we enter the PIN number of the SIM card in the module. This is done as follows (replace **** by your own PIN number. Remember that entering the wrong PIN at least 3 times will block your SIM card. )

DeviceWrite[serial, "AT+CPIN=****\r"];

Sending a SMS

First of all we set the SMS mode to text

DeviceWrite[serial, "AT+CMGF=1\r"];

We insert the phone number the SMS is going to be sent to (the number used here is fictitious, you need to replace this number by your own cell phone destination).

DeviceWrite[serial, "AT+CMGS=\"11192522\"\r"];

The ">" which you see in the response is the prompt which confirms that we can start typing the SMS text. We send the SMS text to the serial port:

DeviceWrite[serial, "Sent from my RPi"];

Once finished with the typing we need to send the key combination Ctrl-Z to indicate it. Ctrl-Z corresponds to the hex code 0x1A which in base 10 is the number 26:

DeviceWrite[serial, FromCharacterCode[{26}]] ;

The module response should confirm that the SMS was sent. In my case this response reads +CMGS: 124 followed by OK. After some time the message will be received by the recipient's phone.

Reading a SMS from the SIM card

Again we choose the SMS tex mode:

DeviceWrite[serial, "AT+CMGF=1\r"];

Next we select the SIM card memory to read the messages in

DeviceWrite[serial, "AT+CPMS=\"SM\",\"SM\",\"SM\"\r"];

In my case the output is a list of the form +CPMS: 3,40,3,40,3,40. This means that there are 3 messages stored of a maximum storage capacity of 40 messages (see the module documentation to find out why 3 memories "SM" are reported). We read the third message (adapt this to the output you got in the previous step)

DeviceWrite[serial, "AT+CMGR=3\r"];

In addition to the SMS message text, the module returns the sender's phone number (in this example the number is fictitious), the sending date and time and the message status "REC UNREAD" which means that the message wasn't read before. We can import the SMS text with ImportString

ImportString[%, "Table"]

This is really cool! It has been ages since I've seen anyone using Hayes commands.

POSTED BY: Arnoud Buzing
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract