Saturday, February 01, 2020

HOWTO: Environment Sensors in the Home

[ it has been a while since I had something to write about; this is something I feel can help others ]

We have a large aquarium at home, and last year a leak occurred in one of the pumps in the equipment space below. Water overflow the catch-bucket and flowed into our walls. We ended up with some water damage. Never fun.

I recall a friend of mine had a pipe leak in his condo, while he was out of town. The management company called him about the water flowing out from under his door. He signed into his in-condo camera system and saw a "shimmer" on the floor -- completely covered with water. Checking his NetAtmo device, he saw (belatedly) the jump in humidity within his condo. That humidity jump gave me an idea that I should monitor the humidity in the aquarium equipment space, and alert us when it got "too high".

While I could put together something with the DHT22 sensors that I have laying around, I wanted something more turnkey. This post will describe the overall system that I assembled, using mostly off-the-shelf, and one small service that I wrote. These were installed onto an Ubuntu 16.04 server in my house, and should work on any Unix-ish server (I don't know the portability of the components to Windows).

In short, the major components:

TeHyBug Sensor

I bought the TeHyBug Mini, but Oleg makes a variety of sensors based on what environmental measurements you want to make, and whether your application will be indoor or outdoor. These are small devices powered by battery, or by a standard USB charger. There are a variety of options that you can select, and shipped from Germany to Texas in a single week. A friend has emailed Oleg, and reports that he is quite responsive and helpful.

In "configuration mode", the TeHyBug presents a nice web interface with all the configuration options. It supports several mechanisms to report environment data, and I configured it to send its data via MQTT to my server. I was able to specify the format of the payload, enabling me to keep it tight, and easy to parse. I only have a single sensor, but I can imagine needing to get a bit more general if you load up on a large variety of sensors and applications.

Mosquitto MQTT Broker

This is a simple pub/sub system. Data arrives from the TeHyBug to a particular "topic", and delivered to all connected clients that are subscribed. There isn't much to do here, except install the software.


This is a high-performance, hugely-scalable time-series database. The website says it can intake millions of samples per second. Yikes. Total overkill for my needs, but it has a simple Python API (see below), and the Grafana frontend knows how to connect to it. Turnkey.

I ended up having to create a username/password because Grafana requires such. Before I started the Grafana setup, I was able to place the MQTT data right into InfluxDB without needing to specify a user. So, just go ahead and create one (I used the Python API to do it; there is a command-line tool for doing stuff like this, but I never installed it).

Bridge Service

This is where I needed to dig in further. Data arrives at the mosquitto MQTT broker, and goes nowhere. There must be a client subscribed to the topic, for the data to move any further. It looks like "Telegraf" may be a package to move samples from MQTT into InfluxDB, but that seemed very complicated.

There were a couple Python examples that I found, to move samples, but I didn't like them. I wanted a configuration file, I wanted it to run as a service, and I needed to handle my specific data and InfluxDB measurement samples/format.

The resulting code for the bridge is located in my OSS repository. It includes a systemd service file, and an example config file. It simply hangs around, waiting for samples to arrive, and shoves them into InfluxDB. Easy peasy.


This part was quite a bit more complicated. There is a Grafana package within the Ubuntu 16.04 package repository. That was my mistake. Way too old. I switched to use the package repository, and installed the stable version from that. Attaching it to InfluxDB was easy, once I did that.

Then I wrestled with configuring a dashboard, but that's just user education. After reading some documentation, and some Q&A on the web, I was able to get the graph I wanted:

My next step is to hook up an alert on the Humidity, and it looks like Grafana has plenty of options.


I hope this helps. The TeHyBug is an affordable, turnkey solution. But you can imagine data from many IoT devices, such as an Arduino, an RPi, a Particle device, and others. Each of these delivering data to the MQTT broker. The rest of the solution would be the same from there, with minor tweaks in the Bridge to deal with parsing the MQTT payloads.