The Arduino Yun is a microcontroller board with the traditional Arduino chip (ATmega32u4) and an Atheros AR9331 that runs a Linux distro based on OpenWrt. The Ubidots-Arduino-YUN is an Arduino library available to help connect and interacting with sensor data using Ubidots IoT Application Development and Deployment Platform.  

IMPORTANT NOTE: During the tests of this sample library we noticed that due to the hardware architecture the library presents inconsistencies making a POST and a GET request inside the same code. If you find the solution to this inconsistency, we invite you to make the Pull Request for our review.

Requiremets

NOTE: Ubidots puts time and resources to make this open source library work. However, we can’t be responsible for its stability since it runs on third-party products. Please contact the hardware manufacturer directly for any device-related inquiries.

Step-by-Step

 1. Setting up the Arduino IDE
2. Sending (POST) Data to Ubidots  
3. Receiving (GET) Data from Ubidots
4. Summary

1. Setting up the Arduino IDE

This guide assumes your Yun is connected to a WiFi network with Internet access. If not there yet, check Arduino’s guide to configure Yun’s WiFi connectivity.

1. Download the Ubidots Arduino YUN Library and install it. For a detailed explanation of how to install libraries using the Arduino IDE, refer to this guide

2. Sending (POST) Data to Ubidots  

With the following sample code you will be able to post the ANALOG readings taken from Arduino YUN Board analog port.

1. To post your first dot to Ubidots from the hardware, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign your unique Ubidots TOKEN where indicated in the sample code. 

// This example is to save a variable to the Ubidots API

/****************************************
 * Include Libraries
 ****************************************/
#include <UbidotsYUN.h>

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "YOUR_TOKEN_HERE"
#define VARIABLE_LABEL "temperature"  // Change for your variable label desired

Ubidots client(TOKEN);

/****************************************
 * Auxiliar Functions
 ****************************************/

//Put here your auxiliar functions

/****************************************
 * Main Functions
 ****************************************/
void setup() {
  client.init();
  Serial.begin(9600);
}

void loop() {
  float value = analogRead(A0);
  client.add(VARIABLE_LABEL, value); // Change for your variable name
  client.sendAll();
  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 Arduino YUN. To do this, choose the "right-arrow" icon beside the "check mark" icon. 

4. To verify the connectivity of the device and that data is being 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. 

5. Confirm your data in Ubidots. Return to your Ubidots account to see the posted data in your Ubidots account, automatically having created a new device, called "yun," and any variables located the device.

3. Receiving (GET) Data from Ubidots

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

1. To start getting 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:

// This example is to obtain a variable value from Ubidots

/****************************************
 * Include Libraries
 ****************************************/
#include <UbidotsYUN.h>

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "YOUR_TOKEN_HERE"
#define DEVICE_LABEL "device_label_here" // Assign the device containing the variable you want to GET
#define VARIABLE_LABEL "variable_label_here" // Assign the variable you want to GET

Ubidots client(TOKEN);

/****************************************
 * Auxiliar Functions
 ****************************************/

//Put here your auxiliar functions

/****************************************
 * Main Functions
 ****************************************/
void setup() {
  client.init();
  Serial.begin(9600);
}

void loop() {
  float value = client.getValue(DEVICE_LABEL, VARIABLE_LABEL);
  Serial.print("the value obtained is: ");
  Serial.println(value);
  /* Print value with 5 decimal points precision */
  //Serial.println(value, 5);
  delay(5000);
}

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 that the data 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. 

4. At this point, you should be able to see the last value received in Ubidots of the variable specified in the sample code above.  

4. Summary 

With this simple tutorial we are able to POST & GET data to/from Ubidots with the ease of the Arduino IDE and an Arduino YUN. If your wish to find more examples to handle context or timestamp values in your request checkout Ubidots documentation with the Arduino YUN by clicking here

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?