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
Arduino Board
SIM card enable for Data
Circuit
Code
Complete Sketch
The complete sketch is below.
1/*2
3 Basic Web Server4
5 A simple web server that replies with nothing, but prints the client's request6
7 and the server IP address.8
9 Circuit:10
11 * GSM shield attached12
13 created14
15 by David Cuartielles16
17 modified 21 Nov 201218
19 by Tom Igoe20
21 http://www.arduino.cc/en/Tutorial/GSMToolsTestWebServer22
23 This example code is part of the public domain24
25 */26#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
38GPRS gprs;39
40GSM gsmAccess; // include a 'true' parameter for debug enabled41
42GSMServer server(80); // port 80 (http default)43
44// timeout45
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 only57
58 }59
60 Serial.println("starting,..");61
62 // connection state63
64 bool connected = true;65
66 // Start GSM shield67
68 // If your SIM has PIN, pass it as a parameter of begin() in quotes69
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 server91
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.