Introduction: IoT Moisture Sensor

I wanted a moisture sensor that would let me know when indoor plants needed water. I wanted something I could use for seed starting and for mature indoor plants. I am always concerned that I am either over or under watering them.

I have spent a bit of time working on IoT software for Arduino devices, using other people's software I had a pretty good idea of the requirements for mine.

  1. I wanted something that I could flash to multiple devices and configure over wifi. I didn't want to have to change a config file each time I uploaded to a new device. I also didn't want to have to put credentials or other specifics in code as I always intended to share this code.
  2. I also wanted a pretty solid framework of software that I could customize for future projects. This one is a moisture sensor. I might build a motion/light/sound/vibration/tilt sensor and I wanted to be able to use some of the same software for that.
  3. Finally I wanted this to be battery powered and as such I wanted it to last a long time. I spent some time figuring out the Deep Sleep mode where the device will spend most of the time in a dormant state.


Wemos D1 Mini

Capacitive Moisture Sensor

18650 Battery

Positive and Negative Battery Connectors

Slide Switches

Access to a 3D Printer is also pretty essential though you could find other ways to connect and house the parts.

Here is a link to my Thingiverse post with all the models I created.

Step 1: Get the Software

I published my software to GITHUB. It is built using PlatformIO

  1. Follow the instructions on the PlatformIO Website to install VSCode and PlatformIO
  2. Download the firmware from my GITHUB repo. Click Clone or download and Download ZIP
  3. Extract and open the folder in VSCode
  4. Connect the Wemos D1 to your computer via micro USB
  5. In VSCode click the alien to open the PlatformIO panel
  6. Click Build and Upload to upload the firmware to the Wemos board

Step 2: Set Up a Blynk Project for Your Sensor to Talk To

I focused on two types of services MQTT and Blynk, either are optional.

Blynk is an easy to use and inexpensive IOT platform. You can create apps mostly using the component credits you get for free. If you run out of credits you can buy more using in app purchases.

  1. Install the Blynk app on your phone
  2. Create an account
  3. Create a new New Project
  4. Give it a name and pick Wemos D1 as the device
  5. Get the Blynk Key from your email, this will be used to configure the device later
  6. Go ahead and tap anywhere on your project dashboard to add a component
  7. Select LCD Display for now but you can change it for a chart or other components later. Blynk lets you recycle components so you don't have to worry about wasting credits
  8. Tap on the LCD display and set the pins. The firmware uses two virtual pins. It doesn't matter which ones you use as long as you use the same in your app as your firmware later

Step 3: Set Up MQTT Service (Home Assistant)

I already use Home Assistant for my home automation and plan to set up notifications for either a plant gone dry or a sensor that stopped reporting (battery dead).

You can find information to set up HA hereif you like or you could just set up a Mosquitto Service for MQTT.

In either case you will want to know your ip address, user id, and password.

If you use Home Assistant you will set up a sensor on the MQTT platform but you will need the chipid. The firmware will publish a message with the topic [chip id]/moisture with the value of the moisture reading

Here is a sample sensor configuration for Home Assistant

    state_topic: "ESP6e4bac/moisture/"
    device_class: humidity

Step 4: Configure the Firmware

  1. When the board resets It will start a wifi access point WifiMoisture
  2. Connect to it using your phone or computer
  3. Open a browser and navigate to
  4. You will see a webform
  5. Add your wifi credentials.
  6. You can set the sleep interval in minutes but I advise you just leave it at the default (the max for your device)
  7. Add Blynk Key, and/or MQTT settings
  8. Hit submit

You can enter Blynk keys and or MQTT credentials depending on how you want to track the moisture. It should work with either but I plan to use both.

I use Home Assistant for my home automation and will be setting up an alert based on MQTT but I also use a graph in Blynk to track things real time.

I would advise doing this configuration with the Wemos device still connected to your PC and while running the serial monitor. If you typed something incorrectly or have other issues you will need the serial output to diagnose.

Step 5: Printing Parts

Go to my Thingiverse post, get the parts and print the latest (v2 at the time of writing).

Nothing should require any supports but make sure that openings are facing up so you don't have any large overhanging areas.

Step 6: Wire Everything Up

You will want to wire everything up prior to inserting it in the box but there are a few printed pieces you need to assemble during the wire up. We will take this one step at a time

Step 7: Start With the Battery

The Battery assembly is constructed from the printed holder, a positive and negative connector, two black wires and one red.

Once you have printed the battery holder insert the positive and negative connectors to either end with the tab sticking out the bottom.

Flip the battery holder over, fold the tabs out and add a dab of solder to them

Twist together the ends of the two black wires and tin them with solder

Tin the end of the red wire with solder

Then solder the black wires to the negative connector (the one with the spring) and the red wire to the positive connector.

Finally fold the tabs to sit flat against the side of the battery holder.

Step 8: Assemble the Switch

This firmware is meant to make the most of the battery using the chips Deep Sleep mode.

The device wakes up takes and publishes a reading and then goes back to sleep. In order for the chip to wake itself up there is a connection made between D0 and RST.

I used the absence of that connection to tell the device that you want to (re)configure it. The first time you started the device went into configuration mode because it didn't already have a configuration saved. Now that it does, if you ever wanted to change that configuration, flip the switch and either power cycle or hit the reset pin.

I also found that I sometimes needed to disconnect the D0-RST connection in order to flash a new version of the firmware. The switch works for that too.

The switch wiring is simple, a lead on one side to the RST pin and the center lead to the D0 pin. Before soldering this up slide the printed switch block onto the switch.

Step 9: Solder Up the Rest of the Connections

Now that the battery assembly and the switch block are wired up it is time to wire up all the other connections.

  1. Solder the red wire from the battery to the 3.5v pin on the Wemos
  2. Solder one of the black wires from the battery to the Ground pin on the Wemos
  3. Solder a wire with both ends stripped to the A0 pin. We will connect this to the yellow lead on the sensor
  4. Solder a red wire with both ends stripped to the D1 pin on the Wemos. This will be turned HIGH by the firmware to power the sensor

You could alternatively cut the female header off the sensor and solder it directly to the Wemos. I didn't do that but there isn't anything wrong with it as long as you don't plan on taking this apart later.

Step 10: Put Everything in the Box

I tried to make everything fit snug but not take too much force but every print is a little different.

  1. Insert the Wemos. push the usb end in first. Make sure it lines up nicely. If you don't have it right to corner then the back end won't go in easily.
  2. Then insert the sensor. With the wiring already connected, slide it in at an angle and when it is in the right position press it down. The box should hold onto the wiring bracket.
  3. Then work the battery holder into place. You might need to flex the walls of the box a little. Make use of the fact that one side of it is open (I may make both sides open in the next revision). Find the two circular pads on the bottom of the box and press the battery holder onto them.
  4. Finally put the switch in place from the inside of the box. Screw in the two 1.7mm x 8mm screws from the outside making sure to apply some pressure on the printed switch block. The screws should grab onto the printed block but remember printed parts are soft and the screws will strip the holes easily.

Once everything is in the box take a few minutes to organize the wires. You might be able to slide them alongside the battery holder but depending on your wire that might cause the sides to spread out.

Step 11: Power It Up and Take Your First Readings

Finally make sure the switch is flipped to connect the D0 and RST pins and insert a battery.

put the lid on and screw in the 6 screws to hold it in place (or don't the lid may just hold with friction).

The device should take a reading right away, go to sleep for the configured time, and then take another.

Now that you have it running you can set up a chart in Blynk, set up an notification in HomeAssistant, or any of a variety of other options to track the moisture and keep your plants alive.

Step 12: Next Iterations

In the future I will probably update the MQTT software to say more than just the raw reading. One of the blynk pins publishes an interpretation so I at least want to add that to MQTT. I also need to include the last

I also plan to add the reading range to the settings page or build a calibration mode. The idea would be that you use the config page to put it into calibration mode. It then takes a number of readings in pretty rapid succession keeping the highest as "Dry" and the lowest as "Wet".

I also think I can make the device smaller using a smaller battery or stacking up some of the parts. There is always work to be done on the model.

Finally there are other boards aside from the Wemos D1 Mini that have battery holder and/or charger built in. Using these might save some space and keep me from having to open the case later.