This sketch turns the Arduino or Genuino board with the GSM shield and a data enabled SIM card into a web server. When the board receives a request from a connected client, it sends back the value of analog inputs 0-5.
Not all network operators allow incoming data requests from outside their network. This means you can create a web server with the GSM shield, but you may not be able to connect to it from the public internet; only from another data enabled device from the same provider on the same network. You should check with your provider to see what specific policies they have in place regarding incoming data connections.
image of the Arduino GSM Shield on top of an Arduino or Genuino board
Optional analog sensors like photoresistors, potentiometers and such may be connected, as explained elsewhere in our tutorials, to pins A0 - A5
First, import the GSM library
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.
Initialize instances of the classes you're going to use. You're going to need the GSM, GPRS, and GSMServer classes. When you instantiate the GSMServer class, you'll need to tell it which port to listen for incoming connections. Port 80 is the default port for HTTP requests.
setup, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.
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 :
Connect to the network by calling
gsmAccess.begin(). It takes the SIM card's PIN as an argument. You'll also connect to the GPRS network using
gprs.attachGPRS(). This requires the APN, login, and password you declared earlier. By placing this inside a
while() loop, you can continually check the status of the connection and wait for them to both become
true before proceeding.
When the modem does connect and has attached itself to the GPRS network,
gsmAccess() will return
GSM_READY. Use this as a flag to set the
notConnected variable to
false. Once connected, the remainder of
setup will run.
Start the server using
server.begin(). You can request the server's IP address with
grps.getIPAddress() and end the setup.
loop, create an instance of
GSMClient and check if there are any active connections
While the client is connected, and there is data waiting to be read, begin to read the request. Read through the available bytes until a newline character has been received.
In this instance, you won't actually do anything with the request, it's assumed that it is a HTTP request, and you'll serve up a web page.
Once the request has been read, start to send a standard HTTP response header with
Read through the analog inputs and send the values to the client.
Send a closing tag for the webpage, and stop the client connection before closing the
Once your code is uploaded, open the serial monitor. Once the IP address is printed to the serial monitor, enter it into a web browser. You should see a webpage that reports the analog input values on each the Arduino's six inputs.
if you cannot connect to the IP address, make sure your network operator enables incoming traffic.
The complete sketch is below.
Last revision 2015/08/13 by SM