The temperature of your room, visible on a gauge on the internet. For a few bucks. Uses Python, Dataplicity and Freeboard.
So you’ve bought a Raspberry Pi, but now what? In this tutorial I’ll show you how to build an internet connected Raspberry Pi that shows you the ambient temperature on a pretty dashboard, wherever you are.
Time: 1–3 hours — This will involve setting up your Pi, wiring up a breadboard, writing some code, setting up a dashboard and a way for your Pi to be visible on the internet.
Cost: ~ 10 US Dollars in accessories (on top of your Raspberry Pi).
Thanks to ModMyPi which provided some of the methods used in this article.
What you’ll need
The links are to Adafruit where you can buy the parts.
- Raspberry Pi B+ or newer You could use an A, but you’ll need to sort out a network adapter. Use a Pi 3 if you’d like it to be wireless.
- 3 x Jump Male->Female cables To connect your breadboard to your Pi, I recommend one red, one black and one yellow.
- 2 x red Male->Male jump cables or pieces of wire [one bundle will fulfil all] To connect the power rail to components.
- 1 x black Male->Male jump cable or piece of wire To connect the ground rail to the One Wire Digital Thermometer
The usual Pi accessories:
- USB Keyboard and Mouse or at least one of the two.
- A MicroSD card with NOOBS or a pre-installed copy of Raspbian.
- Ethernet cablenot necessary for the Raspberry Pi 3 with WiFi.
- Raspberry Pi power supply or USB port + MicroUSB cable with adequate current supply to run your Pi normally.
How we’re going to do it:
- Wire up your circuit on your breadboard [10–20 minutes]
- Install Raspbian OS (if you haven’t already done so) [20–30 minutes]
- Setup a way of seeing your Pi on the internet securely [5 minutes]
- Final wiring [5 minutes]
- Code to read the thermal sensor [15 minutes]
- Setup a dashboard to see your temperature [10–20 min]
[Beginner tip] What is a breadboard?
A breadboard (sometimes known as a plugboard) is a reusable board used to connect electronic components together without needing to solder or tie wires together. It makes it easy to quickly build circuits which you can keep built or disassemble to reuse components.A breadboard comprises of “rails”. All the pins (sockets) along each rail are connected to each other, so if you’d like to connect one component to another, simply place the pin of one component on the same rail as the pin of another component. Be careful not to connect the pins of one component to the same rail as you might short something!There are two types of rails; power rails run vertically along the edges of the board. Regular rails run horizontally. It’s a bit confusing at first, but don’t worry as you get used to building you’ll see why they are made like that.Power rails are marked with positive and negative signs like the terminals on a regular battery. On circuit diagrams you might sometimes see negative referred to as “GND” for ground. Positive however, can be at different voltages on the Raspberry Pi, so be careful to connect to the right one!
1. Wiring up your circuit
[Beginner tip] Don’t let circuit diagrams scare you, this circuit is actually pretty simple — just two components and six wires. When building circuits we usually start with the most complex component and then build around it. In this case it’s the one wire digital thermometer.
- Position your breadboard in front of you portrait (letters at the top, numbers readable from top to bottom).
- Insert the one wire digital thermometer with its pins lined up vertically with the flat side of the thermometer on the left — I did this on the top right side of the board as in the picture below, don’t worry about bending the pins.
- Insert the resistor into a pin along the rail for the middle pin (pin 2) of the thermometer and a pin far below the thermometer. Resistors can be inserted in either direction.
- Now connect a black wire from the rail for the top pin (pin 1) to the negative (ground) power rail on the right. I’ve used a blue wire because I was missing a black.
- Lastly connect a red wire from the rail along the bottom pin (pin 3) to the positive power rail on the left and another one from the rail connecting to the bottom of the resistor to the positive power rail.
OK that’s it! We’ll come back to connecting it to your Pi after we’ve setup your Pi.
2. Install Raspbian OS
Skip this if you’ve already got Raspbian (the default operating system for Raspberry Pis) installed on your Pi. You may also be able to use other operating systems.
You will need a MicroSD card with a copy of NOOBS ready to go to do this. You may already have this if you bought a Pi package, or you can make your own MicroSD card with it by following the instructions on raspberrypi.org: https://www.raspberrypi.org/documentation/installation/noobs.md
[Beginner tip] The Raspberry Pi’s MicroSD card slot is located on the reverse side of the board. If you’re looking at the bottom of the board, the text on the MicroSD card will be facing you as you insert it. When you do, there should be a satisfying click. To remove the card, simply push the card further in at which point it will bounce out.
- Insert your MicroSD card with NOOBS installed into your Pi.
- Hook up your monitor (HDMI port), keyboard (USB), mouse (USB), and network (Ethernet port). The Ethernet cable should hook up to a router.
- Hook up your power supply (MicroUSB port on the corner of the board). After a few seconds you should be presented with some text flashing away on your screen and then the install screen below.
- On the install screen, select “Raspbian” and press the Install button on the top left. You will be presented with a warning that all existing data on your MicroSD card will be overwritten — you don’t need to worry about this unless you have some of your own precious data on the card.
- Enjoy a cup of tea, coffee, juice, water or anything you like to drink because this will take about 15 minutes.
- Follow instructions to finish install.
3. Make your Raspberry Pi visible on the net
In this step we’re using a free service called Dataplicity to make your Pi accessible from anywhere on the internet. Dataplicity allows us to access the Pi’s “shell” from a web browser wherever you are and also allows us to host a simple web server visible on the internet. The latter is needed to allow us to build our pretty gauge that we can access anywhere. We’re using Dataplicity here to avoid having to configure fiddly network settings or VPNs.
[Beginner tip] A shell (or “terminal”) is a way of interacting with a computer whereby you type commands to make them happen. On a Raspberry Pi with Raspbian installed, you can open a shell by clicking on the Terminal on the toolbar of the window manager on the top.
- Visit dataplicity.com
- To create an account enter an email address at which point you’ll be given some text that you can copy and paste into your Raspberry Pi’s shell.
- Click on the Terminal icon on the top left of the screen (see below).
- Paste or type the code from the Dataplicity signup into your Pi. This will install the Dataplicity Client onto your Pi — as with any software you install from the internet, you should feel comfortable that you trust the source.
- After the client is installed, you should see a new Pi in your Dataplicity dashboard. Click on it and after a few seconds you should see a button to “Enable wormhole” near the top. Click on it and keep a copy of the URL that is shown as you’ll need it in Section 6.
Congratulations, you’ve now got a Pi you can access from the internet! We’ll get back to this after writing some code.
4. Wire up!
It’s finally time to hook up your breadboard to your Pi! You’ll be needing to connect three jump wires from your breadboard up to the pins that stick up along the top of your Pi.
This will match up with the circuit diagram back in Step 1. For each of the three wires which we have colour coded, you’ll need to plug the “female” end (er.. the one with the hole) of your jump lead onto the Pin on the Pi with the male (yup, the pointy one) end into the breadboard.
[Beginner’s tip] Knowing which pin is which on the Pi is a little confusing at first. Here’s a diagram to help. Around the pin “header” there’s a white line drawn on the board, in one of the four corners there’s a notch. This notch tells you which way around to read the pin numbers.
Before doing this I recommend you turn off your Raspberry Pi to make sure you don’t accidentally get wires crossed.
- Red jump lead: connect it from the red (positive +) breadboard power rail to Pin 1 on your Pi (see photos and diagram).
- Black jump lead: connect it from the blue/black (negative -) breadboard power rail to Pin 9 on your Pi.
- Yellow jump lead: connect it from the rail of the middle pin of the thermometer to pin 7 on the Pi.
Your Pi is now wired up and ready to go! The next step is to run some code to read the temperature from your Pi and make it available via the internet.
5. Write some code to read your temperature sensor
To keep this tutorial short, I’m providing some code I made earlier, if you’d like to have a tutorial on how this code works, please leave a comment!
- Head back to dataplicity.com and open up your device’s shell. Switch to a user which has super powers and make a folder for your code. Type in the text shown in bold below into your Dataplicity shell. The password is “raspberry”.
In Linux/*nix a super power user is often referred to as a “root” user or a user with “sudo” privileges. This user is one who is allowed to write/delete files on any part of the system. On Raspbian OS the “pi” user is setup with “sudo powers”.
[email protected]:/$ su piPassword: ⌷raspberry[email protected]:/ $ mkdir ~/projects[email protected]:/ $ cd ~/projects
2. Download a copy of the temperature serving code This will download a copy of the code we’re going to use. You can see the code here before downloading too.
[email protected]:/ $ git clone https://github.com/timfernando/temperature-serve-pi.git
3. Then change directory into the folder we’ve just downloaded.
4. Install the software dependencies required. For simplicity in this tutorial, we’re installing these requirements across the system, in a later article we’ll talk about virtual environments for Python code which provide a better way.
5. Run the web server and your temperature gauge is now on the net!
That’s it! You should see something like this:
Starting gunicorn 19.4.5Listening at: http://0.0.0.0:80
You’re now good to move onto the next step. If you need your Pi temperature gauge to be always on even after a restart, leave a comment below and I’ll look at writing a more detailed article for this.
6. Show your temperature on a pretty gauge
OK you’ve made it so far, great! In this step we’ll add the final glue to setup your temperature gauge. For this part, we’ll be using Freeboard, a free service that helps you visualize metrics from your Pi easily.
- Head over to https://freeboard.io and sign up for a free account if you haven’t already got one.
- Add a new board — a board is a dashboard where you can add multiple gauges and widgets. In this tutorial we’ll only be adding one — your temperature reading.
- Add a Datasource– Set the Type to JSON>- Name it (I’ve called mine “Temperature”)- Paste the URL that you recorded from Dataplicity in Section 4- Turn OFF “Try Thingproxy”- Refresh every 1 seconds
- Now add a pane which will hold your Temperature Gauge.
- On the pane, add a widget with the Gauge type.- Give it a title- Hit the Datasource button and click through to select either Celcius or Fahrenheit- Add a label for the units you’ve chosen – Add a minimum and a maximum value (because my Pi is indoors I selected between 10 and 35 degrees celsius).
Et voila! There you are, try touching the top of your temperature gauge and a few seconds later you should see your temperature reading going up! I hope you enjoyed building your thermometer, if you’ve got any questions, please leave a comment below and please recommend if you liked it.