# IoT-tutorial, a home made watering system!
Created by Gustav Graneskog, student credentials: gg222fs.
The project is a basic setup on how you can build your own self watering system at home. It is all based on micropython and works over wifi.
Estimated time to succeed with the project is about two hours if you have installed all the software and hardware updates.
So why have I created a self-watering system? I am new to internet of things and therefore I wanted to start with something rather simple but at the same time I wanted it to seem high tech. I think this tutorial will help me (and hopefully others) to get the basics of internet of things and even though that it is an easy setup it feels like a fun and challenging project. So the purpose is to attain more knowledge about IoT and to get the setup up and running. I think it will give insights in how IoT works and how IoT can help us humans with different challenges.
P.S.
I got a bonsai tree and I want it to get water when needed which got me thinking of this project.
Now to the materials needed.
# Material
| Material | Specs | Where to buy and price |
| --------------------- | ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| FiPy | Development board, with five networks. Micropython based. | [FiPy.](https://pycom.io/product/fipy/ ) - 54 € |
| Expansion board 3.0 | Used to power the FiPy and to give easy access to different pins. | [Expansion Board.](https://pycom.io/product/expansion-board-3-0/) - 16,00 € |
| Wires | Wires to connect sensors and development board. | For example: [Wires.](https://www.amazon.de/Female-Female-Male-Female-Male-Male-Steckbr%C3%BCcken-Drahtbr%C3%BCcken-bunt/dp/B01EV70C78/ref=sr_1_2?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=wires&qid=1592977784&sr=8-2) - 6,99 € |
| Breadboard | Used to wire the components easy and without soldering. | [Breadboard.](https://www.amazon.de/Elegoo-Breadboard-Solderless-Distribution-Verbindungsbl%C3%B6cke/dp/B01MCRZFE5/ref=sr_1_1_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=breadboard&qid=1592977906&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExNEZHWVVFNkRRMVMwJmVuY3J5cHRlZElkPUEwMDU1NjMyM1BVRVZZSDQxOUQ0NCZlbmNyeXB0ZWRBZElkPUExMDQzMzQ0MVRRSDhDN1hGM09PNSZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU=) - 8,99 € |
| Soil moisture sensor | Used to measure the soil moisture. | [Sensor.](https://www.amazon.de/TECNOIOT-Hygrometer-Humidity-Detection-Moisture/dp/B07RG27CFC/ref=sr_1_1_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=soil+moisture&qid=1592978014&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEzVVMxQkhJUjFMM0g3JmVuY3J5cHRlZElkPUEwMzU4Mzg0M0ZHMFRJRjFBRk1DQiZlbmNyeXB0ZWRBZElkPUEwMTAyMjM3MVhYMkJJR0FEWlBVQiZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU=) - 9,03 € |
| Relay module | Used to control when voltage is on or off. | [Relay.](https://www.amazon.de/ARCELI-ST%C3%9CCKE-KY-019-Relaismodul-arduino/dp/B07BVXT1ZK/ref=sr_1_3?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=relay+arduino&qid=1592978094&sr=8-3 )- 7,99 € |
| Water pump | Used to transport water. | [Pump.](https://b2c.cdon.se/hem-tradgard/vattenpump-3-6-volt-pump-passar-till-fontan-bevattning-mm-p53574564) - aprox- 6,0 € |
| Power source 3-6 v | Used to power the water pump | For example a phone charger on 5 v (also used one charger to power the FiPy). [Power.](https://www.amazon.de/Dericam-Micro-USB-Ladeger%C3%A4t-DC-Netzteil-Android-Smartphones-Europen-Standardstecker-Schwarz/dp/B07TBKXYKL/ref=sr_1_3_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=phone+charger+5v&qid=1592979098&quartzVehicle=29-319&replacementKeywords=phone+5v&sr=8-3-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEzQzlHVlAwNjE0TlBIJmVuY3J5cHRlZElkPUEwMjQ4OTg3MVFYQTZIQURKNkxTUyZlbmNyeXB0ZWRBZElkPUEwNjA3NjU0MVpOQVlLS05SWVBMVSZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU= )- 9,99 € |
| Water container (bucket or similar) | Used to contain water. | Hopefully free. :) |
There are of course variations of the gear if you have something else in mind. The FiPy for example is a bit pricier than other development boards so you can choose a cheaper one if you want (LoPy4 or WiPy for example).
# Computer setup
I used Atom to program the FiPy. In Atom I used Pymakr plugin that is optimized for Pycom devices. If everything works as it should you can just plug in the FiPy with a micro-usb cable to the computer and then it should be connected. When this is done you can just put in your code and press the upload button and it should work. This means that the code is uploaded to the device with the help of Atom and the plugin Pymkar.
The first thing you should do is to update the firmware on your device. Both the expansion board and the FiPy. Follow the link for help. [Getting started.](https://docs.pycom.io/gettingstarted/) Go through hardware and software setup for the devices. There are also youtube videos to help you with the initial setup. Core electronics has a good guide. [Guide.](https://www.youtube.com/watch?v=CnCpC55_ILg)
A youtube channel with general tips:[Tips.](https://www.youtube.com/channel/UCj70OvBUiWxO8Rj_qfUcHsQ/videos)
So what I used on my computer was the Atom IDE, the firmware updater and the drivers for the expansion board (see [Guide.](https://www.youtube.com/watch?v=CnCpC55_ILg)) .
# Everything put together

These are the connections I made. (I hope my paint skills are appreciated ;))
Sensor connections Pycom device. (FiPy is mounted on the expansion board but is not shown in the picture)
* P13 on expansion board to A0 on breadboard (Blue line)
* P23 on expansion board to S on breadboard (Purple line)
Lines that are red and black are where the power is drawn from. (red=+ and black =-)
The sensors power come from the expansion board.
The water pump is powered with external power source through the relay.
Relay connections.
* "+" to NO (means that the relay is normally open)
* "+" (from COM) to water pump connects the "+" from power source to the pump when relay is told to do so.
# Platform
I used a cloud based platform, Pbytes, to visualize my data. It is an easy application to use with Pycom devices. It works directly when you made a connection and it is really easy to do a dashboard that show the data. It is free and works fine if you want to show data in a line diagram or a table. [Pbytes.](https://pybytes.pycom.io/device/f7470162-787a-4ff5-a82f-bdf40e33955a/sig/5ee21d7c2df4282cf92152e0)
# The code
```
import machine
import time
from machine import Pin
from machine import ADC
from machine import Timer
# Import the needed imports
adc = machine.ADC() # creates an ADC object
apin = adc.channel(pin='P13',attn=ADC.ATTN_11DB) # creates an analog pin on P13
relay = Pin('P23', Pin.OUT) # creates a pin on P23 for the relay
chrono = Timer.Chrono() # creates a timer
chrono.start() # starts the timer
while True: # creates a loop
time1 = chrono.read() #reads the timer
val = -(((apin()/4095)*100)-100) # reads soil moisture
time.sleep(5) # sleeps for 5 sec
print(val) # prints soil mositure
pybytes.send_signal(1,val) # sends soil mositure value to Pbytes
if time1>120: # breaks the loop after 2 minutes
break
if val<37: # sets threshold value when realy will start the pump
relay.value(1) # starts and stops the pump for good flow
time.sleep(5)
relay.value(0)
time.sleep(5)
relay.value(1)
time.sleep(5)
if time1>110: # sets the device in deepsleep for 12 hours.
machine.deepsleep(4320000)
```
# Connectivity
My setup is dependent on wifi (wireless protocol) and mqtt (transport protocol) servers that Pbytes helped med setup (see below).
I used this platform to connect to the FiPy to wifi too. [Pbytes.](https://pybytes.pycom.io/device/f7470162-787a-4ff5-a82f-bdf40e33955a/sig/5ee21d7c2df4282cf92152e0)
See video for instructions on how to set it up. [Video.](https://www.youtube.com/watch?v=2THov7819GA&list=PLdzAAEJ2nwTD6cPY-ns-fUV4YUhuDerzg&index=7)
My activation worked with the Pymakr plugin in Atom.
The data is sent every 12 hours for 2 minutes with a span on 5 seconds.
# Presenting data
I used a simple dashboard to see the measurement of the soil moisture (line diagram and table). The data is saved every 12 hours for 2 minutes with a span on 5 seconds.

# Finalizing design
I think the project went well. To change it up I could have used LoRa instead of wifi (not sure if I have a connection where I live though). I could also have been using another platform than Pbytes if I wanted more diversity and I assume that the code could have been better.
The final outcome is shown below. I realize that the aesthetics of the project could have been better. Some kind of case and some soldering could have helped with this.

