All Collections
Connect your Devices
Connect a LinkIt ONE to Ubidots using GPRS over HTTP
Connect a LinkIt ONE to Ubidots using GPRS over HTTP

Learn to setup and connect the popular LinkIt ONE board with Ubidots.

Isabel Lopez avatar
Written by Isabel Lopez
Updated over a week ago

The LinkIt ONE development board is an open source board for prototyping IoT projects. We really like the fact that it already includes built-in connectivity like GSM, GPRS, Wi-Fi, Bluetooth and GPS!

By following this guide you will be able to POST data to Ubidots using the LinkIt One module 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 into the Linkit One Board.

2. Attach the GSM antenna in the respective connector located on the back of the board.

2. Setting up the Arduino IDE

1. To be able to work with the LinkIt One in the Arduino IDE, install the MediaTek platform using the preconfigured Arduino Board Manager. If you are not familiar with installing a board with the Arduino IDE, refer to this article for additional guidance.

2. With the MediaTek platform installed, select the LinkItOne device you are working with. In the case, we are working with a “LinkIt One”. To select your board from the Arduino IDE, select Tools > Board “LinkIt One”

3. Sending (POST) Data to Ubidots

With the following sample code you will be able to post the readings taken from the A0, A1 and A2 of the LinkIt One board.

1. To post your first values in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, assign the following parameters: 

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

#include <LGPRS.h>      //include the base GPRS library
#include <LGPRSClient.h>  //include the ability to Post and Get information using HTTP
#include <LGPRSUdp.h>
#include <LWiFi.h>
#include <LWiFiClient.h>

// These are the variables you will want to change based on your IOT data streaming account / provider

#define HOST "industrial.api.ubidots.com"
#define URL "/api/v1.6/devices/"
#define DEVICELABEL "linkit-one"
#define TOKEN "YOUR-UBIDOTS-TOKEN-HERE"


#define APN "" // APN of your cellular provider
#define APN_USER ""
#define APN_PASS ""

const int PACKET_SIZE = 3;
byte packetBuffer[PACKET_SIZE];
LGPRSUDP u;


char payload[180];  // Reserve a char to store the Ubidots data. Account for 60 bytes per variable.
char le[4];
String response;
int temp;

void setup()
{
  Serial.begin(115200);             // setup Serial port
  pinMode(13, OUTPUT);
  Serial.println("Attaching to GPRS network");   // Attach to GPRS network - need to add timeout
  while (!LGPRS.attachGPRS(APN,APN_USER,APN_PASS)) {
    delay(500);
  }
  Serial.println("GPRS attached!");
  delay(10000);
 
}

void loop(){
  int temp = analogRead(A0);
  int ph = analogRead(A1);
  int hum = analogRead(A2);
  save_values(temp, ph, hum);
 
  delay(3000);
}

void save_values(int val1, int val2, int val3){

  LGPRSClient client;
  //LWiFiClient client;

  sprintf(payload,"%s", "{");
  sprintf(payload,"%s%s", payload, "{\"temperature"\":");
  sprintf(payload,"%s%d", payload, val1);
  sprintf(payload,"%s%s", payload, ",");
  sprintf(payload,"%s%s", payload, "\"ph\":");
  sprintf(payload,"%s%d", payload, val2);
  sprintf(payload,"%s%s", payload, ",");
  sprintf(payload,"%s%s", payload, "{\"humidity\":");
  sprintf(payload,"%s%d", payload, val3);
  sprintf(payload,"%s%s", payload, "}");

  // Get length of the entire payload
  sprintf(le,"%d", strlen(payload));

  Serial.println("Sending: ");  
  Serial.println(payload);

  // Connect to Ubidots...

  if (client.connect(HOST, 80)){  
    Serial.println("Connected!");  // Console monitoring

    client.print(F("POST "));
    client.print(URL);
    client.print(DEVICELABEL);
    client.println(F(" HTTP/1.1"));
    client.println(F("User-Agent: LinKit One/1.0"));
    client.print(F("X-Auth-Token: "));
    client.println(TOKEN);
    client.println(F("Connection: close"));
    client.println(F("Content-Type: application/json"));
    client.print(F("Content-Length: "));
    client.println(le);
    client.print(F("Host: "));
    client.println(HOST);
    client.println();  
    client.println(payload);
    client.println();

  } else {
    Serial.println(F("Connection failed"));
  }

  while (client.connected()) {
        while (client.connected() && !client.available()); // wait
        char c = client.read();
        Serial.print(c);
  }

  client.stop();
}

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 “LinkIt One”. 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 monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs. 

NOTE: If no response is seen, try unplugging the LinkIt One and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200.

5. Confirm the data in Ubidots. Now you should see the posted data in your Ubidots account, located the device called "linkit-one"

4. Summary  

With this simple tutorial you are able to POST data to Ubidots with the ease of the Arduino IDE and an LinkIt One and GPRS connectivity. If you desire to handle context or timestamp values in your request checkout Ubidots REST API and modify the request built in the sample code provided.

Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution!  Happy Hacking! :) 

Other readers have also found useful...

Did this answer your question?