Seeedstudio SIM900 based GSM/GPRS shield




Please note: I am based in the UK and the following works with Orange and O2 pre-pay sim cards. The situation where you are may be different!

The Seeedstudio SIM900 based GSM/GPRS shield for Arduino is a popular and easy to use accessory. However, it can be daunting to first time users so here is my simple guide to getting things working. We first check that we can communicate with the shield, and then we will turn a LED, connected to D10, on and off using text messages / SMS. Perhaps at this stage I should mention that this is just a demonstration of how to achieve this, and is not representative of best programming practice. In the real world, don't use delay()!

Power

Despite what is written in the specifications the SIM900 can momentarily draw about 2 amps when operating. The USB power supply to the Arduino will not suffice! For development work I have a 5 volt 2 amp power supply plugged into the shield, and a separate 9 volt 1 amp power supply into the Arduino. Set the power select switch to the appropriate position, either Xduino or Ext.



Operation

Once the shield has been powered up and completed its self check it will start to search for a network signal. During this process the NetLight indicator will blink rapidly, once a second. When a signal is acquired it will then blink once every three seconds.

Preparing

So we need a robust power supply, an unlocked SIM card, our shield, an LED with an appropriate resistor and our Arduino of choice.

Power up

Our first decision is do we want to enable software power control? Initially this might not sound like a difficult decision but think about it for a moment. The same procedure will turn the shield on if it is off and off if it is on. So, if, when you are testing, you reset your Arduino without turning off the shield then it will send the power on/off command and turn the the shield off. So I would suggest that until you are comfortable with the shield leave the pad near pins D8 & D9 (illustrated below) unsoldered and use the PWRKEY switch to turn it on or off.



When your project is up and running you may want to incorporate software control. If so, solder the two pads together and add the following section to your code:

void powerUp()
{
  pinMode(9, OUTPUT);
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2000);
  digitalWrite(9,LOW);
  delay(3000);
}


And then put a call to the procedure in setup().

Arduino Uno (etc)

Using the shield with the Uno is comparatively straight forward. We can use the USB connection to communicate with the board, and use SoftwareSerial on pins D7 & D8 to communicate with the shield. Ensure that the jumpers, adjacent to the aerial connector, are to the right as per the main illustration.

Check that it works

The first job is to ensure that we can communicate with the shield. To do that we will use a fairly common piece of software called Serial Relay (sadly I don't know who wrote it to give credit). This piece of software relays any commands sent from the Serial Monitor to the Arduino on to the SIM900, and sends anything from the SIM900 to the Arduino on to the Serial Monitor. Don't worry about the 8-N-1, that is the default setting, and if you have been playing with that you probably don't need to be reading this introduction!

I have removed quite a lot of the original comments. If you don't understand how it works there are several versions on the internet.

//Serial Relay - Arduino will patch a
//serial link between the computer and the GPRS Shield
//at 19200 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART

#include <SoftwareSerial.h>

SoftwareSerial GPRS(7, 8);

unsigned char buffer[64];  // buffer array for data receive over serial port
int count=0;               // counter for buffer array

void setup()
{
  GPRS.begin(19200);
  Serial.begin(19200);
}

void loop()
{
  if (GPRS.available())
  {
    while(GPRS.available())
    {
      buffer[count++]=GPRS.read();
      if(count == 64)break;
    }
    Serial.write(buffer,count);
    clearBufferArray();
    count = 0;
  }
  if (Serial.available())
    GPRS.write(Serial.read());
}

void clearBufferArray()
{
  for (int i=0; i<count;i++)
  {
    buffer[i]=NULL;
  }
}

So upload this sketch to the Arduino, connect the shield and start up the Serial Monitor (at 19200 baud)

Wait until the green network led is blinking every three seconds, and then type the letters AT into the command bar,



and click the 'Send' button. The sketch will send the command on to the SIM900, which will (hopefully) in turn respond...


We are in business!

At this stage you may wish to
1) Download a manual of SIM900 AT commands and have a play around with the device.
2) Issue the AT&F command to reset the device to the factory settings.

Now we shall get on with the process of receiving and acting upon a SMS / text message.

I traced some of this code back to http://tronixstuff.com/2014/01/08/tutorial-arduino-and-sim900-gsm-modules/

#include <SoftwareSerial.h>

char inchar; // Will hold the incoming character from the GSM shield
SoftwareSerial SIM900(7, 8);

int led = 10;

void setup()
{
  Serial.begin(19200);
  // set up the digital pins to control
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);

  // wake up the GSM shield
  SIM900.begin(19200);
  delay(20000); // give time to log on to network.
  SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(100);
  Serial.println("Ready...");
}

void loop()
{
  //If a character comes in from the cellular module...
  if(SIM900.available() >0)
  {
    inchar=SIM900.read();
    if (inchar=='#')
    {
      delay(10);

      inchar=SIM900.read();
      if (inchar=='a')
      {
        delay(10);
        inchar=SIM900.read();
        if (inchar=='0')
        {
          digitalWrite(led, LOW);
        }
        else if (inchar=='1')
        {
          digitalWrite(led, HIGH);
        }
        delay(10);
        // SIM900.println("AT+CMGD=1,4"); // delete all SMS
      }
    }
  }
}

Arduino Mega

Using the shield with the hardware port isn't as straightforward as the Uno, but it is by no means difficult.

Instead of using SoftwareSerial we will use Serial1, and use two jumpers to connect the output of the SIM900 to the Mega. Connect shield D7 to Mega 19 (Rx1) and shield D8 to Mega 18 (Tx1)




Set the jumper pins (shown below) on the shield to the 'SWserial' position.



First the code for Serial Relay

unsigned char buffer[64];  // buffer array for data receive over serial port
int count=0;               // counter for buffer array

void setup()
{
  Serial1.begin(19200);
  Serial.begin(19200);
}

void loop()
{
  if (Serial1.available())
  {
    while(Serial1.available())
    {
      buffer[count++]=Serial1.read();
      if(count == 64)break;
    }
    Serial.write(buffer,count);
    clearBufferArray();
    count = 0;
  }
  if (Serial.available())
    Serial1.write(Serial.read());
}

void clearBufferArray()
{
  for (int i=0; i<count;i++)
  {
    buffer[i]=NULL;
  }
}


And now the code for receiving and acting upon the instruction:

char inchar; // Will hold the incoming character from the GSM shield

int led = 10;

void setup()
{
  // set up the digital pins to control
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);

  // wake up the GSM shield
  Serial1.begin(19200);
  delay(20000); // give time to log on to network.
  Serial1.print("AT+CMGF=1\r"); // set SMS mode to text
  delay(100);
  Serial1.print("AT+CNMI=2,2,0,0,0\r");
  // blurt out contents of new SMS upon receipt to the GSM shield's serial out
  delay(100);
}

void loop()
{
  //If a character comes in from the cellular module...
  if(Serial1.available() >0)
  {
    inchar=Serial1.read();
    if (inchar=='#')
    {
      delay(10);

      inchar=Serial1.read();
      if (inchar=='a')
      {
        delay(10);
        inchar=Serial1.read();
        if (inchar=='0')
        {
          digitalWrite(led, LOW);
        }
        else if (inchar=='1')
        {
          digitalWrite(led, HIGH);
        }
        delay(10);
        // Serial1.println("AT+CMGD=1,4"); // delete all SMS
      }
    }
  }
}

In either case upload the sketch to the Arduino before assembling the shield and led.

Now if you text the message '#a0' to the shield it should turn the led off, and '#a1' will turn it on. If it doesn't then check that the led is the right way round and connected to the correct pin. If all seems in order dismantle the project, remove the SIM card and place in a mobile phone so you can inspect the messages received. Hence why we don't delete the messages once we have processed them! Once you have everything working you can reinstate the line that deletes the messages. The typical SIM card can hold around 50 standard text messages.

Now you can go away and learn how to use the other AT commands to control the shield. Search online for 'SIM900 AT Commands'.

Send a text / sms

In response to questions, a simple way to send a message. It is meant to show how to, rather than a stand alone sketch. If you do want to try it, change the destinationNumber to your own, and upload to the sketch to the Arduino. Attach shield and apply power. Every time you reset the Arduino it will send the message!

This is for the Uno. I'm sure you can now make the necessary changes if you have a Mega.

#include <SoftwareSerial.h>

SoftwareSerial SIM900(7, 8);

String outMessage = "Hello world!";
String destinationNumber = "+447751123456";

void setup()
{
  SIM900.begin(19200);
  delay(20000); // give time to log on to network.
  SIM900.print("AT+CMGF=1\r");
  delay(1000);
  SIM900.println("AT + CMGS = \"" + destinationNumber +"\"");
  delay(1000);
  SIM900.print(outMessage);
  delay(1000);
  SIM900.write((char)26); //ctrl+z
  delay(1000);
}

void loop()
{
}

http://www.simpasture.com/36023.html 05/05/2015