This library is archived and is no longer being maintained. It can be still be downloaded and used, but is read-only and cannot be contributed to. For more information, you can view this repository on GitHub.
The GSM Library is included with Arduino IDE 1.0.4 and later.
With the Arduino GSM Shield, this library enables an Arduino board to do most of the operations you can do with a GSM phone: place and receive voice calls, send and receive SMS, and connect to the internet over a GPRS network.
The GSM shield has a modem that transfers data from a serial port to the GSM network. The modem executes operations via a series of AT commands. The library abstracts low level communications between the modem and SIM card. It relies on the Software Serial library for communication between the modem and Arduino.
Typically, each individual command is part of a larger series necessary to execute a particular function. The library can also receive information and return it to you when necessary.
To use this library
1#include <GSM.h>
As the library enables multiple types of functionality, there are a number of different classes.
The library tries to be as compatible as possible with the current Ethernet library. Porting a program from an Arduino Ethernet or WiFi library to an Arduino with the GSM Shield should be fairly easy. While it is not possible to simply run Ethernet-compatible code on the GSM shield as-is, some minor, library specific, modifications will be necessary, like including the GSM and GPRS specific libraries and getting network configuration settings from your cellular network provider.
There are two groups of examples for the GSM shield. There are examples to illustrate the possibilities of the shield, like how to send SMS messages and connect to the internet. There is also set of example tools that you can use to debug the functionality of the library and the hardware at lower level.
GSM constructor
GSM is the base class for all GSM based functions.
1GSM GSMAccess2GSM GSMAccess(debug)
debug: boolean (default FALSE) flag for turing on the debug mode. This will print out the AT commands from the modem.
1// libraries2#include <GSM.h>3
4// PIN Number5#define PINNUMBER ""6
7// initialize the library instance8GSM gsmAccess; // include a 'true' parameter for debug enabled9
10void setup()11{12 // initialize serial communications13 Serial.begin(9600);14
15 // connection state16 boolean notConnected = true;17
18 // Start GSM shield19 // If your SIM has PIN, pass it as a parameter of begin() in quotes20 while(notConnected)21 {22 if(gsmAccess.begin(PINNUMBER)==GSM_READY){23 notConnected = false;24 Serial.println("Connected to network");25 }26 else27 {28 Serial.println("Not connected");29 delay(1000);30 }31 }32}33
34void loop()35{36 // Nothing here37}
begin()
Connects to the GSM network identified on the SIM card.
1gsm.begin()2gsm.begin(pin)3gsm.begin(pin, restart)4gsm.begin(pin, restart, sync)
0 if asynchronous. If synchronous, returns status : ERROR, IDLE, CONNECTING, GSM_READY, GPRS_READY, TRANSPARENT_CONNECTED
1#define PINNUMBER ""2
3GSM gsm; // include a 'true' parameter for debug enabled4
5void setup()6{7 // initialize serial communications8 Serial.begin(9600);9
10 // connection state11 boolean notConnected = true;12
13 // Start GSM shield14 // If your SIM has PIN, pass it as a parameter of begin() in quotes15 while(notConnected)16 {17 if(gsm.begin(PINNUMBER)==GSM_READY)18 notConnected = false;19 else20 {21 Serial.println("Not connected");22 delay(1000);23 }24 }25
26 Serial.println("GSM initialized");27}28
29void loop()30{31// once connected do something interesting32}
shutdown()
Disconnects from the GSM network identified on the SIM card by powering the modem off.
1gsm.shutdown()
none
boolean : 0 while executing, 1 when successful
1#define PINNUMBER ""2
3GSM gsm; // include a 'true' parameter for debug enabled4
5void setup()6{7 // initialize serial communications8 Serial.begin(9600);9
10 // connection state11 boolean notConnected = true;12
13 // Start GSM shield14 // If your SIM has PIN, pass it as a parameter of begin() in quotes15 while(notConnected)16 {17 if(gsm.begin(PINNUMBER)==GSM_READY)18 notConnected = false;19 else20 {21 Serial.println("Not connected");22 delay(1000);23 }24 }25
26 Serial.println("GSM initialized");27
28 gsm.shutdown();29 Serial.println("GSM terminated");30
31}32
33void loop()34{35}
GSMVoiceCall constructor
GSMVoiceCall is the base class for all GSM functions relating to receiving and making voice calls.
getVoiceCallStatus()
Returns status of the voice call.
1voice.getVoiceCallStatus()
none
char : IDLE_CALL, CALLING, RECEIVINGCALL, TALKING
1// libraries2#include <GSM.h>3
4// PIN Number5#define PINNUMBER ""6
7// initialize the library instance8GSM gsmAccess; // include a 'true' parameter for debug enabled9GSMVoiceCall vcs;10
11
12char numtel[20]; // buffer for the incoming call13
14void setup()15{16 // initialize serial communications17 Serial.begin(9600);18 Serial.println("Receive Voice Call");19
20 // connection state21 boolean notConnected = true;22
23 // Start GSM shield24 // If your SIM has PIN, pass it as a parameter of begin() in quotes25 while(notConnected)26 {27 if(gsmAccess.begin(PINNUMBER)==GSM_READY)28 notConnected = false;29 else30 {31 Serial.println("Not connected");32 delay(1000);33 }34 }35
36 // This makes sure the modem notifies correctly incoming events37 vcs.hangCall();38
39 Serial.println("Waiting Call");40}41
42void loop()43{44 // Check the status of the voice call45 switch (vcs.getvoiceCallStatus())46 {47 case IDLE_CALL: // Nothing is happening48
49 break;50
51 case CALLING: // This should never happen, as we are not placing a call52
53 Serial.println("CALLING");54 break;55
56 case RECEIVINGCALL: // Yes! Someone is calling us57
58 Serial.println("RECEIVING CALL");59
60 // Retrieve the calling number61 vcs.retrieveCallingNumber(numtel, 20);62
63 // Print the calling number64 Serial.print("Number:");65 Serial.println(numtel);66
67 // Answer the call, establish the call68 vcs.answerCall(); 69 break;70
71 case TALKING: // In this case the call would be established72
73 Serial.println("TALKING. Enter line to interrupt.");74 while(Serial.read()!='\n')75 delay(100);76 vcs.hangCall();77 Serial.println("HANG. Waiting Call."); 78 break;79 }80 delay(1000);81}
ready()
Reports if the previous voice command has executed successfully
1voice.ready()
none
int In asynchronous mode, ready() returns 0 if the last command is still executing, 1 if there is success, and >1 in case of an error. In synchronous mode, it returns 1 if it executed successfully, 0 if not.
voiceCall()
Places a voice call to a specified number. The methods returns different information depending on the GSM connection mode (synchronous or asynchronous). See below for details.
1voice.voiceCall(number)
number : char array. The number to call.
int In asynchronous mode, voiceCall() returns 0 if last command is still executing, 1 if successful, and >1 in case of an error. In synchronous mode, it returns 1 if the call is placed, 0 if not.
1// PIN Number2#define PINNUMBER ""3
4// initialize the library instance5GSM gsmAccess; // include a 'true' parameter for debug enabled6GSMVoiceCall vcs;7
8String remoteNumber = ""; // the number you will call9char charbuffer[20];10
11void setup()12{13
14 // initialize serial communications15 Serial.begin(9600);16
17 Serial.println("Make Voice Call");18
19 // connection state20 boolean notConnected = true;21
22 // Start GSM shield23 // If your SIM has PIN, pass it as a parameter of begin() in quotes24 while(notConnected)25 {26 if(gsmAccess.begin(PINNUMBER)==GSM_READY)27 notConnected = false;28 else29 {30 Serial.println("Not connected");31 delay(1000);32 }33 }34
35 Serial.println("GSM initialized.");36 Serial.println("Enter phone number to call.");37
38}39
40void loop()41{42
43 // add any incoming characters to the String:44 while (Serial.available() > 0)45 {46 char inChar = Serial.read();47 // if it's a newline, that means you should make the call:48 if (inChar == '\n')49 {50 // make sure the phone number is not too long:51 if (remoteNumber.length() < 20)52 {53 // let the user know you're calling:54 Serial.print("Calling to : ");55 Serial.println(remoteNumber);56 Serial.println();57
58 // Call the remote number59 remoteNumber.toCharArray(charbuffer, 20);60
61
62 // Check if the receiving end has picked up the call63 if(vcs.voiceCall(charbuffer))64 {65 Serial.println("Call Established. Enter line to end");66 // Wait for some input from the line67 while(Serial.read() !='\n' && (vcs.getvoiceCallStatus()==TALKING)); 68 // And hang up69 vcs.hangCall();70 }71 Serial.println("Call Finished");72 remoteNumber="";73 Serial.println("Enter phone number to call.");74 }75 else76 {77 Serial.println("That's too long for a phone number. I'm forgetting it");78 remoteNumber = "";79 }80 }81 else82 {83 // add the latest character to the message to send:84 if(inChar!='\r')85 remoteNumber += inChar;86 }87 }88}
answerCall()
Accepts an incoming voice call. The method returns are different depending on the modem mode (asynchronous or synchronous), see below for details.
1voice.answerCall()
none
int In asynchronous mode, answerCall() returns 0 if last command is still executing, 1 if successful, and >1 in case of an error. In synchronous mode, it returns 1 if the call is answered, 0 if not.
1// PIN Number2#define PINNUMBER ""3
4// initialize the library instance5GSM gsmAccess; // include a 'true' parameter for debug enabled6GSMVoiceCall vcs;7
8char numtel[20]; // buffer for the incoming call9
10void setup()11{12 // initialize serial communications13 Serial.begin(9600);14 Serial.println("Receive Voice Call");15
16 // connection state17 boolean notConnected = true;18
19 // Start GSM shield20 // If your SIM has PIN, pass it as a parameter of begin() in quotes21 while(notConnected)22 {23 if(gsmAccess.begin(PINNUMBER)==GSM_READY)24 notConnected = false;25 else26 {27 Serial.println("Not connected");28 delay(1000);29 }30 }31
32 // This makes sure the modem notifies correctly incoming events33 vcs.hangCall();34
35 Serial.println("Waiting Call");36}37
38void loop()39{40 // Check the status of the voice call41 switch (vcs.getvoiceCallStatus())42 {43 case IDLE_CALL: // Nothing is happening44
45 break;46
47 case CALLING: // This should never happen, as we are not placing a call48
49 Serial.println("CALLING");50 break;51
52 case RECEIVINGCALL: // Yes! Someone is calling us53
54 Serial.println("RECEIVING CALL");55
56 // Retrieve the calling number57 vcs.retrieveCallingNumber(numtel, 20);58
59 // Print the calling number60 Serial.print("Number:");61 Serial.println(numtel);62
63 // Answer the call, establish the call64 vcs.answerCall(); 65 break;66
67 case TALKING: // In this case the call would be established68
69 Serial.println("TALKING. Enter line to interrupt.");70 while(Serial.read()!='\n')71 delay(100);72 vcs.hangCall();73 Serial.println("HANG. Waiting Call."); 74 break;75 }76 delay(1000);77}
hangCall()
Hang up an established call or during incoming rings. Depending on the modems mode (synchronous or asynchronous) the method will return differently, see below for more detail.
1voice.hangCall()
none
int In asynchronous mode, hangCall() returns 0 if the last command is still executing, 1 if there is success, and >1 in case of an error. In synchronous mode, it returns 1 if the call is hung, 0 if not.
1// libraries2#include <GSM.h>3
4// PIN Number5#define PINNUMBER ""6
7// initialize the library instance8GSM gsmAccess; // include a 'true' parameter for debug enabled9GSMVoiceCall vcs;10
11
12char numtel[20]; // buffer for the incoming call13
14void setup()15{16 // initialize serial communications17 Serial.begin(9600);18 Serial.println("Receive Voice Call");19
20 // connection state21 boolean notConnected = true;22
23 // Start GSM shield24 // If your SIM has PIN, pass it as a parameter of begin() in quotes25 while(notConnected)26 {27 if(gsmAccess.begin(PINNUMBER)==GSM_READY)28 notConnected = false;29 else30 {31 Serial.println("Not connected");32 delay(1000);33 }34 }35
36 // This makes sure the modem notifies correctly incoming events37 vcs.hangCall();38
39 Serial.println("Waiting Call");40}41
42void loop()43{44 // Check the status of the voice call45 switch (vcs.getvoiceCallStatus())46 {47 case IDLE_CALL: // Nothing is happening48
49 break;50
51 case CALLING: // This should never happen, as we are not placing a call52
53 Serial.println("CALLING");54 break;55
56 case RECEIVINGCALL: // Yes! Someone is calling us57
58 Serial.println("RECEIVING CALL");59
60 // Retrieve the calling number61 vcs.retrieveCallingNumber(numtel, 20);62
63 // Print the calling number64 Serial.print("Number:");65 Serial.println(numtel);66
67 // Answer the call, establish the call68 vcs.answerCall(); 69 break;70
71 case TALKING: // In this case the call would be established72
73 Serial.println("TALKING. Enter line to interrupt.");74 while(Serial.read()!='\n')75 delay(100);76 vcs.hangCall();77 Serial.println("HANG. Waiting Call."); 78 break;79 }80 delay(1000);81}
retrieveCallingNumber()
Retrieves the calling number, and stores it.
1voice.retrieveCallingNumber(number, size)
int In asynchronous mode, retrieveCallingNumber() returns 0 if the last command is still executing, 1 if success, and >1 if there is an error. In synchronous mode, it returns 1 if the number is obtained 0 if not.
1// PIN Number2#define PINNUMBER ""3
4// initialize the library instance5GSM gsmAccess; // include a 'true' parameter for debug enabled6GSMVoiceCall vcs;7
8char numtel[20]; // buffer for the incoming call9
10void setup()11{12 // initialize serial communications13 Serial.begin(9600);14 Serial.println("Receive Voice Call");15
16 // connection state17 boolean notConnected = true;18
19 // Start GSM shield20 // If your SIM has PIN, pass it as a parameter of begin() in quotes21 while(notConnected)22 {23 if(gsmAccess.begin(PINNUMBER)==GSM_READY)24 notConnected = false;25 else26 {27 Serial.println("Not connected");28 delay(1000);29 }30 }31
32 // This makes sure the modem notifies correctly incoming events33 vcs.hangCall();34
35 Serial.println("Waiting Call");36}37
38void loop()39{40 // Check the status of the voice call41 switch (vcs.getvoiceCallStatus())42 {43 case IDLE_CALL: // Nothing is happening44
45 break;46
47 case CALLING: // This should never happen, as we are not placing a call48
49 Serial.println("CALLING");50 break;51
52 case RECEIVINGCALL: // Yes! Someone is calling us53
54 Serial.println("RECEIVING CALL");55
56 // Retrieve the calling number57 vcs.retrieveCallingNumber(numtel, 20);58
59 // Print the calling number60 Serial.print("Number:");61 Serial.println(numtel);62
63 // Answer the call, establish the call64 vcs.answerCall(); 65 break;66
67 case TALKING: // In this case the call would be established68
69 Serial.println("TALKING. Enter line to interrupt.");70 while(Serial.read()!='\n')71 delay(100);72 vcs.hangCall();73 Serial.println("HANG. Waiting Call."); 74 break;75 }76 delay(1000);77}
GSM_SMS constructor
GSM_SMS is the base class for all GSM functions relating to receiving and sending SMS messages.
beginSMS()
Identifies the telephone number to send an SMS message to.
1SMS.beginSMS(number)
number : char array, the phone number to send the SMS to
int In asynchronous mode, beginSMS() returns 0 if the last command is still executing, 1 if success, and >1 if there is an error. In synchronous mode, it returns 1 if it successfully executes, and 0 if it does not.
1#define PINNUMBER ""2
3// initialize the library instance4GSM gsmAccess; // include a 'true' parameter for debug enabled5GSM_SMS sms;6
7void setup()8{9 // initialize serial communications10 Serial.begin(9600);11
12 Serial.println("SMS Messages Sender");13
14 // connection state15 boolean notConnected = true;16
17 // Start GSM shield18 // If your SIM has PIN, pass it as a parameter of begin() in quotes19 while(notConnected)20 {21 if(gsmAccess.begin(PINNUMBER)==GSM_READY)22 notConnected = false;23 else24 {25 Serial.println("Not connected");26 delay(1000);27 }28 }29
30 Serial.println("GSM initialized");31}32
33void loop()34{35
36 Serial.print("Enter a mobile number: ");37 char remoteNumber[20]; // telephone number to send sms38 readSerial(remoteNumber);39 Serial.println(remoteNumber);40
41 // sms text42 Serial.print("Now, enter SMS content: ");43 char txtMsg[200];44 readSerial(txtMsg);45 Serial.println("SENDING");46 Serial.println();47 Serial.println("Message:");48 Serial.println(txtMsg);49
50 // send the message51 sms.beginSMS(remoteNumber);52 sms.print(txtMsg);53 sms.endSMS();54 Serial.println("\nCOMPLETE!\n");55}56
57/*58 Read input serial59 */60int readSerial(char result[])61{62 int i = 0;63 while(1)64 {65 while (Serial.available() > 0)66 {67 char inChar = Serial.read();68 if (inChar == '\n')69 {70 result[i] = '\0';71 Serial.flush();72 return 0;73 }74 if(inChar!='\r')75 {76 result[i] = inChar;77 i++;78 }79 }80 }81}
ready()
Gets the status if the last GSMSMS command.
1SMS.ready()
none
int In asynchronous mode, ready() returns 0 if the last command is still executing, 1 if it was successful, and >1 if there is an error. In synchronous mode, it returns 1 if the previous successfully executed, and 0 if it has not.
endSMS()
Tells the modem that the SMS message is complete and sends it.
1SMS.endSMS()
none
int In asynchronous mode, endSMS() returns 0 if it is still executing, 1 if successful, and >1 if there is an error. In synchronous mode, it returns 1 if the previous successfully executed, and 0 if it has not.
1#define PINNUMBER ""2
3// initialize the library instance4GSM gsmAccess; // include a 'true' parameter for debug enabled5GSM_SMS sms;6
7void setup()8{9 // initialize serial communications10 Serial.begin(9600);11
12 Serial.println("SMS Messages Sender");13
14 // connection state15 boolean notConnected = true;16
17 // Start GSM shield18 // If your SIM has PIN, pass it as a parameter of begin() in quotes19 while(notConnected)20 {21 if(gsmAccess.begin(PINNUMBER)==GSM_READY)22 notConnected = false;23 else24 {25 Serial.println("Not connected");26 delay(1000);27 }28 }29
30 Serial.println("GSM initialized");31}32
33void loop()34{35
36 Serial.print("Enter a mobile number: ");37 char remoteNumber[20]; // telephone number to send sms38 readSerial(remoteNumber);39 Serial.println(remoteNumber);40
41 // sms text42 Serial.print("Now, enter SMS content: ");43 char txtMsg[200];44 readSerial(txtMsg);45 Serial.println("SENDING");46 Serial.println();47 Serial.println("Message:");48 Serial.println(txtMsg);49
50 // send the message51 sms.beginSMS(remoteNumber);52 sms.print(txtMsg);53 sms.endSMS();54 Serial.println("\nCOMPLETE!\n");55}56
57/*58 Read input serial59 */60int readSerial(char result[])61{62 int i = 0;63 while(1)64 {65 while (Serial.available() > 0)66 {67 char inChar = Serial.read();68 if (inChar == '\n')69 {70 result[i] = '\0';71 Serial.flush();72 return 0;73 }74 if(inChar!='\r')75 {76 result[i] = inChar;77 i++;78 }79 }80 }81}
available()
Checks to see if there is a SMS messages on the SIM card to be read, and reports back the number of characters in the message.
1SMS.available()
none
int : the number of characters in the message
1// initialize the library instance2GSM gsmAccess; // include a 'true' parameter for debug enabled3GSM_SMS sms;4
5char remoteNumber[20]; // Holds the emitting number6
7void setup()8{9 // initialize serial communications10 Serial.begin(9600);11
12 Serial.println("SMS Messages Receiver");13
14 // connection state15 boolean notConnected = true;16
17 // Start GSM shield18 // If your SIM has PIN, pass it as a parameter of begin() in quotes19 while(notConnected)20 {21 if(gsmAccess.begin(PINNUMBER)==GSM_READY)22 notConnected = false;23 else24 {25 Serial.println("Not connected");26 delay(1000);27 }28 }29
30 Serial.println("GSM initialized");31 Serial.println("Waiting for messages");32}33
34void loop()35{36 char c;37
38 // If there are any SMSs available() 39 if (sms.available())40 {41 Serial.println("Message received from:");42
43 // Get remote number44 sms.remoteNumber(remoteNumber, 20);45 Serial.println(remoteNumber);46
47 // This is just an example of message disposal 48 // Messages starting with # should be discarded49 if(sms.peek()=='#')50 {51 Serial.println("Discarded SMS");52 sms.flush();53 }54
55 // Read message bytes and print them56 while(c=sms.read())57 Serial.print(c);58
59 Serial.println("\nEND OF MESSAGE");60
61 // delete message from modem memory62 sms.flush();63 Serial.println("MESSAGE DELETED");64 }65
66 delay(1000);67
68}
remoteNumber()
Retrieves the phone number an from an incoming SMS message and stores it in a named array.
1SMS.remoteNumber(number, size)
int In asynchronous mode, remoteNumber() returns 0 if the last command is still executing, 1 if success, and >1 if there is an error. In synchronous mode, it returns 1 if it successfully executes, and 0 if it does not.
1// PIN Number2#define PINNUMBER ""3
4// initialize the library instance5GSM gsmAccess; // include a 'true' parameter for debug enabled6GSM_SMS sms;7
8char remoteNumber[20]; // Holds the emitting number9
10void setup()11{12 // initialize serial communications13 Serial.begin(9600);14
15 Serial.println("SMS Messages Receiver");16
17 // connection state18 boolean notConnected = true;19
20 // Start GSM shield21 // If your SIM has PIN, pass it as a parameter of begin() in quotes22 while(notConnected)23 {24 if(gsmAccess.begin(PINNUMBER)==GSM_READY)25 notConnected = false;26 else27 {28 Serial.println("Not connected");29 delay(1000);30 }31 }32
33 Serial.println("GSM initialized");34 Serial.println("Waiting for messages");35}36
37void loop()38{39 char c;40
41 // If there are any SMSs available() 42 if (sms.available())43 {44 Serial.println("Message received from:");45
46 // Get remote number47 sms.remoteNumber(remoteNumber, 20);48 Serial.println(remoteNumber);49
50 // This is just an example of message disposal 51 // Messages starting with # should be discarded52 if(sms.peek()=='#')53 {54 Serial.println("Discarded SMS");55 sms.flush();56 }57
58 // Read message bytes and print them59 while(c=sms.read())60 Serial.print(c);61
62 Serial.println("\nEND OF MESSAGE");63
64 // delete message from modem memory65 sms.flush();66 Serial.println("MESSAGE DELETED");67 }68
69 delay(1000);70
71}
read()
Reads a byte from an SMS message. read() inherits from the Stream utility class.
1SMS.read()
none
int - the first byte of incoming serial data available (or -1 if no data is available)
1// PIN Number2#define PINNUMBER ""3
4// initialize the library instance5GSM gsmAccess; // include a 'true' parameter for debug enabled6GSM_SMS sms;7
8char remoteNumber[20]; // Holds the emitting number9
10void setup()11{12 // initialize serial communications13 Serial.begin(9600);14
15 Serial.println("SMS Messages Receiver");16
17 // connection state18 boolean notConnected = true;19
20 // Start GSM shield21 // If your SIM has PIN, pass it as a parameter of begin() in quotes22 while(notConnected)23 {24 if(gsmAccess.begin(PINNUMBER)==GSM_READY)25 notConnected = false;26 else27 {28 Serial.println("Not connected");29 delay(1000);30 }31 }32
33 Serial.println("GSM initialized");34 Serial.println("Waiting for messages");35}36
37void loop()38{39 char c;40
41 // If there are any SMSs available() 42 if (sms.available())43 {44 Serial.println("Message received from:");45
46 // Get remote number47 sms.remoteNumber(remoteNumber, 20);48 Serial.println(remoteNumber);49
50 // This is just an example of message disposal 51 // Messages starting with # should be discarded52 if(sms.peek()=='#')53 {54 Serial.println("Discarded SMS");55 sms.flush();56 }57
58 // Read message bytes and print them59 while(c=sms.read())60 Serial.print(c);61
62 Serial.println("\nEND OF MESSAGE");63
64 // delete message from modem memory65 sms.flush();66 Serial.println("MESSAGE DELETED");67 }68
69 delay(1000);70
71}
write()
Writes a character to a SMS message.
1SMS.write(val)
val: a character to send in the message
byte - write() will return the number of bytes written, though reading that number is optional
1#define PINNUMBER ""2
3// initialize the library instance4GSM gsmAccess; // include a 'true' parameter for debug enabled5GSM_SMS sms;6
7void setup()8{9 // initialize serial communications10 Serial.begin(9600);11
12 Serial.println("SMS Messages Sender");13
14 // connection state15 boolean notConnected = true;16
17 // Start GSM shield18 // If your SIM has PIN, pass it as a parameter of begin() in quotes19 while(notConnected)20 {21 if(gsmAccess.begin(PINNUMBER)==GSM_READY)22 notConnected = false;23 else24 {25 Serial.println("Not connected");26 delay(1000);27 }28 }29
30 Serial.println("GSM initialized");31}32
33void loop()34{35
36 Serial.print("Enter a mobile number: ");37 char remoteNumber[20]; // telephone number to send sms38 readSerial(remoteNumber);39 Serial.println(remoteNumber);40
41 // sms text42 Serial.print("Now, enter SMS content: ");43 char txtMsg[200];44 readSerial(txtMsg);45 Serial.println("SENDING");46 Serial.println();47 Serial.println("Message:");48 Serial.println(txtMsg);49
50 // send the message51 sms.beginSMS(remoteNumber);52 sms.print(txtMsg);53 sms.endSMS();54 Serial.println("\nCOMPLETE!\n");55}56
57/*58 Read input serial59 */60int readSerial(char result[])61{62 int i = 0;63 while(1)64 {65 while (Serial.available() > 0)66 {67 char inChar = Serial.read();68 if (inChar == '\n')69 {70 result[i] = '\0';71 Serial.flush();72 return 0;73 }74 if(inChar!='\r')75 {76 result[i] = inChar;77 i++;78 }79 }80 }81}
print()
Writes a char array as a SMS message.
1SMS.print(message)
message - char array, the message to send
int : the number of bytes printed
1#define PINNUMBER ""2
3// initialize the library instance4GSM gsmAccess; // include a 'true' parameter for debug enabled5GSM_SMS sms;6
7void setup()8{9 // initialize serial communications10 Serial.begin(9600);11
12 Serial.println("SMS Messages Sender");13
14 // connection state15 boolean notConnected = true;16
17 // Start GSM shield18 // If your SIM has PIN, pass it as a parameter of begin() in quotes19 while(notConnected)20 {21 if(gsmAccess.begin(PINNUMBER)==GSM_READY)22 notConnected = false;23 else24 {25 Serial.println("Not connected");26 delay(1000);27 }28 }29
30 Serial.println("GSM initialized");31}32
33void loop()34{35
36 Serial.print("Enter a mobile number: ");37 char remoteNumber[20]; // telephone number to send sms38 readSerial(remoteNumber);39 Serial.println(remoteNumber);40
41 // sms text42 Serial.print("Now, enter SMS content: ");43 char txtMsg[200];44 readSerial(txtMsg);45 Serial.println("SENDING");46 Serial.println();47 Serial.println("Message:");48 Serial.println(txtMsg);49
50 // send the message51 sms.beginSMS(remoteNumber);52 sms.print(txtMsg);53 sms.endSMS();54 Serial.println("\nCOMPLETE!\n");55}56
57/*58 Read input serial59 */60int readSerial(char result[])61{62 int i = 0;63 while(1)64 {65 while (Serial.available() > 0)66 {67 char inChar = Serial.read();68 if (inChar == '\n')69 {70 result[i] = '\0';71 Serial.flush();72 return 0;73 }74 if(inChar!='\r')75 {76 result[i] = inChar;77 i++;78 }79 }80 }81}
peek()
Returns the next byte (character) of an incoming SMS without removing it from the message. That is, successive calls to peek() will return the same character, as will the next call to read(). peek() inherits from the Stream utility class.
1SMS.peek()
none
int - the first byte available of a SMS message (or -1 if no data is available)
1SMS receiver2
3 This sketch, for the Arduino GSM shield, waits for SMS messages4 and displays them through the Serial port.5
6 Circuit:7 * GSM shield8
9 created 25 Feb 201210 by Javier Zorzano / TD11
12 This example is in the public domain.13*/14
15// libraries16#include <GSM.h>17
18// PIN Number19#define PINNUMBER ""20
21// initialize the library instance22GSM gsmAccess; // include a 'true' parameter for debug enabled23GSM_SMS sms;24
25char remoteNumber[20]; // Holds the emitting number26
27void setup()28{29 // initialize serial communications30 Serial.begin(9600);31
32 Serial.println("SMS Messages Receiver");33
34 // connection state35 boolean notConnected = true;36
37 // Start GSM shield38 // If your SIM has PIN, pass it as a parameter of begin() in quotes39 while(notConnected)40 {41 if(gsmAccess.begin(PINNUMBER)==GSM_READY)42 notConnected = false;43 else44 {45 Serial.println("Not connected");46 delay(1000);47 }48 }49
50 Serial.println("GSM initialized");51 Serial.println("Waiting for messages");52}53
54void loop()55{56 char c;57
58 // If there are any SMSs available() 59 if (sms.available())60 {61 Serial.println("Message received from:");62
63 // Get remote number64 sms.remoteNumber(remoteNumber, 20);65 Serial.println(remoteNumber);66
67 // This is just an example of message disposal 68 // Messages starting with # should be discarded69 if(sms.peek()=='#')70 {71 Serial.println("Discarded SMS");72 sms.flush();73 }74
75 // Read message bytes and print them76 while(c=sms.read())77 Serial.print(c);78
79 Serial.println("\nEND OF MESSAGE");80
81 // delete message from modem memory82 sms.flush();83 Serial.println("MESSAGE DELETED");84 }85
86 delay(1000);87
88}
flush()
flush() clears the modem memory of any sent messages once all outgoing characters have been sent. flush() inherits from the Stream utility class.
1SMS.flush()
none
none
1SMS receiver2
3 This sketch, for the Arduino GSM shield, waits for SMS messages4 and displays them through the Serial port.5
6 Circuit:7 * GSM shield8
9 created 25 Feb 201210 by Javier Zorzano / TD11
12 This example is in the public domain.13*/14
15// libraries16#include <GSM.h>17
18// PIN Number19#define PINNUMBER ""20
21// initialize the library instance22GSM gsmAccess; // include a 'true' parameter for debug enabled23GSM_SMS sms;24
25char remoteNumber[20]; // Holds the emitting number26
27void setup()28{29 // initialize serial communications30 Serial.begin(9600);31
32 Serial.println("SMS Messages Receiver");33
34 // connection state35 boolean notConnected = true;36
37 // Start GSM shield38 // If your SIM has PIN, pass it as a parameter of begin() in quotes39 while(notConnected)40 {41 if(gsmAccess.begin(PINNUMBER)==GSM_READY)42 notConnected = false;43 else44 {45 Serial.println("Not connected");46 delay(1000);47 }48 }49
50 Serial.println("GSM initialized");51 Serial.println("Waiting for messages");52}53
54void loop()55{56 char c;57
58 // If there are any SMSs available() 59 if (sms.available())60 {61 Serial.println("Message received from:");62
63 // Get remote number64 sms.remoteNumber(remoteNumber, 20);65 Serial.println(remoteNumber);66
67 // This is just an example of message disposal 68 // Messages starting with # should be discarded69 if(sms.peek()=='#')70 {71 Serial.println("Discarded SMS");72 sms.flush();73 }74
75 // Read message bytes and print them76 while(c=sms.read())77 Serial.print(c);78
79 Serial.println("\nEND OF MESSAGE");80
81 // delete message from modem memory82 sms.flush();83 Serial.println("MESSAGE DELETED");84 }85
86 delay(1000);87
88}
GPRS constructor
GPRS is the base class for all GPRS functions, such as internet client and server behaviors.
attachGPRS()
Connects to the specified Access Point Name (APN) to initiate GPRS communication.
Every cellular provider has an Access Point Name (APN) that serves as a bridge between the cellular network and the internet. Sometimes, there is a username and password associated with the connection point. For example, the Bluevia APN is bluevia.movistar.es, but it has no password or login name.
This page lists a number of carrier's information, but it may not be up to date. You may need to get this information from your service provider.
1grps.attachGPRS(APN, user, password)
ERROR, IDLE, CONNECTING, GSM_READY, GPRS_READY, TRANSPARENT_CONNECTED
1// PIN Number2#define PINNUMBER ""3
4// APN data5#define GPRS_APN "GPRS_APN" // replace your GPRS APN6#define GPRS_LOGIN "login" // replace with your GPRS login7#define GPRS_PASSWORD "password" // replace with your GPRS password8
9
10// initialize the library instance11GPRS gprs;12GSM gsmAccess; // include a 'true' parameter for debug enabled13GSMServer server(80); // port 80 (http default)14
15// timeout16const unsigned long __TIMEOUT__ = 10*1000;17
18void setup()19{20 // initialize serial communications21 Serial.begin(9600);22
23 // connection state24 boolean notConnected = true;25
26 // Start GSM shield27 // If your SIM has PIN, pass it as a parameter of begin() in quotes28 while(notConnected)29 {30 if((gsmAccess.begin(PINNUMBER)==GSM_READY) &31 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))32 notConnected = false;33 else34 {35 Serial.println("Not connected");36 delay(1000);37 }38 }39
40 Serial.println("Connected to GPRS network");41
42 // start server43 server.begin();44
45 //Get IP.46 IPAddress LocalIP = gprs.getIPAddress();47 Serial.println("Server IP address=");48 Serial.println(LocalIP);49}50
51void loop() {52
53
54 // listen for incoming clients55 GSM3MobileClientService client = server.available();56
57
58
59 if (client)60 { 61 while (client.connected())62 {63 if (client.available())64 {65 Serial.println("Receiving request!");66 bool sendResponse = false;67 while(char c=client.read()) {68 if (c == '\n') sendResponse = true;69 }70
71 // if you've gotten to the end of the line (received a newline72 // character)73 if (sendResponse)74 {75 // send a standard http response header76 client.println("HTTP/1.1 200 OK");77 client.println("Content-Type: text/html");78 client.println();79 client.println("<html>");80 // output the value of each analog input pin81 for (int analogChannel = 0; analogChannel < 6; analogChannel++) {82 client.print("analog input ");83 client.print(analogChannel);84 client.print(" is ");85 client.print(analogRead(analogChannel));86 client.println("<br />"); 87 }88 client.println("</html>");89 //necessary delay90 delay(1000);91 client.stop();92 }93 }94 }95 }96}
Client
Client is the base class for all GPRS client based calls. It is not called directly, but invoked whenever you use a function that relies on it.
ready()
Gets the status of the last command
1client.ready()
none
In asynchronous mode, ready() returns 0 if the last command is still executing, 1 if it was successful, and >1 if there is an error. In synchronous mode, it returns 1 if the previous successfully executed, and 0 if it has not.
connect()
Connects to a specified IP address and port. The return value indicates success or failure.
1client.connect(ip, port)
boolean : Returns true if the connection succeeds, false if not.
1Web client2
3 This sketch connects to a website through a GSM shield. Specifically,4 this example downloads the URL "http://arduino.cc/" and prints it5 to the Serial monitor.6
7 Circuit:8 * GSM shield attached to an Arduino9 * SIM card with a data plan10
11 created 8 Mar 201212 by Tom Igoe13
14 http://arduino.cc/en/Tutorial/GSMExamplesWebClient15
16 */17
18// libraries19#include <GSM.h>20
21// PIN Number22#define PINNUMBER ""23
24// APN data25#define GPRS_APN "GPRS_APN" // replace your GPRS APN26#define GPRS_LOGIN "login" // replace with your GPRS login27#define GPRS_PASSWORD "password" // replace with your GPRS password28
29// initialize the library instance30GSMClient client;31GPRS gprs;32GSM gsmAccess;33
34// URL, path & port (for example: arduino.cc)35char server[] = "arduino.cc";36char path[] = "/";37int port = 80; // port 80 is the default for HTTP38
39void setup()40{41 // initialize serial communications42 Serial.begin(9600);43 Serial.println("Starting Arduino web client.");44 // connection state45 boolean notConnected = true;46
47 // After starting the modem with GSM.begin()48 // attach the shield to the GPRS network with the APN, login and password49 while(notConnected)50 {51 if((gsmAccess.begin(PINNUMBER)==GSM_READY) &52 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))53 notConnected = false;54 else55 {56 Serial.println("Not connected");57 delay(1000);58 }59 }60
61 Serial.println("connecting...");62
63 // if you get a connection, report back via serial:64 if (client.connect(server, port))65 {66 Serial.println("connected");67 // Make a HTTP request:68 client.print("GET ");69 client.print(path);70 client.println(" HTTP/1.0");71 client.println();72 }73 else74 {75 // if you didn't get a connection to the server:76 Serial.println("connection failed");77 }78}79
80void loop()81{82 // if there are incoming bytes available83 // from the server, read them and print them:84 if (client.available())85 {86 char c = client.read();87 Serial.print(c);88 }89
90 // if the server's disconnected, stop the client:91 if (!client.available() && !client.connected())92 {93 Serial.println();94 Serial.println("disconnecting.");95 client.stop();96
97 // do nothing forevermore:98 for(;;)99 ;100 }101}
beginWrite()
Tells the client to start writing to the server it is connected to.
1client.beginWrite()
none
none
write()
Write data to the server the client is connected to.
1client.write(data)2client.write(buffer)3client.write(buffer, size)
byte - write() returns the number of bytes written. It is not necessary to read this.
endWrite()
Stops writing data to a server
1client.endWrite()
none
none
connected()
Returns whether or not the client is connected. A client is considered connected if the connection has been closed but there is still unread data.
1client.connected()
none
boolean - Returns true if the client is connected, false if not.
1Web client2
3 This sketch connects to a website through a GSM shield. Specifically,4 this example downloads the URL "http://arduino.cc/" and prints it5 to the Serial monitor.6
7 Circuit:8 * GSM shield attached to an Arduino9 * SIM card with a data plan10
11 created 8 Mar 201212 by Tom Igoe13
14 http://arduino.cc/en/Tutorial/GSMExamplesWebClient15
16 */17
18// libraries19#include <GSM.h>20
21// PIN Number22#define PINNUMBER ""23
24// APN data25#define GPRS_APN "GPRS_APN" // replace your GPRS APN26#define GPRS_LOGIN "login" // replace with your GPRS login27#define GPRS_PASSWORD "password" // replace with your GPRS password28
29// initialize the library instance30GSMClient client;31GPRS gprs;32GSM gsmAccess;33
34// URL, path & port (for example: arduino.cc)35char server[] = "arduino.cc";36char path[] = "/";37int port = 80; // port 80 is the default for HTTP38
39void setup()40{41 // initialize serial communications42 Serial.begin(9600);43 Serial.println("Starting Arduino web client.");44 // connection state45 boolean notConnected = true;46
47 // After starting the modem with GSM.begin()48 // attach the shield to the GPRS network with the APN, login and password49 while(notConnected)50 {51 if((gsmAccess.begin(PINNUMBER)==GSM_READY) &52 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))53 notConnected = false;54 else55 {56 Serial.println("Not connected");57 delay(1000);58 }59 }60
61 Serial.println("connecting...");62
63 // if you get a connection, report back via serial:64 if (client.connect(server, port))65 {66 Serial.println("connected");67 // Make a HTTP request:68 client.print("GET ");69 client.print(path);70 client.println(" HTTP/1.0");71 client.println();72 }73 else74 {75 // if you didn't get a connection to the server:76 Serial.println("connection failed");77 }78}79
80void loop()81{82 // if there are incoming bytes available83 // from the server, read them and print them:84 if (client.available())85 {86 char c = client.read();87 Serial.print(c);88 }89
90 // if the server's disconnected, stop the client:91 if (!client.available() && !client.connected())92 {93 Serial.println();94 Serial.println("disconnecting.");95 client.stop();96
97 // do nothing forevermore:98 for(;;)99 ;100 }101}
read()
Read the next byte received from the server the client is connected to (after the last call to read()).
read() inherits from the Stream utility class.
1client.read()
none
int - The next byte (or character), or -1 if none is available.
1Web client2
3 This sketch connects to a website through a GSM shield. Specifically,4 this example downloads the URL "http://arduino.cc/" and prints it5 to the Serial monitor.6
7 Circuit:8 * GSM shield attached to an Arduino9 * SIM card with a data plan10
11 created 8 Mar 201212 by Tom Igoe13
14 http://arduino.cc/en/Tutorial/GSMExamplesWebClient15
16 */17
18// libraries19#include <GSM.h>20
21// PIN Number22#define PINNUMBER ""23
24// APN data25#define GPRS_APN "GPRS_APN" // replace your GPRS APN26#define GPRS_LOGIN "login" // replace with your GPRS login27#define GPRS_PASSWORD "password" // replace with your GPRS password28
29// initialize the library instance30GSMClient client;31GPRS gprs;32GSM gsmAccess;33
34// URL, path & port (for example: arduino.cc)35char server[] = "arduino.cc";36char path[] = "/";37int port = 80; // port 80 is the default for HTTP38
39void setup()40{41 // initialize serial communications42 Serial.begin(9600);43 Serial.println("Starting Arduino web client.");44 // connection state45 boolean notConnected = true;46
47 // After starting the modem with GSM.begin()48 // attach the shield to the GPRS network with the APN, login and password49 while(notConnected)50 {51 if((gsmAccess.begin(PINNUMBER)==GSM_READY) &52 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))53 notConnected = false;54 else55 {56 Serial.println("Not connected");57 delay(1000);58 }59 }60
61 Serial.println("connecting...");62
63 // if you get a connection, report back via serial:64 if (client.connect(server, port))65 {66 Serial.println("connected");67 // Make a HTTP request:68 client.print("GET ");69 client.print(path);70 client.println(" HTTP/1.0");71 client.println();72 }73 else74 {75 // if you didn't get a connection to the server:76 Serial.println("connection failed");77 }78}79
80void loop()81{82 // if there are incoming bytes available83 // from the server, read them and print them:84 if (client.available())85 {86 char c = client.read();87 Serial.print(c);88 }89
90 // if the server's disconnected, stop the client:91 if (!client.available() && !client.connected())92 {93 Serial.println();94 Serial.println("disconnecting.");95 client.stop();96
97 // do nothing forevermore:98 for(;;)99 ;100 }101}
available()
Returns the number of bytes available for reading (that is, the amount of data that has been written to the client by the server it is connected to).
available() inherits from the Stream utility class.
1client.available()
none
The number of bytes available.
1Web client2
3 This sketch connects to a website through a GSM shield. Specifically,4 this example downloads the URL "http://arduino.cc/" and prints it5 to the Serial monitor.6
7 Circuit:8 * GSM shield attached to an Arduino9 * SIM card with a data plan10
11 created 8 Mar 201212 by Tom Igoe13
14 http://arduino.cc/en/Tutorial/GSMExamplesWebClient15
16 */17
18// libraries19#include <GSM.h>20
21// PIN Number22#define PINNUMBER ""23
24// APN data25#define GPRS_APN "GPRS_APN" // replace your GPRS APN26#define GPRS_LOGIN "login" // replace with your GPRS login27#define GPRS_PASSWORD "password" // replace with your GPRS password28
29// initialize the library instance30GSMClient client;31GPRS gprs;32GSM gsmAccess;33
34// URL, path & port (for example: arduino.cc)35char server[] = "arduino.cc";36char path[] = "/";37int port = 80; // port 80 is the default for HTTP38
39void setup()40{41 // initialize serial communications42 Serial.begin(9600);43 Serial.println("Starting Arduino web client.");44 // connection state45 boolean notConnected = true;46
47 // After starting the modem with GSM.begin()48 // attach the shield to the GPRS network with the APN, login and password49 while(notConnected)50 {51 if((gsmAccess.begin(PINNUMBER)==GSM_READY) &52 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))53 notConnected = false;54 else55 {56 Serial.println("Not connected");57 delay(1000);58 }59 }60
61 Serial.println("connecting...");62
63 // if you get a connection, report back via serial:64 if (client.connect(server, port))65 {66 Serial.println("connected");67 // Make a HTTP request:68 client.print("GET ");69 client.print(path);70 client.println(" HTTP/1.0");71 client.println();72 }73 else74 {75 // if you didn't get a connection to the server:76 Serial.println("connection failed");77 }78}79
80void loop()81{82 // if there are incoming bytes available83 // from the server, read them and print them:84 if (client.available())85 {86 char c = client.read();87 Serial.print(c);88 }89
90 // if the server's disconnected, stop the client:91 if (!client.available() && !client.connected())92 {93 Serial.println();94 Serial.println("disconnecting.");95 client.stop();96
97 // do nothing forevermore:98 for(;;)99 ;100 }101}
peek()
Returns the next byte (character) of an incoming message removing it from the message. That is, successive calls to peek() will return the same character, as will the next call to read(). peek() inherits from the Stream utility class.
1client.peek()
none
int - the next byte in an incoming message.
flush()
Discards any bytes that have been written to the client but not yet read.
flush() inherits from the Stream utility class.
1client.flush()
none
none
stop()
Disconnects from the server
1client.stop()
none
none
1Web client2
3 This sketch connects to a website through a GSM shield. Specifically,4 this example downloads the URL "http://arduino.cc/" and prints it5 to the Serial monitor.6
7 Circuit:8 * GSM shield attached to an Arduino9 * SIM card with a data plan10
11 created 8 Mar 201212 by Tom Igoe13
14 http://arduino.cc/en/Tutorial/GSMExamplesWebClient15
16 */17
18// libraries19#include <GSM.h>20
21// PIN Number22#define PINNUMBER ""23
24// APN data25#define GPRS_APN "GPRS_APN" // replace your GPRS APN26#define GPRS_LOGIN "login" // replace with your GPRS login27#define GPRS_PASSWORD "password" // replace with your GPRS password28
29// initialize the library instance30GSMClient client;31GPRS gprs;32GSM gsmAccess;33
34// URL, path & port (for example: arduino.cc)35char server[] = "arduino.cc";36char path[] = "/";37int port = 80; // port 80 is the default for HTTP38
39void setup()40{41 // initialize serial communications42 Serial.begin(9600);43 Serial.println("Starting Arduino web client.");44 // connection state45 boolean notConnected = true;46
47 // After starting the modem with GSM.begin()48 // attach the shield to the GPRS network with the APN, login and password49 while(notConnected)50 {51 if((gsmAccess.begin(PINNUMBER)==GSM_READY) &52 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))53 notConnected = false;54 else55 {56 Serial.println("Not connected");57 delay(1000);58 }59 }60
61 Serial.println("connecting...");62
63 // if you get a connection, report back via serial:64 if (client.connect(server, port))65 {66 Serial.println("connected");67 // Make a HTTP request:68 client.print("GET ");69 client.print(path);70 client.println(" HTTP/1.0");71 client.println();72 }73 else74 {75 // if you didn't get a connection to the server:76 Serial.println("connection failed");77 }78}79
80void loop()81{82 // if there are incoming bytes available83 // from the server, read them and print them:84 if (client.available())85 {86 char c = client.read();87 Serial.print(c);88 }89
90 // if the server's disconnected, stop the client:91 if (!client.available() && !client.connected())92 {93 Serial.println();94 Serial.println("disconnecting.");95 client.stop();96
97 // do nothing forevermore:98 for(;;)99 ;100 }101}
Server
Server is the base class for all GPRS server based calls. It is not called directly, but invoked whenever you use a function that relies on it.
1GSMServer server(port);
port: int, the port the server will accept connections on. The default web port is 80.
ready()
Get last command status to the server
1server.ready()
none
int - 0 if last command is still executing, 1 if success, >1 if an error.
beginWrite()
Begins writing to connected clients.
1server.beginWrite()
none
none
write()
Write data to all the clients connected to a server.
1server.write(data)2server.write(buffer)3server.write(buffer, size)
byte - write() returns the number of bytes written. It is not necessary to read this.
endWrite()
Tells the server to stop writing to connected clients.
1server.endWrite()
none
none
read()
Read the next byte received from an attached client (after the last call to read()).
read() inherits from the Stream utility class.
1server.read()
none
int - The next byte (or character), or -1 if none is available.
available()
Listens for incoming clients
1server.available()
none
int : the number of connected clients
stop()
Tells the server to stop listening for incoming connections.
1server.stop()
none
none
GSMModem Constructor
GSMModem is the base class for calls that have specific diagnostic functionality with the modem. It is not called directly, but invoked whenever you use a function that relies on it.
begin()
Checks the modem status, and restarts it. Call this before GSMModem.getIMEI().
1modem.begin()
none
int : returns 1 if modem is OK, otherwise returns an error.
getIMEI()
Retrieves the modem's IMEI number. Call this after GSMModem.begin().
1modem.getIMEI()
none
String : the modem's IMEI number
1#include <GSM.h>2
3// modem verification object4GSMModem modem;5
6// IMEI variable7String IMEI = "";8
9void setup()10{11 // initialize serial communications12 Serial.begin(9600);13
14 // start modem test (reset and check response)15 Serial.print("Starting modem test...");16 if(modem.begin())17 Serial.println("modem.begin() succeeded");18 else19 Serial.println("ERROR, no modem answer.");20}21
22void loop()23{24 // get modem IMEI25 Serial.print("Checking IMEI...");26 IMEI = modem.getIMEI();27
28 // check IMEI response29 if(IMEI != NULL)30 {31 // show IMEI in serial monitor32 Serial.println("Modem's IMEI: " + IMEI);33 // reset modem to check booting:34 Serial.print("Resetting modem...");35 modem.begin();36 // get and check IMEI one more time37 if(modem.getIMEI() != NULL)38 {39 Serial.println("Modem is functioning properly");40 }41 else42 {43 Serial.println("Error: getIMEI() failed after modem.begin()");44 }45 }46 else47 {48 Serial.println("Error: Could not get IMEI");49 }50 // do nothing:51 while(true);52}
GSMScanner Constructor
GSMScanner is the base class for calls that have specific diagnostic functionality relating to scanning for available networks. It is not called directly, but invoked whenever you use a function that relies on it.
begin()
Resets modem hardware.
1scanner.begin()
none
int : returns 1 if modem is OK, otherwise returns an error.
getCurrentCarrier()
Gets and returns the name of the current network carrier.
1scanner.getCurrentCarrier()
none
String : name of the current network carrier
getSignalStrength()
Gets and returns the strength of the signal of the network the modem is attached to.
1scanner.getSignalStrength()
none
String : signal strength in 0-31 scale. 31 means power > 51dBm. 99=not detectable
readNetworks()
Searches for available carriers, and returns a list of them.
1scanner.readNetworks()
none
String : A string with list of networks available
GSMPIN constructor
GSMPIN is the base class for all GSM based functions that deal with interacting with the PIN on the SIM card.
begin()
Checks the modem status, and restarts it.
1GSMPIN.begin()
none
int : returns 1 if modem is OK, otherwise returns an error.
isPIN()
Checks the SIM card to see if it is locked with a PIN.
1pin.isPIN()
none
int : 0 if PIN lock is off, 1 if PIN lock is on, -1 if PUK lock is on, -2 if error exists.
checkPIN()
Queries the SIM card with a PIN number to see if it is valid.
1pin.checkPIN(PIN)
PIN : String with the PIN number to check
int : Returns 0 if the PIN is valid, returns -1 if it is not.
checkPUK()
Check the SIM if PUK code is correct and establish new PIN code.
1pin.checkPUK(puk, pin)
int : Returns 0 if successful, -1 if it is not.
changePIN()
Changes the PIN number of a SIM, after verifying the existing one.
1pin.changePIN(oldPIN, newPIN)
none
switchPIN()
Change PIN lock status.
1pin.switchPIN(pin)
pin : String with the existing PIN number
none
checkReg()
Check if modem was registered in GSM/GPRS network
1pin.checkReg()
none
int : 0 if modem was registered, 1 if modem was registered in roaming, -1 if error exists
getPinUsed()
Check if PIN lock is used.
1pin.getPinUsed()
none
boolean : TRUE id locked, FALSE if not
setPinUsed()
Set PIN lock status.
1pin.setPinUsed(used)
used : boolean, TRUE to lock the PIN, FALSE to unlock.
none
GSMBand Constructor
GSMBand is the base class for calls that have specific diagnostic functionality relating to the bands the modem can connect to. It is not called directly, but invoked whenever you use a function that relies on it.
begin()
Checks the modem status, and restarts it.
1band.begin()
none
int : returns 1 if modem is OK, otherwise returns an error.
getBand()
Gets and returns the frequency band the modem is currently connected to. Check http://www.worldtimezone.com/gsm.html for general GSM band information. Typical regional configurations are :
1band.getBand()
none
String : name of the frequency band the modem connects to
setBand()
Sets the frequency band the modem connects to. Check http://www.worldtimezone.com/gsm.html for general GSM band information. Typical regional configurations are :
1band.setBand(type)
type : String identifying what frequency band the modem should connect to :
boolean : returns true if the process is successful, false if it is not