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!

Requirements

To complete this tutorial all you need is the LinkIt ONE board:

Setup

  1. Download Arduino IDE version 1.6.5 or higher, here.
  2. From within the Arduino IDE, go to File -> Preferences dialog box.
  3. Look at the text entry field called “Additional Boards Manager URLs:”.
  4. Just copy/paste the following URL into that text field “http://download.labs.mediatek.com/package_mtk_linkit_index.json”. Then click OK to close.
  5. Now select the Tools -> Board -> Board Manager menu from the Arduino IDE, and it will open up the Boards Manager window. From there, scroll down until you see the LinkIt ONE device.
  6. Now choose a LinKit ONE board from the Tools->Board menu and program your LinkIt ONE!

To use this code, put your Ubidots token and variable ID where indicated, as well as the WiFi settings. After uploading each example to your Arduino, open the Serial monitor to check the results. If no response is seen, try unplugging your 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.

Send values to Ubidots

Copy the following code into your Arduino IDE:

    /*
   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    "things.ubidots.com"
  #define TOKEN  "Sxxxxxxxxx9m4wX"          // replace with your Ubidots token generated in your profile tab
  #define LABEL "my-device"                // 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);
    }
  }

Get a value from Ubidots

Copy the following code into your Arduino IDE:

   /*
   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 "MTK Hackathon"
  #define WIFI_PASSWORD ""
  #define WIFI_AUTH LWIFI_OPEN  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.

  // Ubidots information

  #define URL    "things.ubidots.com"
  #define TOKEN  "SdUrtLvwGFlkSUQVIHlVcYguD9m4wX"          // replace with your Ubidots token generated in your profile tab
  #define VAR_LABEL "temp"
  #define DEVICE_LABEL "my-device"

  //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* VAR_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(VAR_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();
    float last_value = get_value(VAR_LABEL);
    delay(1000);
  }
Did this answer your question?