Build an Inexpensive Network of Web-Connected Temperature Sensors using Pi Zeros

Initial State
Initial State
Published in
7 min readMar 22, 2019
Network Temperature Sensor

Temperature and humidity sensors can be invaluable data collection tools when deployed in your home, office, lab, manufacturing line, killer robots, etc. If your project requires multiple temperature/humidity sensors spread across multiple locations, there is a good chance you need something reliable, compact, accurate, and affordable. If you also want your sensors logging to a web-based dashboard and/or sending you SMS/email alerts, then this tutorial is for you. In this tutorial, we will show you how to quickly set up a sensor network to monitor temperatures across any environment that has wi fi. Let’s get started!

Raspberry Pi temperature sensor network video tutorial

The Hardware

A perfect application for a $14 Raspberry Pi Zero WH is to Network Temperature Sensors as this device is compact, inexpensive, powerful, and has built-in WiFi. Here is the list of everything we need per sensor node and the approximate cost (minus shipping):

  • Raspberry Pi Zero WH ($14)
  • Micro SD card ($4)
  • Raspberry Pi power supply ($8)
  • DHT22 Temperature/Humidity Sensor ($5, two-pack for $10 here)
  • (Optional) Raspberry Pi Zero W case ($6)

The setup for each sensor node is going to cost ~$31 plus shipping (~$37+shipping with the case). You can easily get each item above in bulk to minimize shipping costs with the exception of the Raspberry Pi Zero WH, which may be more challenging outside of the UK. As of the time of this writeup, it is hard to find a vendor that allows more than one Zero WH per order except for several UK vendors that have either no limit or a limit of 10. We are using the $14 Zero WH instead of the $10 Zero W for two reasons. 1) No one is allowed to sell more than one unit per order for Zero W per Raspberry Pi rules (boo!) and 2) The Zero WH has the header pre-soldered, which will make our project assembly super quick and easy.

We are using the DHT22 temperature/humidity sensor because of its temperature accuracy (+/- 0.5 °C), humidity range (0–100%), and low cost. We also want something really easy to wire up without having to add a pull-up resistor or anything funky.

Assembly

The DHT22 will have three pins that you will need to connect to your Pi Zero WH — 5V, Gnd, and data. There should be a pin label for power on the DHT22 (e.g. ‘+’ or ‘5V’). Connect this to pin 2 (the top right pin, 5V) of the Pi Zero WH. The Gnd pin will be labeled ‘-’ or ‘Gnd’ or something equivalent. Connect this to pin 6 Gnd (two pins below the 5V pin) on the Zero WH. The remaining pin on the DHT22 is the data pin and will be labeled ‘out’ or ‘s’ or ‘data’. Connect this to one of the GPIO pins on the Zero WH such as GPIO4 (pin 7). You connections should look like the following:

Network Temperature Sensor

With the Pi Zero WH snapped into the case and the micro SD card inserted:

Raspberry Pi Zero Network Temperature Sensor

Software Setup

You will need a monitor and keyboard to setup your Pi Zero WH the first time. Once it is setup, you won’t need either a monitor or a keyboard to run when deployed in your space. We want to keep each node as small and compact as possible.

For this project you’ll need to use Python 3. Adafruit has deprecated their DHT Python library and now uses the CircuitPython-DHT library.

  1. You need to install the standard Raspbian operating system so your Pi Zero WH will boot. You can follow the instructions at https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up to setup your Pi Zero WH.
  2. Connect your Pi Zero WH to your WiFi network https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up/6 .
  3. Install the Adafruit CircuitPython-DHT library at a command prompt to make reading DHT22 sensor data super easy (info on this module):
$ pip3 install adafruit-circuitpython-dht
$ sudo apt-get install libgpiod2

You have everything you need to communicate with your sensor. Next, you need a destination for your sensor data so you can turn that data into an awesome dashboard or an SMS/email alert. We will use Initial State for this step of the project (free for students, $99/yr for every one else). You can follow the detailed instructions here for registering for an account and installing the Initial State Python streaming module. This module will make sending data to your dashboard super easy. The short version:

  1. Register for an account at https://iot.app.initialstate.com .
  2. Install the ISStreamer module at a command prompt (info on this module):
$ sudo pip3 install ISStreamer

Python Script

With our o/s installed along with our two Python modules for reading sensor data and sending data to our Initial State account, we are ready to write our Python script. The following script will create/append to an Initial State data bucket (a collection of data streams grouped together so you can view them in a dashboard), read DHT22 sensor data, and send that data to your real-time dashboard. All you need to do is modify lines 6–11.

https://gist.github.com/25be959d124f4b4c86f7160cf916f4d4.git

The value on line 7should be unique for each node. This could be your sensor node’s room name, physical location, unique identifier, or whatever. Just make sure it is unique for each node to ensure that the data from this node goes to its own data stream in your dashboard.

The name of the data bucket is specified on line 8. This can be changed at any time in the Initial State UI (more info).

The bucket key on line 9 needs to be the same bucket key for every node you want displayed in the same dashboard (more info).

Your Initial State account access key has to be specified on line 10. Copy+paste this key from your Initial State account (more info).

The time between sensor reads is specified on line 11. Change accordingly.

You can specify metric or imperial units on line 12.

After you have set lines 7–12 in your Python script on your Pi Zero WH, kick off the script:

$ python3 tempsensor.py

Repeat these steps for each sensor node. As long as each node is sending data to Initial State using the same access key and bucket key, all data will go into the same data bucket and show up on the same dashboard (more info).

A Real-Time, Web-Based Dashboard

We’ll be using Initial State to remotely monitor our network of temperature sensors. Initial State is an IoT Platform for Data Visualizations that allows you to easily visualize data in dashboards and waveforms. It’s free for students with a .edu (or equivalent) email address and $9.99/mo after a 14 day free trial. So, if you don’t have an Initial State account already, you can complete this project on the free trial and see if it’s for you.

Go to your Initial State account, click on the bucket name on your bucket shelf, and view your data in your dashboard. You can customize your dashboard (more info) and set up SMS/email triggers (more info). The following dashboard shows three sensor nodes collecting temperature and humidity for three different rooms:

Network Temperature Sensor Dashboard
View a live version of this dashboard at https://go.init.st/irxnz1g

Combining all three temperature streams into a single tile (more info) gives us the following view:

Network Temperature Sensor Data

Auto-Run on Boot and Monitor Your Processes / IP Addresses

Temperature Monitoring System

Once you have multiple nodes deployed, you are going to want a way to monitor each node to ensure it is functioning. You will probably run each sensor node without a monitor or keyboard/mouse to keep it compact. That means you will probably want each node to boot and run your script automatically. You can use your Initial State account to create a handy process/IP address dashboard as shown above. A detailed tutorial on creating this dashboard and setting up your Pi Zero WH to auto-run your Python script on boot can be found at https://github.com/initialstate/pi-process-dashboard/wiki .

Conclusion

Once you get a single sensor node set up and running, it is easy and relatively inexpensive to duplicate your setup as many times as needed. Using a Pi Zero WH gives you the flexibility to run other tasks since it has so much horsepower. For example, you can use one of the Pi Zero WHs to pull local weather data from a weather API and add it to your sensor dashboard (tutorial at https://github.com/initialstate/darksky ). If you decide to decommission your sensor nodes, you can reuse your Pi Zero WHs for other projects. This flexibility helps future-proof your project investment.

--

--

Initial State
Initial State

Initial State is a data streaming and visualization company.