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