Skip to main content
All CollectionsConnect your Devices
Connect a LinkIt ONE to Ubidots using Wi-Fi over HTTP
Connect a LinkIt ONE to Ubidots using Wi-Fi over HTTP

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

Sergio M avatar
Written by Sergio M
Updated over 3 months ago

The LinkIt ONE development board is an open source board for prototyping IoT projects. We really liked 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. Setting up Arduino IDE

  2. Sending (POST) data to Ubidots

  3. Receiving (GET) data from Ubidots

  4. Summary

1. Setting up 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 adding 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”

2. Sending (POST) data to Ubidots

With the following sample code you will be able to post the reading taken from the A0 of the LinkIt One board.

1. To post your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, assign your unique Ubidots TOKEN, SSID (WiFi Name), and the Password of the available network. 

    /*
   Ubidots WiFi client

   This sketch reads an analog input and sends this value to Ubidots

   Change the macro WIFI_AP, WIFI_PASSWORD, WIFI_AUTH, URL, TOKEN and VARIABLE_ID accordingly.

   based on the Web Client example created 13 July 2010
   by dlf (Metodo2 srl)
   modified 31 May 2012
   by Tom Igoe
   modified 20 Aug 2014
   by MediaTek Inc.
   modified 2 Sep 2015
   by Ubidots, Inc.
   modified 6 Oct 2016
   by Ubidots, Inc.
   */

  #include <LTask.h>
  #include <LWiFi.h>
  #include <LWiFiClient.h>

  #define WIFI_AP "MTK Hackathon"
  #define WIFI_PASSWORD ""
  #define WIFI_AUTH LWIFI_OPEN  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.

  // Ubidots information

  #define URL    "industrial.api.ubidots.com"
  #define TOKEN  "Sxxxxxxxxx9m4wX"          // replace with your Ubidots token generated in your profile tab
  #define LABEL "linkit-one"                // your device label


  void setup()
  {
    LTask.begin();
    LWiFi.begin();
    Serial.begin(19200);

    // keep retrying until connected to AP
    Serial.println("Connecting to AP");
    while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
    {
      delay(1000);
    }
  }

  boolean wifi_status(LWifiStatus ws){
    switch(ws){
      case LWIFI_STATUS_DISABLED:
        return false;
      break;
      case LWIFI_STATUS_DISCONNECTED:
        return false;
      break;
      case LWIFI_STATUS_CONNECTED:
        return true;
      break;
    }
    return false;
  }


  void loop()
  {  
    Serial.println("Connecting to Ubidots...");
    LWiFiClient c;
    while (!c.connect(URL, 80))
    {
      Serial.println("Retrying to connect...");
      delay(100);
    }

    Serial.println("Connected!");

    while(1){
      LWifiStatus ws = LWiFi.status();
      boolean status = wifi_status(ws);
      if(!status){
        Serial.println("Connecting to AP");
        while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
        {
          delay(500);
        }
      }
      String payload = "{\"temp\":"+ String(analogRead(A0)) + "}";
      String le = String(payload.length());    

      if(!c.connected()){
        while (!c.connect(URL, 80)){
          delay(100);
        }
        Serial.println("Client reconnected!");
      }  

      // Build HTTP POST request
      c.print(F("POST /api/v1.6/devices/"));
      c.print(LABEL);
      c.println(F(" HTTP/1.1"));
      c.println(F("User-Agent: LinKit One/1.0"));
      c.print(F("X-Auth-Token: "));
      c.println(TOKEN);
      c.println(F("Connection: close"));
      c.println(F("Content-Type: application/json"));
      c.print(F("Content-Length: "));
      c.println(le);
      c.print(F("Host: "));
      c.println(URL);
      c.println();
      c.println(payload);
      c.println();

      int v;
      while(c.available()){  
        v = c.read();
        if(v < 0){
          Serial.println("No response.");
          break;
        }
        // Serial.print((char)v);  // Add this line to the code in test case
      }
      delay(1000);
    }
  }

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 19200.

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

 3. Receiving (GET) data from Ubidots

With the following sample code you will be able to subscribe a value from Ubidots to start controlling any asset you desire. 

1. To begin receiving values from Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, be sure to assign the following parameters:

/*
  Ubidots WiFi client

  This sketch reads an analog input and sends this value to Ubidots

  Change the macro WIFI_AP, WIFI_PASSWORD, WIFI_AUTH, URL, TOKEN and VARIABLE_ID accordingly.

  based on the Web Client example created 13 July 2010
  by dlf (Metodo2 srl)
  modified 31 May 2012
  by Tom Igoe
  modified 20 Aug 2014
  by MediaTek Inc.
  modified 2 Sep 2015
  by Ubidots, Inc.
*/

#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>

#define WIFI_AP "xxxxxxx"
#define WIFI_PASSWORD "xxxxxxx"
#define WIFI_AUTH LWIFI_OPEN  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.

// Ubidots information

#define URL    "industrial.api.ubidots.com"
#define TOKEN  "BBFF-xxxxxxxxx"          // replace with your Ubidots token generated in your profile tab
#define VAR_LABEL "temperature"
#define DEVICE_LABEL "maka-test"

//Variables for String response;
int value_index;
String value_string;
String value;

void setup()
{
  LTask.begin();
  LWiFi.begin();
  Serial.begin(9600);

  // keep retrying until connected to AP
  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
  }
}

boolean wifi_status(LWifiStatus ws) {
  switch (ws) {
    case LWIFI_STATUS_DISABLED:
      return false;
      break;
    case LWIFI_STATUS_DISCONNECTED:
      return false;
      break;
    case LWIFI_STATUS_CONNECTED:
      return true;
      break;
  }
  return false;
}

float get_value(char* variable_label) {

  LWiFiClient c;
  if (c.connect(URL, 80)) {
    Serial.println("Connected to Ubidots");
    String response = "";

    // Build HTTP GET request
    c.print(F("GET /api/v1.6/devices/"));
    c.print(DEVICE_LABEL);
    c.print(F("/"));
    c.print(variable_label);
    c.print(F("/values/?page_size=1&token="));
    c.print(TOKEN);
    c.println(F(" HTTP/1.1"));
    c.println(F("Content-Type: application/json"));
    c.print(F("Host: "));
    c.println(URL);
    c.println();
    int count = 0;
    while (!c.available()  && count < 10000) {
      count++;
      delay(1);
    }

    int v;
    while (c.available()) {
      v = c.read();
      if (v < 0) {
        Serial.println("No response.");
        break;
      }
      Serial.println((char)v);
      response.concat((char)v);
    }
    Serial.println("Printing response:");
    value_index = response.indexOf("\"value\": ");
    value_string = response.substring(value_index);
    value = value_string.substring(9, value_string.indexOf(","));
    Serial.println(value);
    return atof(value.c_str());
  }
  else {
    Serial.println("Couldn't connect to Ubidots");
    return -1;
  }
}

void check_wifi() {

  LWifiStatus ws = LWiFi.status();
  boolean status = wifi_status(ws);
  if (!status) {
    Serial.println("Connecting to AP");
    while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) {
      delay(500);
    }
  }
}


void loop()
{
  check_wifi();
  char* variable_label = VAR_LABEL;
  float last_value = get_value(variable_label);
  Serial.print("The value received is: ");
  Serial.println(last_value);
  delay(1000);
}

2. Verify & Upload the code into the board following the same steps provided in the POST step above.

3. To verify the connectivity of the device and the data which is being received, 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 9600.

4. In the serial monitor, you will be able to see the last value received in Ubidots of the variable specified in the firmware.

4. Summary  

With this simple tutorial you are able to POST & GET data to/from Ubidots with the ease of the Arduino IDE and an LinkIt One. 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?