GSM Web Server

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.

Circuit

Optional analog sensors like photoresistors, potentiometers and such may be connected, as explained elsewhere in our tutorials, to pins A0 - A5

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.

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.

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 true or false. Once connected, the remainder of setup will run.

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.

/*
GSM Web Server

A simple web server that shows the value of the analog input pins.
using a GSM shield.