# Soil Moisture Sensor with Pycom and WiFi Date: 2022-07-01 Author: Therese Grass, tg222kv Course: 1DT305 at Linnaeus University Estimated setup time: 4-6 hours ## Overview In this tutorial, we will set up a project that measures the moisture content of soil, in this case the soil of a tomato plant. In addition to presenting the data in Adafruit.io, we will also add a trigger that sends an email to a specified address when the soil in the tomato plant becomes too dry. ## Objective The selection of this project stemmed from an interest in both indoor and outdoor plants. There is also a great interest in lawn care and irrigation systems, and one future idea is to use the project to measure lawn moisture and thereby optimize irrigation. As we all know, it is important to try to conserve water and an effective way to do this is to measure when irrigation is really needed, this is especially important when large amounts of water are needed, for example when watering the lawn. But in order to maintain sustainable water consumption, any way we can save water is important. My goal for the project is to gain insight into how often you should actually water and thereby optimize your irrigation. ## List of Materials Due to the global shortage of electronic components, it was initially difficult to obtain the materials needed for the project, so the components that could not be purchased were borrowed, as indicated by a (B) in the list below. In converting EUR to SEK, the exchange rate of 1 EUR = 10.6 SEK has been used. NOTE! Import VAT and some additional handling charges has not been taken into account. Please check requirements for each site before placing an order. | Product | Where to buy | Description | Price | |:-------- |:--------:| ---------:|--------:| | Pycom expansion board 3.0 (B) | [PYCOM](https://pycom.io/product/expansion-board-3-0/) | Allows you to create and connect your IoT-project with WiPy 3.0, LoPy4, SiPy, FiPy and GPy. | 188 SEK | | Pycom LoPy4 (B) | [PYCOM](https://pycom.io/product/lopy4/) | A quadruple network MicroPython enabled development board (LoRa, Sigfox, WiFi, Bluetooth) Connects your things everywhere. | 411 SEK | | Solderless Breadboard 400 tie-points (B) | [ELECTROKIT](https://www.electrokit.com/en/product/solderless-breadboard-400-tie-points/) | Breadboard for solderfree experiment circuits. | 59 SEK | | Jumper wires (B) | [ELECTROKIT](https://www.electrokit.com/en/product/hook-up-wires-w-pins-for-breadboard-smooth-65-pcs/) | They are used to connect two points in a circuit without soldering. Fits breadboards. | 39 SEK | | Soil Moisture Sensor | [ELECTROKIT](https://www.electrokit.com/en/product/soil-hygrometer-module/) | Sensor for measuring the moisture level in soil. The sensor works by passing a current from one leg of the sensor to the other. If the soil is dry, the resistance is high and if the soil is wet, the resistance drops. | 29 SEK | | USB cable 1.8 m (B) | [ELECTROKIT](https://www.electrokit.com/en/product/usb-cable-a-male-microb-male-1-8m/) | For connecting your expansion board to a computer. | 39 SEK | ![HackMD logo](https://i.imgur.com/uGroZf9.png) ## Computer setup Before we go through the installation and setup process, here is a brief description of the various softwares used in this project. **Windows 11 - [Windows Link](https://www.microsoft.com/en-us/windows/windows-11?r=1)** The latest major release of Microsoft's Windows NT operating system, released in October 2021. **Atom IDE - [Atom IDE Link](https://atom.io/)** An integrated development environment (IDE) is a software suite that consolidates basic tools required to write and test software. Atom IDE is a free and open-source text and source code editor for macOS, Linux, and Microsoft Windows. **PyMakr plugin - [PyMakr Link](https://atom.io/packages/pymakr)** Pymakr enables you to communicate to your Pycom board using the build in command line REPL. Run a single file to your board, upload your entire project or directly type and execute commands. **Node.js - [Node.js Link](https://nodejs.org/en/)** Node.js is very good at handling simultaneous connections. Since IoT is built on many devices sending small messages that must be handled quickly, Node. js makes a good backend for these kinds of applications, providing serverless architecture and real-time communication support. ### Getting Started The programming language we are going to use is MicroPython, which is a tiny open source Python programming language interpretor that runs on small embedded development boards, perfect for IoT projects! But in order for us to be able to program our device we must follow a couple of steps. First of all, be sure to familiarize yourself with the various components before proceeding to the next step. **1.** Download the Atom IDE that suits your environment and go ahead and install it. **2.** Head over to Node.js and download the recommended version for most users. When the download is finished go ahead and install it. **3.** Open Atom IDE and go to Settings -> Install and search for Pymakr package. Install it. ![](https://i.imgur.com/qejSbo6.png) **4.** Create a project folder in the desired location on your computer then open up Atom and select File -> open folder. Now create four files and name them as the image shows. ![](https://i.imgur.com/v6GSMh9.png) **5.** Pycom has a simple and easy-to-understand tutorial that we will use so go ahead and open up the Pycoms [Getting Started](https://docs.pycom.io/gettingstarted/) guide. In the guide, you will learn how to write and run a simple code and upload it to your device. It is worth noting that it is always a good idea to flash your device before using it, flashing a device simply means updating the board to the latest firmware and erase any previous data stored on the device (optional). If you want to flash your board you can go ahead and watch a Pycom tutorial here [Pycom - How To Flash Your Board](https://www.youtube.com/watch?v=TEfiMnkvyyM&ab_channel=Pycom) or if you prefer to read the documentation you can do it here [Pycom - Firmware Update](https://docs.pycom.io/updatefirmware/device/). Make sure to do this before following the Getting Started guide. ## Putting everything together When you have finished the Getting Started tutorial it's time to connect all the remaining components and the goal is to connect it as the diagram shown below. It is important to understand how the wires are connected so as not to risk damaging the board so take a good look on the circuit diagram below before you proceed. The color of the jumper wires doesn't matter so feel free to choose any colour of your liking. ### Step-by-step 1. Connect the power pin to the + row, and the ground pin to the - row, on the breadboard. 2. On the sensor module connect a wire from VCC to the + row on the breadboard. Connect a wire from GND (ground) to the - row. 3. On the Pycom Board connect pin P15 to ‘A0’ on the sensor with a wire. 4. Finally, connect the 3V3 (current) on the Pycom Board to the ‘+’ row on the bread board, and the GND (ground) to the - row. NOTE! The sensor is suitable for development only. As the probe passes current through the soil, it carries ions that will damage the surface layer over time. As such the sensor should not be operated permanently. Instead it should only be powered up when a measurement is taken and then instantly shut down again. If you are looking for sensors that fits for production, this might suit your needs better: [Electrokit - Sensor.](https://www.electrokit.com/produkt/jordfuktighetssensor-kapacitiv-i2c/) ![](https://i.imgur.com/SwqCVH2.png) Because this solution is only for a development setup, and the sensor already have built-in resistor, no electrical calculations has been done or external resitor has been used. ## Platform The platform used in this project is Adafruit.io which is a cloud service for connecting your devices and displaying their data over the Internet. The reason for choosing Adafruit is mainly the ease of use, which has been one of the most important factors as I have never worked with IoT before taking this course, but also that Adafruit already in its free service offers all the functionality I need, and more. Should I then want to develop my knowledge of IoT further, there are tons of guides and tools on their site to help me along. Finally, the fact that Adafruit was founded and is run by women and minorities may not be a decisive reason, but it's definitely a breath of fresh air in the otherwise fairly male-dominated engineering world. Read more about that here [About Adafruit.](https://www.adafruit.com/about) ## Source Code The code used in this project can be found here [Source Code.](https://github.com/TessGrass/Soil-Moisture-Monitor-with-Pycom) The code is fairly straightforward and well documented and should therefore not cause any major headache or issues. Keep in mind that no sensitive data is available in the public source code but is stored in a file called config.py which is not available for public view. I have highlighted a few lines of code that are the core of the project, see images below. The following loop is run when a connection to the WiFi has been established. ![](https://i.imgur.com/Y0MBtGL.png) Retrieves the data from PIN 15. I have chosen to let the program sleep for 1 hour before reading the value. This way the acquired value is sent directly to Adafruit once the sensor value has been read. ![](https://i.imgur.com/9O4n8hW.png) The value is then published to the feed and MQTT is used to connect to Adafruit. ![](https://i.imgur.com/6PVHc0V.png) In my `moist_sensor()` function I have used `time.sleep()` to control how often the data is being read from the sensor and transmitted to Adafruit IO. In development mode I have used `time.sleep(3)` which corresponds to the number of seconds the program will sleep, in production the `time.sleep()` is set to 3600 seconds or 1 hour. I have noticed two issues at the end of the project, first `time.sleep()` seems to work in such a way that it is not possible to interrupt the sleep with CTRL+C but it is only interrupted when the time has elapsed and the other problem is that `time.sleep()` doesn't take into consideration the time necessary to run the code. So what you're in reality getting is code execution time + `time.sleep()` + code execution time. This means that if I send this code to someone it will calculate differently based on how fast their machine can execute the code. If I continue to build on the project, this is something I will look into, but for my current small-scale project, `time.sleep()` works satisfactorily. And lastly, in the `check_plant()` function the if statement checks the value and prints out the correct message in the console. ![](https://i.imgur.com/cvRKtPF.png) ## Transmitting Data The data is sent to Adafruit IO once an hour, Adafruit then triggers an email (the limit is set to one email per day) to the user if the value received exceeds 70, which means in this case that the soil is too dry. I have used my existing WiFi for data transfer and the advantage of this is that it is easy to set up and provides a stable and fast connection. The disadvantage of WiFi is that it consumes a lot of power compared to LoRa and the range is limited. The reason I did not choose LoRa is that there is only one LoRa Gateway reasonably close to me (about 5km, the coverage map can be found here [TTN - Map](https://www.thethingsnetwork.org/map)) and I was not able to get an antenna in a reasonable time (again, I had to borrow most of the devices used in this project) Trying to connect Pycom devices to a LoRa network without an antenna would result in the Pycom devices being destroyed. At present my project is powered by a USB cable that receives power from my computer but should I wish to move it out to, say, the greenhouse and rely on battery power instead, LoRa with its low battery consumption is almost a must. The data packets sent from the device are small in size which also make LoRa a suitable option. Another option I considered was sigfox which is a similar protocol as LoRa, but closer to the cellular network in style. Rather than set up your own LoRa network, sigfox is an existing network set up by the self-same company. I also looked at the sigfox coverage map found here [sigfox Coverage](https://www.sigfox.com/en/coverage) and I could see that it was in fact possible to use sigfox. Sigfox also has low battery consumption and satisfactory range, just like LoRa. However, I opted out of sigfox early on for two main reasons, again the lack of an antenna and also that I couldn't seem to find an alternative to subscription that didn't cost money. Another aspect I was thinking about is the security around sigfox, the security level is rated as low and in its current state should not be used for critical applications so should I in the future consider sigfox again then I will have to do a deeper dive into the subject and how it would affect my project. The security around my application has not been a high priority as the data I send is not sensitive, the sensitive data that do exist is stored in a config.py file which is not publicly accessible. ## Presenting the Data I have used Adafruit.io (the reason for the choice of Adafruit can be read higher up under Platform) and the feed connected to my project can be seen below. New data in the database (called history feed in Adafruit.io) is saved every time new information has been sent to the feed that has been triggered. I also setup a trigger to my email as mention before. My Dashboard connected to the feed tomato_plant_soil_moisture ![](https://i.imgur.com/LrgmG34.png) The email received when the soil is too dry ![](https://i.imgur.com/mZGuExK.png) ## Finalizing the project I think the project has gone well, considering that before the course I had no experience of IoT, other than the smartwatch I wear on my arm. Initially I had a hard time getting started, I didn't get my products delivered, I didn't know how exactly to get started and the feeling that I was in over my head hit me a few times. Good thing I'm stubborn. I also have to praise the TA's involved in this course, it's thanks to their willingness to help and their quick responses that the course started to feel fun and I dared to try more things. So all in all I am happy with the outcome. However, there is always room for improvement and I would have liked to see more functionality included, such as a temperature sensor or more moisture sensors. One of the goals I had before starting the course was to be able to put the unit in the greenhouse and monitor my tomato plants from there. But the lack of time made me postpone that functionality for the future. Finally, even though the project didn't turn out as big as I had envisioned, I'm happy with how it turned out. From the first 1-2 weeks feeling a stress of not understanding simple IoT concepts to now feel that it's fun and even after the course is finished wanting to build on my project does make me feel proud of what I have accomplished. ![](https://i.imgur.com/3JQnJEU.jpg) ![](https://i.imgur.com/RvX3hvC.jpg) ![](https://i.imgur.com/7epGX0h.jpg)