# Watering system using lopy4 and soil moisture sensor.
## Tutorial on how to build a automatic watering system using a soil moisture sensor and the lopy4
Vidar Godow Bratt - vg222ex
This projects aim is to automize watering of plants with the help of a soil moisture sensor using a microcontroller(Lopy4) and visualising the process on an internet platform(Pybytes).
The project can be expected to take about one week of work depending on your prior knowledge. That is about 8-10 hours of effective work.
### Objective
The purpose of this specific project was to give an insight into how IOT and microcontrollers work while also being of practical value, the end product is intended to actually work to decrease the amount of work needed to water plants. The project is intended to be fun and not to hard for a beginner in the area of IOT, programming and microcontrollers.
### Material
Below is a list of all the material used in the project. The actual construction of the non electronic parts is optionall and to make it easier buying a pump instead of a motor will make the process simpler.
| Material | Where to buy & cost| About |
| -------- | -------- | -------- |
| Pycom - LoPy4 | Electrokit - 350 kr https://pycom.io/product/lopy4/ |  Microcontroller from Pycom. Suports use of Wifi, Bluetooth, Lora and Sigfox.|
| Pycom - Expansion board | Electrokit - 150 kr [https://pycom.io/product/expansion-board-3-0/](https://) |  Expansionboard compatible with the pycom products. Great for making the pins easily available.|
| Breadboard | Electrokit - 47.20 kr https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/ |  Breadboard - 400 conections. To make the conections of the jumper wires easy and convienient.|
| USB - micro USB | Electrokit - 31.20 kr | For connecting the expansion board to your computer. |
| Stepmotor 5V - VMA401 | https://www.kjell.com/se/produkter/el-verktyg/arduino/moduler/stegmotor-med-vaxellada-och-drivare-p87062 - 100kr |  5V Stepmotor for creating the movement used to move the water. |
| Soil moisture and water level sensors - VMA303 | https://www.kjell.com/se/produkter/el-verktyg/arduino/moduler/vattensensor-och-jordfuktsmatare-for-arduino-p87066 - 140 kr |  Looks at the resistence between the two arms and gives and analog signal that can be translated into soilmoisture/water level. Requires 5 Voltages|
| Resistor 330 ohm | Electrokit - 10 kr | |
|Jumper wires M/F and M/M| Electrokit - 60 kr| |
|Material for construction of watering mechanism| I used what ever leftovers i could find at home in combination with the motor to construct the non electronic part of the system. Buying specific material for the project will be more conviniet but add that cost. ||
### Computer setup
The microcontroller was setup following the instructions on the pycoms website, i recomend following the instrutions there,
https://docs.pycom.io/gettingstarted/.
I did not update the firmware of the expansionboard - which proved to be no problem im my case, so if that step seems complicated it can probably be ignored safely.
I chose to use Atom (https://atom.io/) as my IDE and used the pymakr plugin while writing the code. The pymakrs plugin makes it possible to quickly flash code to your board and gives you and REPl enviroment to quickly try oout new code.
### Putting everything together
While the specifications for the sensors and the motor says that they require 5 volts it was no problem to use the 3.3 volt - everything worked smoothly. So, the first step is to connect the 3v3 to the + of the sensors and the motor. For the sensor a resistans was used since the value gained from the sensor otherwise sometimes maxed out which required a restart of the device to get them working again. Remember to put the resistance in serie with the 3v3 but the + wires in parallel. The ground(GND) is simmilarly connected to the - on sensors and motor. The data outputs from the sensors needs to be connected to analog input pins on the board. I choose pin 16 and 17 (see https://docs.pycom.io/gitbook/assets/expansion3-pinout.pdf).
The yellow,green and blue wires from the motor then needs to be connected to an analog output pin. The picture below shows how the wires should be connected.

### Platform
I used pybytes for this project since it is easy to use and works well with the pycom devices. It is really easy and straight forward to get the data sent to the platform and to create a dashboard. Pybytes is also completly for free. It is however a bit limited in more advanced functions, so for a more advanced project another platform should be considered.
### The code
For the libraries used, see the github. The "motor" and "ULN2003" libraries which includes code used to control the motor is inspired by code written in micropython for a BBC micro:bit device (https://github.com/IDWizard/uln2003) that i adjusted to work for the pycom devices. The main code follows below, see comments in the code for an explanation of how it works.
https://github.com/VidarBratt/Pycom-wateringDevice
```python=
# Import libraries needed
import machine
import time
import pycom
import motor
#Turn of the lopy4:s led.
pycom.heartbeat(False)
# Instantiate the pins
adc = machine.ADC()
apin = adc.channel(pin='P16')
wpin = adc.channel(pin='P17')
while True:
# Get the values from the sensors and print them
V = apin.voltage()
W = wpin.voltage()
print("Water level: {}" .format(W))
print("Soil moisture: {}" .format(V))
# Send the data from your sensors to the pybytes platform
pybytes.send_signal(1,W)
pybytes.send_signal(2,V)
# If soilmoisture above chosen threshold -> green led on.
# If below -> red led on and rotate the motor 2 laps
if V > 410: # calibrate threshold
pycom.rgbled(0x00FF00) # Green
else:
pycom.rgbled(0xFF0000) # Red
motor.motor(2)
# Sleep 60 seconds before repeating the process.
time.sleep(60)
```
### Transmitting the data / connectivity
All the data is sent over Wifi with the help of Pybytes. Pybytes uses the MQTT protocol to send the data. Using pybytes is really easy, start by connecting your device following their guide (https://docs.pycom.io/pybytes/connect/). To send the data we use the function pybytes.send_signal(signalNumber, value)) from the pybytes library that exists on our device after connecting it to pybytes. SignalNumber should be given the signal you want your data to be sent to on the Pybytes platform and the value parameter should be filled with the variable with the data you want to send. Since we have put a sleep function of 60 seconds in our code the data will be sent once every minute.
### Presenting the data
For visualising the data i am using the built in dashboard in pybytes as can be seen below. The graphs are customizable, i chose simple line graphs displaying the data per hour. The data is stored for one month in Pybytes.

### Finalizing the design
As you can see below the contstruction i made of leftover stuff at home uses the principle of an archmedes screw that the motor rotates when the soilmoisture is not satisfactory high. I fill the plastic box with water where i also have my water level sensor stuck. The motor rotates the screw, bringing the water into the cut waterbottle which leads the water to the plant with the soilmoisture sensor. The way you choose to construct this is optionall and i did not choose the easiest method limiting myself to reusing stuff and trying to make use of an archimedes screw.

While everything works and i am happy with the insights the project have given the end product is a bit to instable to be of practical use. Especially as one use of this project would be to have automatic watering while you are away from home for a longer period of time, the product then needs to safely work for the safety of my house and my plants. The solution to make it work better in practice have been to buy materials for the construction or buying a pump to pump the water instead of using a motor.