MKRGSM Library Examples

A list of examples related to the MKRGSM Library, which can be used to make voice calls, send SMS and connect to website with a data enabled SIM card.

The Arduino MKR GSM 1400 is a powerful IoT board that communicates over the mobile network. In this article, you will find a lot of useful examples, such as sending SMS, making voice calls and making http requests. All examples are available in the MKRGSM library, which is available for download through the Arduino IDE library manager.

You can also visit the MKRGSM GitHub repository to learn more about this library.

Hardware & Software Required

  • Arduino MKR GSM 1400
  • Antenna
  • SIM card enable for Data
  • (optional) 6 potentiometers or other analog inputs attached to A0-A5

Circuit

Connect the antenna to the board.
Connect the antenna to the board.

Examples

MKR GSM GPRS Ping

This sketch connects an host and continuously ping it.

1/*
2
3 This uses an MKR GSM 1400 to continuously pings given host specified by IP Address or name.
4
5Circuit:
6
7* MKR GSM 1400 board
8
9* Antenna
10
11* SIM card with a data plan
12
13 created 06 Dec 2017
14
15 by Arturo Guadalupi
16
17*/
18#include <MKRGSM.h>
19
20#include "arduino_secrets.h"
21// Please enter your sensitive data in the Secret tab or arduino_secrets.h
22// PIN Number
23
24const char PINNUMBER[] = SECRET_PINNUMBER;
25// APN data
26
27const char GPRS_APN[] = SECRET_GPRS_APN;
28
29const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
30
31const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
32
33// initialize the library instance
34
35GSMSSLClient client;
36
37GPRS gprs;
38
39GSM gsmAccess;
40
41// Specify IP address or hostname
42
43String hostName = "www.google.com";
44int pingResult;
45
46void setup() {
47
48 // Initialize serial and wait for port to open:
49
50 Serial.begin(9600);
51
52 while (!Serial) {
53
54 ; // wait for serial port to connect. Needed for native USB port only
55
56 }
57
58 Serial.println("Starting Arduino GPRS ping.");
59
60 // connection state
61
62 bool connected = false;
63
64 // After starting the modem with GSM.begin()
65
66 // attach the shield to the GPRS network with the APN, login and password
67
68 while (!connected) {
69
70 if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
71
72 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
73
74 connected = true;
75
76 } else {
77
78 Serial.println("Not connected");
79
80 delay(1000);
81
82 }
83
84 }
85}
86
87void loop() {
88
89 Serial.print("Pinging ");
90
91 Serial.print(hostName);
92
93 Serial.print(": ");
94
95 pingResult = gprs.ping(hostName);
96
97 if (pingResult >= 0) {
98
99 Serial.print("SUCCESS! RTT = ");
100
101 Serial.print(pingResult);
102
103 Serial.println(" ms");
104
105 } else {
106
107 Serial.print("FAILED! Error code: ");
108
109 Serial.println(pingResult);
110
111 }
112
113 delay(5000);
114}

MKR GSM GPRS Udp Ntp

In this example, you will use your MKR GSM 1400, to query a Network Time Protocol (NTP) server. In this way, your board can get the time from the Internet.

1/*
2
3 Udp NTP Client
4
5 Get the time from a Network Time Protocol (NTP) time server
6
7 Demonstrates use of UDP sendPacket and ReceivePacket
8
9 For more on NTP time servers and the messages needed to communicate with them,
10
11 see http://en.wikipedia.org/wiki/Network_Time_Protocol
12
13 created 4 Sep 2010
14
15 by Michael Margolis
16
17 modified 9 Apr 2012
18
19 by Tom Igoe
20
21 modified 6 Dec 2017 ported from WiFi101 to MKRGSM
22
23 by Arturo Guadalupi
24
25
26
27 This code is in the public domain.
28
29*/
30
31#include <MKRGSM.h>
32
33#include "arduino_secrets.h"
34// Please enter your sensitive data in the Secret tab or arduino_secrets.h
35// PIN Number
36
37const char PINNUMBER[] = SECRET_PINNUMBER;
38// APN data
39
40const char GPRS_APN[] = SECRET_GPRS_APN;
41
42const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
43
44const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
45
46unsigned int localPort = 2390; // local port to listen for UDP packets
47
48IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
49
50const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
51
52byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
53
54// initialize the library instance
55
56GSMClient client;
57
58GPRS gprs;
59
60GSM gsmAccess;
61
62// A UDP instance to let us send and receive packets over UDP
63
64GSMUDP Udp;
65
66void setup()
67{
68
69 // Open serial communications and wait for port to open:
70
71 Serial.begin(9600);
72
73 while (!Serial) {
74
75 ; // wait for serial port to connect. Needed for native USB port only
76
77 }
78
79 Serial.println("Starting Arduino GPRS NTP client.");
80
81 // connection state
82
83 bool connected = false;
84
85 // After starting the modem with GSM.begin()
86
87 // attach the shield to the GPRS network with the APN, login and password
88
89 while (!connected) {
90
91 if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
92
93 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
94
95 connected = true;
96
97 } else {
98
99 Serial.println("Not connected");
100
101 delay(1000);
102
103 }
104
105 }
106
107 Serial.println("\nStarting connection to server...");
108
109 Udp.begin(localPort);
110}
111
112void loop()
113{
114
115 sendNTPpacket(timeServer); // send an NTP packet to a time server
116
117 // wait to see if a reply is available
118
119 delay(1000);
120
121 if ( Udp.parsePacket() ) {
122
123 Serial.println("packet received");
124
125 // We've received a packet, read the data from it
126
127 Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
128
129 //the timestamp starts at byte 40 of the received packet and is four bytes,
130
131 // or two words, long. First, esxtract the two words:
132
133 unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
134
135 unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
136
137 // combine the four bytes (two words) into a long integer
138
139 // this is NTP time (seconds since Jan 1 1900):
140
141 unsigned long secsSince1900 = highWord << 16 | lowWord;
142
143 Serial.print("Seconds since Jan 1 1900 = " );
144
145 Serial.println(secsSince1900);
146
147 // now convert NTP time into everyday time:
148
149 Serial.print("Unix time = ");
150
151 // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
152
153 const unsigned long seventyYears = 2208988800UL;
154
155 // subtract seventy years:
156
157 unsigned long epoch = secsSince1900 - seventyYears;
158
159 // print Unix time:
160
161 Serial.println(epoch);
162
163 // print the hour, minute and second:
164
165 Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
166
167 Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
168
169 Serial.print(':');
170
171 if ( ((epoch % 3600) / 60) < 10 ) {
172
173 // In the first 10 minutes of each hour, we'll want a leading '0'
174
175 Serial.print('0');
176
177 }
178
179 Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
180
181 Serial.print(':');
182
183 if ( (epoch % 60) < 10 ) {
184
185 // In the first 10 seconds of each minute, we'll want a leading '0'
186
187 Serial.print('0');
188
189 }
190
191 Serial.println(epoch % 60); // print the second
192
193 }
194
195 // wait ten seconds before asking for the time again
196
197 delay(10000);
198}
199
200// send an NTP request to the time server at the given address
201unsigned long sendNTPpacket(IPAddress& address)
202{
203
204 //Serial.println("1");
205
206 // set all bytes in the buffer to 0
207
208 memset(packetBuffer, 0, NTP_PACKET_SIZE);
209
210 // Initialize values needed to form NTP request
211
212 // (see URL above for details on the packets)
213
214 //Serial.println("2");
215
216 packetBuffer[0] = 0b11100011; // LI, Version, Mode
217
218 packetBuffer[1] = 0; // Stratum, or type of clock
219
220 packetBuffer[2] = 6; // Polling Interval
221
222 packetBuffer[3] = 0xEC; // Peer Clock Precision
223
224 // 8 bytes of zero for Root Delay & Root Dispersion
225
226 packetBuffer[12] = 49;
227
228 packetBuffer[13] = 0x4E;
229
230 packetBuffer[14] = 49;
231
232 packetBuffer[15] = 52;
233
234 //Serial.println("3");
235
236 // all NTP fields have been given values, now
237
238 // you can send a packet requesting a timestamp:
239
240 Udp.beginPacket(address, 123); //NTP requests are to port 123
241
242 //Serial.println("4");
243
244 Udp.write(packetBuffer, NTP_PACKET_SIZE);
245
246 //Serial.println("5");
247
248 Udp.endPacket();
249
250 //Serial.println("6");
251}

MKR GSM Make Voice Call

Get your board to make phone calls from the Serial Monitor.

1/*
2
3 Make Voice Call
4
5 This sketch, for the MKR GSM 1400 board, puts a voice call to
6
7 a remote phone number that you enter through the serial monitor.
8
9 To make it work, open the serial monitor, and when you see the
10
11 READY message, type a phone number. Make sure the serial monitor
12
13 is set to send a just newline when you press return.
14
15 Circuit:
16
17 * MKR GSM 1400 board
18
19 * Antenna
20
21* SIM card that can send voice calls
22
23 created Mar 2012
24
25 by Javier Zorzano
26
27*/
28
29// libraries
30#include <MKRGSM.h>
31
32#include "arduino_secrets.h"
33// Please enter your sensitive data in the Secret tab or arduino_secrets.h
34// PIN Number
35
36const char PINNUMBER[] = SECRET_PINNUMBER;
37
38// initialize the library instance
39
40GSM gsmAccess; // include a 'true' parameter for debug enabled
41
42GSMVoiceCall vcs;
43
44String remoteNumber = ""; // the number you will call
45char charbuffer[20];
46
47void setup() {
48
49 // initialize serial communications and wait for port to open:
50
51 Serial.begin(9600);
52
53 while (!Serial) {
54
55 ; // wait for serial port to connect. Needed for native USB port only
56
57 }
58
59 Serial.println("Make Voice Call");
60
61 // connection state
62
63 bool connected = false;
64
65 // Start GSM shield
66
67 // If your SIM has PIN, pass it as a parameter of begin() in quotes
68
69 while (!connected) {
70
71 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
72
73 connected = true;
74
75 } else {
76
77 Serial.println("Not connected");
78
79 delay(1000);
80
81 }
82
83 }
84
85 Serial.println("GSM initialized.");
86
87 Serial.println("Enter phone number to call.");
88
89}
90
91void loop() {
92
93 // add any incoming characters to the String:
94
95 while (Serial.available() > 0) {
96
97 char inChar = Serial.read();
98
99 // if it's a newline, that means you should make the call:
100
101 if (inChar == '\n') {
102
103 // make sure the phone number is not too long:
104
105 if (remoteNumber.length() < 20) {
106
107 // let the user know you're calling:
108
109 Serial.print("Calling to : ");
110
111 Serial.println(remoteNumber);
112
113 Serial.println();
114
115 // Call the remote number
116
117 remoteNumber.toCharArray(charbuffer, 20);
118
119 // Check if the receiving end has picked up the call
120
121 if (vcs.voiceCall(charbuffer)) {
122
123 Serial.println("Call Established. Enter line to end");
124
125 // Wait for some input from the line
126
127 while (Serial.read() != '\n' && (vcs.getvoiceCallStatus() == TALKING));
128
129 // And hang up
130
131 vcs.hangCall();
132
133 }
134
135 Serial.println("Call Finished");
136
137 remoteNumber = "";
138
139 Serial.println("Enter phone number to call.");
140
141 } else {
142
143 Serial.println("That's too long for a phone number. I'm forgetting it");
144
145 remoteNumber = "";
146
147 }
148
149 } else {
150
151 // add the latest character to the message to send:
152
153 if (inChar != '\r') {
154
155 remoteNumber += inChar;
156
157 }
158
159 }
160
161 }
162}

MKR GSM Receive SMS

This sketch waits for an SMS message and prints it to the serial monitor. It uses the GSM library of the Arduino GSM Shield and an active SIM card. To operate, the SIM card doesn't need a data plan.

1/*
2
3 SMS receiver
4
5 This sketch, for the MKR GSM 1400 board, waits for a SMS message
6
7 and displays it through the Serial port.
8
9 Circuit:
10
11 * MKR GSM 1400 board
12
13 * Antenna
14
15 * SIM card that can receive SMS messages
16
17 created 25 Feb 2012
18
19 by Javier Zorzano / TD
20
21*/
22
23// include the GSM library
24#include <MKRGSM.h>
25
26#include "arduino_secrets.h"
27// Please enter your sensitive data in the Secret tab or arduino_secrets.h
28// PIN Number
29
30const char PINNUMBER[] = SECRET_PINNUMBER;
31
32// initialize the library instances
33
34GSM gsmAccess;
35
36GSM_SMS sms;
37
38// Array to hold the number a SMS is retreived from
39char senderNumber[20];
40
41void setup() {
42
43 // initialize serial communications and wait for port to open:
44
45 Serial.begin(9600);
46
47 while (!Serial) {
48
49 ; // wait for serial port to connect. Needed for native USB port only
50
51 }
52
53 Serial.println("SMS Messages Receiver");
54
55 // connection state
56
57 bool connected = false;
58
59 // Start GSM connection
60
61 while (!connected) {
62
63 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
64
65 connected = true;
66
67 } else {
68
69 Serial.println("Not connected");
70
71 delay(1000);
72
73 }
74
75 }
76
77 Serial.println("GSM initialized");
78
79 Serial.println("Waiting for messages");
80}
81
82void loop() {
83
84 int c;
85
86 // If there are any SMSs available()
87
88 if (sms.available()) {
89
90 Serial.println("Message received from:");
91
92 // Get remote number
93
94 sms.remoteNumber(senderNumber, 20);
95
96 Serial.println(senderNumber);
97
98 // An example of message disposal
99
100 // Any messages starting with # should be discarded
101
102 if (sms.peek() == '#') {
103
104 Serial.println("Discarded SMS");
105
106 sms.flush();
107
108 }
109
110 // Read message bytes and print them
111
112 while ((c = sms.read()) != -1) {
113
114 Serial.print((char)c);
115
116 }
117
118 Serial.println("\nEND OF MESSAGE");
119
120 // Delete message from modem memory
121
122 sms.flush();
123
124 Serial.println("MESSAGE DELETED");
125
126 }
127
128 delay(1000);
129
130}

MKR GSM Receive Voice Call

This sketch receives a voice call from an Arduino MKR GSM 1400. Once the call is received and connected, it shows the number that is calling, and hangs up.

1/*
2
3 Receive Voice Call
4
5 This sketch, for the MKR GSM 1400 board, receives voice calls,
6
7 displays the calling number, waits a few seconds then hangs up.
8
9 Circuit:
10
11 * MKR GSM 1400 board
12
13 * Antenna
14
15 * SIM card that can accept voice calls
16
17 created Mar 2012
18
19 by Javier Zorzano
20
21*/
22
23// Include the GSM library
24#include <MKRGSM.h>
25
26#include "arduino_secrets.h"
27// Please enter your sensitive data in the Secret tab or arduino_secrets.h
28// PIN Number
29
30const char PINNUMBER[] = SECRET_PINNUMBER;
31
32// initialize the library instance
33
34GSM gsmAccess;
35
36GSMVoiceCall vcs;
37
38// Array to hold the number for the incoming call
39char numtel[20];
40
41void setup() {
42
43 // initialize serial communications and wait for port to open:
44
45 Serial.begin(9600);
46
47 while (!Serial) {
48
49 ; // wait for serial port to connect. Needed for native USB port only
50
51 }
52
53 Serial.println("Receive Voice Call");
54
55 // connection state
56
57 bool connected = false;
58
59 // Start GSM shield
60
61 // If your SIM has PIN, pass it as a parameter of begin() in quotes
62
63 while (!connected) {
64
65 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
66
67 connected = true;
68
69 } else {
70
71 Serial.println("Not connected");
72
73 delay(1000);
74
75 }
76
77 }
78
79 // This makes sure the modem correctly reports incoming events
80
81 vcs.hangCall();
82
83 Serial.println("Waiting for a call");
84}
85
86void loop() {
87
88 // Check the status of the voice call
89
90 switch (vcs.getvoiceCallStatus()) {
91
92 case IDLE_CALL: // Nothing is happening
93
94 break;
95
96 case RECEIVINGCALL: // Yes! Someone is calling us
97
98 Serial.println("RECEIVING CALL");
99
100 // Retrieve the calling number
101
102 vcs.retrieveCallingNumber(numtel, 20);
103
104 // Print the calling number
105
106 Serial.print("Number:");
107
108 Serial.println(numtel);
109
110 // Answer the call, establish the call
111
112 vcs.answerCall();
113
114 break;
115
116 case TALKING: // In this case the call would be established
117
118 Serial.println("TALKING. Press enter to hang up.");
119
120 while (Serial.read() != '\n') {
121
122 delay(100);
123
124 }
125
126 vcs.hangCall();
127
128 Serial.println("Hanging up and waiting for the next call.");
129
130 break;
131
132 }
133
134 delay(1000);
135}

MKR GSM Send SMS

This sketch send a SMS message from an Arduino MKR GSM 1400. Using the serial monitor of the Arduino Software (IDE), you'll enter the number to connect with, and the text message to send.

1/*
2
3 SMS sender
4
5 This sketch, for the MKR GSM 1400 board,sends an SMS message
6
7 you enter in the serial monitor. Connect your Arduino with the
8
9 GSM shield and SIM card, open the serial monitor, and wait for
10
11 the "READY" message to appear in the monitor. Next, type a
12
13 message to send and press "return". Make sure the serial
14
15 monitor is set to send a newline when you press return.
16
17 Circuit:
18
19 * MKR GSM 1400 board
20
21 * Antenna
22
23 * SIM card that can send SMS
24
25 created 25 Feb 2012
26
27 by Tom Igoe
28
29*/
30
31// Include the GSM library
32#include <MKRGSM.h>
33
34#include "arduino_secrets.h"
35// Please enter your sensitive data in the Secret tab or arduino_secrets.h
36// PIN Number
37
38const char PINNUMBER[] = SECRET_PINNUMBER;
39
40// initialize the library instance
41
42GSM gsmAccess;
43
44GSM_SMS sms;
45
46void setup() {
47
48 // initialize serial communications and wait for port to open:
49
50 Serial.begin(9600);
51
52 while (!Serial) {
53
54 ; // wait for serial port to connect. Needed for native USB port only
55
56 }
57
58 Serial.println("SMS Messages Sender");
59
60 // connection state
61
62 bool connected = false;
63
64 // Start GSM shield
65
66 // If your SIM has PIN, pass it as a parameter of begin() in quotes
67
68 while (!connected) {
69
70 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
71
72 connected = true;
73
74 } else {
75
76 Serial.println("Not connected");
77
78 delay(1000);
79
80 }
81
82 }
83
84 Serial.println("GSM initialized");
85}
86
87void loop() {
88
89 Serial.print("Enter a mobile number: ");
90
91 char remoteNum[20]; // telephone number to send sms
92
93 readSerial(remoteNum);
94
95 Serial.println(remoteNum);
96
97 // sms text
98
99 Serial.print("Now, enter SMS content: ");
100
101 char txtMsg[200];
102
103 readSerial(txtMsg);
104
105 Serial.println("SENDING");
106
107 Serial.println();
108
109 Serial.println("Message:");
110
111 Serial.println(txtMsg);
112
113 // send the message
114
115 sms.beginSMS(remoteNum);
116
117 sms.print(txtMsg);
118
119 sms.endSMS();
120
121 Serial.println("\nCOMPLETE!\n");
122}
123
124/*
125
126 Read input serial
127
128 */
129int readSerial(char result[]) {
130
131 int i = 0;
132
133 while (1) {
134
135 while (Serial.available() > 0) {
136
137 char inChar = Serial.read();
138
139 if (inChar == '\n') {
140
141 result[i] = '\0';
142
143 Serial.flush();
144
145 return 0;
146
147 }
148
149 if (inChar != '\r') {
150
151 result[i] = inChar;
152
153 i++;
154
155 }
156
157 }
158
159 }
160}

MKR GSM SSL Web Client

This sketch connects an Arduino MKR GSM 1400 board to the Arduino homepage, http://arduino.cc, through the GSM network. It then prints the content of the page through the serial monitor of the Arduino Software (IDE).

1/*
2 Web client
3
4 This sketch connects to a website using SSL through a MKR GSM 1400 board. Specifically,
5
6 this example downloads the URL "http://www.arduino.cc/asciilogo.txt" and
7
8 prints it to the Serial monitor.
9
10 Circuit:
11
12 * MKR GSM 1400 board
13
14 * Antenna
15
16 * SIM card with a data plan
17
18 created 8 Mar 2012
19
20 by Tom Igoe
21
22*/
23
24// libraries
25#include <MKRGSM.h>
26
27#include "arduino_secrets.h"
28// Please enter your sensitive data in the Secret tab or arduino_secrets.h
29// PIN Number
30
31const char PINNUMBER[] = SECRET_PINNUMBER;
32// APN data
33
34const char GPRS_APN[] = SECRET_GPRS_APN;
35
36const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
37
38const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
39
40// initialize the library instance
41
42GSMSSLClient client;
43
44GPRS gprs;
45
46GSM gsmAccess;
47
48// URL, path and port (for example: arduino.cc)
49char server[] = "arduino.cc";
50char path[] = "/asciilogo.txt";
51int port = 443; // port 443 is the default for HTTPS
52
53void setup() {
54
55 // initialize serial communications and wait for port to open:
56
57 Serial.begin(9600);
58
59 while (!Serial) {
60
61 ; // wait for serial port to connect. Needed for native USB port only
62
63 }
64
65 Serial.println("Starting Arduino web client.");
66
67 // connection state
68
69 bool connected = false;
70
71 // After starting the modem with GSM.begin()
72
73 // attach the shield to the GPRS network with the APN, login and password
74
75 while (!connected) {
76
77 if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
78
79 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
80
81 connected = true;
82
83 } else {
84
85 Serial.println("Not connected");
86
87 delay(1000);
88
89 }
90
91 }
92
93 Serial.println("connecting...");
94
95 // if you get a connection, report back via serial:
96
97 if (client.connect(server, port)) {
98
99 Serial.println("connected");
100
101 // Make a HTTP request:
102
103 client.print("GET ");
104
105 client.print(path);
106
107 client.println(" HTTP/1.1");
108
109 client.print("Host: ");
110
111 client.println(server);
112
113 client.println("Connection: close");
114
115 client.println();
116
117 } else {
118
119 // if you didn't get a connection to the server:
120
121 Serial.println("connection failed");
122
123 }
124}
125
126void loop() {
127
128 // if there are incoming bytes available
129
130 // from the server, read them and print them:
131
132 if (client.available()) {
133
134 char c = client.read();
135
136 Serial.print(c);
137
138 }
139
140 // if the server's disconnected, stop the client:
141
142 if (!client.available() && !client.connected()) {
143
144 Serial.println();
145
146 Serial.println("disconnecting.");
147
148 client.stop();
149
150 // do nothing forevermore:
151
152 for (;;)
153
154 ;
155
156 }
157}

MKR GSM Web Client

This sketch connects an Arduino MKR GSM 1400 board to the Arduino homepage, http://arduino.cc, through the GSM network. It then prints the content of the page through the serial monitor of the Arduino Software (IDE).

1/*
2
3 Web client
4
5 This sketch connects to a website through a MKR GSM 1400 board. Specifically,
6
7 this example downloads the URL "http://www.example.org/" and
8
9 prints it to the Serial monitor.
10
11 Circuit:
12
13 * MKR GSM 1400 board
14
15 * Antenna
16
17 * SIM card with a data plan
18
19 created 8 Mar 2012
20
21 by Tom Igoe
22
23*/
24
25// libraries
26#include <MKRGSM.h>
27
28#include "arduino_secrets.h"
29// Please enter your sensitive data in the Secret tab or arduino_secrets.h
30// PIN Number
31
32const char PINNUMBER[] = SECRET_PINNUMBER;
33// APN data
34
35const char GPRS_APN[] = SECRET_GPRS_APN;
36
37const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
38
39const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
40
41// initialize the library instance
42
43GSMClient client;
44
45GPRS gprs;
46
47GSM gsmAccess;
48
49// URL, path and port (for example: example.org)
50char server[] = "example.org";
51char path[] = "/";
52int port = 80; // port 80 is the default for HTTP
53
54void setup() {
55
56 // initialize serial communications and wait for port to open:
57
58 Serial.begin(9600);
59
60 while (!Serial) {
61
62 ; // wait for serial port to connect. Needed for native USB port only
63
64 }
65
66 Serial.println("Starting Arduino web client.");
67
68 // connection state
69
70 bool connected = false;
71
72 // After starting the modem with GSM.begin()
73
74 // attach the shield to the GPRS network with the APN, login and password
75
76 while (!connected) {
77
78 if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
79
80 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
81
82 connected = true;
83
84 } else {
85
86 Serial.println("Not connected");
87
88 delay(1000);
89
90 }
91
92 }
93
94 Serial.println("connecting...");
95
96 // if you get a connection, report back via serial:
97
98 if (client.connect(server, port)) {
99
100 Serial.println("connected");
101
102 // Make a HTTP request:
103
104 client.print("GET ");
105
106 client.print(path);
107
108 client.println(" HTTP/1.1");
109
110 client.print("Host: ");
111
112 client.println(server);
113
114 client.println("Connection: close");
115
116 client.println();
117
118 } else {
119
120 // if you didn't get a connection to the server:
121
122 Serial.println("connection failed");
123
124 }
125}
126
127void loop() {
128
129 // if there are incoming bytes available
130
131 // from the server, read them and print them:
132
133 if (client.available()) {
134
135 char c = client.read();
136
137 Serial.print(c);
138
139 }
140
141 // if the server's disconnected, stop the client:
142
143 if (!client.available() && !client.connected()) {
144
145 Serial.println();
146
147 Serial.println("disconnecting.");
148
149 client.stop();
150
151 // do nothing forevermore:
152
153 for (;;)
154
155 ;
156
157 }
158}

MKR GSM Web Server

This sketch turns the Arduino MKR GSM 1400 into a web server. When the board receives a request from a connected client, it sends back the value of analog inputs 0-5.

Not all network operators allow incoming data requests from outside their network. This means you can create a web server with the GSM shield, but you may not be able to connect to it from the public internet; only from another data enabled device from the same provider on the same network. You should check with your provider to see what specific policies they have in place regarding incoming data connections.

1/*
2
3 GSM Web Server
4
5 A simple web server that shows the value of the analog input pins.
6
7 using a MKR GSM 1400 board.
8
9 Circuit:
10
11 * MKR GSM 1400 board
12
13 * Antenna
14
15 * Analog inputs attached to pins A0 through A5 (optional)
16
17 created 8 Mar 2012
18
19 by Tom Igoe
20
21*/
22
23// libraries
24#include <MKRGSM.h>
25
26#include "arduino_secrets.h"
27// Please enter your sensitive data in the Secret tab or arduino_secrets.h
28// PIN Number
29
30const char PINNUMBER[] = SECRET_PINNUMBER;
31// APN data
32
33const char GPRS_APN[] = SECRET_GPRS_APN;
34
35const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
36
37const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
38
39// initialize the library instance
40
41GPRS gprs;
42
43GSM gsmAccess; // include a 'true' parameter for debug enabled
44
45GSMServer server(80); // port 80 (http default)
46
47// timeout
48
49const unsigned long __TIMEOUT__ = 10 * 1000;
50
51void setup() {
52
53 // initialize serial communications and wait for port to open:
54
55 Serial.begin(9600);
56
57 while (!Serial) {
58
59 ; // wait for serial port to connect. Needed for native USB port only
60
61 }
62
63 // connection state
64
65 bool connected = false;
66
67 // Start GSM shield
68
69 // If your SIM has PIN, pass it as a parameter of begin() in quotes
70
71 while (!connected) {
72
73 if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
74
75 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
76
77 connected = true;
78
79 } else {
80
81 Serial.println("Not connected");
82
83 delay(1000);
84
85 }
86
87 }
88
89 Serial.println("Connected to GPRS network");
90
91 // start server
92
93 server.begin();
94
95 //Get IP.
96
97 IPAddress LocalIP = gprs.getIPAddress();
98
99 Serial.println("Server IP address=");
100
101 Serial.println(LocalIP);
102}
103
104void loop() {
105
106 // listen for incoming clients
107
108 GSMClient client = server.available();
109
110 if (client) {
111
112 while (client.connected()) {
113
114 if (client.available()) {
115
116 Serial.println("Receiving request!");
117
118 bool sendResponse = false;
119
120 while (int c = client.read()) {
121
122 if (c == -1) {
123
124 break;
125
126 } else if (c == '\n') {
127
128 sendResponse = true;
129
130 }
131
132 }
133
134 // if you've gotten to the end of the line (received a newline
135
136 // character)
137
138 if (sendResponse) {
139
140 // send a standard http response header
141
142 client.println("HTTP/1.1 200 OK");
143
144 client.println("Content-Type: text/html");
145
146 client.println();
147
148 client.println("<html>");
149
150 // output the value of each analog input pin
151
152 for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
153
154 client.print("analog input ");
155
156 client.print(analogChannel);
157
158 client.print(" is ");
159
160 client.print(analogRead(analogChannel));
161
162 client.println("<br />");
163
164 }
165
166 client.println("</html>");
167
168 //necessary delay
169
170 delay(1000);
171
172 client.stop();
173
174 }
175
176 }
177
178 }
179
180 }
181}

MKR GSM Tools Band Management

This example is part of the tools supplied to manage the Arduino MKR GSM 1400 and shows how to use the GSM Library to manage the GSM band the modem connects to.

Check http://www.worldtimezone.com/gsm.html for general GSM band information. Typical regional configurations are:

  • Europe, Africa, Middle East: E-GSM(900)+DCS(1800)

  • USA, Canada, South America: GSM(850)+PCS(1900)

  • Mexico: PCS(1900)

  • Brazil: GSM(850)+E-GSM(900)+DCS(1800)+PCS(1900)

1/*
2
3 Band Management
4
5 This sketch, for the MKR GSM 1400 board, checks the band
6
7 currently configured in the modem and allows you to change
8
9 it.
10
11 Please check http://www.worldtimezone.com/gsm.html
12
13 Usual configurations:
14
15 Europe, Africa, Middle East: E-GSM(900)+DCS(1800)
16
17 USA, Canada, South America: GSM(850)+PCS(1900)
18
19 Mexico: PCS(1900)
20
21 Brazil: GSM(850)+E-GSM(900)+DCS(1800)+PCS(1900)
22
23 Circuit:
24
25 * MKR GSM 1400 board
26
27 * Antenna
28
29 created 12 June 2012
30
31 by Javier Zorzano, Scott Fitzgerald
32
33*/
34
35// libraries
36#include <MKRGSM.h>
37
38// initialize the library instance
39
40GSMBand band;
41
42void setup() {
43
44 // initialize serial communications and wait for port to open:
45
46 Serial.begin(9600);
47
48 while (!Serial) {
49
50 ; // wait for serial port to connect. Needed for Leonardo only
51
52 }
53
54 // Beginning the band manager restarts the modem
55
56 Serial.println("Restarting modem...");
57
58 band.begin();
59
60 Serial.println("Modem restarted.");
61
62};
63
64void loop() {
65
66 // Get current band
67
68 String bandName = band.getBand(); // Get and print band name
69
70 Serial.print("Current band:");
71
72 Serial.println(bandName);
73
74 Serial.println("Want to change the band you're on?");
75
76 String newBandName;
77
78 newBandName = askUser();
79
80 // Tell the user what we are about to do...
81
82 Serial.print("\nConfiguring band ");
83
84 Serial.println(newBandName);
85
86 // Change the band
87
88 bool operationSuccess;
89
90 operationSuccess = band.setBand(newBandName);
91
92 // Tell the user if the operation was OK
93
94 if (operationSuccess) {
95
96 Serial.println("Success");
97
98 } else {
99
100 Serial.println("Error while changing band");
101
102 }
103
104 if (operationSuccess) {
105
106 while (true);
107
108 }
109}
110
111// This function offers the user different options
112// through the Serial interface
113// The user selects one
114
115String askUser() {
116
117 String newBand;
118
119 Serial.println("Select band:");
120
121 // Print the different options
122
123 Serial.println("1 : E-GSM(900)");
124
125 Serial.println("2 : DCS(1800)");
126
127 Serial.println("3 : PCS(1900)");
128
129 Serial.println("4 : E-GSM(900)+DCS(1800) ex: Europe");
130
131 Serial.println("5 : GSM(850)+PCS(1900) Ex: USA, South Am.");
132
133 Serial.println("6 : GSM800(800)+GSM(850)+E-GSM(900)+PCS(1900)");
134
135 Serial.println("7 : UMTS(2100)");
136
137 Serial.println("8 : GSM(850)+E-GSM(900)+PCS(1900)+UMTS(2100)");
138
139 // Empty the incoming buffer
140
141 while (Serial.available()) {
142
143 Serial.read();
144
145 }
146
147 // Wait for an answer, just look at the first character
148
149 while (!Serial.available());
150
151 char c = Serial.read();
152
153 if (c == '1') {
154
155 newBand = GSM_MODE_EGSM;
156
157 } else if (c == '2') {
158
159 newBand = GSM_MODE_DCS;
160
161 } else if (c == '3') {
162
163 newBand = GSM_MODE_PCS;
164
165 } else if (c == '4') {
166
167 newBand = GSM_MODE_EGSM_DCS;
168
169 } else if (c == '5') {
170
171 newBand = GSM_MODE_GSM850_PCS;
172
173 } else if (c == '6') {
174
175 newBand = GSM_MODE_GSM850_EGSM_DCS_PCS;
176
177 } else if (c == '7') {
178
179 newBand = GSM_MODE_UMTS;
180
181 } else if (c == '8') {
182
183 newBand = GSM_MODE_GSM850_EGSM_PCS_UMTS;
184
185 } else {
186
187 newBand = "GSM_MODE_UNDEFINED";
188
189 }
190
191 return newBand;
192}

MKR GSM Tools Gsm Scan Networks

This example prints out the IMEI number of the modem, then checks to see if it's connected to a carrier and prints out its signal strength. It also scans for all nearby networks.

1/*
2
3 GSM Scan Networks
4
5 This example prints out the IMEI number of the modem,
6
7 then checks to see if it's connected to a carrier. If so,
8
9 it prints the phone number associated with the card.
10
11 Then it scans for nearby networks and prints out their signal strengths.
12
13 Circuit:
14
15 * MKR GSM 1400 board
16
17 * Antenna
18
19 * SIM card
20
21 Created 8 Mar 2012
22
23 by Tom Igoe, implemented by Javier Carazo
24
25 Modified 4 Feb 2013
26
27 by Scott Fitzgerald
28
29*/
30
31// libraries
32#include <MKRGSM.h>
33
34#include "arduino_secrets.h"
35// Please enter your sensitive data in the Secret tab or arduino_secrets.h
36// PIN Number
37
38const char PINNUMBER[] = SECRET_PINNUMBER;
39
40// initialize the library instance
41
42GSM gsmAccess; // include a 'true' parameter to enable debugging
43
44GSMScanner scannerNetworks;
45
46GSMModem modemTest;
47
48// Save data variables
49
50String IMEI = "";
51
52// serial monitor result messages
53
54String errortext = "ERROR";
55
56void setup() {
57
58 // initialize serial communications and wait for port to open:
59
60 Serial.begin(9600);
61
62 while (!Serial) {
63
64 ; // wait for serial port to connect. Needed for Leonardo only
65
66 }
67
68 Serial.println("GSM networks scanner");
69
70 scannerNetworks.begin();
71
72 // connection state
73
74 bool connected = false;
75
76 // Start GSM shield
77
78 // If your SIM has PIN, pass it as a parameter of begin() in quotes
79
80 while (!connected) {
81
82 if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
83
84 connected = true;
85
86 } else {
87
88 Serial.println("Not connected");
89
90 delay(1000);
91
92 }
93
94 }
95
96 // get modem parameters
97
98 // IMEI, modem unique identifier
99
100 Serial.print("Modem IMEI: ");
101
102 IMEI = modemTest.getIMEI();
103
104 IMEI.replace("\n", "");
105
106 if (IMEI != NULL) {
107
108 Serial.println(IMEI);
109
110 }
111}
112
113void loop() {
114
115 // scan for existing networks, displays a list of networks
116
117 Serial.println("Scanning available networks. May take some seconds.");
118
119 Serial.println(scannerNetworks.readNetworks());
120
121 // currently connected carrier
122
123 Serial.print("Current carrier: ");
124
125 Serial.println(scannerNetworks.getCurrentCarrier());
126
127 // returns strength and ber
128
129 // signal strength in 0-31 scale. 31 means power > 51dBm
130
131 // BER is the Bit Error Rate. 0-7 scale. 99=not detectable
132
133 Serial.print("Signal Strength: ");
134
135 Serial.print(scannerNetworks.getSignalStrength());
136
137 Serial.println(" [0-31]");
138
139}

MKR GSM Tools Pin Management

This example is part of the tools supplied for the Arduino MKR GSM 1400 and helps you change or remove the PIN of a SIM card .

1/*
2
3 This example enables you to change or remove the PIN number of
4
5 a SIM card inserted into a GSM shield.
6
7 Circuit:
8
9 * MKR GSM 1400 board
10
11 * Antenna
12
13 * SIM card
14
15 Created 12 Jun 2012
16
17 by David del Peral
18
19*/
20
21// libraries
22#include <MKRGSM.h>
23
24// pin manager object
25
26GSMPIN PINManager;
27
28// save input in serial by user
29
30String user_input = "";
31
32// authenticated with PIN code
33
34bool auth = false;
35
36// serial monitor result messages
37
38String oktext = "OK";
39
40String errortext = "ERROR";
41
42void setup() {
43
44 // initialize serial communications and wait for port to open:
45
46 Serial.begin(9600);
47
48 while (!Serial) {
49
50 ; // wait for serial port to connect. Needed for Leonardo only
51
52 }
53
54 Serial.println("Change PIN example\n");
55
56 PINManager.begin();
57
58 // check if the SIM have pin lock
59
60 while (!auth) {
61
62 int pin_query = PINManager.isPIN();
63
64 if (pin_query == 1) {
65
66 // if SIM is locked, enter PIN code
67
68 Serial.print("Enter PIN code: ");
69
70 user_input = readSerial();
71
72 // check PIN code
73
74 if (PINManager.checkPIN(user_input) == 0) {
75
76 auth = true;
77
78 PINManager.setPINUsed(true);
79
80 Serial.println(oktext);
81
82 } else {
83
84 // if PIN code was incorrected
85
86 Serial.println("Incorrect PIN. Remember that you have 3 opportunities.");
87
88 }
89
90 } else if (pin_query == -1) {
91
92 // PIN code is locked, user must enter PUK code
93
94 Serial.println("PIN locked. Enter PUK code: ");
95
96 String puk = readSerial();
97
98 Serial.print("Now, enter a new PIN code: ");
99
100 user_input = readSerial();
101
102 // check PUK code
103
104 if (PINManager.checkPUK(puk, user_input) == 0) {
105
106 auth = true;
107
108 PINManager.setPINUsed(true);
109
110 Serial.println(oktext);
111
112 } else {
113
114 // if PUK o the new PIN are incorrect
115
116 Serial.println("Incorrect PUK or invalid new PIN. Try again!.");
117
118 }
119
120 } else if (pin_query == -2) {
121
122 // the worst case, PIN and PUK are locked
123
124 Serial.println("PIN and PUK locked. Use PIN2/PUK2 in a mobile phone.");
125
126 while (true);
127
128 } else {
129
130 // SIM does not requires authetication
131
132 Serial.println("No pin necessary.");
133
134 auth = true;
135
136 }
137
138 }
139
140 // start GSM shield
141
142 Serial.print("Checking register in GSM network...");
143
144 if (PINManager.checkReg() == 0) {
145
146 Serial.println(oktext);
147
148 }
149
150 // if you are connect by roaming
151
152 else if (PINManager.checkReg() == 1) {
153
154 Serial.println("ROAMING " + oktext);
155
156 } else {
157
158 // error connection
159
160 Serial.println(errortext);
161
162 while (true);
163
164 }
165}
166
167void loop() {
168
169 // Function loop implements pin management user menu
170
171 // Only if you SIM use pin lock, you can change PIN code
172
173 // user_op variables save user option
174
175 Serial.println("Choose an option:\n1 - On/Off PIN.");
176
177 if (PINManager.getPINUsed()) {
178
179 Serial.println("2 - Change PIN.");
180
181 }
182
183 String user_op = readSerial();
184
185 if (user_op == "1") {
186
187 Serial.println("Enter your PIN code:");
188
189 user_input = readSerial();
190
191 // activate/deactivate PIN lock
192
193 PINManager.switchPIN(user_input);
194
195 } else if (user_op == "2" && PINManager.getPINUsed()) {
196
197 Serial.println("Enter your actual PIN code:");
198
199 String oldPIN = readSerial();
200
201 Serial.println("Now, enter your new PIN code:");
202
203 String newPIN = readSerial();
204
205 // change PIN
206
207 PINManager.changePIN(oldPIN, newPIN);
208
209 } else {
210
211 Serial.println("Incorrect option. Try again!.");
212
213 }
214
215 delay(1000);
216}
217
218/*
219
220 Read input serial
221
222 */
223
224String readSerial() {
225
226 String text = "";
227
228 while (1) {
229
230 while (Serial.available() > 0) {
231
232 char inChar = Serial.read();
233
234 if (inChar == '\n') {
235
236 return text;
237
238 }
239
240 if (inChar != '\r') {
241
242 text += inChar;
243
244 }
245
246 }
247
248 }
249}

MKR GSM Tools Test GPRS

This sketch tests the GPRS data connection on the Arduino MKR GSM 1400. It tries to connect to arduino.cc.

To use a data connection with the GSM shield, you'll need your provider's Access Point Name (APN), login, and password. To obtain this information, contact the network provider for the most up to date information. This page has some information about various carrier settings, but it may not be current.

1/*
2
3 This sketch test the MKR GSM 1400 board's ability to connect to a
4
5 GPRS network. It asks for APN information through the
6
7 serial monitor and tries to connect to example.org.
8
9 Circuit:
10
11 * MKR GSM 1400 board
12
13 * Antenna
14
15 * SIM card with data plan
16
17 Created 18 Jun 2012
18
19 by David del Peral
20
21*/
22
23// libraries
24#include <MKRGSM.h>
25
26#include "arduino_secrets.h"
27// Please enter your sensitive data in the Secret tab or arduino_secrets.h
28// PIN Number
29
30const char PINNUMBER[] = SECRET_PINNUMBER;
31
32// initialize the library instance
33
34GSM gsmAccess; // GSM access: include a 'true' parameter for debug enabled
35
36GPRS gprsAccess; // GPRS access
37
38GSMClient client; // Client service for TCP connection
39
40// messages for serial monitor response
41
42String oktext = "OK";
43
44String errortext = "ERROR";
45
46// URL and path (for example: example.org)
47char url[] = "example.org";
48char urlproxy[] = "http://www.example.org";
49char path[] = "/";
50
51// variable for save response obtained
52
53String response = "";
54
55// use a proxy
56
57bool use_proxy = false;
58
59void setup() {
60
61 // initialize serial communications and wait for port to open:
62
63 Serial.begin(9600);
64
65 while (!Serial) {
66
67 ; // wait for serial port to connect. Needed for Leonardo only
68
69 }
70}
71
72void loop() {
73
74 use_proxy = false;
75
76 // start GSM shield
77
78 // if your SIM has PIN, pass it as a parameter of begin() in quotes
79
80 Serial.print("Connecting GSM network...");
81
82 if (gsmAccess.begin(PINNUMBER) != GSM_READY) {
83
84 Serial.println(errortext);
85
86 while (true);
87
88 }
89
90 Serial.println(oktext);
91
92 // read APN introduced by user
93
94 char apn[50];
95
96 Serial.print("Enter your APN: ");
97
98 readSerial(apn);
99
100 Serial.println(apn);
101
102 // Read APN login introduced by user
103
104 char login[50];
105
106 Serial.print("Now, enter your login: ");
107
108 readSerial(login);
109
110 Serial.println(login);
111
112 // read APN password introduced by user
113
114 char password[20];
115
116 Serial.print("Finally, enter your password: ");
117
118 readSerial(password);
119
120 // attach GPRS
121
122 Serial.println("Attaching to GPRS with your APN...");
123
124 if (gprsAccess.attachGPRS(apn, login, password) != GPRS_READY) {
125
126 Serial.println(errortext);
127
128 } else {
129
130 Serial.println(oktext);
131
132 // read proxy introduced by user
133
134 char proxy[100];
135
136 Serial.print("If your carrier uses a proxy, enter it, if not press enter: ");
137
138 readSerial(proxy);
139
140 Serial.println(proxy);
141
142 // if user introduced a proxy, asks them for proxy port
143
144 int pport;
145
146 if (proxy[0] != '\0') {
147
148 // read proxy port introduced by user
149
150 char proxyport[10];
151
152 Serial.print("Enter the proxy port: ");
153
154 readSerial(proxyport);
155
156 // cast proxy port introduced to integer
157
158 pport = (int) proxyport;
159
160 use_proxy = true;
161
162 Serial.println(proxyport);
163
164 }
165
166 // connection with example.org and realize HTTP request
167
168 Serial.print("Connecting and sending GET request to example.org...");
169
170 int res_connect;
171
172 // if use a proxy, connect with it
173
174 if (use_proxy) {
175
176 res_connect = client.connect(proxy, pport);
177
178 } else {
179
180 res_connect = client.connect(url, 80);
181
182 }
183
184 if (res_connect) {
185
186 // make a HTTP 1.0 GET request (client sends the request)
187
188 client.print("GET ");
189
190 // if use a proxy, the path is example.org URL
191
192 if (use_proxy) {
193
194 client.print(urlproxy);
195
196 } else {
197
198 client.print(path);
199
200 }
201
202 client.println(" HTTP/1.0");
203
204 client.println();
205
206 Serial.println(oktext);
207
208 } else {
209
210 // if you didn't get a connection to the server
211
212 Serial.println(errortext);
213
214 }
215
216 Serial.print("Receiving response...");
217
218 bool test = true;
219
220 while (test) {
221
222 // if there are incoming bytes available
223
224 // from the server, read and check them
225
226 if (client.available()) {
227
228 char c = client.read();
229
230 response += c;
231
232 // cast response obtained from string to char array
233
234 char responsechar[response.length() + 1];
235
236 response.toCharArray(responsechar, response.length() + 1);
237
238 // if response includes a "200 OK" substring
239
240 if (strstr(responsechar, "200 OK") != NULL) {
241
242 Serial.println(oktext);
243
244 Serial.println("TEST COMPLETE!");
245
246 test