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. 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.
  • Ubidots TOKEN
  • Variable ID of the variables desired to be updated.
#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 WEBSITE "industrial.api.ubidots.com"
#define URL "/api/v1.6/collections/values/?token={your_ubidots_token}=true"
#define idvariable1 "your_variable_id_here"
#define idvariable2 "your_variable_id_here"
#define idvariable3 "your_variable_id_here"

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

/*
#define WIFI_AP "SOBIOTECH"
#define WIFI_PASSWORD "BIOTECH007"
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
*/

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("Attach 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);
  /*
  LWiFi.begin();
  // keep retrying until connected to AP
  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
  }
  Serial.println("Connected to WiFi");
  */
}

void loop(){
  int temp = analogRead(A0);
  int ph = analogRead(A1);
  int orp = analogRead(A2);
  //save_values(temp, ph, orp);
  save_values2(temp, ph, orp);
  delay(1000);
}

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

  LGPRSClient client;
  //LWiFiClient client;

  sprintf(payload,"%s", "[");
  sprintf(payload,"%s%s", payload, "{\"variable\":\"" idvariable1 "\",\"value\":");
  sprintf(payload,"%s%d", payload, val1);
  sprintf(payload,"%s%s", payload, "},{\"variable\":\"" idvariable2 "\",\"value\":");
  sprintf(payload,"%s%d", payload, val2);
  sprintf(payload,"%s%s", payload, "},{\"variable\":\"" idvariable3 "\",\"value\":");
  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(WEBSITE, 80)){  
    Serial.println("Connected!");  // Console monitoring

    client.print(F("POST "));
    client.print(URL);
    client.print(F(" HTTP/1.1\r\n"));
    client.print(F("Content-Type: application/json\r\n"));
    //client.print(F("Connection: close\r\n"));
    client.print(F("Host: "));
    client.print(WEBSITE);
    client.print(F("\r\n"));
    client.print(F("Content-Length: "));
    client.print(le);
    client.print(F("\r\n"));
    client.print(F("\r\n"));
    client.print(payload);
    client.println();

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

  client.stop();
}

void save_values2(uint8_t val1, uint8_t val2, uint8_t val3){

  Serial.println(val1);
  Serial.println(val2);
  Serial.println(val3);

  memset(packetBuffer, 0, PACKET_SIZE);
  packetBuffer[0] = val1;
  packetBuffer[1] = val2;
  packetBuffer[2] = val3;

  u.beginPacket(IPAddress(50,23,124,66), 1234);  
  u.write(packetBuffer, 3);
  u.endPacket();  // actually send the packet
  Serial.println("Sent");
}

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?