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