Tutorial on how to build a soil moisture sensor
===
This tutorial will show how to build a soil mositsure sensor, that can be used to check if your plant moisture level. By following this tutorial it should't take more than 30 minutes to set up, given you already purchased the hardware.
This tutorial is made Amanda Tell (at223er).
### Objective
I recently got a new plant and in order to know when it needs watering I decided I wanted to monitor the soil moisture. The sensor will give values to indicate whether the plant needs more water and help me to keep it alive! The lower the value (eg. around 500-800) the more moist is the soil. If the value reaches above 900, then that could serve as an indicator that the plant needs more water. If it is dry or the sensor is taken out of the soil then the value will be 1023.
### Material
| Material | Specification | Vendor | Price | Photo
| -------- | -------- | -------- |-------- | --------
| Soil moisture sensor FC-28 | Sensor for measuring the amount of moisture in soil | electrokit.se |29kr |  |
|Luxorparts Li-Po-batteri 3,7 V med kontakt 500 mAh | Rechargeable battery that was used to power to Pycom-devive without a USB-cable | kjell.com | 99.90kr |  |
| LNU – 1DT305 Tillämpad IoT – LoPy4 Basic bundle* | Bundle med Pycom, LoPy och två sensorer | electrokit.se |995kr |
**The bundle contained various material and only some were used, which I will list below*
#### LoPy4 Basic bundle- used material
| Material | Specification | Photo
| -------- | -------- | ------- |
| Pycom Expansion board | Expansion Board, compatible with the WiPy 2.0, WiPy 3.0, LoPy, LoPy4, SiPy, FiPy and GPy. |  |
| LoPy 4 | The LoPy4 is a compact quadruple network MicroPython enabled development board (LoRa, Sigfox, WiFi, Bluetooth) |  |
| Jumpers | Used to connect sensors and Pycom-device | 
|
| Breadboard 400 connections | Used to more conveniently connect jumpers to sensors to Pycom-device |  |
| Micro USB-cable | Used to connect the power the Pycom-device and add to Pybytes |
| External WiFi Antenna | To help boost the development board's WiFi performance |  |
### Computer setup
I chose to work with Visual Studio Code as it is an IDE I have used it previously and feel comfortable with it. I installed the plugin Pymakr and Node.js, which is a requirement for Windows users. I added the device to the platform Pybytes and connected it via USB. Then I activated the device with the firmware updater on Pybytes website. Once I had followed the steps for the firmware updater I connected to it in Visual Studio code with Pymakr and uploaded the code via WiFi.
### Putting everything together
The sensor and the device is connected through the breadboard. I have three wires connected to GND, P5 and P15, which is the analog pin. The sensor is connected by having the GND on the device connected to the GND on the sensor, the P5 connected to VCC to give power and the P15 connected to the AO. In order to get better performance for WiFi I also conntected the external antenna as shown in the picture.

*The sensor doesn't have wires to the breadboard but it positioned directly in the breadbaord, however it was hard to show that in the picture. For a more accurate visualisation please see the last picture.*
### Platform
I chose Pybytes as a platform which is free and cloud-based. In Pybytes you can connect different devices, create projects and integrate with external services. You can also use their own Pymakr plugin to upload your code to the device directly from their website, however I chose to do it from Visual Studio Code.
Moving forward I would like to try Ubidots or another free cloud-based platform.
### The code
```python=
import machine
import time
mpin = 'P17'
mhsmoistpin = machine.Pin(mpin, mode=machine.Pin.IN) # set up pin mode to input
adc = machine.ADC(bits=10) # create an ADC object
apin = adc.channel(attn=machine.ADC.ATTN_11DB, pin=mpin) #create an analog pin on P17
p_out = machine.Pin('P5', mode=machine.Pin.OUT) # to power P5
p_out.value(0) # turn off the P5
while True:
p_out.value(1) # turn on P5
time.sleep(0.1) # sleep for 100 ms
millivolts = apin() # read value from P16
p_out.value(0) # turn off P5
print('Value: ' + str(millivolts))
time.sleep(60) # sleep for 60 sec
pybytes.send_signal(0,millivolts) # send signal to Pybytes
```
### Transmitting the data / connectivity
The data is sent every 60 seconds, through pybytes' send_signal() funcion in the code. The data is transmitted to the internet via WiFi as wireless protocol and MQTT as transport protocol. WiFi is sufficient for this project as I only plan to have to plant in my house, however I wanted to try out LoRa. As I attempted to do so I realised the closest gateway was too far away.
### Presenting the data
The dashboard is built in Pybytes, which I have learnt can be quite limiting in term of what dashboards can be built. However, I chose to only work with Pybytes for this project as it was recommended in the course material the beginning and due to time restricts I decided o continue using it.

In the picture above I watered the plant between 12:36-12:38 and as seen the sensor value goes down. It could be worth mentioning that I had already watered the plant earlier that day which is why the sensor value was already relatively low and not too dry.
The data is saved every 60 seconds, as it is sent, in the database on Pybytes.
### Finalizing the design
I think the project was successful, however reflecting on it retropectively I would liked to have attempted a more advanced project. I did struggle with the electronics and the hardware as I have no previous experience with this, which is the reason I chose to keep it simple during this course. I would also have liked to use another platform, for example Ubidots, however due to lack of time I chose to stick with Pybytes. Trying out LoRa or Sigfox would also have been interesting and maybe it will be something I'll try out in the future, although I would have to get closer to a LoRa gateway in that case.

---