Getting Started with the Arduino GSM Shield 2

Arduino + GSM SIM Card? Absolutely!

Today we’ll be talking about the Arduino GSM Shield 2. Welcome to the ultimate “getting started” tutorial. This board allows the user to make phone calls, send SMS, and even access the internet by using the integrated Quectel M10 modem.

Quectel M10 Modem
Quectel M10 Modem

It features a download speed of 84,6 kbps (wow! :p) and uses the old GPRS network to get access to the internet.

Quick setup

Obviously, we need to grab an activated SIM card, know its PIN code, and make sure that we are not incurring in any other costs.

SIM Cards

We also need to know our carrier APN. The APN is the network access point, a gateway that mobile phone carriers use to connect you to the internet. This information is normally available to the public, but as phones download automatically these settings from the GSM network, it may be a bit tough to find. Solution: use a google search: ” apn”. And solved! In the code below, the Vodafone APN for Spain is used.

Getting Started

Let’s try to download a single page to our device. First off, we need to decide which page we’re going to download, and the purpose of downloading it right away to our arduino. We might want to download the current timestamp from an internet server, the weather of a specific place, or make complex interactions with server-side scripts. We can even upload our data to a server (communication is bidirectional) and develop an IoT device. Anyways, for this simple example we will download a simple page: the arduino’s latest version txt file located at http://arduino.cc/latest.txt.


//Import the GSM library
#include <GSM.h>

// PIN Number
#define PINNUMBER "1234"

// APN information obrained from your network provider
#define GPRS_APN       "ac.vodafone.es" // replace with your GPRS APN
#define GPRS_LOGIN     "vodafone"    // replace with your GPRS login
#define GPRS_PASSWORD  "vodafone" // replace with your GPRS password

// initialize the library instances
GSMClient client;
GPRS gprs;
GSM gsmAccess;

// This example downloads the URL "http://arduino.cc/latest.txt"

char server[] = "arduino.cc"; // the base URL
int port = 80; // the port, 80 for HTTP

void setup()
{
  // initialize serial communications
  Serial.begin(9600);
  Serial.println("Starting Arduino web client.");
  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // pass the PIN of your SIM as a parameter of gsmAccess.begin()
  while(notConnected)
  {
    if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("Connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port))
  {
    Serial.println("Connected");
    // Make a HTTP request:
    client.print("GET /latest.txt HTTP/1.1\r\nHost: arduino.cc\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36\r\n\r\n");
  } 
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("Connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}

Two important remarks: do NOT break your query down into several parts like this:

//The following will cause a "400 Bad Request" error 95% of the time

client.print("GET ");
client.print(path);
client.println(" HTTP/1.0");
client.println();

And last, but not least, do not use “client.println”, for maximum compatibility use “client.print” and add a final “\r\n” right after your request header.

Test it, and leave your comments!

Aerospace Engineer
Aeronautical Engineer. My dream is to work for a leading company in the aerospace field to develop projects that can make the world a better place; improving people's life, safety, and comfort. I am interested in Aerospace Engineering, big data, CFD, photography, automatic control, maths and electronics.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.