---
# System prepended metadata

title: Untitled

---

## PLANTS WATERING CONTROL AND MONOTERING SYSTEM

###### Student : `Emad Abdulsamad`
###### Contact : 'ea223zb@student.lnu.se'



##  Introduction

Two years ago, Lars Olof had a small area of the home garden for planting and planted three seedlings of different types of cranberries beside flowers and other plants.  In recent years, summer temperatures reached very high levels, in addition to the lack of rain, which caused the drying of the soil, which affected plants and trees.

The redberry plant had died last year of thirst, while Lars Olov wasa in his summer house for two months, and I did not know that I needed to water the plant. For this reason we are in need for a smart system that tell us the situation of temperature and soil moisture even when we far away from home

##  Objective

The aim of this project is to build a smart system that is able to measure the weather temperature, soil moisture, and raining status. For that I bought a Pycom Lopy4 board and the required extension board, plus a temperature sensor MCP 9700, soil moisture sensor, and raining sensor.

The system will connect to the home wifi network and send the measurements to Pybytes account to visualize the data and make it available online. 

The budget of the project is around 1000 SEK.

## List of Material

The following devices and components were used to build my project.

| Component         |Function                          | Price  (SKE)             |
| ----------------- | -----------------                 |------------------- |
| [ Pycom - LoPy4][lopy4] | IoT device with ESP32 processor.       | 385     |
| [Expansion board][Expansion-board] 	 | PyCom Expansion board. | 176    |
| [Breadboard][Breadboard]	 | A construction base used to build semi-permanent prototypes of electronic circuits. Unlike stripboard (Veroboard), breadboards do not require soldering or destruction to tracks and are hence reusable.	 | 59     |
| [ Soil moisture sensor][soil-moisture] | A sensor that instill in the soil to measure moisture.       | 29     |
| [ Raining sensor][rain] | A sensor that detect raning status and send a meauserment of the preciptation raning rate.      | 39     |
| [ Temperature sensor][Temperature] | Temperature sensor MCP9700e        | 8     |
| [ Battery Box][battery-box] | A battery box that hold three AAA rechargable battery that power the pycom board to work out side with need to a electrecity sourse.       | 29    |
| [ Hook-up wires	][wires] | Pack of different sizes hook-up wires with different colors. Hook-up wires are used to connect different elements together and with the bredboard.       | 39     |
| [ Batteries ][battery] | 4xAAA rechargable battery package.      | 59    |



[lopy4]: https://pycom.io/product/lopy4/
[Expansion-board]: https://pycom.io/product/expansion-board-3-0/
[Breadboard]: https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/
[soil-moisture]: https://www.electrokit.com/produkt/jordfuktighetssensor/
[rain]: https://www.electrokit.com/produkt/regnsensor/
[Temperature]: https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/?gclid=Cj0KCQjwn4qWBhCvARIsAFNAMig085GAX0U4Qk57JhnEi5OYOTrSCcV4SUJE3RHRx0BpMVyjaW9OMp0aAuJYEALw_wcB
[battery-box]: https://www.electrokit.com/produkt/batterihallare-3xaaa-med-strombrytare-och-jst-kontakt/
[wires]: https://www.electrokit.com/en/product/hook-up-wires-w-pins-for-breadboard-smooth-65-pcs/
[battery]: https://www.ikea.com/se/sv/p/ladda-laddningsbart-batteri-hr03-aaa-1-2v-90509819/


## :Computer: Computer Setup
###  Pycom IoT Setup

- Setup the Pycom board on the extenssion board to supply it with power and computer connection to update framware and program it. [Guide][iot1].
- Connect the device to USB-prot and update the frameware [click here][iot2].

[iot1]:https://docs.pycom.io/gitbook/assets/expansion3-pinout.pdf
[iot2]:https://hackmd.io/@lnu-iot/SJ91R_jSO



### Software and IDE
A python IDE to write the code required for our project and able to connect with Pycom device; We have two choices but I prefered to us Atom IDE .

#### Atom:
- Download Atom.[Download][atom1].
- Install Atom and add pymker package form the setting -> Extensions to allow plugging the connecting Pycom board. [Guide][atom2].
    
[atom1]: https://atom.io/
[atom2]: https://hackmd.io/@lnu-iot/SydH7MTcw

    
#### NodeJS:
Atom require a java scripte compiler to work and I choose NodeJS. [Download][NodeJS].

[NodeJS]: https://nodejs.org/en/


#### Pybytes:
To store the sensor measurements and visualize these measurement data I choose to link my Pycom device with Pybytes service. [ Register][pybytes1]
You should add extra code to your program to send data to Pybytes which can be found in the following [Guide.][pybytes2]

[pybytes1]:https://pybytes.pycom.io/
[pybytes2]:https://hackmd.io/@lnu-iot/rk8uGYo_c

#### Programing Steps:
After connecting and updating the firmware for my Pycom device I start programming as follow

1st Step: I connected the temperature sensor and use a pre-programed library 'MCP97000' in the program and defined the pin the sensor is connected to 'Pin15' and started getting measurements from the sensor.

2nd Step: I connected the soil moisture sensor and its control board and supplied it with power. Then I defined the pin the sensor is connected to 'Pin13'. I defined a ADC channel and added the pin to it and started getting measurements from the sensor. I covered the sensor with a wet tissue and checked the value the sensor sent and it was between 1200 and 1500, so I decided to set the value 1000 as 100 present moisture.

3rd Step: I connected the raining status sensor and its control board and supplied it with power. Then I defined the pin the sensor is connected to 'Pin14' and added the pin to the ADC channel and started getting measurements from the sensor. I put the sensor in a cup of water and checked the received measurement which was between 1100 and 1200, so I chose to set the value 1000 as heavy rain.

4th Step: I used the boot file 'boot.py' which was provided in the workshop to set the wifi connection setting, and programmed the Pycom device to connect to Wi-Fi when the device booted up.

5th Step: I used Pybytes to receive the measurement of different factors and added the required code to send each measurement in a separate transaction. I chose to send  measurements every 10 minutes due to the limitation of Pybytes.


### Hardware Setup
After connecting the Pycom board to the computer I powered the lower lines in the breadboard from the extension board using Pin24 3.3v and Pin25 GND. The power line in the extension board will be used to power the different sensors.

I set the temperature sensor and powered it from the breadboard and used a blue wire to connect it to the extension board in the PIN15.
I set the soil moisture sensor and powered it from the breadboard and used a green wire to connect it to the extension board in the PIN13.
I set the raining status sensor and powered it from the breadboard and used a yellow wire to connect it to the extension board in the PIN14.
The following pictures show The previously described connection to build my project.

![](https://i.imgur.com/1cNCkxn.jpeg)

## Code:

 I use the following code to run my project. The change in temperature,soil moisture, and raining status occurs over a long period of time, so I set up the system to take a reading and send this reading every 10 minutes in addition to the transmission limitation of Pybytes.
```
import machine
import time
from machine import ADC
from machine import Pin
from mcp9700 import MCP9700

#Define used Pin for each sensor
soil_moisture_pin = 'P13' # sensor is connected to anologe output of soil moisture sensor.
rain_sensor_pin = 'P14' # sensor is connected to anologe output of rain drop sensor.
temp_sensor_pin = 'P15' # sensor is connected to output of tempreture sensor MCP9700.

#Define mode of used Pins
SM_pin = Pin(soil_moisture_pin, mode=Pin.IN)  # set up pin mode to input
rain_pin = Pin(rain_sensor_pin, mode=Pin.IN)  # set up pin mode to input

#Use ADC function to link Pins with channel to read current input
adc = ADC()
spin = adc.channel(pin=soil_moisture_pin, attn=ADC.ATTN_11DB)
rpin = adc.channel(pin=rain_sensor_pin, attn=ADC.ATTN_11DB)
temp = MCP9700(adc, temp_sensor_pin)

#Function receive tempreture, soil moisture, and rain status value and send it to Data Cake
def send_to_pybytes(tempreture, soil_moisture, raining_status):
    pybytes.send_signal(15, tempreture)
    pybytes.send_signal(13, soil_moisture)
    pybytes.send_signal(14, raining_status)


while True:
    # Get mesure of soil musiture and convert it to a presentage value
    # Note that the sensor in a soil filled of water gives a value bigger than 1000
    soil_moisture = spin.value()
    soil_moisture = (4095 - soil_moisture) / 30.95
    soil_moisture = int(soil_moisture)

    # Get mesure of rainning status and convert it to a presentage value
    # Note that the sensor inside a cup of water gives a value near to 1000
    rainning  = rpin.value()
    rainning = (4095 - rainning) / 30.95
    rainning = int(rainning)

    # Get a mesuement of the tempreture and convert to form one number after digit
    tempreture = "{:.1f}".format(temp.read())

    print('soil:', soil_moisture, '%')
    print('rain:', rainning, '%')
    print('temp:', tempreture, 'C' )

    # Send recordes to Pybytes
    send_to_pybytes(tempreture, soil_moisture, rainning)
    time.sleep(600)


```

## Visualization
The Pybytes can receive now the meuserments from my system now. I added wedgets that show the current meuserment for different factors , and I add wedgets that drow a line graph of each factor through rinning the time as showing in the following figures.

- Temperature. 

![](https://i.imgur.com/w6pTBM5.jpeg)

![](https://i.imgur.com/BW2mnEk.jpeg)

 - Soil Moisture.

![](https://i.imgur.com/t2dpGbQ.jpeg)

![](https://i.imgur.com/81NFo4V.jpeg)

- Raining status.

![](https://i.imgur.com/1vrw5lM.jpeg)

![](https://i.imgur.com/iKR7Zqv.jpeg)

##### Note: I used wet tissues and hair dryer to get different meuserements in a short time and took screen shots.

## Finalizing
My project now can measure the weather temperature, soil moisture, and raining status, so it is ready to be set outside near the plants. The battery needs to be tested to know the required charging cycle. The code may need to be modified later to get the best power consumption and longest charging cycle.
My watering control and monitoring system does not do a critical job so any failure happened can be fixed with no damages.


![](https://i.imgur.com/sngA3yh.jpeg)
