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.
- 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.
- 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.
- 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.
Step 1: Get the Software
I published my software to GITHUB. It is built using PlatformIO
- Follow the instructions on the PlatformIO Website to install VSCode and PlatformIO
- Download the firmware from my GITHUB repo. Click Clone or download and Download ZIP
- Extract and open the folder in VSCode
- Connect the Wemos D1 to your computer via micro USB
- In VSCode click the alien to open the PlatformIO panel
- 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.
- Install the Blynk app on your phone
- Create an account
- Create a new New Project
- Give it a name and pick Wemos D1 as the device
- Get the Blynk Key from your email, this will be used to configure the device later
- Go ahead and tap anywhere on your project dashboard to add a component
- 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
- 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).
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
- When the board resets It will start a wifi access point WifiMoisture
- Connect to it using your phone or computer
- Open a browser and navigate to 192.168.4.1
- You will see a webform
- Add your wifi credentials.
- You can set the sleep interval in minutes but I advise you just leave it at the default (the max for your device)
- Add Blynk Key, and/or MQTT settings
- 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.
- Solder the red wire from the battery to the 3.5v pin on the Wemos
- Solder one of the black wires from the battery to the Ground pin on the Wemos
- Solder a wire with both ends stripped to the A0 pin. We will connect this to the yellow lead on the sensor
- 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.
- 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.
- 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.
- 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.
- 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.