Skip to main content
All CollectionsConnect your Devices
Connect an Arduino UNO + GSM Shield to Ubidots over HTTP
Connect an Arduino UNO + GSM Shield to Ubidots over HTTP

Learn to setup and connect the Arduino Uno board + GSM Shield to POST data over TCP/UDP to/from Ubidots.

Sergio M avatar
Written by Sergio M
Updated over a week ago

Arduino is an open-source electronics platform based on easy-to-use hardware and software. The primary board for Arduino is the Arduino UNO which connects to a series of different shields to expand it’s possibilities.  

The Arduino UNO is a microcontroller board based on the ATmega328P, which contains everything needed to support the microcontroller including simple power supplies via a USB cable, an AC-to-DC adapter, or by battery. For official overviews of the Arduino UNO please refer to its documentation here.

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

Following this guide you will be able to POST data to/from Ubidots using the Arduino GSM Shield connected to an Arduino Board in just a couple of minutes!

Requirements

Step-by-Step 

 1. Hardware Setup
2. Setting up the Arduino IDE
3. Sending (POST) Data to Ubidots  
4. Summary

 1. Hardware Setup

 1. Insert the SIM card with an activated data plan to the GSM Shield.

2. Place the GSM Shield atop of the Arduino UNO and apply gentle pressure until the shield's pins and the board's headers at tightly connected.  

NOTE:  Check out these notes from Arduino to work with an Arduino Mega, Mega ADK, or Leonardo if is desired.  

IMPORTANT NOTE: 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. At these levels, your laptop or PC's USB port will not be able to support the power load. 

2. Setting up the Arduino IDE

The library used in the code below comes pre installed in the Arduino IDE so you don't need to do any extra setup at this point. If is your first time working with Arduino we recommend you take a quick look of this guide

The code used is based on an Arduino Example which can be simply found here. We recommend you refer to the link to have a better understanding of each section of the code and learn how to build a correct HTTP request to reach the internet. As this is the a 3rd party code, the below code has been updated to fit the standardization of Ubidots sample code structure. For additional details to structuring an HTTP request to Ubidots, checkout
Ubidots REST API Reference.

3. Sending (POST) Data to Ubidots  

Using the following sample code you will be able to post the ANALOG readings taken from Arduino UNO analog port A0.

1. Go to the device section of your Ubidots account and create a new device with name desired to fit your App's nomenclature.

2. With the device already created, create a new raw variable in the device. This variable will receive the analog pin data received from the A0 sensor of the Arduino UNO board. 

3 To be able to establish the communication with the variable created in the previously step, you will need to assign the variable ID in the sample code below. At this point, if you don't know how to get the variable ID, please refer to this guide.  

4. Now is time to POST data in Ubidots. To post your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign the following parameters: 

  • APN (Access Point Name) of your cellular provider with username and password

  • Variable ID of the variable desired to be obtained

     /*
         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 = "variable_id_here";   // replace with your Ubidots Variable ID
        String token = "ubidots_token_here";  // 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("industrial.api.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: industrial.api.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);
          }
        }

2. Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon; press it to verify your code. 

3. Upload the code into your Arduino UNO + GSM Shield. To do this, choose the "right-arrow" icon beside the "check mark" icon. 

4. To verify the connectivity of the device and the data sent, open the serial monitorby selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs. 

5. Confirm your data in Ubidots. Now you should see the posted data in your Ubidots account, located the device and variable assigned in your code.

4. Summary

With this simple tutorial we are able to POST data to Ubidots with the ease of the Arduino IDE and an Arduino UNO + GSM Shield. If your desire send more than one variable to Ubidots, reference Ubidots REST API to learn how to build the request properly. :) 

Now its time to create Ubidots Dashboards to visualize your data and deploy your internet connected monitoring solution!  

Other readers have also found useful...

Did this answer your question?