Today I’m going to show you how to monitor ambient room temperature with a Raspberry Pi. We’re going to wire up a temperature sensor, gather temperature values and send them to Firebase. This could be the start of a weather station or other crazy experiments. In my case we’re going to check the temperature of a room to make sure our newborn puppies don’t get too hot or cold during the day.
What you’ll need:
- Raspberry Pi (I am using a Raspberry Pi 2, but you don’t have to)
- Temperature Sensor
- A Firebase account (optional)
Hooking up the Hardware
I ordered the DS18B20 Waterproof Digital temperature sensor + extras from Adafruit ( Get it here ) which is an excellent value at $10.
Out of the box the wires are stripped, but since I’m putting this in a bread board, I decided to solder them. I quickly learned my last roll of solder is rather poor quality, but it will work:
I then wired it up to the breadboard. There are plenty of diagrams online for wiring this up, I hooked
- Red: +3v
- Yellow: GPIO 4
- Blue: Ground
The kit comes with a 4.7k resistor which you place between the red and yellow. The breadboard I use is from the Canakit for Raspberry Pi. This is connected to a Raspberry Pi 2, although a Raspberry Pi B would work just the same.
Testing the Sensor
The support for the DS18B20 on the Pi makes this part really trivial. First you need to edit /boot/config.txt:
and add the following line:
Then save and reboot the Pi.
To test the functionality, load in the kernel modules:
Run the following to find your device:
This will show you the directory your device is in, based on your serial number. It should start with 28x:
cd into that directory, then type:
and it should look like this:
Look for crc=da YES. The YES means it’s working, and the t=n is the celcius*1000. This verifies the sensor is functioning.
Writing the Temp Gathering Script.
For this step, you will be creating a Python script to read the sensor. You can use Nano on the Pi, or use an IDE on your desktop and copy the file up to the Pi. I used PyCharm in Linux to write the script then copied it up to my Pi with each iteration with the following command:
This can be a little more fluid when developing.
The initial file we’ll create is some code lifted from the Adafruit tutorials:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
This is a super simple script that loads in the kernel modules and reads the device. The function read_temp() opens the device and reads it by parsing through the lines and spitting out Celsius and Fahrenheit values. Then at the bottom we have a loop to do this every second.
Your output should look like this:
This is great, but we can’t do much with it. At this point you could just write these values to a file and parse them out later. But we’re going to store these values in a datastore.
In readtemp.py, let’s make some changes.
Remove the loop:
1 2 3
and replace it with this:
Then add the following lines to the top:
Run the file again and you should see this:
We’re getting closer! Here is what the final script should look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
Pushing the results to Firebase
This is an optional step, but here we are going to push the results up to Firebase. You can create an account for free, and I would encourage you to check it out. The URL will be the url of the project you create and the name of a JSON file to use for storage. For example:
You can replace with Firebase with any API call at this point, and it’s easy to make a POST in Python:
Add the following includes:
Then add the following to the bottom:
1 2 3 4 5 6 7 8 9 10 11 12
Now run the file a few times, and open up your Firebase console, and you should see something that looks like this:
NOTE: You really should add some authentication to your Firebase database if you care about the data.
Create a CRON Job to Automatically gather data
There’s two ways we can run this script automatically. We can add the loop back in, and just run the Python script and keep it running. You can use something like nohup to run it as a service but that’s a little shaky. I’d rather just run the script once a minute with a CRON job. To do this, type in
to run the cron editor. Add an entry like this:
And save the file. CRON will automatically update and start running this command every minute (or whatever you set it to).
This will ensure that your script is run at regular intervals. You can set it to run every minute, hourly, daily or whatever you’d like. Here’s some more information about scheduling cron jobs.
I hope this tutorial has helped show you how to monitor ambient air temperature with a Raspberry Pi. If you purchase this same sensor it’s waterproof which means there is much more you can do with it. You can also hook up multiple sensors to the Pi, separated by serial numbers.
There are tons of neat things you can do with this information, and I encourage you to experiment. In a future tutorial we’ll take some of this data I’ve gathered and do something with it. Thanks for reading.
You can download the source code for this article at GitHub.
My New Course: Hands on Internet of Things with the Raspberry Pi
I have just released my new course titled ”Hands on Internet of Things” which is intended to be a course for people who want to get started with IoT. I’ll take you through blinking lights and reading sensors to creating YOUR own IoT endpoint and dashboard. I’m offering this special coupon for readers of my site: get %50 off this course here. Thanks for being a reader!