IoT Moisture Sensor

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.

Supplies

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 192.168.4.1
  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.

Be the First to Share

    Recommendations

    • Micro:bit Contest

      Micro:bit Contest
    • Soup & Stew Speed Challenge

      Soup & Stew Speed Challenge
    • Back to School: Student Design Challenge

      Back to School: Student Design Challenge

    3 Comments

    0
    stephenlofgren
    stephenlofgren

    1 year ago on Step 12

    I spent some more time living with 4 of these in different plants and found some room to improve the code.

    I found that each of these devices wound up with a different min, max, and ideal moisture reading. So I added a calibration option to the settings web page. The Idea is you water the plant the way you want it to be. Then you calibrate the device to that moisture level.

    Start by watering the plant to give the water time to be absorbed and normal. Then put the device into configuration mode, connect to it using your phone etc. Open a browser to 192.168.4.1 which will display the current settings. Then you put it in the plant you want to monitor and hit calibrate in the browser.

    As of right now you won't see any change in the browser after you hit calibrate, just hit refresh if you want to confirm the target value changed. Flip the switch to running mode, hit the reset button and you are off. The device will now report more dry as a positive percentage of the range and more wet as a negative percentage of the range expressed as a decimal <1. The range will auto adjust as a new max or min is read it will store that to the settings.

    If you run into trouble feel free to let me know I also put a clear and reset button on the web page in case your settings broken due to the size and shape of the settings object in eeprom changing from version to version. If you see some odd settings values don't panic. It probably won't happen to you if you update the software infrequently but as I was making changes myself I reset a few times.

    There is also a clear eeprom button which I used when I extended the eeprom to fit more settings. This probably won't be needed by any normal user.

    I also made several updates to the 3D printed case including a sliding lid rather than the screwed on one, a spot for a battery charging board, and generally better freecad composition. Check those out on Thingiverse if you like, the old version is still up there too.

    0
    nirutech
    nirutech

    Question 1 year ago on Step 1

    Hello,

    at first thank you for sharing this project.
    Somehow I do have a problem during upload the code to the Wemos. Maybe you have an idea what it could be. This is the first time that I use Platform IO, so it could be something pretty easy maybe :/.
    Thanks in advance.

    Unbenannt.PNG
    0
    stephenlofgren
    stephenlofgren

    Answer 1 year ago

    Good catch it was a setup step I forgot to mention. You are going to need to add htmlark using pip. If your setup is anything like mine you will need to activate the virtual env that platformio runs in and within that virtual env run the pip install command. In my env (in linux) I did this

    cd ~/.platformio/penv
    source bin/activate
    pip install htmlark
    deactivate