1 | /*
|
---|
2 | Web client
|
---|
3 |
|
---|
4 | This sketch connects to a website through a GSM shield. Specifically,
|
---|
5 | this example downloads the URL "http://arduino.cc/asciilogo.txt" and
|
---|
6 | prints it to the Serial monitor.
|
---|
7 |
|
---|
8 | Circuit:
|
---|
9 | * GSM shield attached to an Arduino
|
---|
10 | * SIM card with a data plan
|
---|
11 |
|
---|
12 | ATTENTION: to work correctly with M0/M0 pro you have to connect pin 2 to pin 4 of the shield
|
---|
13 |
|
---|
14 | created 8 Mar 2012
|
---|
15 | by Tom Igoe
|
---|
16 |
|
---|
17 | http://arduino.cc/en/Tutorial/GSMExamplesWebClient
|
---|
18 |
|
---|
19 | */
|
---|
20 |
|
---|
21 | // libraries
|
---|
22 | #include <GSM.h>
|
---|
23 |
|
---|
24 | // PIN Number
|
---|
25 | #define PINNUMBER ""
|
---|
26 |
|
---|
27 | // APN data
|
---|
28 | #define GPRS_APN "GPRS_APN" // replace your GPRS APN
|
---|
29 | #define GPRS_LOGIN "login" // replace with your GPRS login
|
---|
30 | #define GPRS_PASSWORD "password" // replace with your GPRS password
|
---|
31 |
|
---|
32 | // initialize the library instance
|
---|
33 | GSMClient client;
|
---|
34 | GPRS gprs;
|
---|
35 | GSM gsmAccess;
|
---|
36 |
|
---|
37 | // URL, path & port (for example: arduino.cc)
|
---|
38 | char server[] = "arduino.cc";
|
---|
39 | char path[] = "/asciilogo.txt";
|
---|
40 | int port = 80; // port 80 is the default for HTTP
|
---|
41 |
|
---|
42 | void setup()
|
---|
43 | {
|
---|
44 | // initialize serial communications and wait for port to open:
|
---|
45 | SerialUSB.begin(9600);
|
---|
46 | while (!SerialUSB) {
|
---|
47 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
48 | }
|
---|
49 |
|
---|
50 | SerialUSB.println("Starting Arduino web client.");
|
---|
51 | // connection state
|
---|
52 | boolean notConnected = true;
|
---|
53 |
|
---|
54 | // After starting the modem with GSM.begin()
|
---|
55 | // attach the shield to the GPRS network with the APN, login and password
|
---|
56 | while (notConnected)
|
---|
57 | {
|
---|
58 | if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
|
---|
59 | (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY))
|
---|
60 | notConnected = false;
|
---|
61 | else
|
---|
62 | {
|
---|
63 | SerialUSB.println("Not connected");
|
---|
64 | delay(1000);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | SerialUSB.println("connecting...");
|
---|
69 |
|
---|
70 | // if you get a connection, report back via serial:
|
---|
71 | if (client.connect(server, port))
|
---|
72 | {
|
---|
73 | SerialUSB.println("connected");
|
---|
74 | // Make a HTTP request:
|
---|
75 | client.print("GET ");
|
---|
76 | client.print(path);
|
---|
77 | client.println(" HTTP/1.1");
|
---|
78 | client.print("Host: ");
|
---|
79 | client.println(server);
|
---|
80 | client.println("Connection: close");
|
---|
81 | client.println();
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | // if you didn't get a connection to the server:
|
---|
86 | SerialUSB.println("connection failed");
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void loop()
|
---|
91 | {
|
---|
92 | // if there are incoming bytes available
|
---|
93 | // from the server, read them and print them:
|
---|
94 | if (client.available())
|
---|
95 | {
|
---|
96 | char c = client.read();
|
---|
97 | SerialUSB.print(c);
|
---|
98 | }
|
---|
99 |
|
---|
100 | // if the server's disconnected, stop the client:
|
---|
101 | if (!client.available() && !client.connected())
|
---|
102 | {
|
---|
103 | SerialUSB.println();
|
---|
104 | SerialUSB.println("disconnecting.");
|
---|
105 | client.stop();
|
---|
106 |
|
---|
107 | // do nothing forevermore:
|
---|
108 | for (;;)
|
---|
109 | ;
|
---|
110 | }
|
---|
111 | }
|
---|