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.
- A Spark Core.
- A Temperature sensor DS18B20.
- Some jumper wires
- A 4.7k Pull up Resistor
Follow the next diagram, as you see you need a 4.7kohms resistor between the digital signal and Vcc.
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.i>
- Click on the created Data Source and then “Add New Variable”.
- Please take note of your variable's ID, we need it later.
- Create a token under "My profile" tab.
Now we're ready to receive data from the Spark! Let's code the device.
We will create a new app called "tankTemperature" in the Spark Build IDE, then we need to include the "HTTPCLIENT" Library in our app to be able to send the data to Ubidots, as well as the libraries "OneWire" and "spark-dallas-temperature" to read the DS18B20 sensor.
Please note that the code has two defined constants: TOKEN and VARIABLE_ID. Replace their values with the ones noted above.
1. 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. Remember that the code has two defined constants: TOKEN and VARIABLE_ID. Replace their values with the ones noted above.
5. Flash the code and wait until the core stops flashing magenta. You should now be able to see your temperature data in your Ubidots account. As you can see, the code is pretty simple for such a great project :)
Now to get an SMS whenever the aquarium's temperature is under or above a threshold, you need to create an Event. To do so, go to the "Events" tab and add two events; one for the temperature below 21ºC and another one when it's above 26ºC. In both cases, choose Spark Core as data source, then Temperature as variable.
- First Event
- Select SMS
- Second event
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:
In this article we covered how to send temperature data from an aquarium 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.