Taillieu.Info

More Than a Hobby..

Arduino GSM shield SIM900

This is a very low cost and simple Arduino GSM and GPRS shield. We use the module SIMCom SIM900. It’s the cheaper module now avalaible in the market. The module is not simple to mount by an hobbyst, so we use the Breakboard TDGGSM_900 that we presented in this post.

You can buy the GSM module premounted from our cart.

To connect this module to Arduino we make a PCB that include a LM317 some capacitor filter and no more.

The LM317 give to module about 3.9V.
There is a switch to select the way to comunicate with arduino:
- throw serial hardware (pin 0 and 1)
- throw serial software (pin 4 and 5)

R1: 470 ohm
R2: 1 kohm
C1-C3: 100 nF
C2-C4: 470 µF 25 VL 
U1: LM317
GSM: TDGGSM_900 module

SW1: switch
P1: Microswitch

[singlepic id=194 w=500 h=375 float=center]

Selecting the serial hardware there are no problem with the baudrate (the default module baudrate is 115200) but there are problem programming the module (the serial is also used to upload the sketch).
Using instead the pin 4 and 5 there aren’t problems to upload the sketch but the maximum baudrate for NewSoftSerial (the serial library) is 57600.
We performed a GSM library to controll easly the module. The GSM library is a modified version of the library of HWKitchen.
With our version we controll the module throw the pin 4 and 5 (so normal digital pin) and our GSM libray include also the NewSoftSerial, so you can easy control the module, send and read SMS, make call, control the GSM state ecc.

Download GSM_Shield_Library

New GSM/GPRS library

 

[singlepic id=206 w=500 h=375 float=center]

[singlepic id=159 w=500 h=375 float=center]

[singlepic id=158 w=500 h=375 float=center]

[singlepic id=160 w=500 h=375 float=center]

[singlepic id=193 w=500 h=375 float=center]

 

Download GSM_Shield_Library

 

New GSM/GPRS library

 

 

 

 

This is the GSM_shield library documentation:

 

Here is a GSM_shield Library intended for GSM Shield by Futura Elettronica (www.futurashop.it).

This Library is derived from Hwkitchen’s GSM Library http://www.hwkitchen.comand include the NewSoftSerial library to comunicate using the pin 4 (RX) and 5 (TX).

You can also use the pin 0 and 1 (RX and TX) but you must disconnect the module to upload the sketch (so it’s not very nice), and you must modify the library.

How install the library for Arduino

After downloading of the GSM_Shield Library unzip the GSM_Shield folder toArduino

Folder\libraries\ (es. C:\Programs\arduino-0022\libraries\GSM_Shield)

In the GSM_Shield.zip you can find the library to comunicate with gsm module and the file for use the NewSoftSerial.

Basic description

GSM_Shield library is created as standard class with the Gsm_Shield.cpp and Gsm_Shield.h source files. GSM_Shield class is based mainly on the serial communication

between the Arduino board and the GSM module placed on the GSM Shield. There are used standard AT commands for the communication with the GSM Module.

The Current version of library uses blocking version of communication.

It means that the program is blocked until the communication function is finished – so until required data are sent and required answer is received.The Advantage of that blocking approach is that it is easy to

understand the program flow. On the other hand there is also disadvantage that we can’t use the processor resources in the time when the program just waits for the incoming data.

Note: please pay attention to serial comunication mode. With the serial hardware (pin 0 and 1) you can reach the default baudrate of the module SIM900 (115200). But if you use the pin 4 and 5 the NewSoftSerial library can’t support (receive) the baudrate, so you must chose a lower baudrate.

With the command TurnOn(baudrate) you turn on the module and fix the baudrate. e.g. TurnOn(9600);

Methods

There are described important functions for the end user in this document.

The GSM_Shield Library contains also some functions that are used internally

and are not described in the document. It is also possible to use these

functions by the end user of course as they are defined as public but there

is needed to check the library source code with notes.
int LibVer(void)

returns library version in format XYY -it means X.YY (e.g. 100 means vers. 1.00)

Sample: GSM_Shield_LibVer
void TurnOn(baud)

turns on the GSM module in case module is off and sends some initialization AT commands which are possible to send before registration -> InitParam(PARAM_SET_0)

Should be used at the beginning of the sketch in the setup() function.

Set also the module baudrate (Note: if you use the hardware serial, there are no limit to the baudrate, 115200 is possibile. But using the pin 4 and 5 the NewSoftSerial must be use and the baud limit is 57600).

baud value possibile: 4800, 9600, 19200, 38400, 57600, 115200(no use with this library which include NewSoftSerial)

void setup()

{

gsm.TurnOn(9600);

}

Sample: GSM_Shield_LibVer

 

void InitParam(byte group)

Sends parameters for initialization of GSM module
group:  0 – parameters of group 0 – not necessary to be registered in the GSM

AT&F

1 – parameters of group 1 – it is necessary to be registered

AT+CLIP=1

AT+CMEE=0

AT+CMGF=1

Sample: GSM_Shield_LibVer

void Echo(byte state)
Function to enable or disable echo

Echo(1)   enable GSM echo mode

Echo(0)   disable GSM echo mode

Sample: GSM_Shield_LibVer

byte CheckRegistration(void)

checks if the GSM module is registered in the GSM net.

This method communicates directly with the GSM module in contrast to the method IsRegistered() which reads the flag from the module_status

(this flag is set inside this method)

must be called regularly at the one place in the main sketch loop

(recommendation repeat time is from 1sec. to 10 sec.)

return valus:

REG_NOT_REGISTERED – not registered

REG_REGISTERED – GSM module is registered

REG_NO_RESPONSE – GSM doesn’t response

REG_COMM_LINE_BUSY – comm line is not free

Sample: GSM_Shield_Reg

 

byte IsRegistered(void)

returns flag if the GSM module is registered in the GSM net

this method does not communicate directly with the GSM module,

just reads the flag so it is very fast unlike CheckRegistration()

which takes more than 20msec.

it is recommended to use this function everytime it is necessary to use some GSM function which needs GSM module is registered – checking ingoing SMSs, checking calls etc.

return valus:

0 – not registered

>0 – GSM module is registered

Sample: GSM_Shield_Reg

 

byte CallStatus(void)

checks call status

return values:

CALL_NONE – no call

CALL_INCOM_VOICE – incoming voice call

CALL_ACTIVE_VOICE – active voice call

CALL_NO_RESPONSE – no response

Sample: GSM_Shield_Call

 

byte CallStatusWithAuth(char *phone_number, byte first_authorized_pos, byte last_authorized_pos)

checks status of call(incoming or active) and makes authorization with specified SIM positions range

parameters and return values:

phone_number: a pointer where the tel. number string of current call will be placed so the space for the phone number string must be reserved

first_authorized_pos: initial SIM phonebook position where the authorization process starts

last_authorized_pos: last SIM phonebook position where the authorization process finishes

Note(important):

================

In case first_authorized_pos=0 and also last_authorized_pos=0 the received incoming phone number is NOT authorized at all, so every incoming is considered as authorized (CALL_INCOM_VOICE_NOT_AUTH is returned)

return:

CALL_NONE – no call activity

CALL_INCOM_VOICE_AUTH – incoming voice – authorized

CALL_INCOM_VOICE_NOT_AUTH – incoming voice – not authorized

CALL_ACTIVE_VOICE – active voice

CALL_INCOM_DATA_AUTH – incoming data call – authorized

CALL_INCOM_DATA_NOT_AUTH – incoming data call – not authorized

CALL_ACTIVE_DATA – active data call

CALL_NO_RESPONSE – no response to the AT command

CALL_COMM_LINE_BUSY – comm line is not free

 

void PickUp(void)

picks up the incoming call

Sample: GSM_Shield_Call

 

void HangUp(void)

hangs up call(incoming or active)

Sample: GSM_Shield_Call

 

void Call(char *number_string)

calls the specific number

e.g. gsm.Call(“+390123456789″);

 

void Call(int sim_position)

calls the number stored at the specified SIM position

e.g. gsm.Call(1); // call to the number stored at the 1st SIM position

 

char SendSMS(char *number_str, char *message_str)

sends SMS to the specific phone number

parameters and return values:

number_str: pointer to the phone number string

message_str: pointer to the SMS text string

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – GSM module has answered “ERROR” string

OK ret val:

———–

0 – SMS was not sent

1 – SMS was sent 9/15

example of use:

gsm.SendSMS(“00XXXYYYYYYYYY”, “SMS text”);

 

char SendSMS(byte sim_phonebook_position, char *message_str)

sends SMS to the specified SIM phonebook position

parameters and return values:

sim_phonebook_position: SIM phonebook position <1..20>

message_str: pointer to the SMS text string

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – specified position must be > 0

OK ret val:

———–

0 – SMS was not sent

1 – SMS was sent

an example of usage:

GSM gsm;

gsm.SendSMS(1, “SMS text”);

 

char IsSMSPresent(byte required_status)

finds out if there is present at least one SMS with specified status

if there is new SMS before IsSMSPresent() is executed this SMS has a status UNREAD and then after calling IsSMSPresent() method status of SMS is automatically changed to READ

parameters and return values:

required_status:

SMS_UNREAD – new SMS – not read yet

SMS_READ – already read SMS

SMS_ALL – all stored SMS

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

OK ret val:

———–

0 – there is no SMS with specified status

1..20 – position where SMS is stored

example of use:

char position;

char phone_number[20]; // array for the phone number string

char *sms_text;

position = gsm.IsSMSPresent(SMS_UNREAD);

if (position) { // read new SMS

gsm.GetGSM(position, tel_number, &sms_text);

}

 

char GetSMS(byte position, char *phone_number, char *SMS_text, byte max_SMS_len)

reads SMS from specified memory(SIM) position

parameters and return values:

position: SMS position <1..20>

phone_number: a pointer where the phone number string of received SMS will be placed

so the space for the phone number string must be reserved – see example

SMS_text : a pointer where SMS text will be placed

max_SMS_len: maximum length of SMS text excluding also string terminating 0×00 character

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – specified position must be > 0

OK ret val:

———–

GETSMS_NO_SMS – no SMS was found at the specified position

GETSMS_UNREAD_SMS – new SMS was found at the specified position

GETSMS_READ_SMS – already read SMS was found at the specified position

GETSMS_OTHER_SMS – other type of SMS was found an example of usage:

GSM gsm;

char position;

char phone_num[20]; // array for the phone number string

char sms_text[100]; // array for the SMS text string

position = gsm.IsSMSPresent(SMS_UNREAD);

if (position) {

// there is new SMS => read it

gsm.GetGSM(position, phone_num, sms_text, 100);

Serial.println(“DEBUG SMS phone number: “, 0);

Serial.println(phone_num, 0);

Serial.println(“\r\n SMS text: “, 0);

Serial.println(sms_text, 1);

}

char GetAuthorizedSMS( byte position, char *phone_number, char *SMS_text, byte max_SMS_len, byte first_authorized_pos, byte last_authorized_pos)

reads SMS from specified memory(SIM) position and makes authorization -

it means SMS phone number is compared with specified SIM phonebook position(s) and in case numbers match GETSMS_AUTH_SMS is returned, otherwise GETSMS_NOT_AUTH_SMS is returned

parameters and return values:

position: SMS position to be read <1..20>

phone_number: a pointer where the tel. number string of received SMS will be placed so the space for the phone number string must be reserved – see example

SMS_text : a pointer where SMS text will be placed

max_SMS_len: maximum length of SMS text excluding terminating 0×00 character

first_authorized_pos: initial SIM phonebook position where the authorization process starts

last_authorized_pos: last SIM phonebook position where the authorization proces finishes

Note(important):

================

In case first_authorized_pos=0 and also last_authorized_pos=0

the received SMS phone number is NOT authorized at all, so every

SMS is considered as authorized (GETSMS_AUTH_SMS is returned)

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – position must be > 0

OK ret val:

———–

GETSMS_NO_SMS – no SMS was found at the specified position

GETSMS_NOT_AUTH_SMS – NOT authorized SMS found at the specified position

GETSMS_AUTH_SMS – authorized SMS found at the specified position

an example of usage:

GSM gsm;

char phone_num[20]; // array for the phone number string 12/15

char sms_text[100]; // array for the SMS text string

// authorize SMS with SIM phonebook positions 1..3

if (GETSMS_AUTH_SMS == gsm.GetAuthorizedSMS(1, phone_num, sms_text, 100, 1, 3)) {

// new authorized SMS was detected at the SMS position 1

Serial.println(“DEBUG SMS phone number: “, 0);

Serial.println(phone_num, 0);

Serial.println(“\r\n SMS text: “, 0);

Serial.println(sms_text, 1);

}

// don’t authorize SMS with SIM phonebook at all

if (GETSMS_AUTH_SMS == gsm.GetAuthorizedSMS(1, phone_num, sms_text, 100, 0, 0)) {

// new SMS was detected at the SMS position 1

// because authorization was not required

// SMS is considered authorized

Serial.println(“DEBUG SMS phone number: “, 0);

Serial.println(phone_num, 0);

Serial.println(“\r\n SMS text: “, 0);

Serial.println(sms_text, 1);

}

 

char DeleteSMS(byte position)

deletes SMS from specified SMS position

parameters and return values:

position: SMS position <1..20>

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – position must be > 0

OK ret val:

———–

0 – SMS was not deleted

1 – SMS was deleted

 

char GetPhoneNumber(byte position, char *phone_number)

reads phone number string from specified SIM position

parameters and return values:

position: SMS position <1..20>

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – position must be > 0

phone_number is empty string

OK ret val:

———–

0 – there is no phone number on the position

1 – phone number was found

phone_number is filled by the phone number string finished by 0×00

so it is necessary to define string with at least

15 bytes(including also 0×00 termination character)

an example of usage:

GSM gsm;

char phone_num[20]; // array for the phone number string

if (1 == gsm.GetPhoneNumber(1, phone_num)) {

// valid phone number on SIM pos. #1

// phone number string is copied to the phone_num array

Serial.println(“DEBUG phone number: “, 0);

Serial.println(phone_num, 1);

}

else {

// there is not valid phone number on the SIM pos.#1

Serial.println(“DEBUG there is no phone number”, 1);

}

 

char WritePhoneNumber(byte position, char *phone_number)

writes phone number string to the specified SIM position

parameters and return values:

position: SMS position <1..20>

phone_number: phone number string for the writing

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – position must be > 0

OK ret val:

———–

0 – phone number was not written

1 – phone number was written 14/15

char DelPhoneNumber(byte position)

del phone number from the specified SIM position

parameters and return values:

position: SIM position <1..20>

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – position must be > 0

OK ret val:

———–

0 – phone number was not deleted

1 – phone number was deleted

 

char ComparePhoneNumber(byte position, char *phone_number)

compares specified phone number string with phone number stored at the specified SIM position

parameters and return values:

position: SMS position <1..20>

phone_number: phone number string which should be compare

return:

ERROR ret. val:

—————

-1 – comm. line to the GSM module is not free

-2 – GSM module didn’t answer in timeout

-3 – position must be > 0

OK ret val:

———–

0 – phone numbers are different

1 – phone numbers are the same

an example of usage:

if (1 == gsm.ComparePhoneNumber(1, “123456789″)) {

// the phone num. “123456789″ is stored on the SIM pos. #1

// phone number string is copied to the phone_num array

Serial.println(“DEBUG phone numbers are the same”, 1);

}

else {

Serial.println(“DEBUG phone numbers are different”, 1);

}

 

Download GSM_Shield_Library

 

New GSM/GPRS library

 

 

 

 

 

In this page you can find some example.

Example to test library

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* GSM Shield example
 
 created 2011
 by Boris Landoni
 
 This example code is in the public domain.
 
 
 
 
 */
 
#include <GSM_Shield.h>
//for enable disable debug rem or not the string       #define DEBUG_PRINT
// definition of instance of GSM class
GSM gsm;
 
void setup() {
  Serial.begin(9600);
  Serial.println("system startup");
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.TurnOn(9600);              //module power on
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.InitParam(PARAM_SET_1);//configure the module
  gsm.Echo(1);               //enable AT echo
}
 
void loop()
{
 
    int ver;
    ver=gsm.LibVer();
    Serial.print("Response Ver ");
    Serial.println(ver);
    delay(50000);
}

 

Example to network registration

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* GSM Shield example
 
 created 2011
 by Boris Landoni
 
 This example code is in the public domain.
 
 
 
 
 */
 
#include <GSM_Shield.h>
//for enable disable debug rem or not the string       #define DEBUG_PRINT
// definition of instance of GSM class
GSM gsm;
 
void setup() {
  Serial.begin(9600);
  Serial.println("system startup");
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.TurnOn(9600);              //module power on
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.InitParam(PARAM_SET_1);//configure the module
  gsm.Echo(1);               //enable AT echo
}
 
void loop()
{
 
    int reg;
    reg=gsm.CheckRegistration();
    switch (reg){
      case REG_NOT_REGISTERED:
        Serial.println("not registered");
        break;
      case REG_REGISTERED:
        Serial.println("GSM module is registered");
        break;
      case REG_NO_RESPONSE:
        Serial.println("GSM doesn't response");
        break;
      case REG_COMM_LINE_BUSY:
        Serial.println("comm line is not free");
        break;
    }
    delay(2000);
 
    reg=gsm.IsRegistered();
      Serial.print("Registration ");
      Serial.println(reg);
 
    delay(50000);
}

 

Example to test call

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* GSM Shield example
 
 created 2011
 by Boris Landoni
 
 This example code is in the public domain.
 
 
 
 
 */
 
#include <GSM_Shield.h>
//for enable disable debug rem or not the string       #define DEBUG_PRINT
// definition of instance of GSM class
GSM gsm;
 
void setup() {
  Serial.begin(9600);
  Serial.println("system startup");
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.TurnOn(9600);              //module power on
  //gsm.InitSerLine(9600);   //initialize serial 1
  gsm.InitParam(PARAM_SET_1);//configure the module
  gsm.Echo(1);               //enable AT echo
}
 
void loop()
{
 
    int call;
    call=gsm.CallStatus();
    switch (call){
      case CALL_NONE:
        Serial.println("no call");
        break;
      case CALL_INCOM_VOICE:
        Serial.println("incoming voice call");
        delay(5000);
        gsm.PickUp();
        break;
      case CALL_ACTIVE_VOICE:
        Serial.println("active voice call");
        delay(5000);
        gsm.HangUp();
        break;
      case CALL_NO_RESPONSE:
        Serial.println("no response");
        break;
    }
    delay(1000);
 
}

 

 

Download GSM_Shield_Library

 

New GSM/GPRS library

 

 

 

 

 

Bigger example

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* GSM Shield example
 
 created 2011
 by Boris Landoni
 
 This example code is in the public domain.
 
 
 
 
 */
 
#include <GSM_Shield.h>
 
//**************************************************************************
char number[]="+39123456789"//Destination number
char text[]="hello world"//SMS to send
byte type_sms=SMS_UNREAD;      //Type of SMS
byte del_sms=0;                //0: No deleting sms - 1: Deleting SMS
//**************************************************************************
 
GSM gsm;
char sms_rx[122]; //Received text SMS
//int inByte=0;    //Number of byte received on serial port
char number_incoming[20];
int call;
int error;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("system startup");
  gsm.TurnOn(9600);          //module power on
  gsm.InitParam(PARAM_SET_1);//configure the module
  gsm.Echo(0);               //enable AT echo
 
}
 
void loop()
{
  char inSerial[5];
  int i=0;
  delay(2000);
 
    Check_Call(); //Check if there is an incoming call
    Check_SMS();  //Check if there is SMS
    //Check data serial com
 
    if (Serial.available() > 0)
    {
       while (Serial.available() > 0) {
         inSerial[i]=(Serial.read()); //read data
         i++;
       }
       inSerial[i]='\0';
      Check_Protocol(inSerial);
    }
 
 
void Check_Protocol(String inStr)
{
       Serial.print("Command: ");
       Serial.println(inStr);
 
  Serial.println("Check_Protocol");