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
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 board8
9* Antenna10
11* SIM card with a data plan12
13 created 06 Dec 201714
15 by Arturo Guadalupi16
17*/18#include <MKRGSM.h>19
20#include "arduino_secrets.h"21// Please enter your sensitive data in the Secret tab or arduino_secrets.h22// PIN Number23
24const char PINNUMBER[] = SECRET_PINNUMBER;25// APN data26
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 instance34
35GSMSSLClient client;36
37GPRS gprs;38
39GSM gsmAccess;40
41// Specify IP address or hostname42
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 only55
56 }57
58 Serial.println("Starting Arduino GPRS ping.");59
60 // connection state61
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 password67
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 Client4
5 Get the time from a Network Time Protocol (NTP) time server6
7 Demonstrates use of UDP sendPacket and ReceivePacket8
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_Protocol12
13 created 4 Sep 201014
15 by Michael Margolis16
17 modified 9 Apr 201218
19 by Tom Igoe20
21 modified 6 Dec 2017 ported from WiFi101 to MKRGSM22
23 by Arturo Guadalupi24
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.h35// PIN Number36
37const char PINNUMBER[] = SECRET_PINNUMBER;38// APN data39
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 packets47
48IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server49
50const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message51
52byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets53
54// initialize the library instance55
56GSMClient client;57
58GPRS gprs;59
60GSM gsmAccess;61
62// A UDP instance to let us send and receive packets over UDP63
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 only76
77 }78
79 Serial.println("Starting Arduino GPRS NTP client.");80
81 // connection state82
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 password88
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 server116
117 // wait to see if a reply is available118
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 it126
127 Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer128
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 integer138
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 second192
193 }194
195 // wait ten seconds before asking for the time again196
197 delay(10000);198}199
200// send an NTP request to the time server at the given address201unsigned long sendNTPpacket(IPAddress& address)202{203
204 //Serial.println("1");205
206 // set all bytes in the buffer to 0207
208 memset(packetBuffer, 0, NTP_PACKET_SIZE);209
210 // Initialize values needed to form NTP request211
212 // (see URL above for details on the packets)213
214 //Serial.println("2");215
216 packetBuffer[0] = 0b11100011; // LI, Version, Mode217
218 packetBuffer[1] = 0; // Stratum, or type of clock219
220 packetBuffer[2] = 6; // Polling Interval221
222 packetBuffer[3] = 0xEC; // Peer Clock Precision223
224 // 8 bytes of zero for Root Delay & Root Dispersion225
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, now237
238 // you can send a packet requesting a timestamp:239
240 Udp.beginPacket(address, 123); //NTP requests are to port 123241
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 Call4
5 This sketch, for the MKR GSM 1400 board, puts a voice call to6
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 the10
11 READY message, type a phone number. Make sure the serial monitor12
13 is set to send a just newline when you press return.14
15 Circuit:16
17 * MKR GSM 1400 board18
19 * Antenna20
21* SIM card that can send voice calls22
23 created Mar 201224
25 by Javier Zorzano26
27*/28
29// libraries30#include <MKRGSM.h>31
32#include "arduino_secrets.h"33// Please enter your sensitive data in the Secret tab or arduino_secrets.h34// PIN Number35
36const char PINNUMBER[] = SECRET_PINNUMBER;37
38// initialize the library instance39
40GSM gsmAccess; // include a 'true' parameter for debug enabled41
42GSMVoiceCall vcs;43
44String remoteNumber = ""; // the number you will call45char 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 only56
57 }58
59 Serial.println("Make Voice Call");60
61 // connection state62
63 bool connected = false;64
65 // Start GSM shield66
67 // If your SIM has PIN, pass it as a parameter of begin() in quotes68
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 number116
117 remoteNumber.toCharArray(charbuffer, 20);118
119 // Check if the receiving end has picked up the call120
121 if (vcs.voiceCall(charbuffer)) {122
123 Serial.println("Call Established. Enter line to end");124
125 // Wait for some input from the line126
127 while (Serial.read() != '\n' && (vcs.getvoiceCallStatus() == TALKING));128
129 // And hang up130
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 receiver4
5 This sketch, for the MKR GSM 1400 board, waits for a SMS message6
7 and displays it through the Serial port.8
9 Circuit:10
11 * MKR GSM 1400 board12
13 * Antenna14
15 * SIM card that can receive SMS messages16
17 created 25 Feb 201218
19 by Javier Zorzano / TD20
21*/22
23// include the GSM library24#include <MKRGSM.h>25
26#include "arduino_secrets.h"27// Please enter your sensitive data in the Secret tab or arduino_secrets.h28// PIN Number29
30const char PINNUMBER[] = SECRET_PINNUMBER;31
32// initialize the library instances33
34GSM gsmAccess;35
36GSM_SMS sms;37
38// Array to hold the number a SMS is retrieved from39char 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 only50
51 }52
53 Serial.println("SMS Messages Receiver");54
55 // connection state56
57 bool connected = false;58
59 // Start GSM connection60
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 number93
94 sms.remoteNumber(senderNumber, 20);95
96 Serial.println(senderNumber);97
98 // An example of message disposal99
100 // Any messages starting with # should be discarded101
102 if (sms.peek() == '#') {103
104 Serial.println("Discarded SMS");105
106 sms.flush();107
108 }109
110 // Read message bytes and print them111
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 memory121
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 Call4
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 board12
13 * Antenna14
15 * SIM card that can accept voice calls16
17 created Mar 201218
19 by Javier Zorzano20
21*/22
23// Include the GSM library24#include <MKRGSM.h>25
26#include "arduino_secrets.h"27// Please enter your sensitive data in the Secret tab or arduino_secrets.h28// PIN Number29
30const char PINNUMBER[] = SECRET_PINNUMBER;31
32// initialize the library instance33
34GSM gsmAccess;35
36GSMVoiceCall vcs;37
38// Array to hold the number for the incoming call39char 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 only50
51 }52
53 Serial.println("Receive Voice Call");54
55 // connection state56
57 bool connected = false;58
59 // Start GSM shield60
61 // If your SIM has PIN, pass it as a parameter of begin() in quotes62
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 events80
81 vcs.hangCall();82
83 Serial.println("Waiting for a call");84}85
86void loop() {87
88 // Check the status of the voice call89
90 switch (vcs.getvoiceCallStatus()) {91
92 case IDLE_CALL: // Nothing is happening93
94 break;95
96 case RECEIVINGCALL: // Yes! Someone is calling us97
98 Serial.println("RECEIVING CALL");99
100 // Retrieve the calling number101
102 vcs.retrieveCallingNumber(numtel, 20);103
104 // Print the calling number105
106 Serial.print("Number:");107
108 Serial.println(numtel);109
110 // Answer the call, establish the call111
112 vcs.answerCall();113
114 break;115
116 case TALKING: // In this case the call would be established117
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 sender4
5 This sketch, for the MKR GSM 1400 board,sends an SMS message6
7 you enter in the serial monitor. Connect your Arduino with the8
9 GSM shield and SIM card, open the serial monitor, and wait for10
11 the "READY" message to appear in the monitor. Next, type a12
13 message to send and press "return". Make sure the serial14
15 monitor is set to send a newline when you press return.16
17 Circuit:18
19 * MKR GSM 1400 board20
21 * Antenna22
23 * SIM card that can send SMS24
25 created 25 Feb 201226
27 by Tom Igoe28
29*/30
31// Include the GSM library32#include <MKRGSM.h>33
34#include "arduino_secrets.h"35// Please enter your sensitive data in the Secret tab or arduino_secrets.h36// PIN Number37
38const char PINNUMBER[] = SECRET_PINNUMBER;39
40// initialize the library instance41
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 only55
56 }57
58 Serial.println("SMS Messages Sender");59
60 // connection state61
62 bool connected = false;63
64 // Start GSM shield65
66 // If your SIM has PIN, pass it as a parameter of begin() in quotes67
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 sms92
93 readSerial(remoteNum);94
95 Serial.println(remoteNum);96
97 // sms text98
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 message114
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 serial127
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, through the GSM network. It then prints the content of the page through the serial monitor of the Arduino Software (IDE).
1/*2 Web client3
4 This sketch connects to a website using SSL through a MKR GSM 1400 board. Specifically,5
6 this example downloads the URL "http://arduino.tips/asciilogo.txt" and7
8 prints it to the Serial monitor.9
10 Circuit:11
12 * MKR GSM 1400 board13
14 * Antenna15
16 * SIM card with a data plan17
18 created 8 Mar 201219
20 by Tom Igoe21
22*/23
24// libraries25#include <MKRGSM.h>26
27#include "arduino_secrets.h"28// Please enter your sensitive data in the Secret tab or arduino_secrets.h29// PIN Number30
31const char PINNUMBER[] = SECRET_PINNUMBER;32// APN data33
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 instance41
42GSMSSLClient client;43
44GPRS gprs;45
46GSM gsmAccess;47
48// URL, path and port (for example: arduino.tips)49char server[] = "arduino.tips";50char path[] = "/asciilogo.txt";51int port = 443; // port 443 is the default for HTTPS52
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 only62
63 }64
65 Serial.println("Starting Arduino web client.");66
67 // connection state68
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 password74
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 available129
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, 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 client4
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/" and8
9 prints it to the Serial monitor.10
11 Circuit:12
13 * MKR GSM 1400 board14
15 * Antenna16
17 * SIM card with a data plan18
19 created 8 Mar 201220
21 by Tom Igoe22
23*/24
25// libraries26#include <MKRGSM.h>27
28#include "arduino_secrets.h"29// Please enter your sensitive data in the Secret tab or arduino_secrets.h30// PIN Number31
32const char PINNUMBER[] = SECRET_PINNUMBER;33// APN data34
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 instance42
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 HTTP53
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 only63
64 }65
66 Serial.println("Starting Arduino web client.");67
68 // connection state69
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 password75
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 available130
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 Server4
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 board12
13 * Antenna14
15 * Analog inputs attached to pins A0 through A5 (optional)16
17 created 8 Mar 201218
19 by Tom Igoe20
21*/22
23// libraries24#include <MKRGSM.h>25
26#include "arduino_secrets.h"27// Please enter your sensitive data in the Secret tab or arduino_secrets.h28// PIN Number29
30const char PINNUMBER[] = SECRET_PINNUMBER;31// APN data32
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 instance40
41GPRS gprs;42
43GSM gsmAccess; // include a 'true' parameter for debug enabled44
45GSMServer server(80); // port 80 (http default)46
47// timeout48
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 only60
61 }62
63 // connection state64
65 bool connected = false;66
67 // Start GSM shield68
69 // If your SIM has PIN, pass it as a parameter of begin() in quotes70
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 server92
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 clients107
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 newline135
136 // character)137
138 if (sendResponse) {139
140 // send a standard http response header141
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 pin151
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 delay169
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 Management4
5 This sketch, for the MKR GSM 1400 board, checks the band6
7 currently configured in the modem and allows you to change8
9 it.10
11 Please check http://www.worldtimezone.com/gsm.html12
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 board26
27 Antenna28
29 created 12 June 201230
31 by Javier Zorzano, Scott Fitzgerald32
33*/34
35// libraries36#include <MKRGSM.h>37
38// initialize the library instance39
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 only51
52 }53
54 // Beginning the band manager restarts the modem55
56 Serial.println("Restarting modem...");57
58 band.begin();59
60 Serial.println("Modem restarted.");61
62};63
64void loop() {65
66 // Get current band67
68 String bandName = band.getBand(); // Get and print band name69
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 band87
88 bool operationSuccess;89
90 operationSuccess = band.setBand(newBandName);91
92 // Tell the user if the operation was OK93
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 options112// through the Serial interface113// The user selects one114
115String askUser() {116
117 String newBand;118
119 Serial.println("Select band:");120
121 // Print the different options122
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 buffer140
141 while (Serial.available()) {142
143 Serial.read();144
145 }146
147 // Wait for an answer, just look at the first character148
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 newBand = GSM_MODE_GSM850_EGSM_PCS_UMTS;183
184 } else {185
186 newBand = "GSM_MODE_UNDEFINED";187
188 }189 return newBand;190}
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 Networks4
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 board16
17 * Antenna18
19 * SIM card20
21 Created 8 Mar 201222
23 by Tom Igoe, implemented by Javier Carazo24
25 Modified 4 Feb 201326
27 by Scott Fitzgerald28
29*/30
31// libraries32#include <MKRGSM.h>33
34#include "arduino_secrets.h"35// Please enter your sensitive data in the Secret tab or arduino_secrets.h36// PIN Number37
38const char PINNUMBER[] = SECRET_PINNUMBER;39
40// initialize the library instance41
42GSM gsmAccess; // include a 'true' parameter to enable debugging43
44GSMScanner scannerNetworks;45
46GSMModem modemTest;47
48// Save data variables49
50String IMEI = "";51
52// serial monitor result messages53
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 only65
66 }67
68 Serial.println("GSM networks scanner");69
70 scannerNetworks.begin();71
72 // connection state73
74 bool connected = false;75
76 // Start GSM shield77
78 // If your SIM has PIN, pass it as a parameter of begin() in quotes79
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 parameters97
98 // IMEI, modem unique identifier99
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 networks116
117 Serial.println("Scanning available networks. May take some seconds.");118
119 Serial.println(scannerNetworks.readNetworks());120
121 // currently connected carrier122
123 Serial.print("Current carrier: ");124
125 Serial.println(scannerNetworks.getCurrentCarrier());126
127 // returns strength and ber128
129 // signal strength in 0-31 scale. 31 means power > 51dBm130
131 // BER is the Bit Error Rate. 0-7 scale. 99=not detectable132
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 of4
5 a SIM card inserted into a GSM shield.6
7 Circuit:8
9 * MKR GSM 1400 board10
11 * Antenna12
13 * SIM card14
15 Created 12 Jun 201216
17 by David del Peral18
19*/20
21// libraries22#include <MKRGSM.h>23
24// pin manager object25
26GSMPIN PINManager;27
28// save input in serial by user29
30String user_input = "";31
32// authenticated with PIN code33
34bool auth = false;35
36// serial monitor result messages37
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 only51
52 }53
54 Serial.println("Change PIN example\n");55
56 PINManager.begin();57
58 // check if the SIM have pin lock59
60 while (!auth) {61
62 int pin_query = PINManager.isPIN();63
64 if (pin_query == 1) {65
66 // if SIM is locked, enter PIN code67
68 Serial.print("Enter PIN code: ");69
70 user_input = readSerial();71
72 // check PIN code73
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 incorrected85
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 code93
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 code103
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 incorrect115
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 locked123
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 authentication131
132 Serial.println("No pin necessary.");133
134 auth = true;135
136 }137
138 }139
140 // start GSM shield141
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 roaming151
152 else if (PINManager.checkReg() == 1) {153
154 Serial.println("ROAMING " + oktext);155
156 } else {157
158 // error connection159
160 Serial.println(errortext);161
162 while (true);163
164 }165}166
167void loop() {168
169 // Function loop implements pin management user menu170
171 // Only if you SIM use pin lock, you can change PIN code172
173 // user_op variables save user option174
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 lock192
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 PIN206
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 serial221
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 a4
5 GPRS network. It asks for APN information through the6
7 serial monitor and tries to connect to example.org.8
9 Circuit:10
11 * MKR GSM 1400 board12
13 * Antenna14
15 * SIM card with data plan16
17 Created 18 Jun 201218
19 by David del Peral20
21*/22
23// libraries24#include <MKRGSM.h>25
26#include "arduino_secrets.h"27// Please enter your sensitive data in the Secret tab or arduino_secrets.h28// PIN Number29
30const char PINNUMBER[] = SECRET_PINNUMBER;31
32// initialize the library instance33
34GSM gsmAccess; // GSM access: include a 'true' parameter for debug enabled35
36GPRS gprsAccess; // GPRS access37
38GSMClient client; // Client service for TCP connection39
40// messages for serial monitor response41
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 obtained52
53String response = "";54
55// use a proxy56
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 only68
69 }70}71
72void loop() {73
74 use_proxy = false;75
76 // start GSM shield77
78 // if your SIM has PIN, pass it as a parameter of begin() in quotes79
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 user93
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 user103
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 user113
114 char password[20];115
116 Serial.print("Finally, enter your password: ");117
118 readSerial(password);119
120 // attach GPRS121
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 user133
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 port143
144 int pport;145
146 if (proxy[0] != '\0') {147
148 // read proxy port introduced by user149
150 char proxyport[10];151
152 Serial.print("Enter the proxy port: ");153
154 readSerial(proxyport);155
156 // cast proxy port introduced to integer157
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 request167
168 Serial.print("Connecting and sending GET request to example.org...");169
170 int res_connect;171
172 // if use a proxy, connect with it173
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 URL191
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 server211
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 available223
224 // from the server, read and check them225
226 if (client.available()) {227
228 char c = client.read();229
230 response += c;231
232 // cast response obtained from string to char array233
234 char responsechar[response.length() + 1];235
236 response.toCharArray(responsechar, response.length() + 1);237
238 // if response includes a "200 OK" substring239
240 if (strstr(responsechar, "200 OK") != NULL) {241
242 Serial.println(oktext);243
244 Serial.println("TEST COMPLETE!");245
246 test = false;247
248 }249
250 }251
252 // if the server's disconnected, stop the client:253
254 if (!client.connected()) {255
256 Serial.println();257
258 Serial.println("disconnecting.");259
260 client.stop();261
262 test = false;263
264 }265
266 }267
268 }269}270
271/*272
273 Read input serial274
275 */276int readSerial(char result[]) {277
278 int i = 0;279
280 while (1) {281
282 while (Serial.available() > 0) {283
284 char inChar = Serial.read();285
286 if (inChar == '\n') {287
288 result[i] = '\0';289
290 return 0;291
292 }293
294 if (inChar != '\r') {295
296 result[i] = inChar;297
298 i++;299
300 }301
302 }303
304 }305}
MKR GSM Tools Test Modem
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.
1/*2
3 This example tests to see if the modem of the4
5 MKR GSM 1400 board is working correctly. You do not need6
7 a SIM card for this example.8
9 Circuit:10
11 * MKR GSM 1400 board12
13 * Antenna14
15 Created 12 Jun 201216
17 by David del Peral18
19 modified 21 Nov 201220
21 by Tom Igoe22
23*/24
25// libraries26#include <MKRGSM.h>27
28// modem verification object29
30GSMModem modem;31
32// IMEI variable33
34String IMEI = "";35
36void setup() {37
38 // initialize serial communications and wait for port to open:39
40 Serial.begin(9600);41
42 while (!Serial) {43
44 ; // wait for serial port to connect. Needed for Leonardo only45
46 }47
48 // start modem test (reset and check response)49
50 Serial.print("Starting modem test...");51
52 if (modem.begin()) {53
54 Serial.println("modem.begin() succeeded");55
56 } else {57
58 Serial.println("ERROR, no modem answer.");59
60 }61}62
63void loop() {64
65 // get modem IMEI66
67 Serial.print("Checking IMEI...");68
69 IMEI = modem.getIMEI();70
71 // check IMEI response72
73 if (IMEI != NULL) {74
75 // show IMEI in serial monitor76
77 Serial.println("Modem's IMEI: " + IMEI);78
79 // reset modem to check booting:80
81 Serial.print("Resetting modem...");82
83 modem.begin();84
85 // get and check IMEI one more time86
87 if (modem.getIMEI() != NULL) {88
89 Serial.println("Modem is functioning properly");90
91 } else {92
93 Serial.println("Error: getIMEI() failed after modem.begin()");94
95 }96
97 } else {98
99 Serial.println("Error: Could not get IMEI");100
101 }102
103 // do nothing:104
105 while (true);106}
MKR GSM Tools Test Web Server
This sketch creates a web server to accept incoming connections on the Arduino MKR GSM 1400. Some network providers only allow requests from inside their own network. You will need to check with your network provider to make sure your SIM card will accept incoming HTTP requests.
1/*2
3 Basic Web Server4
5 A simple web server that replies with nothing, but prints the client's request6
7 and the server IP address.8
9 Circuit:10
11 * MKR GSM 1400 board12
13 * Antenna14
15 created16
17 by David Cuartielles18
19 modified 21 Nov 201220
21 by Tom Igoe22
23*/24#include <MKRGSM.h>25
26#include "arduino_secrets.h"27// Please enter your sensitive data in the Secret tab or arduino_secrets.h28// PIN Number29
30const char PINNUMBER[] = SECRET_PINNUMBER;31// APN data32
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 instance40
41GPRS gprs;42
43GSM gsmAccess; // include a 'true' parameter for debug enabled44
45GSMServer server(80); // port 80 (http default)46
47// timeout48
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 Leonardo only60
61 }62
63 Serial.println("starting,..");64
65 // connection state66
67 bool connected = false;68
69 // Start GSM shield70
71 // If your SIM has PIN, pass it as a parameter of begin() in quotes72
73 while (!connected) {74
75 if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&76
77 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {78
79 connected = true;80
81 } else {82
83 Serial.println("Not connected");84
85 delay(1000);86
87 }88
89 }90
91 Serial.println("Connected to GPRS network");92
93 // start server94
95 server.begin();96
97 //Get IP.98
99 IPAddress LocalIP = gprs.getIPAddress();100
101 Serial.println("Server IP address=");102
103 Serial.println(LocalIP);104}105
106void loop() {107
108 GSMClient client = server.available();109
110 if (client) {111
112 if (client.available()) {113
114 Serial.write(client.read());115
116 }117
118 }119
120}
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.