Note: this page refers to a product that is retired.

GSM Test Web Server

Create a webserver with your GSM shield.

This sketch creates a web server to accept incoming connections on the GSM shield. Some network providers only allow requests from inside their own network. You will need to check with your network provider to make sure your SIM card will accept incoming HTTP requests.

Hardware Required

Circuit

image of the Arduino GSM Shield on top of an Arduino board
image of the Arduino GSM Shield on top of an Arduino board

Code

Complete Sketch

The complete sketch is below.

1/*
2
3 Basic Web Server
4
5 A simple web server that replies with nothing, but prints the client's request
6
7 and the server IP address.
8
9 Circuit:
10
11 * GSM shield attached
12
13 created
14
15 by David Cuartielles
16
17 modified 21 Nov 2012
18
19 by Tom Igoe
20
21 http://www.arduino.cc/en/Tutorial/GSMToolsTestWebServer
22
23 This example code is part of the public domain
24
25 */
26#include <GSM.h>
27
28// PIN Number
29#define PINNUMBER ""
30
31// APN data
32#define GPRS_APN "GPRS_APN" // replace your GPRS APN
33#define GPRS_LOGIN "login" // replace with your GPRS login
34#define GPRS_PASSWORD "password" // replace with your GPRS password
35
36// initialize the library instance
37
38GPRS gprs;
39
40GSM gsmAccess; // include a 'true' parameter for debug enabled
41
42GSMServer server(80); // port 80 (http default)
43
44// timeout
45
46const unsigned long __TIMEOUT__ = 10 * 1000;
47
48void setup() {
49
50 // initialize serial communications and wait for port to open:
51
52 Serial.begin(9600);
53
54 while (!Serial) {
55
56 ; // wait for serial port to connect. Needed for Leonardo only
57
58 }
59
60 Serial.println("starting,..");
61
62 // connection state
63
64 bool connected = true;
65
66 // Start GSM shield
67
68 // If your SIM has PIN, pass it as a parameter of begin() in quotes
69
70 while (!connected) {
71
72 if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
73
74 (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
75
76 connected = true;
77
78 } else {
79
80 Serial.println("Not connected");
81
82 delay(1000);
83
84 }
85
86 }
87
88 Serial.println("Connected to GPRS network");
89
90 // start server
91
92 server.begin();
93
94 //Get IP.
95
96 IPAddress LocalIP = gprs.getIPAddress();
97
98 Serial.println("Server IP address=");
99
100 Serial.println(LocalIP);
101}
102
103void loop() {
104
105 GSMClient client = server.available();
106
107 if (client) {
108
109 if (client.available()) {
110
111 Serial.write(client.read());
112
113 }
114
115 }
116
117}

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.