The Arduino GSM Shield connects your Arduino to the internet using the GPRS wireless network. Just plug this module onto your Arduino board, put a SIM card from an operator offering GPRS coverage and follow a few simple instructions to start measuring your world through the internet.

Requirements

 Check out these notes from Arduino to work with an Arduino Mega, Mega ADK, or Leonardo.

Connect to a external power supply that can provide between 700mA and 1000mA. The modem can pull up to 2A of current at peak usage, which can occur during data transmission.

Setup

  1. Open your Arduino IDE
  2. Click on Files -> Examples -> GSM ->GsmWebClient
  3. This example is too similiar to our code, try to understand it

Send one value to Ubidots

The following sketch will use the GSM Shield to send an arbitrary value to Ubidots. You can replace this value with any analogue input value depending on your project:

     /*
         Based on the example "Web client" from Arduino

         Circuit:
         * GSM shield attached to an Arduino
         * SIM card with a data plan

         created 8 Mar 2012
         by Tom Igoe
         - Modified 15 Nov 2014
         by Mateo Velez for Ubidots,Inc.

         http://arduino.cc/en/Tutorial/GSMExamplesWebClient

         */

        // libraries
        #include <GSM.h>

        // PIN Number
        #define PINNUMBER ""

        // APN data
        #define GPRS_APN       "GPRS_APN" // replace with your GPRS APN
        #define GPRS_LOGIN     "login"    // replace with your GPRS login
        #define GPRS_PASSWORD  "password" // replace with your GPRS password

        // initialize the library instance
        GSMClient client;
        GPRS gprs;
        GSM gsmAccess;
        uint16_t reset = 0;


        String idvariable = "53baaf3c76254244e1c8e408";   // replace with your Ubidots Variable ID
        String token = "CCN8FrVulRYGulPTkbaiR9Myx8qN2o";  // replace with your Ubidots token

        void setup(){
          // initialize serial communications and wait for port to open:
          Serial.begin(9600);
          while (!Serial){
            ; // wait for serial port to connect. Needed for Leonardo only
          }
          Serial.println("Starting Arduino web client.");
          // connection state
          boolean notConnected = true;
          // After starting the modem with GSM.begin()
          // attach the shield to the GPRS network with the APN, login and password
          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);
            }
          }      
        }

        void loop(){
          int value = analogRead(A0);
          if(save_value(value)){
          }
          else{
            reset++;
            if (reset == 10){
              asm volatile ("  jmp 0");    // reset the Arduino board if the data transmission fail
            }
          }      
          // if the server's disconnected, stop the client:
          if (!client.available() && !client.connected()){
            Serial.println();
            Serial.println("disconnecting.");
            client.stop();
            // do nothing forevermore:        
          }
        }
        boolean save_value(int value){
          Serial.println("connecting...");        
          int num=0;
          String var = "{\"value\":"+ String(value) + "}";
          num = var.length();
          // if you get a connection, report back via serial:
          if (client.connect("things.ubidots.com", 80)){
            Serial.println("connected");
            // Make a HTTP request:
            client.print("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1\nContent-Type: application/json\nContent-Length: "+String(num)+"\nX-Auth-Token: "+token+"\nHost: things.ubidots.com\n\n");
            client.println(var);
            client.println();          
          }else{
            // if you didn't get a connection to the server:
            Serial.println("connection failed");
            return false;
          }
            while (client.available())
          {        
            char c = client.read();
            Serial.print(c);
          }
        }
Did this answer your question?