*Oskar Deubler
od222ee*
# Short Description
This project aims to monitor the soil moisture and temperature of plants using the Adafruit Seesaw Soil Sensor connected to a Raspberry Pi Pico WH
Estimated project time is 2-3 hours.
:::info
This project was created within the summer course *Introduction to Applied IoT* at Linneaus University, Kalmar (Sweden).
:::
# Objective
The project idea spawned when I read about the course. The main objective is of course to learn how to create IoT-devices. But I also wanted to make something that I can actually utilize.
I've been nurturing my chili plant since it sprouted. I had quadruplets from the beginning but only one plant have survived my 2 years at university. So I´m determined to keep this plant hydrated.
I hope to gain new insights into how often I need to water the plant in order to keep it well. It will also be intresting to see how the need for water changes over the seasons.
# List of Material
Every item for this project was purchased from [Electrokit.com](https://www.electrokit.com).
| Item | Description | Link/Cost|
| -------------------- | ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| Raspberry PI Pico WH | Microcontroller with soldered headers (pins) | [109kr](https://www.electrokit.com/produkt/raspberry-pi-pico-wh/) |
| Breadboard | Solderless, 840 tie-points| [69kr](https://www.electrokit.com/produkt/kopplingsdack-840-anslutningar/) |
| Soil moisture sensor | Adafruit, capacitive I2C. Measures humidity and temperature. | [115kr](https://www.electrokit.com/en/product/jordfuktighetssensor-kapacitiv-i2c/) |
| Cable | Cable with pre-crimped JST-PH 4-pin female connectors in one end and 0.64mm male connectors. Used with moisture sensor. | [24 kr](https://www.electrokit.com/en/product/kabel-med-jst-ph-4-pol-hona-0-64mm-stift-200mm/) |
| Cables | Any hook-up wires w. pins for breadboard. Only 2 needed (V+, GND). Example given. | [39 kr](https://www.electrokit.com/en/product/hook-up-wires-w-pins-for-breadboard-smooth-65-pcs/) |
**Total** | **Soil humidity and temperature project** | **356 kr**
# Computer setup
## Some things to touch on before starting
:::info
This is perhaps not the best way of doing this project, but it's the way that I did it.
:::
This project was created in Visual Studio Code (VS Code). If you are meaning to follow along with this tutorial, I would highly recommend you also use VS Code, as we will make use of the extension PyMakr to connect to the RPI Pico. More about this later in the tutorial.
# 1. Flashing the firmware
1. Download the latest stable release of CircuitPython for the Pico [using this link](https://circuitpython.org/board/raspberry_pi_pico_w/).

> CircuitPython is a derivative of MicroPython designed to simplify experimentation and education on low-cost microcontrollers. It makes it easier than ever to get prototyping by requiring no upfront desktop software downloads. Simply copy and edit files on the CIRCUITPY drive to iterate.
(If you wish to learn more about CircuitPython for the Pico, visit [this Adafruit guide.](https://learn.adafruit.com/pico-w-wifi-with-circuitpython/pico-w-with-adafruit-io))
2. Locate the .UF2-file you just downloaded.
3. Take your Pico, plug in the Micro-USB into it while the USB-A is not plugged in. Whilst holding down the BOOTSEL button on the Pico, plug in the other end of the power cable into your computer.

A new drive will open in your file system named RPI-RP2.
4. Copy the .UF2-file onto the RPI-RP2 file system. When the copy is done, the Pico will restart itself automatically.
You have now installed CircuitPython on your Pico!
If you ever want to reset your Pico to it's initial state, you can repeat these steps.
The drive will now appear as **CIRCUITPY**, and already contains some files.
# 2. Installing required software
:::info
:warning: This tutorial is written for Windows users. However, much is similar to MacOS, so you should be able to follow along anyways.
:::
Your computer needs specific software to communicate with the microcontroller (Pico). VisualStudio Code is a popular IDE, is easy to use and have many handy plugins.
- [ ] Visit [the VS Code website](https://code.visualstudio.com/) to download and install VS Code.
- [ ] Visit [Node js](https://nodejs.org/en) to download and install Node js
- [ ] Install the *Pymakr* plugin by pressing on "Extensions" in the VS Code menu bar and search for *Pymakr*. When the installation is done, a new icon should appear in the VS Code menu bar:

## 3. Communication with the Pico


This should start a terminal which shows the Pico output. Now, sometimes it starts in REPL (*read–eval–print loop*) mode from the beginning, which will be identified by two different outputs from the terminal:
Either the terminal will print out the following:
`Adafruit CircuitPython 8.1.0 on 2023-05-22; Raspberry Pi Pico W with rp2040`
`>>>`
What we are looking for is the `>>>` which indicates you can write CircuitPython commands directly into the terminal.
Sometimes it does not automatically start in REPL mode.
In this case you will have follow the instructions from the terminal that tells you to `Press any key to enter the REPL. Use CTRL-D to reload.`
Once inside REPL-mode, we can try a basic print command to see if we get the expected result.
In the terminal, input `print("Hello World!)"`.
The response should be, on the line under `Hello World!`.
# 4. Putting everything together
:::danger
Before connecting any sensor, make sure to DISCONNECT the Pico power cable from your computer!
Also, I recommend you to always unplug the USB type A side (that's in your computer usb port), to lower the risk of hurting the Micro-USB port on your Pico, which is known to be a bit fragile.
:::
As this is project is made for a development setup, it is recommended to connect your pico onto your breadboard from this point forward. Remember to unplug it from your compuer before doing so.
1. Carefully line up the pins on the back of the Pico with the connections on the breadboard.
2. Make sure the Micro-USB is facing outwards from the breadboard.
3. Carefully apply preassure when you know all the pins are lined up.
We can now start to connect the wires to apply power and ground to the breadboard circuit.
If you want to know more about the Pico W pinouts, [visit this website.](h[ttps://](https://picow.pinout.xyz/))

When the sensor is positioned as in the picture, the pinout is:
* **Black** - Ground
* **Red** - VIN, 3-5V DC, Power
* **White** - I2C SDA
* **Green** - I2C SCL
[Visit this link for more information about the sensor.](https://learn.adafruit.com/adafruit-stemma-soil-sensor-i2c-capacitive-moisture-sensor?view=all)
# 5. Platform
Using a platform simplifies the process of sending and utilizing data collected from our sensors. We gain data from our sensor(s) but can not really reach it if our microcontroller is not connected to a screen or a platform that helps visualize and comprehend the data. While it is possible to self-host an IoT solution like this, it is probably easier to just let a platform handle all of your data.
For this project, I chose to use [Adafruit IO](https://io.adafruit.com/) as my platform. It it fairly simple to use and even provides handy tutorials for many different microcontrollers. While you are not bound to use this platform in any way, this tutorial will only manage Adafruit IO.
Adafruit uses MQTT to transfer data. MQTT is a standard messaging protocol for Internet of Things.
1. [Create an Adafruit account](https://accounts.adafruit.com/users/sign_up)
2. Navigate to `Feeds`, then press `New Feed` and name it *Temperature*. Press `Create`.
3. Do step 2 again but name it *Humidity* instead.
4. Press `Dashboards`, `New Dashboard` and choose a name for it, *Humidity & Temperature sensor* for example.
5. Press the cogwheel to open dashboard settings, press `Create New Block` and choose *Line Chart*. Select *Temperature* and press `Next step`. Next, choose a Block Title. It's a good idea to give this Block the same name as it's feed. Once named, scroll down a bit and press `Create block`.
6. Redo step 5 again but select *Humidity* instead.
7. We now need to fetch some user credentials that is going to be used later, in the code. It is recommended to open a text editor to store some information.
8. Press the key icon next to `New Device` in your dashboard.
9. Copy your **Username** and **Active Key**. Close the popup.
10. Navigate to `Feeds` and select *Temperature*. Press `Feed info`. Copy the **Key**.
11. Do step 10 again, and copy the **Key** for *Humidity*.
Your platform is now ready!
# 6. Code
The program code is built up of three files and some libraries.
#### Every file you need can be found on this [**Github repository.**](https://github.com/RokasBudleer/Soil-Humidity-IoT)
The repository includes:
* `lib`, all the dependencies needed for the code to work.
* `boot.py`, the script running when the Pico first powers up. This file connects to a WiFi network.
* `main.py`, the main code of the project where everything from sensors to connecting to Adafruit IO happens.
* `secrets.py`, keeps your credentials and keys from being displayed openly in the other two files.
Go ahead and download the files or create your own and copy the code. The file structure of **CIRCUITPY** should look something like this:

The code is filled with handy comments to help make the code more understandable and perhaps ease any troubleshooting.
## 7. Changes you will have to make
You will need to change a few lines in the files to set this project up for yourself:
* In `secrets.py`, change the right side of ssid, password, io_username and io_key
```python=
secrets = {
'ssid' : 'YOUR WIFI NAME',
'password' : 'YOUR WIFI PASSWORD',
"io_username": "ADAFRUIT USERNAME",
"io_key": "ADAFRUIT KEY",
}
```
* In `main.py` change line 31 & 32
```python=
picowTemp_feed = io.get_feed("TEMPERATURE KEY HERE")
picowHumid_feed = io.get_feed("HUMIDITY KEY HERE")
```
If you wish to change the interval of which the microcontroller sends data to your platform, change `INTERVAL_L` on line 15. It's default setting is 60 seconds.
# Presenting data

This is an example of a layout for the dashboard. To view the feeds history, it is possible to set how far back in time each line diagram will show by pressing the cogwheel, `Edit Layout`, then press any of the cogwheels by the blocks. The default is 24h but can be easily changed to anything from *Live* to *60 days*.
# Final Thoughts

While Adafruit is known for high-quality sensors, I have to admit that this soil humidity and temperature sensor did not live up to the expectations. Maybe I need more time to experiment with it. The data sheet claims it will give values from 200 - 2000 to read humidity but mine sometimes give very erratic data reads, plummeting up and down from 315 and caps out on 1015.
I have spent some time to troubleshoot this, and found that many people have the same problem with this particular sensor.
It has nonetheless been very educational to do this project. I started out trying to make it work with MicroPython but realized after some time that it would be smarted to go with CircuitPython, which is almost the same thing really.