A temperature monitoring system provides valuable insights in both commercial and industrial environments to reduce inefficiencies or maintain quality of products and their quality. What if I told you that you can monitor the temp of your self-built wine-cellar or your family's aquarium at home using the same device. Further, what if I told you that the same device could be used to monitor air and liquid temperatures of fluids at your factory too? The makers of our world have made this possible and this guide is here to help kickstart your own initiatives at home or on the shop floor.
This guide will be your tutorial for a simple DIY temperature monitoring system that is also waterproof to boot. Using a Raspberry Pi and Ubidots we'll show you how to connect your Pi and display in real-time your temperature system's metrics. Using Ubidots, you can also create emails or SMS events to ensure your "variable" (in this case, the temperature) remains within a set of defined limits assigned by you to ensure quality and efficiency of your system's conditions.
For this project we are going to use a 1-wire pre-wired and waterproof version of the DS18B20 sensor. What is 1-wire? It's a communication protocol that makes connecting your IoT sensors simpler by aggregating all cabling into is a single wire (...well actually it's three, two are ground and power connections for energy, the third being the 1-wire for data transmission).
IMPORTANT NOTE: The 1-Wire temperature sensor has different versions for sale; one with a resistor integrated into the sensor and the other without. When purchasing or setting up your hardware, best to make sure your devices and sensors are compatible prior to moving further in this tutorial.
Requirements
- Raspberry Pi 3 Model (Already configured)
- OneWire Temperature Sensor - DS18B20
- Ubidots account -or- Educational License
IMPORTANT NOTE: This guide assumes your Raspberry Pi has been configured and is connected to the Internet. If not, you can quickly do so using this quick start guide from the Raspberry Pi Foundation.
Wiring Setup
As previously mentioned, the OneWire temperature sensor is sold with different versions containing resistors. For this tutorial, we will illustrate both versions–with and without a resister. No matter which you choose for your system, make sure to double check any connections are properly based on the below diagrams and photos.
With resistor integrated - with grove connector
Please follow the table below to make the right connections for your OneWire temperature sensor with resistor.
TIP: The Arduberry is new campaign in Kickstarter, which brings a simple and inexpensive way to bring Arduino shields to the Raspberry Pi. This incredible option is the easy way to start connecting your grove sensors using an Arduino Grove shield. For more information about this, please reference to the campaing :)
Without resistor integrated- without grove connector
The resistor in this setup is used as a pull-up for the data-line, and should be connected between the data wire and the power wire. This ensures that the data line is at a defined logic level, and limits interference from electrical noise if our pin was left floating.
Use a 4.7kΩ (or 10kΩ) resistor and follow the diagram below to make the correct connections. Note that the pins connected in the Raspberry Pi are the same used in the table above:
Sensor Setup
-
4. The Raspberry Pi comes equipped with a range of drivers for interfacing. In this case, to be able to load the 1-Wire sensor's driver on the GPIO pins, we have to use these below two drivers. These drivers are therefore stored as loadable modules and the command modprobe is employed to boot them into the Linux kernel when required.
Run the commands below:
$ sudo modprobe w1-gpio
$ sudo modprobe w1-therm
5. Now, we need to change the directory to our 1-Wire device folder and list the devices in order to ensure that our sensor has loaded correctly. Copy and paste the below commands into your terminal and press "enter" after each to run the commands.
$ cd /sys/bus/w1/devices/
$ ls
At this moment you sensor has already been assembled and connected and should be listed as a series of numbers and letters. In our case, the device is registered as
28-00000830fa90 , but your case will be a different series of letters and numbers, so replace our serial number with your own and run the command.
$ cd 28-00000830fa90
The sensor periodically writes to the
w1_slave file, to read your temp sensor, please run the command below:
$ cat w1_slave
This command will show you two lines of text with the output
t= showing the temperature in degrees Celsius. Please note that a decimal point should be placed after the the first two digits (this is provided in the final code- do not worry); for example, the temperature reading we've received is
29.500 degrees Celsius.
Now that you are able to take temperatures readings, it is time to post them to Ubidots!
Sending data to Ubidots for visualization
Now it is time to code! :)
Create and run a Python script in your computer's terminal:
$ nano onewire_temp_ubidots.py
Then paste and save the below code to your terminal:
import os
import time
import requests
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = '/sys/bus/w1/devices/28-00000830fa90/w1_slave'
payload = {'temp_celsius': temp_c, 'temp_fahrenheit': temp_f}
return payload
while True:
try:
r = requests.post('{Assign_your_Ubidots_Token}', data=read_temp())
print('Posting temperatures in Ubidots')
print(read_temp())
except:
pass
time.sleep(10)
Make sure to replace the serial number
28-00000830fa90 with yours, and assign
your Ubidots account token in the request URL. If you don't know how to get your Ubidots Token, please reference the article below for help:
Completed code terminal window:
Now let's test the script. Paste and run the below script in your computer's terminal.
python onewire_temp_ubidots.py
If it is working properly, you will see a new device in your Ubidots account with two variables:
temp_celsius and
temp_fahrenheit
Optional Steps: Rename the Device and Variables
The names of the variables created are the same as the API labels, which are the IDs used by the API. This doesn't mean their names can't be changed, so it is recommended to change the names of your devices and variables to make them friendlier to your nomenclature. To learn how to rename your variables names, see below:
You can also add and adjust the units of each variable from your list of options:
As you can see below, we've assigned different units to the each variable, and also assigned more friendly names to fit our projects nomenclature. This is highly recommended to users seeking deployments of 100s or devices.
Event Setup
An event (or alert) is any action triggered when data fulfills or exceeds a design rule. For example, an email or SMS message can be sent anytime a sensor stops sending data or a temperature exceeds a maximum or minimum threshold.
To create the event, please reference the article below:
Result
In just a few minutes you've built an easy DIY temperature monitoring system. Now place your sensors where needed and start tracking temperatures from your device today!
Happy hacking :)