This sketch tests the modem on the GSM shield to see if it is working correctly. You do not need a SIM card for this example.
Arduino Board
First, import the GSM library
#include <GSM.h>
Create an instance of the GSMModem class:
GSMModem modem;
Create a variable to hold the IMEI number of the modem
1String IMEI = "";
In
setup
, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.1void setup(){2
3 Serial.begin(9600);4
5 Serial.print("Starting modem test...");
Call
modem.begin()
to start the modem. Send a status message depending on the outcome, and end setup()
.1if(modem.begin())2
3 Serial.println("modem.begin() succeeded");4
5 else6
7 Serial.println("ERROR, no modem answer.");8}
Inside
loop
, use modem.getIMEI()
to return the IMEI number of the modem. This number is unique to your GSM shield.1void loop()2{3
4 // get modem IMEI5
6 Serial.print("Checking IMEI...");7
8 IMEI = modem.getIMEI();
If there is a valid response from
getIMEI()
, print it to the serial monitor and reset the modem with modem.begin()
.1if(IMEI != NULL)2
3 {4
5 // show IMEI in serial monitor6
7 Serial.println("Modem's IMEI: " + IMEI);8
9 // reset modem to check booting:10
11 Serial.print("Resetting modem...");12
13 modem.begin();
Once reset, check the IMEI again. If it is a valid return again, the modem is functioning as expected.
1if(modem.getIMEI() != NULL)2
3 {4
5 Serial.println("Modem is functioning properly");6
7 }
If, after resetting the modem, there is not a valid return from
getIMEI()
, report an error1else2
3 {4
5 Serial.println("Error: getIMEI() failed after modem.begin()");6
7 }
If you never received an IMEI after starting the sketch, report it, and end the program.
1}2
3 else4
5 {6
7 Serial.println("Error: Could not get IMEI");8
9 }10
11 // do nothing:12
13 while(true);14}
Once your code is uploaded, open the serial monitor. You should see the HTML of http://arduino.cc print out on screen when it is received.
The complete sketch is below.
1/*2
3 This example tests to see if the modem of the4
5 GSM shield is working correctly. You do not need6
7 a SIM card for this example.8
9 Circuit:10
11 * GSM shield attached12
13 Created 12 Jun 201214
15 by David del Peral16
17 modified 21 Nov 201218
19 by Tom Igoe20
21 http://www.arduino.cc/en/Tutorial/GSMToolsTestModem22
23 This sample code is part of the public domain24
25 */26
27// libraries28#include <GSM.h>29
30// modem verification object31
32GSMModem modem;33
34// IMEI variable35
36String IMEI = "";37
38void setup() {39
40 // initialize serial communications and wait for port to open:41
42 Serial.begin(9600);43
44 while (!Serial) {45
46 ; // wait for serial port to connect. Needed for Leonardo only47
48 }49
50 // start modem test (reset and check response)51
52 Serial.print("Starting modem test...");53
54 if (modem.begin()) {55
56 Serial.println("modem.begin() succeeded");57
58 } else {59
60 Serial.println("ERROR, no modem answer.");61
62 }63}64
65void loop() {66
67 // get modem IMEI68
69 Serial.print("Checking IMEI...");70
71 IMEI = modem.getIMEI();72
73 // check IMEI response74
75 if (IMEI != NULL) {76
77 // show IMEI in serial monitor78
79 Serial.println("Modem's IMEI: " + IMEI);80
81 // reset modem to check booting:82
83 Serial.print("Resetting modem...");84
85 modem.begin();86
87 // get and check IMEI one more time88
89 if (modem.getIMEI() != NULL) {90
91 Serial.println("Modem is functioning properly");92
93 } else {94
95 Serial.println("Error: getIMEI() failed after modem.begin()");96
97 }98
99 } else {100
101 Serial.println("Error: Could not get IMEI");102
103 }104
105 // do nothing:106
107 while (true);108}
Last revision 2018/08/23 by SM