How to send and receive SMS with GSM/GPRS & GPS shield
This post is one in a series showing you the main features of the GSM /GPRS/GPS shield and of its related library. The examples are great to have inspiration and reuse the material to create your own solutions. We already talked about how to use the shield to make calls, send DTMF tones and check whether the sender of an incoming call and command was in the trusted phonebook.
This is possible thanks to the call.h class contained within the call management library. Here we will show you few examples about how to deal with SMS management. The sms.h file, shows you how to send a text message in correspondence to a particular condition or how to extrapolate the content of a received text message and how to interprete the commands contained within it.
EXAMPLE 1
Let’s start from a sketch that can be used to send a text message to a preset number in case an alarm condition is happening. This example is similar to the one we shown regarding placing calls, it has been obviously tweaked to send a text message instead of placing a call.
In this case, the triggering condition is reached when the Arduino board reads an analog value that is below to one that has been preset as a threshold value and the notification is done by sending a text message.
As for each sketch concerning this shield, first thing to do is to include the library (in this case the sms.h file) and then instantiate an object. This procedure can be done with the following commands:
#include “sms.h”
SMSGSM sms;
Each function belonging to this class must be initialized as follows:
object_name.function_name(parameters)
In case the alarm status condition is verified, in order to send a message you simply call the following function:
sms.SendSMS(number,message);
The first parameter contains the message recipient, while the second one is the string containing the message body.
For example:
char message[]=“Hello world”;
char number[]=“3921234567”;
sms.SendSMS(number,message);
In this way the string “Hello world” will be sent.
In this example, in addition to sending the static string containing the warning message, we’ve also chosen to communicate the value that triggered the alarm condition: we must therefore transform the analog integer value read from the converter, in a string to be concatenated to the static one.
The function that takes care of this conversion is the on that follows:
itoa(int integer, char* string, int base)
where base is usually 10 since we work in decimal base. If we were to work with numbers in different bases we just need to change this value as appropriate.
The function used to concatenate a string to another is:
strcat(char* string1, char* string2)
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;
char number[]="3921234567";
int value=0;
int value_old=0;
char message[40];
char value_str[4];
int pin=A0;
void setup()
{
Serial.begin(9600);
if (gsm.begin(2400))
Serial.println("nstatus=READY");
else Serial.println("nstatus=IDLE");
value=analogRead(pin);
value_old=analogRead(pin);
};
void loop()
{
value=analogRead(pin);
Serial.println(value);
if(value<50&&value_old>=50){
message[0]=' |

