Date: 2023-07-02
Author: Sam Valashjardi - Sv222rk
Course: Introduction to Applied IoT- 1DT305 at Linnaeus University
Estimated setup time: I would say between 5 to 7 hours
This project tutorial will aim to give you guidance on how to measure the moisture in soil, and with the data collected from the soil sensor then open the water valve from our pulse-controlled valve and water our plants. At first, I was a bit torn whether to base the opening of the valve on the collected data from the sensor, or to set a timer to open the valve and keep the soil sensor for the measuring of moisture in the soil. I choose to control the valve manually through Adafruit.
I will be using a Raspberry Pi Pico WH, a Bodenfeuchtesensor Hygrometer Boden Feuchtigkeit Module soil moisture sensor and pulse-controlled valve. More detailed list of all the material will be presented later on in the tutorial.
In addition to presenting the data in Adafruit.io I will also use a webhook to my discord server to get an updated notification from Adafruit.
I keep forgetting to water my plants, all the time. I have even managed to kill a cactus.
So it was a no-brainer that this project was the right one to create, in order to keep my plants happy and healthy. Automatization has always been a passion and hobby of mine and that reflects in my smart home. Robot-vacuum, Hue-lights, smart-TV & soundbar, security cameras and NFC-tags and so forth all over the place with automatization configurations.
I was also inspired by a colleague of mine who has a greenhouse and has been looking for a similar solution to his problem, which is forgetting to water some of the plants. He will use this tutorial to replicate this project later on, with some of his own tweaks.
Other than to create a system that water my plants, the purpose and goal is to study and learn as much as possible of the roadmap to IoT. With that I mean from step A to Z. How to collect and store data, then how to use it in an efficient way to make your life, or even the world a better place.
Hopefully this project and tutorial will bring some insight and understanding in one of the ways that you can use microcontrollers, sensors, a pulse valve and a bit of microPython as well. The goals are that you have hopefully learned something new at the end of this tutorial report. If you haven't, then I will most likely receive a lower grade.
So I urge you to press this link so that I can receive my A+.
Material | Function | Where to buy & Price | Picture |
---|---|---|---|
Raspberry Pi Pico WH | The Raspberry Pi Pico WH is a microcontroller board. It allows you to program and control various components, making it versatile for a wide range of projects. | Electrokit.com 109:- |
Image Not Showing
Possible Reasons
|
Valve for water control (5V) - Pulse controlled | Pulse-controlled valve that can turn water on and off with a microcomputer or Raspberry Pi | Styrahem.se 299:- |
Image Not Showing
Possible Reasons
|
Relay board (2 relays/channels) | For controlling equipment from a microcomputer (has optical isolator) | Styrahem.se 59:- |
Image Not Showing
Possible Reasons
|
Bodenfeuchtesensor Hygrometer Boden Feuchtigkeit Module soil moisture sensor | Used to accurately measure the moisture in the substrate | Amazon.com 49:- |
Image Not Showing
Possible Reasons
|
Broadboard 840 connections | High-quality connection decks for solder-free connections | Electrokit.com 69:- |
Image Not Showing
Possible Reasons
|
Lab cable 40-pin 30cm male/male | 40-pin lab cord with connectors at both ends for connector decks and pin/sleeve strips | Elektrokit.com 49:- |
Image Not Showing
Possible Reasons
|
USB Cable (USB-A to Micro-USB) | Data capable USB cable | Had one at home |
Image Not Showing
Possible Reasons
|
Water horse | Transfer the water from point A to B | Had the material at home |
Image Not Showing
Possible Reasons
|
Windows 10, 2 screens, mouse, keyboard, headset, and shit for brains.
My choice of IDE is Visual studio code (that you can download from here) because I have the most experience using it. I already have all my favourite extensions installed as well. There are several other very good IDE's that you can use, such as Atom, Thonny, Arduino IDE 2.1.0 and so on
After you have chosen your IDE and installed it, head over here and install Node.js, and what is that you ask?
Node.js is an open-source, cross-platform JavaScript runtime environment and library for running web applications outside the client's browser.
Next step is to flash our Raspberry Pi Pico with the latest firmware. And here is an easy and detailed guide on how to do that.
You will find the latest firmware here
Now we need to install the extension Pymakr (<– link to some information and install guide) on our IDE. This makes it possible for us to communicate with our Raspberry Pi Pico using microPython.
Afterwards we will connect the Raspberry Pi Pico and create a project so that we can start implementing code.
Steg/Step 1: Installing Pymakr
Steg/Step 2: Connecting our device
Steg/Step 3 & 4: Create a project
Steg/Step 5 & 6: Add your device to the project and enter Development mode.
So let’s connect all the materials and components together as meticulous as we possibly can. Because it's important to understand how the wiring should be connected, so that you don't risk damaging the board and components.
The colour of the wires does not matter.
The setup is shown in the figure below. Note that it is not the exact same component models, but the wiring and spots where you connect the wires are.
The figure below will show you how I connected the relay.
Now we move on to Relay –> water valve side of the picture.
Please note that this setup is suitable for development and hobby usage only.
There are better sensors for production.
As I mentioned earlier in the report, a free account at Adafruit IO is the platform of choice for this project. I can't provide a comparison to other platforms, since I did not try any.
So what is Adafruit IO?
Adafruit.io is needed to display the IOT project’s data online in real-time. It is a cloud server with MQTT broker that can be used to connect to IoT devices through Wi-Fi and to control these devices through a dashboard. It can be used as a free service and it has got a simple easy-to-use interface to design dashboards.
Source: https://www.tutorialspoint.com/the-adafruit-io
Press the Adafruit link stated above and create a free account.
Before we start going over the code, I would like to give praise to the teaching-assistants in this course. I have never encountered such a group of helpful, kind, patient, understanding, driven and talented people in my academic life before. They really took the time to explain and made sure that you understand everything from material, setup and the code.
I have learned immensely thanks to them. Special shoutout to Jonas & Hamed for their patience and expertise.
First of all we have to import the necessary libraries that is needed in order for the project to work. We have the library 'Machine' which is used for connecting to our raspberry Pi Pico. 'Network' is used to be able to connect our device to Wi-Fi, 'Secret' is a file I created in order to protect sensitive information from being available in main part of the code (Wi-Fi credentials).
We also have 'mqtt' for sending data over the internet to our platform.
I have defined the relay pins that we connected earlier (Pin1 & Pin2).
Then we have 3 functions for the control of the relay.
relay_high(): Will activate relay Pin1 and deactivate relay Pin2, which will cause the water valve to open up.
relay_low(): Will activate relay Pin2 and deactivate relay Pin1, which will cause the water valve to close.
relay_off(): Will deactivate both relays and turn off the valve.
We define soil = ADC (which is imported from machine) to Pin27 in order to get the data from our sensor. We also define our max and min value for our sensor readings with a reading delay at 15 seconds.
Then we have our function that calculates and the moisture readings and prints it out for us.
Last step is to publish the value to Adafruit through the feed.
So the callback function sub_cb will handle the subscribed messages.
We have created a feed in Adafruit called TOGGLE, and when we send the message "ON" it will open the valve, since we implemented the function relay_high there, and the same goes for the message "OFF" with relay_low.
Then we have the code that tries to set up a connection between the device and adafuit by using MQTT.
Lastly, we have a while loop that calls client.check_msg and our soil_sensor function.
The entirely of the code will be published at my Github profile.
I have chosen to send the soil measure data once an hour to Adafruit and will then get notified on my discord channel with the help of a webhook that I installed. I could have chosen to get the notification when the moisture value in the soil hits a minimum and maximum value as well, but I feel that an hourly update will suffices.
I have also installed a webhook for every time the water valve opens and closes.
Pictures of my webhook actions and notifications on my discord channel
The Internet connection to my device is a good old wireless protocol on my home Wi-Fi network. My project is fairly close to my router as well, and with less bandwidth restrictions and low latency, it's the obvious choice. I tried connecting my project to the Hotspot on my phone as well and it worked just fine.
Here is a code snippet of the Wi-Fi setup
As for the transport protocol, MQTT is used for sending the data measured by the soil sensor to Adafruit. It is a stable and good transport protocol to use, and we got it from the course roadmap.
My adafruit dashboard with a gauge & chart for the soil moisture data and a toggle for the water valve
Here is the data being presented in Adafruit IO dashboard. We see the value of moisture in % in the gauge block and chart values underneath (same values, different presentation). We also have a toggle block for turning the water valve ON and OFF.
Each feed preserves the data given for 30 days. I have chosen to display the data from the last 24 hours in my dashboard.
Here is a graph illustrating the fate of my plant.
We are almost there! Here are some pictures of my project.
All in all, I'm very happy with my performance in this course and project, considering the fact that I don't have that much experience with IoT. I'm proud of my project and what I accomplished with it.
I do have some future plans as well, 3D-print a case, dust & water secure all the wires, cabels and components, perhaps buy a watering system with drip function so that I can water multiple plants simultaneously.
The online lectures and roadmap given to us were really good, high quality and easy to understand and use. I never had any doubts about what to do and what to study, the quizzes were also fun and educational.
Thank you for your time.