There's a number of Spark tutorials that deal with reading temperature sensors. Today we're taking this a step further: we'll monitor the temperature of a fish tank remotely and have Ubidots alert us whenever it's outside a specified threshold.
1. Materials
A Spark Core.
A Temperature sensor DS18B20.
Some jumper wires
A 4.7k Pull up Resistor
3. Wiring
Follow the next diagram, as you see you need a 4.7kohms resistor between the digital signal and Vcc.
3. Set up your Dashboard.
As a logged user in Ubidots, create a Data source called “Spark Core” and then add a new variable called "Temperature”.
Go to the "Sources" tab and create a Data Source called "Spark Core" by clicking in the 'plus' button located in the upper right corner.
2. Click on the created Data Source and then “Add New Variable”.
3. Please take note of your variable's ID, we need it later.
4. Create a token under "My profile" tab.
5. To make your fish alert you when the temperature is under or above a threshold, you need to create new Event. To do so go to the "Events" tab and add two events; one for the temperature below 21C and another one when it's above 26C. In both cases, choose Spark Core as data source, then Temperature as variable.
First Event
Select SMS
Second event
4. Coding
We will create a new app called "tankTemperature" in the Spark Build IDE, then we need to include in our app the "HTTPCLIENT" Library to send the data to Ubidots, as well as the libraries "OneWire" and "spark-dallas-temperature" to read the DS18B20 sensor. Do not forget to include your Ubidots token and variable's ID into the code.
Create a new App called "tankTemperature" and then click on the "Libraries" icon and look for an HTTP library called “HTTPCLIENT”.
2. Click on the “HTTPCLIENT” library and then click onthe button “INCLUDE IN APP”.
3. Repeat the same steps but this time look for the libraries called "OneWire" and "spark-dallas-temperature".
4. Now go to the presence app, then copy and paste the following code into your code:
// This #include statement was automatically added by the Spark IDE.
#include "HttpClient/HttpClient.h"
// This #include statement was automatically added by the Spark IDE.
#include "OneWire/OneWire.h"
// This #include statement was automatically added by the Spark IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"
#define ONE_WIRE_BUS D2OneWire
oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
float temperature = 0.0;
char resultstr[64];
HttpClient http;
#define VARIABLE_ID "5489bf717625424489244e2a"
#define TOKEN "pSZJt9W7v5W3fpUsRYNKiDMgJ770NK"
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.port = 80;
request.hostname = "industrial.api.ubidots.com";
request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
Serial.begin(9600);
sensor.begin();
}
void loop()
{
sensor.requestTemperatures();
temperature=sensor.getTempCByIndex(0);
sprintf(resultstr, "{\"value\":%.4f}",temperature);
request.body = resultstr;//Sending presence to Ubidots
http.post(request, response, headers);
Serial.println(response.status);
//For debug only
Serial.println(response.body);
delay(1000);
}
view rawDS18B20.ino hosted with ❤ by GitHub
5. Flash the code and wait until the core stops flashing magenta. As you can see, the code is pretty simple for such a great project :)
5. Scaling your Results
A neat Ubidots feature is the ability to transform your data through math expressions. This is called a "Derived Variable", so let's change the measured results from Celsius to Farenheit:
6. Wrapping Up
In this article we covered how to send temperature data from a fish bowl to Ubidots, and then get an SMS alert whenever the temperature went outside a given range. Ubidots helps you rapidly create Internet of Things projects without the need to code web servers, visualizations or APIs. Don't forget to check out other projects using the Spark Core.
Do you have an IoT project in mind? Make it happen in days, not months!
This content was originally published in Ubidots' Blog on December 22, 2014.