Connecting a Raspberry Pi-based hygrometer to Azure IoT Central

Willie Wheeler
Nov 13, 2020 · 7 min read
My son wiring up a hygrometer

Last week was hackathon week at work. I decided to do an Azure Internet of Things (IoT) project to learn more about Azure’s IoT offerings.

Project overview

My son and I built a hygrometer, and then I connected it to Azure IoT Central so we could see its telemetry in a real-time dashboard. A hygrometer is a device that measures both humidity (actually, relative humidity, as I discovered during the course of this project) and temperature. It’s good for monitoring attics, crawlspaces and other hard-to-reach locations. For time series geeks like me, it’s also a fun way to generate time series data, so I’ll be thinking about other devices I can build.

We built the device using a Raspberry Pi 4, a DHT11 sensor and a simple Python script for reading the sensor and dumping its telemetry to the screen. Then I used Azure IoT Central to create a simple (no coding required) dashboard app. Finally, I modified the script to push the telemetry to Azure IoT Central for viewing in the dashboard app.

Here’s the architecture:

Connecting my device to Azure IoT Central

It was fun. In this post I’ll cover each of the steps above in more detail.

Building the device

The device has three main components:

  • a Raspberry Pi 4 microcomputer (or single-board computer), which comes with Linux and Python 3 preinstalled. (You have to goof around with symlinks a bit to make Python 3 the default over Python 2. You’ll need that.)
  • a DHT11 sensor to capture humidity and temperature data and send it to a GPIO pin on the Pi
  • a Python script to read the DHT11 data off the GPIO data pin and publish it — initially just printing it on the screen, but later pushing it to Azure IoT Central

The Raspberry Pi is overkill for this project (a basic Arduino microcontroller would suffice), but I had an unopened Raspberry Pi 4 and FreeNove Ultimate Starter Kit for Raspberry Pi 4 (assorted doodads, including the DHT11 sensor) sitting around. So that’s what I used.

The FreeNove Ultimate Starter Kit for Raspberry Pi 4

The starter kit includes, among several others, a hygrometer project tutorial. (The tutorial calls it a “hygrothermograph”, but I found out that our device is really a hygrometer. Apparently a hygrothermograph writes the measurements to graph paper on a rotating drum.) Assembly is easy enough that a young child can do it, so I rounded one up to assist. There’s a GPIO ribbon and extension board to connect the Pi to a breadboard. Then you plug the DHT11 sensor, four jumper wires and a resistor into the breadboard, and that’s it.

Here’s how it looks. The DHT11 is the light blue thing on the breadboard. The resistor is hidden from view in this photo, but it’s there, between the DHT11 and the short yellow jumper wire:

The completed device hardware. The USB cords are connected to a keyboard and a mouse.

FreeNove provides a GitHub repo that includes both C and Python code for the various projects. I chose Python for no particular reason. The script reads the humidity and temperature from a GPIO data pin and simply prints it to the console. Azure IoT isn’t part of the picture yet. We’ll circle back to that.

At this point my son and I paused to breathe on the DHT11 to try to drive up the humidity reading (it worked), and then held a Ziploc bag full of ice cubes against the DHT11 to drive down the temperature (that worked too). I started to hold a lighter to the DHT11 but then decided that I didn’t want to risk melting the thing. Anyway, after about three minutes the novelty wore off and my small collaborator abandoned me for Minecraft.

The breathing and ice cube experiments were a useful exercise, though, because one of the limitations of the DHT11 became apparent. It’s slow to respond to changes — several seconds. But for the applications I have in mind (like attic or crawlspace monitoring), that would be more than sufficient.

Building the Azure IoT Central app

Azure IoT Central is a platform for quickly building management applications for IoT devices at scale. “Management” means being able to view, control and update the devices, and “at scale” means managing billions of devices (at least that’s what Azure IoT overall supports — not sure whether that extends to Azure IoT Central). But I’m starting with just one.

Create the app

This part was pretty simple. The Azure Portal has a section for IoT Central Applications. I went there and created a new app using the “Smart Meter Analytics” app template. No code required — you just choose the template and IoT Central stamps it out.

After that, the portal provides a link to the app, so I clicked that. It brought me to a default dashboard for the template I chose.

The default app dashboard for the Smart Meter Analytics app template

This dashboard exposes various simulated devices. What I wanted to do was add my own device, which brings us to creating a device template.

Create the device template and (software) device

My original plan was this:

  • Create a hygrometer device template inside the app.
  • Instantiate the device template for my Raspberry Pi hygrometer. This basically creates a software counterpart on the IoT Central side for the physical device.
  • Modify the sample code from this IoT Central tutorial to push the simulated data it generates from my Pi to my new IoT Central device.
  • Modify the sample code to read the actual data from the GPIO data pin.

This plan was plausible, but it turned out not to be the path of least resistance. Creating the device template and device were both easy. But the sample code isn’t exactly minimalistic, and I wasn’t having much luck with device registration. I don’t know what the issue was.

So instead of screwing around with that, I changed course and just did what the tutorial wanted me to do in the first place: import my device from a JSON file that the tutorial provided, and then have the sample code push its simulated telemetry to that. Amazingly, it worked like a charm on the very first attempt.

So I revised my plan to use the tutorial’s imported device instead of the one I created manually through the UI. I still want to go back and try again, but I’m into just making it work first.

Publishing device telemetry to Azure IoT Central

Now my Pi was pushing fake telemetry to IoT Central. My next goal was to push real telemetry. To do this, I’d need to modify the IoT Central sample code to read the GPIO data pin.

This ended up being easy, as the FreeNove hygrometer code is minimalistic. It basically uses a library to read the temperature and humidity from the pin into corresponding variables. So I just forklifted those few lines of code from over to the IoT Central sample code and my Frankencode was alive.

Creating and viewing the device dashboard

Now real humidity and temperature data was flowing into IoT Central. But to see it, I still needed to create a device dashboard for the device. To do that, I went to the corresponding device template, created a new view (dashboard), and added a couple of “tiles” (charts) to the view: one for humidity and one for temperature.

After that, I returned to the device and saw the device dashboard:

My device dashboard

That was cool for sure, but there’s something I like even better: the IoT Central analytics view, which I think comes from Azure IoT Time Series Insights. (Not totally sure about that, but I think so.) I let the device run for several hours and here’s what I saw:

Looking at this, several thoughts came to mind:

  • The up/downs in the temperature series are the result of the thermostat working to maintain a comfortable temperature.
  • My wife (aka Supreme Monarch of the Thermostat) and I have very different ideas about what counts as a comfortable temperature.
  • The (shared) seasonality for these time series is interesting. It will have a daily seasonality owing to daily temperature patterns. But no weekly pattern since the temperature doesn’t care whether it’s a weekday or the weekend. Also, the much shorter cycles look like they have a fixed period, and maybe they do. But because it’s the thermostat driving them, it’s conceivable (likely?) that the period isn’t absolutely fixed.
  • Why does humidity go up when temperature goes down, and vice versa?

I was sufficiently curious about the last question to do a web search, which resulted in my learning about relative humidity and its relationship to temperature.

That’s all, folks!

Further reading

DHT11 and Raspberry Pi

Azure IoT

Temperature and relative humidity


Willie Wheeler’s personal blog