GSM Test GPRS
Test the proper functionality of the GPRS network using your SIM card.
This sketch tests the GPRS data connection on the GSM shield. 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.
Hardware Required
Arduino Board
SIM card enable for Data
Circuit
Code
First, import the GSM library
#include <GSM.h>
SIM cards may have a PIN number that enables their functionality. Define the PIN for your SIM. If your SIM has no PIN, you can leave it blank :
#define PINNUMBER ""
Initialize instances of the classes you're going to use. You're going to need the GSM, GPRS, and GSMClient classes.
1GSMClient client;2
3GPRS gprsAccess;4
5GSM gsmAccess;
Create some status messages to send to the serial monitor:
1String oktext = "OK";2
3String errortext = "ERROR";
Create some variables to hold the server, path, and proxy you wish to connect to.
1char url[] = "arduino.cc";2char path[] = "/";3char urlproxy[] = "http://arduino.cc";
Create a variable to hold a response from the server, and a flag to indicate f you will be using a proxy or not.
1String response = "";2
3boolean use_proxy = false;
In
setup
, open a serial connection to the computer.1void setup(){2
3 Serial.begin(9600);4}
You're going to create a custom function to handle serial input from the serial monitor. make a named function that accepts a
char
array as an argument.1int readSerial(char result[])2{
make a variable to act as a counter. While there is serial information available, read it into the
char
array. If a newline character is encountered, terminate the array and return to the main program.1int i = 0;2
3 while(1)4
5 {6
7 while (Serial.available() > 0)8
9 {10
11 char inChar = Serial.read();12
13 if (inChar == '\n')14
15 {16
17 result[i] = '\0';18
19 return 0;20
21 }22
23 if(inChar!='\r')24
25 {26
27 result[i] = inChar;28
29 i++;30
31 }32
33 }34
35 }36}
In
loop()
, set the proxy flag to false1void loop()2{3
4 use_proxy = false;
Start the connection to the GSM network by passing the PIN number (if applicable) to
gsmAccess.begin()
.1Serial.print("Connecting GSM network...");2
3 if(gsmAccess.begin(PINNUMBER)!=GSM_READY)4
5 {6
7 Serial.println(errortext);8
9 while(true);10
11 }12
13 Serial.println(oktext);
Create a
char
array to hold the APN. Use the readSerial()
function you created to get the bytes from the serial monitor1char apn[50];2
3 Serial.print("Enter your APN: ");4
5 readSerial(apn);6
7 Serial.println(apn);
Create a
char
array to hold the APN login. Use the readSerial()
function you created to get the bytes from the serial monitor1char login[50];2
3 Serial.print("Now, enter your login: ");4
5 readSerial(login);6
7 Serial.println(login);
Create a
char
array to hold the APN password. Use the readSerial()
function you created to get the bytes from the serial monitor1char password[20];2
3 Serial.print("Finally, enter your password: ");4
5 readSerial(password);
Connect to the GPRS network using
gprs.attachGPRS()
. This requires the APN, login, and password you just entered.When the modem has attached itself to the GPRS network,
attachGPRS()
will return GPRS_READY
.1Serial.println("Attaching to GPRS with your APN...");2
3 if(gprsAccess.attachGPRS(apn, login, password)!=GPRS_READY)4
5 {6
7 Serial.println(errortext);8
9 }10
11 else{12
13 Serial.println(oktext);
Create a
char
array to hold any proxy information you may need. Use the readSerial()
function you created to get the bytes from the serial monitor.1char proxy[100];2
3 Serial.print("If your carrier uses a proxy, enter it, if not press enter: ");4
5 readSerial(proxy);6
7 Serial.println(proxy);
If a proxy was indicated, ask for the port number, and set the proxy flag to
true
1int pport;2
3 if(proxy[0] != '\0'){4
5 // read proxy port introduced by user6
7 char proxyport[10];8
9 Serial.print("Enter the proxy port: ");10
11 readSerial(proxyport);12
13 // cast proxy port introduced to integer14
15 pport = (int) proxyport;16
17 use_proxy = true;18
19 Serial.println(proxyport);20
21 }
Create a variable to indicate if you are connected to the server or not. With
client.connect()
, make a connection to the server. How you make the connection will depend on if you are using a proxy or not.1Serial.print("Connecting and sending GET request to arduino.cc...");2
3 int res_connect;4
5 if(use_proxy)6
7 res_connect = client.connect(proxy, pport);8
9 else10
11 res_connect = client.connect(url, 80);
if you have connected, make a HTTP GET request using
client.print()
.1if (res_connect)2
3 {4
5 client.print("GET ");6
7 if(use_proxy)8
9 client.print(urlproxy);10
11 else12
13 client.print(path);14
15 client.println(" HTTP/1.0");16
17 client.println();18
19 Serial.println(oktext);20
21 }
If no connection was made, print out an error
1else2
3 {4
5 // if you didn't get a connection to the server6
7 Serial.println(errortext);8
9 }
Check to see if the server returned any bytes using
client.available()
. If there are, read them into the response
String, then cast them into a char array. Check the array for the substring "200 OK", which indicates a valid response from arduino.cc.1Serial.print("Receiving response...");2
3 boolean test = true;4
5 while(test)6
7 {8
9 if (client.available())10
11 {12
13 char c = client.read();14
15 response += c;16
17 char responsechar[response.length()+1];18
19 response.toCharArray(responsechar, response.length()+1);20
21 if(strstr(responsechar, "200 OK") != NULL){22
23 Serial.println(oktext);24
25 Serial.println("TEST COMPLETE!");26
27 test = false;28
29 }30
31 }
If the server has disconnected, stop the client and close
loop()
1if (!client.connected())2
3 {4
5 Serial.println();6
7 Serial.println("disconnecting.");8
9 client.stop();10
11 test = false;12
13 }14
15 }16
17 }18}
Once your code is uploaded, open the serial monitor to see the status of the connection.
Complete Sketch
The complete sketch is below.
1/*2
3 This sketch test the GSM shield's ability to connect to a4
5 GPERS network. It asks for APN information through the6
7 serial monitor and tries to connect to arduino.cc.8
9 Circuit:10
11 * GSM shield attached12
13 * SIM card with data plan14
15 Created 18 Jun 201216
17 by David del Peral18
19 This example code is part of the public domain20
21 http://www.arduino.cc/en/Tutorial/GSMToolsTestGPRS22
23 */24
25// libraries26#include <GSM.h>27
28// PIN Number29#define PINNUMBER ""30
31// initialize the library instance32
33GSM gsmAccess; // GSM access: include a 'true' parameter for debug enabled34
35GPRS gprsAccess; // GPRS access36
37GSMClient client; // Client service for TCP connection38
39// messages for serial monitor response40
41String oktext = "OK";42
43String errortext = "ERROR";44
45// URL and path (for example: arduino.cc)46char url[] = "arduino.cc";47char urlproxy[] = "http://www.arduino.cc";48char path[] = "/";49
50// variable for save response obtained51
52String response = "";53
54// use a proxy55
56bool use_proxy = false;57
58void setup() {59
60 // initialize serial communications and wait for port to open:61
62 Serial.begin(9600);63
64 while (!Serial) {65
66 ; // wait for serial port to connect. Needed for Leonardo only67
68 }69}70
71void loop() {72
73 use_proxy = false;74
75 // start GSM shield76
77 // if your SIM has PIN, pass it as a parameter of begin() in quotes78
79 Serial.print("Connecting GSM network...");80
81 if (gsmAccess.begin(PINNUMBER) != GSM_READY) {82
83 Serial.println(errortext);84
85 while (true);86
87 }88
89 Serial.println(oktext);90
91 // read APN introduced by user92
93 char apn[50];94
95 Serial.print("Enter your APN: ");96
97 readSerial(apn);98
99 Serial.println(apn);100
101 // Read APN login introduced by user102
103 char login[50];104
105 Serial.print("Now, enter your login: ");106
107 readSerial(login);108
109 Serial.println(login);110
111 // read APN password introduced by user112
113 char password[20];114
115 Serial.print("Finally, enter your password: ");116
117 readSerial(password);118
119 // attach GPRS120
121 Serial.println("Attaching to GPRS with your APN...");122
123 if (gprsAccess.attachGPRS(apn, login, password) != GPRS_READY) {124
125 Serial.println(errortext);126
127 } else {128
129 Serial.println(oktext);130
131 // read proxy introduced by user132
133 char proxy[100];134
135 Serial.print("If your carrier uses a proxy, enter it, if not press enter: ");136
137 readSerial(proxy);138
139 Serial.println(proxy);140
141 // if user introduced a proxy, asks him for proxy port142
143 int pport;144
145 if (proxy[0] != '\0') {146
147 // read proxy port introduced by user148
149 char proxyport[10];150
151 Serial.print("Enter the proxy port: ");152
153 readSerial(proxyport);154
155 // cast proxy port introduced to integer156
157 pport = (int) proxyport;158
159 use_proxy = true;160
161 Serial.println(proxyport);162
163 }164
165 // connection with arduino.cc and realize HTTP request166
167 Serial.print("Connecting and sending GET request to arduino.cc...");168
169 int res_connect;170
171 // if use a proxy, connect with it172
173 if (use_proxy) {174
175 res_connect = client.connect(proxy, pport);176
177 } else {178
179 res_connect = client.connect(url, 80);180
181 }182
183 if (res_connect) {184
185 // make a HTTP 1.0 GET request (client sends the request)186
187 client.print("GET ");188
189 // if use a proxy, the path is arduino.cc URL190
191 if (use_proxy) {192
193 client.print(urlproxy);194
195 } else {196
197 client.print(path);198
199 }200
201 client.println(" HTTP/1.0");202
203 client.println();204
205 Serial.println(oktext);206
207 } else {208
209 // if you didn't get a connection to the server210
211 Serial.println(errortext);212
213 }214
215 Serial.print("Receiving response...");216
217 bool test = true;218
219 while (test) {220
221 // if there are incoming bytes available222
223 // from the server, read and check them224
225 if (client.available()) {226
227 char c = client.read();228
229 response += c;230
231 // cast response obtained from string to char array232
233 char responsechar[response.length() + 1];234
235 response.toCharArray(responsechar, response.length() + 1);236
237 // if response includes a "200 OK" substring238
239 if (strstr(responsechar, "200 OK") != NULL) {240
241 Serial.println(oktext);242
243 Serial.println("TEST COMPLETE!");244
245 test = false;246
247 }248
249 }250
251 // if the server's disconnected, stop the client:252
253 if (!client.connected()) {254
255 Serial.println();256
257 Serial.println("disconnecting.");258
259 client.stop();260
261 test = false;262
263 }264
265 }266
267 }268}269
270/*271
272 Read input serial273
274 */275int readSerial(char result[]) {276
277 int i = 0;278
279 while (1) {280
281 while (Serial.available() > 0) {282
283 char inChar = Serial.read();284
285 if (inChar == '\n') {286
287 result[i] = '\0';288
289 return 0;290
291 }292
293 if (inChar != '\r') {294
295 result[i] = inChar;296
297 i++;298
299 }300
301 }302
303 }304}
Last revision 2018/08/23 by SM
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.