Water Tracking Systems

Overall Landscape

I've been wanting to do a small writeup about this arduino project, so here we go. I killed some of my houseplants by over-watering them and then killed more through under-watering. This isn't rocket science, but I thought it would be fun to build out some sensors and automation to help.

Each sensor consists of a D1 Mini/ESP8266 and resistive moisture sensor that I picked up from Waveshare.These use wifi to talk to HomeAssistant where they report their sensor states. HomeAssistant stores the states to a MySQL database.

Code

This is all on github, and you'll find it's pretty simple if you step through it. The PubSubClient library helps to publish MQTT messages to the broker, and I'm using ArduinoJson to format those messages.

There's not a lot going on in setup() besides getting the wifi running and setting configuration for the MQTT broker. The loop is neat: every hour, I read the moisture sensor's value 100 times then take the average of those and publish it to the MQTT broker. This ensures a less noisy value than if I was just posting the first read each time.

void setup() { 
        WiFiManager wifiManager;  
        wifiManager.autoConnect();
        mqttClient.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
        mqttClient.setCallback(callback);
    } 

publishData() takes that float value, adds it as a value on a JSON element named "moisture", and then publishes it to HomeAssistant. HA listens on a topic specific to this physical sensor kit. (If you're looking at the code and it still contains all of the Serial. lines, I was just using that during debugging and never cleaned it up.) That's everything though! Wait an hour, publish a reading. Wait an hour, publish a reading. repeat. repeat. repeat. F̷̪̱̙̯̌́͒̓̓̈́O̸̪̝̭̪̻̔R̷͌́E̷̱̤͇̩̠̊͑̒͝V̷̛̺̆̊Ḙ̸̼͖͇͗̔̔̇́͑ͅR̵͎̤̞̈͛̇

Hardware

Wiring ended up being very simple. 5v on the arduino connects to power on the sensor. Ground to ground. Then a third connection is made on one of the analog pins. To keep these safe in a wet setting, I edited some housing STLs and tried to create a waterproof enclosure for the sensor. It looks good so far, but I wont be surprised if I need to iterate on the design. Fortunately, the components are only a few dollars each, and I can sacrifice a couple if it gets me closer to the set-it-and-forget-it solution that I'm aiming for.

Integrations

What are we doing with the data? HomeAssistant allows for notifications on state criteria, so "water me" messages are in play. This also lets me know if one of the units has failed which is nice (and sad). Getting the data to MySQL supports Grafana dashboard history and visualization which is always the best part anyway.

Wrap Up

Thanks for reading! This was a fun project that can scale out as much as you like. Obviously, you can buy an out-of-the-box solution to do the same thing, but I really enjoy learning and understanding what's going on under the hood. Some backlog items to try in the future:

  • Running multiple sensors on the same arduino (if I can find one with more analog pins)
  • Integrating servos with rainwater to physically water the plants
  • Building out "heat map" visualization to overlay w/ property map
  • Move to sensor-driven lawn watering rather than timers