# Greenhouse Monitoring System
## Greenhouse Monitoring System
###### By: `Robal Saker - rs222zd@student.lnu.se `
## :memo: **Introduction**
Greenhouse builds to protect plants against excessive environmental changes from heat or cold., and it provides a suitable place for plants to live out of season too.
In this case, we have two different external and internal environments. We need a mechanism to monitor the internal environment and ensure that it is suitable and achieves the living conditions for these plants.
This project will approximately take one day to assemble and implement.
## **Objective**
Caring for plants and observing them is not an easy task, watering, checking soil moisture, giving sunlight or artificial light, and checking the air humidity, all of that takes time.
This project makes the idea of owning a greenhouse interesting to me and can encourage others to have it because it can be developed to reduce direct human intervention in controlling plants' irrigation and lighting systems, motivating many people to own a greenhouse in their homes.
## **Material**
I chose the development board Lopy4 as MicroPython enabled with support for LoRa, Sigfox, WiFi, and Bluetooth connectivity. with many channels supporting analogs to digital converter (ADC). which make analog sensors simple to use and connect directly like MCP9700 to measure the temperature, DHT11 sensor for Humdity, Soil moisture sensor, and Light-dependent resistance (LDR).
- Table of components
| Component | Purchased |Function | Price (SKE) |
| ----------------- |:----------------------- | ----------------- |------------------- |
| [ Pycom - LoPy4][lopy4] |electro:kit | Development board supporting MicroPython | 390 |
| [Expansion board 3][Expansion-board] |electro:kit | PyCom Expansion board 3 | 180 |
| [Photoresistors][Photoresistors] |electro:kit | Light dependent resistor (LDR). resistance decreases when light increases. | 8 |
| [ Solderless Breadboard 400 tie-points][Solderless] |electro:kit | Breadboard for solderfree experiment circuits | 59 |
| [ Jumper wire ][Jumper] |electro:kit | Pack of 65pcs jumper wires in assorted lengths and colors. A must in every lab! Fits breadboards. Each cable is terminated with a ø0.5mm pin. | 39 |
| [ LED Green][Green] |electro:kit | LED 5mm green diffuse 80mcd | 5 |
| [ 2 x LED Red][Red] |electro:kit | LED 5mm red diffuse 1500mcd | 10 |
| [ LED Orange][Orange] |electro:kit | LED 5mm orange diffuse 1500mcd | 5 |
| [ 2 x Resistor metal film 0.6W 1% 330ohm (330R)][Resistor1] |electro:kit | Metal film resistor 0.6W 330ohm (330R) | 6 |
| [ 1 x Resistor metal film 0.6W 1% 10kohm (10k))][Resistor2] |electro:kit | Metal Film 0.6W 1% 10kohm (10k) | 3 |
| [ USB-cable A-male – micro B male 5m][USB] |electro:kit | USB cable with A male and micro-B male. Length 5m. | 59 |
| [ Temperature sensor MCP9700][Temperature] |electro:kit | Temperature sensor in a TO-92 package. The sensor outputs a linear voltage, proportional to the measured temperature. MCP9700T has a wide measurement range, -40°-125°C, and a low current consumption of 6µA. | 8 |
| [ Soil moisture sensor][Soil] |electro:kit | Sensor for measuring the amount of liquid in soil, e.g. in a flowerpot, by measuring the resistance between the two legs. If the soil is dry, the resistance is high, if it is wet, the resistance decreases. The amplifier on the board converts the resistance to a voltage presented at the analog output. There is also a digital output available that indicates either dry / wet with adjustable threshold value via the trim potentiometer | 29 |
| [ Digital temperature and humidity sensor DHT11][DHT11] |electro:kit | Digital temperature and humidity sensor. The sensor outputs serial data that can be read with a microcontroller. | 49 |
[lopy4]: https://pycom.io/product/lopy4/
[Expansion-board]: https://pycom.io/product/expansion-board-3-0/
[Photoresistors]: https://www.electrokit.com/produkt/fotomotstand-cds-2-5-kohm/
[Solderless]: https://www.electrokit.com/en/product/solderless-breadboard-400-tie-points/
[Jumper]: https://www.electrokit.com/en/product/hook-up-wires-w-pins-for-breadboard-smooth-65-pcs/
[Green]: https://www.electrokit.com/en/product/led-5mm-gron-diffus-80mcd/
[Red]: https://www.electrokit.com/en/product/led-5mm-rod-diffus-1500mcd/
[Orange]: https://www.electrokit.com/en/product/led-5mm-orange-diffus-700mcd/
[Resistor1]: https://www.electrokit.com/en/product/resistor-metal-film-0-6w-1-330ohm-330r/
[Resistor2]: https://www.electrokit.com/en/product/resistor-metal-film-0-6w-1-10kohm-10k/
[USB]: https://www.electrokit.com/en/product/usb-cable-a-male-micro-b-male-5m/
[Temperature]: https://www.electrokit.com/en/product/mcp9700-e-to-to-92-temperature-sensor/
[Soil]: https://www.electrokit.com/produkt/jordfuktighetssensor/
[DHT11]: https://www.electrokit.com/en/product/digital-temperature-and-humidity-sensor-dht11/
[click1]:https://docs.pycom.io/updatefirmware/expansionboard/
[click2]:https://zadig.akeo.ie/#
[click3]:https://docs.pycom.io/updatefirmware/device/
[click4]:https://atom.io/
[click5]:https://docs.pycom.io/pybytes/gettingstarted/
## :rocket: Computer Setup
### Firmware
- We need to update the expansion board firmware using DFU-util in which can be downloaded from pycome [ click here][click1].
- we need to change the driver for the expansion board and for that we downloaded Zadig tool [ click here][click2].
- Plug the USB-prot to the expansion board and run the command "dfu-util-static.exe -D filename.dfu".
- Now we can connect the lopy4 to the expansion board and download the pycom_firmware_updater to update the firmware of the lopy4.[ click here][click3].
### Software and IDE
I use Atom as my text editor to connect my lopy4 and upload my python code using "ctrl+alt+s" command or just click in the up arrow on the taskbar.

### Atom
- Downdload Atom [ click here][click4].
- From Atom settings we chose Packages and install pymaker, and now we are ready to connect our device.
## Hardware Setup
As all our sensors can run on 3 volts, as you can see in our diagram, we use only the 3v pin in the lopy4 to supply the power to our breadboard.
We use the DHT11 sensor for humidity detection, connecting the analog pin to lopy4 pin 2.
We use the Soil moisture sensor to detect the soil moisture level and connect the analog pin of the sensor to lopy4 pin 18.
We connect pin 1 in the Photoresistor to 10k ohm resistant and feed it with 3v.
and we connect that pin to pin16 in lopy4 to read the internal resistance in the photoresistor, and according to that, we can find out if the external light is enough or if we need to switch the interior light if the greenhouse by sending the signal to pin 4 of the lopy4.
We use the MCP9700 sensor to read the temperature by connecting the pin in the middle to pin 20 in the lopy4.
And according to the read data, we can control three LEDs to determine if the temperature is high, middle, or low.
We send all data to the Pybyte website with an interval of 10 seconds without facing any problem even after two days of the project running.

## Platform
#### Pybytes
we need to create an account on the Pybytes website `https://pybytes.pycom.io/` to make use of the service provided by the site, where we can send and collect the data generated from our connected sensors in a friendly user interface.
when we register our lopy4 on the website we proceed to configure our device from the website itself and allow the wifi in the device by providing the SSID and password. and the website will manage to update the lopy4 using plug-in tools.
Information about adding and recive data you can find [ click here][click5]
## Code
```
import pycom
import _thread
from network import WLAN
import machine # Interfaces with hardware components
from machine import Pin # Pin object to configure pins
from machine import ADC # ADC object to configure reading values
import time # Allows use of time.sleep() for delays
from dht import DHT
#MQTT callback
def sub_cb(topic, msg):
print(msg)
print("Welcome to the Greenhouse Monitoring System v1.1")
soil_moisture_pin = 'P18' # sensor is connected to anologe output of soil moisture sensor.
coil_pin = Pin(soil_moisture_pin, mode=Pin.IN) # set up soil moisture sensor pin mode to input
adc = ADC()
coil_read = adc.channel(pin=soil_moisture_pin, attn=ADC.ATTN_11DB) #save soil_moisture reading values
temp_pin = adc.channel(pin='P20') # Analog indoor temperature sensor pin
Plant_Light=Pin('P4', Pin.OUT, pull = Pin.PULL_DOWN) # initialize `P4` in gpio mode and make it an output and PULL_DOWN enabled to control Plant_Light in dark.
def LDR(): #photoresistor passive component that decreases resistance with respect to receiving luminosity.
LDRSensorPin = 'P16'
lightPin = Pin(LDRSensorPin, mode=Pin.IN)
#adc = ADC(bits=10) # create an ADC object bits=10 means range 0-1024 the lower value the less light detected
LDR_pin = adc.channel(attn=ADC.ATTN_11DB, pin=LDRSensorPin)
LDR_value = LDR_pin() # read an analog value
#print("plantlight :", LDR_value)
# if LDR value higher than 1000 then switch the Plant_Light on
if(LDR_value>1000):
Plant_Light.value(1) #plantlight on
else:
Plant_Light.value(0) #plantlight off
return LDR_value
redLed=Pin('P10', Pin.OUT, pull = Pin.PULL_DOWN) #initialize `P10` in gpio mode and make it an output and PULL_DOWN enabled Red Led for temperature
yalloLed=Pin('P9', Pin.OUT, pull = Pin.PULL_DOWN) #initialize `P9` in gpio mode and make it an output and PULL_DOWN enabled Yallow led fot temperature
greenLed=Pin('P8', Pin.OUT, pull = Pin.PULL_DOWN) #initialize `P8` in gpio mode and make it an output and PULL_DOWN enabled Green led for temperature
th = DHT(Pin('P2', mode=Pin.OPEN_DRAIN), 0)
while True:
millivolts = temp_pin.voltage() # Analog temperature measured in millivolts
degC = (millivolts - 500.0) / 10.0 # Convert millivolts to celsius
soil_moisture = coil_read.value() #recall for soil_moisture values
result = th.read()
pybytes.send_signal(20, str(degC)) #send temperature value to pybytes
pybytes.send_signal(18, soil_moisture) #send soil_moisture value to pybytes
pybytes.send_signal(16, str(LDR())) #send LDR value to pybytes
pybytes.send_signal(4, result.humidity)
print('Temp:', result.temperature)
print('RH:', result.humidity)
print("LDR ", str(LDR()))
print("Temp_value ", str(degC))
print("coil value: ",soil_moisture)
time.sleep(10) # put the program in sleep for n second
# If temperature is higher than 28 degrees celsius
if(degC >= 28):
redLed.value(1)
greenLed.value(0)
yalloLed.value(0)
elif(degC <28 and degC >22):
redLed.value(0)
greenLed.value(0)
yalloLed.value(1)
else:
redLed.value(0)
greenLed.value(1)
yalloLed.value(0)
```


- we run the code and the output in the IDE will be as below:

## Transmitting the data / connectivity
The Lopy4 will establish a wireless connection and send the data harvest from the sensors every 10 seconds to Pybytes using MQTT protocol as a signal message "pybytes.send_signal(20,str(degC))" with two values, the "PIN" and the "sensor reading".
## Visualization
- **Dashboard.**


## Finalizing the design
I was impressed with the outcome of the project. I was supposed to add an air quality sensor to this project, but I postponed that implementation because of Amazon's delivery delay.
I wanted to use LORA network instead of the WiFi, But the ease of connecting to a wireless network settled the matter. But as the project is working now, I will try to establish connections to the LORA network.

