Skip to main content
All CollectionsIoT Projects Tutorials
Logging Temperature and Humidity with Spark Core and Ubidots
Logging Temperature and Humidity with Spark Core and Ubidots

In this Spark Core Tutorial we will connect the Spark core to Ubidots, logging temperature and humidity variables in the cloud.

David Sepúlveda avatar
Written by David Sepúlveda
Updated over 2 months ago

The Spark Core is an open source, cloud-powered development platform. It's tiny but powerful, and it is easy to program because it's Arduino-compatible.

In this Spark Core Tutorial we will connect the Spark core to Ubidots, logging temperature and humidity variables in the cloud.

1. Materials

  • A DHT11 humidity and temperature sensor or similar

  • 10k resistor

  • Some jumper wires

2. Wiring

Follow the diagram below to setup the hardware.

3. Coding

  • First of all, as a logged user in Ubidots, create a Data Source called "Spark Core" and add two new variables (temperature and humidty) to it. Take note of your token and variable id.

  • Create a new app called "send-value-ubidots" in the Spark Build IDE.

  • Click on the “Libraries” icon in the botton left corner and look for an HTTP library called “HTTPCLIENT”:

  • Click on the “HTTPCLIENT” library and then click on “INCLUDE IN APP”

  • Click again on the Libraries icon. This time look for a library called "Adafruit_dht"

  • Click on the “ADAFRUIT_DHT” library and then click on “INCLUDE IN APP”

  • Now copy and paste the following code into your “send-value-ubidots” application (don't forget to replace the TOKEN and VARIABLES ID with the ones from your Ubidots account):


//This #include statement was automatically added by the Spark IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"

// This #include statement was automatically added by the Spark IDE.
#include "HttpClient/HttpClient.h"
HttpClient http;

#define VARIABLE_ID "53f6caea76254277ef8837cd"
#define VARIABLE_ID2 "53f63fa576254235bec59148"
#define TOKEN "cfaI5R1bcr7wlwf58hAnNZNNvfSoUk"

int lightLevel = 0;

#define DHTPIN 2 // what pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11

//DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a // very slow sensor)
// Read temperature as Celsius
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {{ "Content-Type", "application/json" }, { "X-Auth-Token" , TOKEN }, { NULL, NULL }};
// NOTE: Always terminate headers will NULL};

http_request_t request;
http_response_t response;

void setup()
{
request.hostname = "industrial.api.ubidots.com";
request.port = 80;
Serial.begin(9600);
dht.begin();
}

void loop()
{
float h = dht.getHumidity();
float t = dht.getTempCelcius();
request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
request.body = "{\"value\":" + String(t) + "}";
http.post(request, response, headers);
Serial.println(response.body);
request.path = "/api/v1.6/variables/"VARIABLE_ID2"/values";
request.body = "{\"value\":" + String(h) + "}";
http.post(request, response, headers);
Serial.println(response.body);
Serial.print("la temperatura es: ");
Serial.println(t);
Serial.print("la humedad relativa es: ");
Serial.println(h);
delay(2000);
}

view rawtempSpark.ino hosted with ❤ by GitHub

4. Create live widgets to display the data

  •  In your dashboard  click on "Add New Widget".

  • Add two line charts, choosing temperature and humidity as variables.

  • Click on the share icon and select embed code, then copy the iframe tag. You can paste this in your own web page!

  • This is how it looks in a local web page I just created:

5. Create new data using a math formula

One of the nice things about Ubidots is how easy it is to create math formulas to transform the original data into other variables.

For example, let's convert between Celsuis and Farenheit degrees. To do this, just create a new variable and check the box "This is a derived variable", then enter your formula:

Pro Tip: This feature is quite useful when calibrating your sensor for a given offset addition or subtraction and re-calibration as needed. 

6. Wrapping Up

In this post we learn how to connect a Spark core with to Ubidots, Measure physical variables, and sharing your outcomes. Now you can try another widgets like "metric", "gauge" and "multi-line chart." In just a few steps, you can add nice and clean graphs to your projects.

Don't forget to check out some of our latest posts:

If you're new to Ubidots, click below to get started:

Content originally published in Ubidots Blog on August 8, 2014. 

Did this answer your question?