t r o n i x s t u f f

fun and learning with electronics

Tutorial: Arduino and GSM Cellular – Part Two

This is the revised, 2012 edition of chapter twenty-seven of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Welcome back fellow arduidans!

Assumed understanding for this article is found in part one. If you have not already done so, please read and understand it.

Today we are going to harness the awesome power of the telephone network to control an Arduino board via a SM5100B Cellular Shield:

Are you using an Arduino Mega board?

Mega users – things will be slightly different for you. Please make sure the TX and RX pins of your GSM shield DO NOT plug into your Mega. Furthermore, run a jumper wire from GSM shield pin D2 to Mega pin 19, and a jumper from GSM shield pin D3 to Mega pin 18, as shown below:

Finally, the example sketches will be different. Mega will not use the NewSoftSerial library, instead we use the Serial1 port. Please use the following Mega-equivalent sketches for this article: Example 27.1 and 27.2.

Reach out and control something

First we will discuss how to make something happen by a simple telephone call. And the best thing is that we don’t need the the GSM module to answer the telephone call (thereby saving money) – just let the module ring a few times. How is this possible? Very easily. Recall example 26.1 – we monitored the activity of the GSM module by using our terminal software. In this case what we need to do is have our Arduino examine the text coming in from the serial output of the GSM module, and look for a particular string of characters.

When we telephone the GSM module from another number, the module returns the text as shown in the image below (click to enlarge):

We want to look for the text “RING”, as (obviously) this means that the GSM shield has recognised the ring signal from the exchange. Therefore need our Arduino to count the number of rings for the particular telephone call being made to the module. (Memories – Many years ago we would use public telephones to send messages to each other. For example, after arriving at a foreign destination we would call home and let the phone ring five times then hang up – which meant we had arrived safely). Finally, once the GSM shield has received a set number of rings, we want the Arduino to do something.

From a software perspective, we need to examine each character as it is returned from the GSM shield. Once an “R” is received, we examine the next character. If it is an “I”, we examine the next character. If it is an “N”, we examine the next character. If it is a “G”, we know an inbound call is being attempted, and one ring has occurred. We can set the number of rings to wait until out desired function is called. In the following example, when the shield is called, it will call the function doSomething() after three rings.

The function doSomething() controls two LEDs, one red, one green. Every time the GSM module is called for 3 rings, the Arduino alternately turns on or off the LEDs. Using this sketch as an example, you now have the ability to turn basically anything on or off, or call your own particular function. Another example would be to return some type of data, for example you could dial in and have the Arduino send you a text message containing temperature data.

Example 27.1 (Mega version)

/*  Example 27.1  Turn something on or off by telephoning the GSM shield for a set number of rings
tronixstuff.com/tutorials > chapter 27  CC by-sa-nc
NOT for Arduino Mega */
#include <NewSoftSerial.h>  //Include the NewSoftSerial library to send serial commands to the cellular module
char inchar;       // Will hold the incoming character from the Serial Port
NewSoftSerial cell(2,3);    //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin
int numring=0; // used to count the number of 'rings' received by the GSM module
int comring=3; // this is the number of rings to wait for until calling the function doSomething();
int onoff=0; // used to track LED status - 0 = off, 1 = on
void setup()
{
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);  // LEDs - off = red, on = green
digitalWrite(12, HIGH);
digitalWrite(13, LOW);  //Initialize serial port for communication.
cell.begin(9600);
}

void doSomething()
{
if (onoff==0)
{
onoff=1;
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
}   else
if (onoff==1)
{
onoff=0;
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
}
}

void loop()
{  //If a character comes in from the cellular module...
if(cell.available() >0)
{
inchar=cell.read();
if (inchar=='R')
{
delay(10);
inchar=cell.read();
if (inchar=='I')
{
delay(10);
inchar=cell.read();
if (inchar=='N')
{
delay(10);
inchar=cell.read();
if (inchar=='G')
{
 delay(10);
// So the phone (our GSM shield) has 'rung' once, i.e. if it were a real phone
// it would have sounded 'ring-ring'
numring++;
if (numring==comring)
{
numring=0; // reset ring counter
doSomething();
}
}
}
}
}
}
}

And now for a quick video demonstration. The first call is made, and the LEDs go from red (off) to green (on). A second call is made, and the LEDs go from green (on) to red (off). Although this may seem like an over-simplified example, with your existing Ardiuno knowledge you now have the ability to run any function by calling your GSM shield.

Now although turning one thing on or off is convenient, how can we send more control information to our GSM module? For example, control four or more digital outputs at once? These sorts of commands can be achieved by the reception and analysis of text messages.

Doing so is similar to the method we used in example 27.1. Once again, we will analyse the characters being sent from the GSM module via its serial out. However, there are two AT commands we need to send to the GSM module before we can receive SMSs, and one afterwards. The first one you already know:

AT+CMGF=1

Which sets the SMS mode to text. The second command is:

AT+CNMI=3,3,0,0

This command tells the GSM module to immediately send any new SMS data to the serial out. An example of this is shown in the terminal capture below (click to enlarge):

Two text messages have been received since the module was turned on. You can see how the data is laid out. The blacked out number is the sender of the SMS. The number +61418706700 is the number for my carrier’s SMSC (short message service centre). Then we have the date and time. The next line is the contents of the text message – what we need to examine in our sketch.

The second text message in the example above is how we will structure our control SMS. Our sketch will wait for a # to come from the serial line, then consider the values after a, b, c and d – 0 for off, 1 for on. Finally, we need to send one more command to the GSM module after we have interpreted our SMS:

AT+CMGD=1,4

This deletes all the text messages from the SIM card. As there is a finite amount of storage space on the SIM, it is prudent to delete the incoming message after we have followed the instructions within. But now for our example. We will control four digital outputs, D9~12. For the sake of the exercise we are controlling an LED on each digital output, however you could do anything you like. Although the sketch may seem long and complex, it is not – just follow it through and you will see what is happening.

Example 27.2 (Mega version)

/*    Example 27.2   Control four digital outputs via SMS
tronixstuff.com/tutorials > chapter 27   CC by-sa-nc
NOT for Arduino Mega   */
#include <NewSoftSerial.h>  //Include the NewSoftSerial library to send serial commands to the cellular module.
char inchar;                //Will hold the incoming character from the Serial Port.
NewSoftSerial cell(2,3);    //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{    // prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);    //Initialize GSM module serial port for communication.
cell.begin(9600);
delay(30000); // give time for GSM module to register on network etc.
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
cell.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{    //If a character comes in from the cellular module...
if(cell.available() >0)
{
inchar=cell.read();
if (inchar=='#') // OK - the start of our command
{
delay(10);
inchar=cell.read();
if (inchar=='a')
{
delay(10);
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='b')
{
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='c')
{
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='d')
{
delay(10);
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
cell.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
}
}

And now for a video demonstration:

So there you have it – controlling your Arduino digital outputs via a normal telephone or SMS. Now it is up to you and your imagination to find something to control, sensor data to return, or get up to other shenanigans.

If you enjoyed this article, you may find this of interest – controlling AC power outlets via SMS.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.


January 31, 2011 - Posted by | arduino, cellphone hacking, cellular, GSM, hardware hacking, microcontrollers | , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

26 Comments

  1. This board lets us imagine tons and tons of fun projects and/or enhancements for already existing devs.
    Thank you once again for this lesson John!

    Keep on

    Gilles

    Comment by Gilles | February 1, 2011

  2. Thanks for this and all other articles!

    Comment by jim | February 3, 2011

    • Your welcome Jim, thank you for your comment
      cheers
      John

      Comment by John Boxall | February 3, 2011

  3. Hi, How would one use DTMF codes to control the arduino ?

    Comment by daniel | February 7, 2011

  4. Hi, I think these tutorials are great. I would really like to see a GSM Cellular – Part 3 Tutorial on sending data (like a picture or other file) from a SD card via MMS or GPRS to web.

    Cheers,
    Paddy

    Comment by Paddy | February 11, 2011

    • Hi Paddy
      I have already started part 3, it is about GPS module and GSM, for tracking and so on.
      However your idea is a good one. I’ll look into it.
      cheers
      john

      Comment by John Boxall | February 11, 2011

      • Hey John,

        Any update on your GPS/GSM tutorial? I am stuck and cannot seem to make a connection between my SM5100B and my server. I am using the TMobile network and continually only get CSQs of 15 or 16. Is this too low for GSM transmission? Any help you could provide would be great.

        Comment by Jesse | March 27, 2011

      • Hello Jesse
        At the moment I have lent my GSM shield to someone else, so will be a month or so before the next update.
        That CSQ is a bit low. If you are not already doing so, you might want to try a full-size aerial such as the type used on a motor vehicle.
        cheers
        john

        Comment by John Boxall | March 27, 2011

  5. Hey John

    I bought a GSM shield and a Arduino duemilanove.
    Can you help me? How I connect the shield and the Arduino?

    Thanks

    Comment by Alexandre | April 19, 2011

  6. Hello i’m from Mexico your description is very complete. i use pic microcontrollers. your web page is very helpful. thanks for the informations and congratulations for your web page.

    Comment by Alex | May 21, 2011

    • Hello Alex
      Thank you for your feedback, I really appreciate it
      cheers
      John

      Comment by John Boxall | May 22, 2011

  7. Hi
    Great tutorial. When will part 3 be ready?

    Comment by Trond | June 22, 2011

    • Thank you. Not too sure, am rather busy with other things at the moment. Is there anything in particular you would like to see?
      Cheers
      John

      Comment by John Boxall | June 22, 2011

      • I would love to learn how to read data from various sensors and send it to a web page, twitter etc…. with the gprs/gsm shield.

        Thanks
        Trond

        Comment by Trond | June 23, 2011

      • OK, I’ll put it on my to-do list – but can’t guarantee when it will happen
        cheers
        john

        Comment by John Boxall | June 23, 2011

  8. Thanks :) Looking forward to it…..

    Trond

    Comment by Trond | June 24, 2011

  9. Hello John, Firstly great insight into gsm basics. Could i trouble you for an example of sending a data stream via the system such as binary data collected from a monitoring device. I don’t want the full tutorial just a basic example of how to send the data.

    Cheers Pete

    Comment by peter baines | July 18, 2011

    • Could you please email me with a bit more detail, e.g. the monitoring device, and how you want to send the data (e.g. wireless, GSM, etc)? john at tronixstuff dot com.
      Thanks
      john

      Comment by John Boxall | July 18, 2011

  10. Hi, great tutorial, well explained!!

    Do you know if it’s possible (and do you have any examples), of the exact same type of thing, only NOT asking the GSM module to output to the serial port immediately, but rather keeping the information on the GSM module until requested by the host uC? The reason I’m asking, is that we have a limited buffer length on our uC, and a relatively large amount of code doing other things, and we run the risk of losing information due to buffer overrun if our “loop()” doesn’t get round and check the serial port quickly enough. I guess it means using AT commands to ask for status and as for data, but I’ve been looking at this now for a total of about 3 hours, so any pointers in the right direction would be great as I’m totally new to it! :-)

    Also (sorry to be so demanding), do you have any examples on ways to manage the error handling?

    Very satisfying getting code to send a message to my phone…!! :-)

    Peter

    Comment by Peter Mackel | August 27, 2011

    • Hello Peter
      Thanks for your feedback. No, I haven’t seen a module that keeps the data to itself. Perhaps you could have one microcontroller board (such as an Arduino Nano) controlling the GSM module and storing relevant data, and the host microcontroller would be separate from that. With regards to error handling some people have been using string functions to receive and work with the text coming from the GSM module, however I have not done this myself as I have moved on from this area of work.
      cheers
      john

      Comment by John Boxall | August 28, 2011

  11. Hi John,
    How could I go about getting the system to only respond to a certain mobile number?
    Thanks
    Jules

    Comment by Jules | September 8, 2011

    • Lazy answer – don’t let anyone find out the number attached to the SIM card. Sorry, couldn’t help myself :)
      Seriously – when an SMS is received the sender’s number comes with it. You can have the sketch examine the number (as we do with the SMS commands) and check it matches your allowed number(s) – or if no CLI comes in, ignore it. Etc.
      cheers
      john

      Comment by John Boxall | September 8, 2011

  12. Hey John,

    is there any chance to show me a sample code on how to receive sms to my gsm shield. I’m using the same sheild as you.

    Cheers Feda

    Comment by Feda Adel | September 13, 2011

    • Not at the moment – I don’t have this shield anymore. When I get another one I will revisit this for you.

      Comment by John Boxall | September 13, 2011


Sorry, the comment form is closed at this time.

Follow

Get every new post delivered to your Inbox.

Join 2,588 other followers