GSM Web Client
Download the content of a website to your Arduino board through GPRS.
This sketch connects an Arduino board to the Arduino homepage, http://arduino.cc, through the GSM shield. It then prints the content of the page through the serial monitor of the Arduino Software (IDE).
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 ""Define a number of constants that contain information about the GPRS network you're going to connect to. You'll need the Access Point Name (APN), login, and password. To obtain this information, contact your 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#define GPRS_APN       "GPRS_APN"2#define GPRS_LOGIN     "login"3#define GPRS_PASSWORD  "password"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 gprs;4
5GSM gsmAccess;Create some variables to hold the server, path, and port you wish to connect to.
1char server[] = "arduino.cc";2char path[] = "/";3int port = 80;In
setup1void setup(){2
3  Serial.begin(9600);4
5  Serial.println("Starting Arduino web client.");Create a local variable to track the connection status. You'll use this to keep the sketch from starting until the SIM is connected to the network :
1boolean notConnected = true;Connect to the network by calling
gsmAccess.begin()gprs.attachGPRS()while()trueWhen the modem does connect and has attached itself to the GPRS network,
gsmAccess()GSM_READYnotConnectedtruefalsesetup1while(notConnected)2
3  {4
5    if(gsmAccess.begin(PINNUMBER)==GSM_READY)6
7        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))8
9      notConnected = false;10
11    else12
13    {14
15      Serial.println("Not connected");16
17      delay(1000);18
19    }20
21  }Attempt to connect to the server using
client.connect()connect()client.print()print1if (client.connect(server, port))2
3  {4
5    Serial.println("connected");6
7    client.print("GET ");8
9    client.print(path);10
11    client.println(" HTTP/1.0");12
13    client.println();14
15  }If the connection to the server failed, make a note of it in the serial monitor, and close up
setup1else2
3  {4
5    Serial.println("connection failed");6
7  }8}Inside
loop1if (client.available())2
3  {4
5    char c = client.read();6
7    Serial.print(c);8
9  }If the server disconnects, which it will typically do once it has completed the HTTP request, stop the client locally and close the
loop1if (!client.available() && !client.connected())2
3  {4
5    Serial.println();6
7    Serial.println("disconnecting.");8
9    client.stop();10
11    // do nothing forevermore:12
13    for(;;)14
15      ;16
17  }18}Once your code is uploaded, open the serial monitor. You should see the HTML of http://arduino.cc print out on screen when it is received. It will takes about one minute, so don't worry if you haven't a feedback immediately!
Complete Sketch
The complete sketch is below.
1/*2
3  Web client4
5 This sketch connects to a website through a GSM shield. Specifically,6
7 this example downloads the URL "http://arduino.tips/asciilogo.txt" and8
9 prints it to the Serial monitor.10
11 Circuit:12
13 * GSM shield attached to an Arduino14
15 * SIM card with a data plan16
17 created 8 Mar 201218
19 by Tom Igoe20
21 http://www.arduino.cc/en/Tutorial/GSMExamplesWebClient22
23 */24
25// libraries26#include <GSM.h>27
28// PIN Number29#define PINNUMBER ""30
31// APN data32#define GPRS_APN       "GPRS_APN" // replace your GPRS APN33#define GPRS_LOGIN     "login"    // replace with your GPRS login34#define GPRS_PASSWORD  "password" // replace with your GPRS password35
36// initialize the library instance37
38GSMClient client;39
40GPRS gprs;41
42GSM gsmAccess;43
44// URL, path & port (for example: arduino.cc)45char server[] = "arduino.tips";46char path[] = "/asciilogo.txt";47int port = 80; // port 80 is the default for HTTP48
49void setup() {50
51  // initialize serial communications and wait for port to open:52
53  Serial.begin(9600);54
55  while (!Serial) {56
57    ; // wait for serial port to connect. Needed for native USB port only58
59  }60
61  Serial.println("Starting Arduino web client.");62
63  // connection state64
65  bool notConnected = true;66
67  // After starting the modem with GSM.begin()68
69  // attach the shield to the GPRS network with the APN, login and password70
71  while (notConnected) {72
73    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &74
75        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {76
77      notConnected = false;78
79    } else {80
81      Serial.println("Not connected");82
83      delay(1000);84
85    }86
87  }88
89  Serial.println("connecting...");90
91  // if you get a connection, report back via serial:92
93  if (client.connect(server, port)) {94
95    Serial.println("connected");96
97    // Make a HTTP request:98
99    client.print("GET ");100
101    client.print(path);102
103    client.println(" HTTP/1.1");104
105    client.print("Host: ");106
107    client.println(server);108
109    client.println("Connection: close");110
111    client.println();112
113  } else {114
115    // if you didn't get a connection to the server:116
117    Serial.println("connection failed");118
119  }120}121
122void loop() {123
124  // if there are incoming bytes available125
126  // from the server, read them and print them:127
128  if (client.available()) {129
130    char c = client.read();131
132    Serial.print(c);133
134  }135
136  // if the server's disconnected, stop the client:137
138  if (!client.available() && !client.connected()) {139
140    Serial.println();141
142    Serial.println("disconnecting.");143
144    client.stop();145
146    // do nothing forevermore:147
148    for (;;)149
150      ;151
152  }153}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.