ESP and MongoDB, sitting in a tree.

Jim Blackhurst
3 min readMar 4, 2021

--

Part 1: Sensing the Environment with an ESP32

For a little while now I’ve been concerned about the state of one my houseplants. At the end of last year, it took a serious downturn and looked like it was close to meeting its maker. The good folks over at /r/houseplants got me fixed up and you’ll be glad to know the palm is now on the road to recovering. However, like any critical patient in hospital, monitoring is key to recovery, and so became the story of how I built an environmental sensor around an ESP32 and successfully pushed the data into MongoDB for live rendering of dashboards. Part one of this story is how I built the sensor which captures the environmental data. Part two focuses on the code I’m running on the ESP32 and Part three will show how I took this data and sent it to MongoDB Atlas for storage and processing. Part four (if it happens) will look at how we can build a dashboard, and set up alerts.

The ESP32 in situ

The Sensor is based on the brilliant ESP32 wifi module, which runs the Arduino core. It is entirely possible to use an ESP8266 for this job too, but I had one of these lying around. This ESP32 board is an MH-ET MiniKit, which is has a form factor that is compatible with the Wemos (Lolin) D1 mini shields. As you can see from the photo above, I have an SHT30 D1 Mini shield attached for monitoring the temperature and humidity.

SHT30 D1 Mini shield for temperature and humidity

On the back side of this board, I’ve also soldered an LDR in a voltage divider to an analogue pin. Finally, the board is connected to a capacitive moisture sensor, which is unceremoniously plunged into the middle of the plant pot. It’s important to use a capacitive sensor for this job, because resistive ones corrode alarmingly quickly, even if you do keep them powered down most of the time. The ESP32 module is powered through a USB cable to a phone charger, although I have used them outside during the summer powered by 18650 cells, and a small solar panel. (not terribly effective, but that's another story). We’ll explore the code in greater detail in part two of this series, but the rough order of events is:

  • Wake up
  • Take ten measurements of the temperature, humidity, light, and moisture, with 100ms in between them, then average them.
  • Assemble a JSON package with the name of the sensor, and the sensor readings embedded.
  • Connect to the local WiFi
  • Once connected, send the JSON package to MongoDB Realm (more on this next time)
  • Go back to sleep for 10 seconds.

On to Part two? (or maybe straight to part three?)

--

--

Jim Blackhurst
Jim Blackhurst

Written by Jim Blackhurst

Obsessed by managing data at planetary scale. (and Music, multicopters, 3D printing and geeky stuff). Opinions are my own, not my Employer's