Arduino Yun

Ubidots-Arduino-YUN is an Arduino library for interacting with Ubidots through its API. 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.

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

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.

Setup

  1. 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.
  2. Go to the Arduino IDE and make sure you can upload Arduino sketches to the Yun. For example, go to File -> Examples -> 01.Basics –> Blink, check under Tools menu that the board is set to “Arduino Yun” and the port is pointing to your board (either through WiFi or serial), then upload the Blink sketch to your board. If it’s all working, you should see the LED13 pin blinking in your board.
  3. Download the ubidots-arduino-yun library.
  4. In your Arduino IDE, click on Sketch -> Include Library -> Add .ZIP Library.
  5. Select the downloaded .ZIP file and then click “Accept” or “Choose”.
  6. Close the Arduino IDE and open it again.

This library creates a Ubidots Device named “YUN” when you send variables. There the library will save your variables.

Send values to Ubidots

To send multiple values you can use our example in our library or copy the next code and do not forget to change set there your TOKEN

// 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);
}

Get one value from Ubidots

To get the last value of a variable from Ubidots, go to Sketch -> Examples -> UbidotsYUN library and select the “UbidotsGetValue” example.

// 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 "yun" // Assign the device label where the variable desired is located
#define VARIABLE_LABEL "temperature" // Assign the variable label desired to obtain data

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);
}

Send a value with latitude and longitude

The library has a feature to add extra information inside of a variable. In the next example, the code will send a Temperature value and inside of the variable will be saved an extra information: latitude and longitude.

Ubidots has a widget to show the position in a map. To show the position this widget need to receive the latitude and longitude in a specific format. The format is “lat” for latitude and “lng” for longitude.

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

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

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

char str_lat[15];
char str_lng[15];

Ubidots client(TOKEN);

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

//Put here your auxiliar functions

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

void loop() {
 
  char context[50];
 
  float lat = 25.761681; // Assign the Latitude desired
  float lng = -80.191788; // Assign the Longitude desired
 
  /* float value is copied onto str_lat/str_lng */
  dtostrf(lat, 4, 6, str_lat);
  dtostrf(lng, 4, 6, str_lng);
 
  /* Build the coordinates context to be sent*/
  sprintf(context, "lat=%s\\$lng=%s", str_lat, str_lng);
  /* Send variable with coordinates context to Ubidots*/
  client.add(VARIABLE_LABEL, 1, context);
  client.sendAll();
  delay(1000);
}
Did this answer your question?