---
# System prepended metadata

title: Automated (Moss Poles) Plants

---

Project by Stefan Sandberg as part of the course "Applied IoT" at Linnaeus University, during the summer of 2023. The project got the highest grade available in the course.

# Brief overview

This project sets up three plants on moss poles for moisture monitoring and automated watering as needed. The system includes, among other things, security monitoring of the lid to the box where everything is stored, and monitoring of the water level in the reservoir.

The project is a bit extensive, and the time invested is very dependent on your previous experience and comfort with working with both hardware and coding. I probably invested 50+ hours all in all, including bug hunting and rewiring some stuff as I went along.

Before starting, be mindful that this is a project you definitely should ***not*** attempt as is, if you don't have the proper knowledge and experience with regards to dealing with strong current. This project includes manually wiring up a transformer that takes wall socket power, 230V AC, as an input. There are other ways of getting the 12V DC power needed in the project, an example would be a 12V DC adapter and the proper attachment to split power to the relays.

# Solving a problem

I'm quite interested and invested in plants, and about a year ago I got into growing tropical plants on moss poles. Turns out, there's a lot of manual labor involved with watering all the moss poles as a lot of the water evaporates continuously from the poles.

So the first purpose of this project is to just automate watering and monitor moisture in the moss poles in an easy way, in a smart home setup I already use for a lot of other stuff - Home Assistant.

The system in short consists of moisture sensors that sit in the moss poles. These pick up the moisture from the poles and send the data to a Raspberry Pi Pico. The Pico continuously run some code, and whenever the moisture of a moss pole goes below a preset threshold, a peristaltic pump turns on and pumps water to the top of the moss pole, so it can then trickle down through the moss in the pole.

Hopefully I will get a better understanding of what drives water consumtion and evaporation in my plants - when do they dry up faster, and when do they dry up slower?

This project can most likely be made to work with regular house plants in pots with just a few minor tweaks, if any.

# Hardware

Some of the prices listed are estimates.

* 1 x Raspberry Pi Pico - $7
    * The brain of the project. Where all the wires go. Communicates over wifi & MQTT with Home Assistant.
* 1 x Breadboard - Pre-owned
    * Just for easily connecting everything that goes in or out the Pico.
* 3 x 12V Peristaltic Pump - $60
    * Pumps for pumping water to the plants. Peristaltic because they can more easily push water higher, and they pump slower than regular water pumps which gives finer control of the actual amount of water being moved. Runs on 12V DC.
* 3 x 5V Relay - $10
    * For turning the pumps on and off. The system actually use a 8x 5V relay board (for project scaling reasons), hence the cost. Controlled by the Raspberry Pi Pico, it uses the NO (Normally Open) pins on the 12V side that controls the pump - so that all the pumps are turned off by default and when the 5V side of the system isn't running.
* 3 x Capacitive Moisture Sensor - $15
    * Measures moisture in the moss poles. Sends out an analog signal up to 3V maximum, that needs to be attached to an ADC pin on the Pico so it can be converted to a digital value.
* 1 x 230V -> 12V Transformer - $20
    * For converting 230V AC to 12V DC for the peristaltic pumps.
* 1 x WS2812 RGB LED Strip - Pre-owned
    * For lighting and as an indicator of if the strong current in the box is on or off (safety measure). Runs on 5V.
* 1 x Float Valve - $5
    * Monitors water level, sends a signal to Home Assistant that can visualize and send out push notifications to the user's phone for example.
* 1 x Door Sensor - $10
    * Monitors if the main box is open or closed, if it's open the LED strip turns on and wall socket power turns off via Home Assistant that shuts off the wifi relay supplying the power.
* Lots of 4mm hose - $30
* Lots of cables and related stuff - $50
* 1 x Big Box - $40
    * Houses the main hardware.
* 1 x Small Box - $15
    * Houses the peristaltic pumps.
* 1 x Water Reservoir - Pre-owned
* 1 x Raspberry Pi (for Home Assistant) - Pre-owned

All in all, somewhere between $250 and $300 for the complete project. Most of what I bought for the project was acquired via Amazon, Swedish online electronics stores or local physical stores. Note that if you need to buy a Raspberry Pi and use it as a Home Assistant server, your costs will jump up quite a bit.

# Wiring

This system probably isn't a good starting point if you're a complete beginner with regards to wiring. There's a lot of current flowing and mistakes can be both costly and dangerous. Since the system combines water with electricity, protecting your wiring in IP rated containers in a safe manner is a must.

In the wiring diagram below, a few things to note:

* The power transformer displayed isn't the one I used. It's hard finding the correct part in Fritzing at times. The point here is that it takes 230V AC in, and gives 12V DC out.
* The motors displayed here are the 12V peristaltic pumps in the system. Same reasoning as above, hard to find the correct part.
* Most of the system can be wired with simple jumper wires for breadboards or similar. For the 12V DC to the motors, I'm personally sticking to a bit thicker cable than simple jumper wires. For the 230V AC, use appropriate cabling. If you don't know what that is, this project is not for you.
* The wiring and hosing that connect to the sensors and pumps need to be *long*. I went for 150 cm wire to the pumps and 200 cm to the sensors. Honestly could've used more. It works, but there's not much wiggle room when moving the system or working with the plants. Your mileage may vary.

![](https://hackmd.io/_uploads/HkfrHfZth.png)

I chose to put the peristaltic pumps in their own box, mostly for noise reasons.

Note that I connected 3V3(OUT) (Pin 36) and GND (Pin 38) to separate 'power strips' on the breadboard, and ran all corresponding 3V3 and ground cables in the system to those strips.

The LED strip I settled on has 16 LEDs and this is reflected in code. The system can definitely power more LEDs, but changing the amount of LEDs also needs to be changed in the code.

Be careful when attaching the Pico to the breadboard, the pins are somewhat fragile and bend easily. A good tip is to align the Pico with the corresponding numbers on the breadboard.

After connecting everything together and getting ready to write the software, first the Pico needs to be updated with the latest firmware. The Raspberry Pi Foundation made an excellent guide on how to get up and running that explains it a lot better than I ever could. It's available at https://projects.raspberrypi.org/en/projects/get-started-pico-w/1.

It's a good plan to do a mockup on the hardware side of the project as you write the software, and in the end do a more proper setup of everything. There's a lot of wiring and connections going on in this project, so you might need longer cables and/or more space than you thought when starting out.

This is a also a good opportunity to hot glue the moisture sensors. We want the back end of the sensors, with all the electronic bits, to be completely covered in glue so we don't get any issues from water in the wrong place. Be careful not to get glue on yourself, or in the cable connector.

There also needs to be a bit of drilling going on in getting the system set up. The boxes used in the project need drilled holes for cables, for holding the pumps, for the float valve, etc. Do this properly, so that the system remains IP rated and safe from any water drops from your plants.

![](https://hackmd.io/_uploads/HJCHqmbFn.jpg)

# Software

* Thonny (for writing software)
    * Installation instructions for Windows: https://github.com/thonny/thonny/wiki/Windows
    * Installation instructions for MacOS: https://github.com/thonny/thonny/wiki/MacOSX
    * Installation instructions for Linux: https://github.com/thonny/thonny/wiki/Linux
* Home Assistant (MQTT Broker and visualization)
    * Installing and working with Home Assistant is outside of the scope of this walkthrough, so will not be explained here. THe MQTT Broker Mosquitto was chosen in Home Assistant, for that side of the communication.



The description of this project is for three plants, because the Pico has three ADC (analog/digital converter) pins. One ADC pin is needed per plant, since the moisture sensors used need ADC. The project can be sized up, but that would require several Raspberry Pi Picos and a slightly different configuration both on the hardware and the software side.

(The watchful eye will notice that only two plants are running in the photos in this report. One of the pumps died on me during development and I haven't had time to replace it yet.)

The software to this project is written completely in MicroPython.

This project uses an object oriented approach. Other approaches can probably be successfully used as well, but this is what I'm most comfortable and familiar with.

---

Regarding the software implementation, a first good step is to write some calibration code for the moisture sensors used. They're not necessarily identical, and we need to establish max and min settings for each individually. Just write a quick loop that runs forever and prints out the current ADC value on the connected pin to the screen with a one second delay. Attach the sensor to ground, 3.3V and one ADC pin on the Pico. Run the calibration code and test the moisture sensor when completely dry, and when in a glass of water. Do this separately for all three sensors used. This code is not included in the project but can easily be hacked together with a few lines of code. (There is a single line in *plant.py* (line 52) that has been commented out and that can be used by commenting it back in, but this step can definitely be improved by writing something yourself. If you use this code, a tip is to only calibrate one plant at a time, since the ADC value gets spammed out once per second per plant.)

Note the max and min value for each sensor, for example as a comment in your code. Also be aware that you might need to change these values when deploying your project, since your moss poles will never be as dry as air or as moist as a glass of water.

Now it's time to write some proper code!

The code in the project relies on a main file that runs everything, four manually written classes in different files, a config file, and two classes in their own files downloaded from the internet.

Note: the code linked is provided as-is. It's working properly, but it's nowhere near finalized or especially good looking at the time of this write-up (July 3rd 2023). It also has to be pointed out that in its current state, the system does not have a way of detecting and/or dealing with leaks, flooding, etc. Implement this at your own risk.

https://github.com/nothxor/Automated-Moss-Poles

---

The different classes and files in the project are described in short below.

* *Main* deals with setting up the system properly. This is where we initialize our plants and boot up the system, connect to the wifi, connect to the MQTT broker and decide what to include in our asynchronous loop that will run continuously.

* *System* holds everything together. As part of the constructor we set up an initial blink of the onboard LED on the Pico, to make sure it's booting up. We initialize our plants, door sensor and float valve from parameters included in the call. This file also has methods for connecting to the wifi, connecting to the MQTT broker, and an asynchronous method that runs all the tasks needed from other objects to have our system working properly.

* *Plant* sets up a lot of values in its constructor. Apart from that, it includes functionality for getting values from a sensor, and for checking if the pump needs to run (and if so, run it).

* *Magnet* handles the door sensor. A small class with just one function, that checks if the lid is open or closed. Also turns the LED strip on and off depending on door status.

* *Valve* monitors the float valve. When the water level in the water reservoir goes below the valve level, that gets picked up by the valve, and in turn - by this class.

* *Config* is the system's config file that has a lot of values stored. Everything from user names and passwords to timer and threshold values. If you need to tweak something in the software to get it to work for you, this is the first place you look.

* *Ntptime* handles connection to an NTP server to synchronize the system's local time. Downloaded from the internet, not written by me. Imported and used in the *main.py* file.

* *Neopixel* is how the system controls the LED strip. Downloaded from the internet, not written by me. Imported and used in the *magnet.py* file.

# Platform

The system runs in tandem with Home Assistant, for the simple reason that that's what I already use for my smart home. Home Assistant runs locally and unlike most other smart home systems out there, it cares about your integrity. Your data is your own, and there's no need to connect to the internet at all, apart from stuff like updates and third party integrations that might want internet connectivity for various reasons.

Since there's not much data that needs to be going back and forth, and at this stage in the project there's no real need to save the data over time, MQTT was chosen with no implementation to save the data apart from what Home Assistant already provides.

# Connectivity

As far as choosing a connection type goes, wifi was chosen since this project runs on my local smart home with plants in my own home, and there was no real point in setting up LoRaWAN or similar. It's probably pretty easy to switch around to that sort of setup, since there's not a lot of data transfer necessary.

The Pico is connected via wifi to Home Assistant running on a Raspberry Pi, where the data is sent. 

Moisture sensor data is currently measured every second but sent to Home Assistant every 30 seconds, but this can obviously be lowered a lot. The sensors, even after being calibrated, aren't 100% reliable. So instead of sending the current value once every 30 seconds, a list with values is built up over time, with an average of the values in said list being used in the system. This stabilizes the values used for running the pumps, and for what to send to Home Assistant.

The data for the float valve is on the same timer as the moisture sensor data, so currently sent every 30 seconds. This can also be lowered a lot, sending the data once every hour would definitely be enough.

The door sensor data is currently sent once every second, for safety reasons. Since this is the trigger in Home Assistant to turn the wifi relay off that supplies 230V AC to the box, when the box is opened, any changes need to happen fast. This piece of code should be rewritten to only be sent when a change occurs, and then sent immediately.

# Presentation

Data is saved automatically by Home Assistant into an SQLite database. This project does not directly interact with said data in the database. The data is visually presented as time graphs in Home Assistant for the moisture, with Home assistant itself doing the heavy lifting with regards to database connections.

![](https://hackmd.io/_uploads/S1cVm7ZYn.png)

More importantly, the sensors and sensor readings are available in Home Assistant after setting the sensors up in the *configuration.yaml* file. This is what gives Home assistant the possibility to set up automation triggers; for example to turn the 230V AC to the system off, to send a push notification to your phone when the water in the reservoir drops too low, etc.

# Finalizing


The project works as intended. Still, there are lots of things that can be improved and/or extended, as touched upon earlier in this report. The scope of the project was probably a bit too much for a five week introductory course, and the software suffers in some places as a result. As previously stated, it works properly, but it's not necessarily a well written piece of software.

However, I'm still satisfied. I learnt a lot developing this, both on the software and hardware side. Plant automation will definitely continue to be a hobby of mine.

![](https://hackmd.io/_uploads/rydhiXZYh.jpg)

![](https://hackmd.io/_uploads/Hkn3sQ-t2.jpg)
