Skip to main content
All CollectionsIoT Projects Tutorials
DIY Raspberry Pi Temperature System with Ubidots
DIY Raspberry Pi Temperature System with Ubidots

Build your own air and liquid temperature monitoring system using a Raspberry Pi and Ubidots.

Sergio M avatar
Written by Sergio M
Updated over 3 months ago

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

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 

  1. With your Raspberry Pi connected to the internet, verify the IP address assigned to the board access using ssh in your computer's terminal:

ssh pi@{IP_Address_assigned}

If you haven't already configured the credentials of your Raspberry Pi, note that you will have to use the default credentials provided:

  • User Name: pi 

  • Password: raspberry

When your pi is configured and connected  correctly, the user of your terminal becomes listed as:pi@raspberrypi 

2. Now let's upgrade some packages and install pip, Python's packet manager. Copy and paste the below commands into your terminal and press "enter" after each to run the commands. 

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-pip python-dev build-essential 

3. Then, install Request library, which is a popular Python library that simplifies making HTTP requests. Copy and paste the below commands into your terminal and press "enter" run the command. 

$ pip install requests

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'

def temp_raw():
    f = open(temp_sensor, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = temp_raw()
    temp_output = lines[1].find('t=')
    if temp_output != -1:
        temp_string = lines[1].strip()[temp_output+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        payload = {'temp_celsius': temp_c, 'temp_fahrenheit': temp_f}
        return payload

while True:
        try:
            r = requests.post('http://industrial.api.ubidots.com/api/v1.6/devices/raspberry/?token={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 :) 

Did this answer your question?