Chris Dzombak

Environmental Data Logging to InfluxDB using BME280/AM2301 Sensors on Raspberry Pi

I’ve deployed a couple sensors outdoors to log temperature, humidity, and atmospheric pressure data to InfluxDB. This is partly because I’d like to keep a history of temperature/humitidy/pressure (weather forecasts are easy to find and browse, while historical data isn’t); partly because I want to know how cold it is inside the chicken coop in the winter; and partly to provide a more complete home environmental and energy usage dashboard using Grafana.

Previous Posts & Software in this series

Logger Software

Both these projects’ READMEs (briefly) cover hardware setup, and they provide an overview of the software’s usage. I run these on Raspberry Pi Zero W hardware.

Features of Note

The BME280 logger has a couple features I’d like to call out:

MSLP Adjustment: after putting together the sensor that logs atmospheric pressure, I noticed the readings didn’t match up with other data sources. This led me to learn about mean sea level pressure, which is how pressure is reported in weather forecasts. So, the BME280 logger accepts an elevation flag, which is used to calculate and report MSLP. The uncompensated pressure is also reported, in case that’s needed for any other use cases.

Indoor Humidity Recommendation: The BME280 logger will also use the recorded temperature to calculate and report the recommended maximum indoor humidity level. This is useful when adjusting a whole-house humidifier in the winter time. (The humidity lookup table came from some reputable online source; I forget exactly where.)

Sensor Hardware

There are plenty of sensor options for projects like this. In current deployments, I’m using the BME280 temperature/humidity/pressure sensor and the AM2301a temperature/humidity sensor.

These options come with different trade-offs. The BME280 sensor family is excellent, but relatively pricey. They communicate with the Raspberry Pi using the standard I2C interface.

The AM2301 family is good, but the cheaper options are somewhat less accurate than the BME280, and they communicate with their own “one-wire” protocol which requires precise timing. This protocol isn’t supported directly in Raspberry Pi hardware, and reading them from userspace in Linux is somewhat unreliable, as discussed in the README for the library I’m using. The library works around this by providing an API that retries reading the sensor until it works successfully. My logger goes a step further, by sanity-checking the values read before logging them. (I found this to be necessary after seeing a few bogus readings indicating the temperature of the chicken coop was several thousand degrees.)

There are a number of sensors in the AM2301 family, each with different accuracy and packaging. Adafruit has a comparison of the DHT11 and DHT22/AM2302. There is also apparently a DHT12, which I know nothing about, but the go-dht library supports it. I recall reading that there are some communication differences between these models, and the AM2301a isn’t discussed in any of these resources, but treating it as a DHT22 seems to work.

In any case, for a Raspberry Pi project, I’d recommend you go the BME280 route, or find some other sensor that communciates over I2C. Next time I use a sensor from the DHT family, I’ll use a simpler microcontroller like the ESP8266, which can handle the precise timing required to read those sensors reliably. (In preparation for projects like this, I’ve put together a sample project that logs to InfluxDB from an ESP8266-based Wemos D1 Mini board. I’ll write a blog post about this — and better README for that demo project — eventually.)

An older version of my BME280 logger actually used two different sensors simultaneously, the BMP280 (temperature/pressure) and HTS221 (temperature/humidity). I’ve no idea why I made that choice, aside from maybe not being aware the BME280 existed. After several months of service, the HTS221 started to return incorrect humdity readings, and I replaced both sensors by the BME280. The old repo is now archived and unmaintained.

3D-Printed Cases

I’ve 3D-printed custom enclosures that meet my particular mounting/usage requirements for these environment loggers.

The BME280 logger is mounted on a sheltered, north-facing wall, in a box that houses the Pi Zero W, the sensor, and a small fan to encourage airflow:

Pi Zero W Weather Logger Enclosure

The AM2301a logger is impermanently mounted inside the chicken coop:

Custom Enclosure/Mount for Raspberry Pi Zero

Here’s the AM2301a logger being assembled:

Custom Enclosure for Raspberry Pi Zero W

3D printing is magic.

Future Environmental Monitoring: AirGradient

Via Jeff Geerling, I recently became aware of AirGradient, who provide plans and software for indoor air quality sensors. These sensors log temperature, humidity, CO2 levels, and PM2.5 dust levels; and they can display current readings on a small OLED display. There are also 3D-printable models for nicely-designed enclosures. AirGradient will even send you PCBs for a small fee.

I’ve ordered all the requisite parts; I’m just waiting on the PCBs and CO2 sensors to arrive. I’m excited to build these and integrate their readings into my home environment dashboards! I’m planning to put one near my desk in the basement and one in my garage workshop.