Skip to content

A home environment sensor using ESP32 and BME280 logging to the TICK stack

License

Notifications You must be signed in to change notification settings

GJKrupa/home-environment-sensor

Repository files navigation

Environment Monitor with Logging to MQTT

This device measures environmental conditions such as temperature, humidity, relative pressure and rainfall and reports that data to MQTT topics based on the location. The data pushed only includes the reading (no timestamp) but should be suitable for consumption by systems such as Home Assistant.

MQTT deployment

I used the eclipse/mosquitto image deployed into a Kubernetes cluster and connected it to HomeAssistant.

Components Required

Please note that all links are via Amazon UK. This is solely because that's where I purchased them. I did not receive any consideration from Amazon for linking to their site. While these are the specific components that I used, there are many similar pin-compatible items available as alternatives.

  • JZK ESP32 DevKit clone. Any DevKitC variant will do but this was the cheapest I could find in the UK
  • Raspberry Pi - this is just to host the firmware update server - there are lots of alternative solutions for that
  • Voltage Detector
  • Rain Sensor
  • BME280-based Multi-Sensor
  • 18650 battery holder
  • 18650 battery. The product page for the version I ordered is no longer active and there are a lot of reports of fakes in reviews I've seen so caveat emptor. Having a decent charger such as a Nightcore D4 is also a good idea.
  • DuPont wires (female to female) and header pins. The wires can be purchased ready-made but I prefer to crimp my own to both control the length of the cables and to use more flexible wire. I used this wire with this connector kit though the 22 gauge wire can be fiddly to get into the DuPont connectors.
  • Project box. For indoor sensors I used this small box and for outdoors I used this larger one. For outdoors I would highly recommend using a Stevenson Screen instead unless you have somewhere that's always in shade otherwise you will get extremely high temperature readings in direct sunlight. They are very expensive to buy but there are a number of guides on building your own. In order to add some ventilation to the outdoor box I cut a grille in the bottom using a laser cutter though it was a time-consuming process to get through the ABS without melting it too much.
  • Nylon Standoffs to attach the board and sensors to the case
  • A drill (I use a Dremel with a router bit) to cut holes for cables and screws.

Connections

This diagram shows the connector layout. Note that I'm using pins 27 and 32 in digital output mode to act as switched power supplies so that the sensor boards don't receive any voltage while the board is in deep sleep mode.

Circuit Connections

The Code

The microcontroller code was written using PlatformIO in VSCode. Hopefully this is reasonably self-explanatory.

Construction

I'll leave that as an exercise for the user since I'm not 100% happy with the assembly I've done and you're likely to use a different enclosure to me anyway. Here's a photo of what it looks like when put together:

Assembled Outdoor Sensor

Since the rain sensor needs to be outside the box I had to drill a few holes for the standoffs and cabling which I sealed (hopefully) using Sugru.

Setup

On first launch the device will create a WiFi hotspot called sensor-XXXXXX (where XXXXXX is the MAC address of the device). Connect a laptop or phone to this hotspot to use the captive portal to configure your WiFi, MQTT sever and (optionally) syslog server and/or firmware server.

If the system detects a new firmware has been installed that requires additional configuration it will automatically switch back into configuration mode with an AP named sensor-LOCATION instead.

To force the device back into configuration mode, press the reboot button and the press the BOOT button twice in quick succession.

Firmware Server

This software uses the esp32FOTA library to detect firmware updates. This library expects a JSON descriptor that identifies the current firmware version and provides a link to the firmware's binary image. I have provided a Dockerfile and Python script that will provide such a server. To run it needs an environment BASE_URL to indicate where it is being server from. This is because the esp32FOTA library does not support relative URLs.

I've configured esp32FOTA to support insecure HTTPS without validating certificates since I'll be serving this from a closed environment on a LAN. You may want to add your own code for certificate validation if you want to improve security.