# Andreas Greenhouse Project
1DT305 - Tillämpad Internet of Things, introduktion - Linnéuniversitetet
Andreas Stadin, as226hx
---
[ToC]
---
## Project overview
This project uses a Pycom Lopy4 to give you the *temperature*, *light*, *humidity* in a plant and if it's *raining* using sensors from your greenhouse or inside your apartment and then uploads it to Pybytes platform. You can easily see the sensor values and you will know when it's time to watering your plants or when to take your fragile plants need to move inside due to cold.
In order for you to make this project, it doesn't require any knowledge of how sensors works or how to use a breadboard. To setup everything up, I would say you need between 2-6 hours depending how good you are using computers, how used you are to coding or using electrical devices.
## Project objective
This project was made to help me keep track of different values inside my mother's greenhouse and also keep track of the values using Internet. The values I want the keep track of:
- [x] The temperature
- [x] The amount of light
- [x] Moisture in a plant
- [x] If it's raining outside
I always wanted to create something to help saving time and energy for the user which this project does. You don't have to go out the the greenhouse to check if it's time for watering. If you by any chance go to the greenhouse you can check the LED to see if it's time for watering or not (green = water is OK, red = in need of water)
This project gives a brief insight of how Internet of Things is built up and how you can your data visually and how to make a program collect the data you requested.
With the data gathered from this project you know:
* When it's time to watering your plants
* If a certain plant can survive in your greenhouse (with the correct temperature)
* If your plants are getting enough light to survive.
## Material
### List of materials
| Unit | Picture | Description | Link | Price (SEK) |
|----------------------------------------- | ----------------------------------- | ----------- | ----------------------------------------------- | ----------- |
| Pycom LoPy4 | | LoPy4 is a microcontroller that is progammable with MicroPython, it has four networks; LoRa, Sigfox, WiFi and bluetooth and makes it a very good IoT device due to ultra-low power usage.|https://pycom.io/product/lopy4/ | 350 |
| Pycom Expansion Board 3.0 | | The Expansion board got some good features like: 5p-Female connector to connect your device via USB, MicroSD card slot, JST style battery connector, Power LED and charge status LED.|https://pycom.io/product/expansion-board-3-0/ | 160 |
| LoRa (868MHz/915MHz) & Sigfox Antenna Kit | | Antenna used to connect to WiFi to send your data to Pybytes. |https://pycom.io/product/lora-868mhz-915mhz-sigfox-antenna-kit/ | 80 |
| USB-cable A-male - micro B 5p male 1.8m | | USB cable to connect your computer with the Expansion board.|https://www.electrokit.com/produkt/usb-kabel-a-hane-micro-b-5p-hane-1-8m/ | 39 |
| Breadboard 400 pins | | What makes it possible to connect your microcontroller to the electrical circuit.|https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/ | 59 |
| MCP9700 TO-92 Temperature sensor | | Temperature Sensor.|https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/ |8|
| Photo resistor CdS 2-5 kohm | | Light Sensor.|https://www.electrokit.com/produkt/fotomotstand-cds-2-5-kohm/ | 8 |
| Rain Sensor | | Rain Sensor.|https://www.electrokit.com/produkt/regnsensor/ | 39 |
| Soil Moisture Sensor | | Soil Moisture Sensor.|https://www.electrokit.com/produkt/jordfuktighetssensor/ | 29 |
| Coupling wire, soft, 65 pieces | | In order for you to connect the electrical circuit.|https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/ | 36 |
| LED 5mm red standard | | Red LED to show you when it's time to watering your plants.|https://www.electrokit.com/produkt/led-rod-5mm-standard-diffus/ | 1.7 |
| LED 5mm green 80mcd | | Green LED to show you that your plants got enough water.|https://www.electrokit.com/produkt/led-5mm-gron-diffus-80mcd/ | 5 |
| Resistor 220 Ohm, 0.25W |  | Used to be able to read your light sensor value.| https://www.electrokit.com/produkt/motstand-kolfilm-0-25w-220ohm-220r/ | 1 |
| 2x Resistor 1k Ohm, 0.125W |  | Keeps your LED's from getting too much voltage and getting destroyed.| https://www.electrokit.com/produkt/motstand-metallfilm-0-125w-1-1kohm-1k/ |2|
### Total cost
The total price for this project is 817.7 kr + shipping.
## Computer Setup
In order to get everything started you need to setup your computer correctly and download an editor. With the editor you upload the code to the device.
---
If you are using **Windows 7** or **Linux** you might be required to install drivers or adjust permissions for serial ports before you can continue on the steps in the next section.
If you have **Windows 7** or **Linux**, check out and follow the steps on https://docs.pycom.io/gettingstarted/installation/drivers/
---
If you are using **Windows 8/10/+** or **macOS** you can follow these steps:
1. Start by register an account on Pybytes platform. Follow the steps using the link https://docs.pycom.io/pybytes/getstarted
2. When you have an account, login on https://pybytes.pycom.io and follow these steps:
1: Chose Add Device
2: Add Device via USB
3: LoPy4
4: WiFi and press next
5: Name your project and press 'manage WiFi'
6: Add WiFi
7: Enter SSID and Password
8: Save
3. You will need to setup a graph for your Pybytes, follow from step 2 and forward from https://docs.pycom.io/pybytes/dashboard/
4. Next step is to update your firmware on your device, go to https://docs.pycom.io/pybytes/connect/quick/ and follow the steps.
5. Next step is to chose an editor for your computer. With an editor you upload your code from your computer to the device. For this project I chose Atom. Go to https://atom.io/ to download and install Atom.
6. Within Atom you will need a plugin to make it super easy to use your Pycom device. With the Pymakr plugin, the code is uploaded to your device with just one click. Follow the steps from https://docs.pycom.io/pymakr/installation/atom/
## Putting everything together
### Circuit diagram

#### Good to know
- [x] **All** Black wires are connected to ground.
- [x] **All** Red wires are connected to +3.3V.
- [x] Orange connected to P16.
- [x] Light Green connected to P15.
- [x] Dark Green connected to P14.
- [x] Blue connected to P13.
- [x] Purple connected to P4.
- [x] Pink connected to P3.
#### Tips
When you are putting everything together, make sure you put the two LED's (green and red) the correct way. As you can see in the figure below, the anode needs to be connected with the 1k resistor and the cathode needs to go to ground. That applies for both LED's.

#### Electrical calculations
Temperature = (read voltage - 500) / 10 [°C]
Light = (read value / 4095) * 3.3 [V]
Moisture = (read value / 4095) * 100 [%]
Rain = (read value / 4095) * 3.3 [V]
## Platform
For this project I chose Pybytes platform. Pybytes is free and provides an easy way get started for new users. There is alot of guides on Internet and it's easy to setup and show your sensor data visually. The data is stored on the cloud so you can easily login on any computer or phone to see your data values.
I have not tried out any other platforms for this project. But I would love to try out Ubidots (https://ubidots.com/) in the future.
## The code
Copy this code into your project and name it 'main.py'. To upload the code, simply press the "Upload to Device" button.
```python=1
import pycom
import time
from machine import Pin, ADC
pycom.heartbeat(False)
green = Pin('P3', mode=Pin.OUT) # Green LED
red = Pin('P4', mode=Pin.OUT) # Red LED
adc = ADC() # ADC object
temperature = adc.channel(pin='P13') # Analog pin on P13 (temp)
light = adc.channel(pin='P14') # Analog pin on P14 (light)
moisture = adc.channel(pin='P15') # Analog pin on P15 (humidity)
rain = adc.channel(pin='P16') # Analog pin on P16 (rain)
averageTemp = 0
averageLight = 0
averageMoisture = 0
averageRain = 0
i = 0
while True:
total_temp = (temperature.voltage() - 500) / 10 # Temperature reading
total_light = (light() / 4095) * 3.3 # Light reading
total_moisture = (moisture() / 4095) * 100 # Moisture humidity reading
total_rain = (rain() / 4095) * 3.3 # Rain reading
# print("Light: ", total_light)
# print("Temperature: ", total_temp)
# print("Humidity: ", total_moisture)
# print("Rain: ", total_rain)
# print("")
if (total_moisture <= 50):
green.value(1) # Green ON
time.sleep(6)
green.value(0) # Green OFF
else:
red.value(1) # Red ON
time.sleep(6)
red.value(0) # Red OFF
averageTemp = averageTemp + total_temp # Calculate average values from the sensor data
averageLight = averageLight + total_light
averageMoisture = averageMoisture + total_moisture
averageRain = averageRain + total_rain
i += 1
if i == 10:
print("Sending data...")
print("Light: ", averageLight/i)
print("Temperature: ", averageTemp/i)
print("Humidity: ", averageMoisture/i)
print("Rain: ", averageRain/i)
print("")
pybytes.send_signal(0, averageTemp/i) # Send the data to Pybytes
pybytes.send_signal(1, averageLight/i)
pybytes.send_signal(2, averageMoisture/i)
pybytes.send_signal(3, averageRain/i)
averageTemp = 0
averageLight = 0
averageMoisture = 0
averageRain = 0
i = 0
```
### Tips
If you want to print the sensor values on the screen aswell, you can *uncomment* the code starting from **row 28** (these values will be printed to the screen every 6th seconds). To uncomment a code, remove the # infron of the text.
## Transmitting the data / connectivity
### How often is the data sent?
In this project the data is sent every minute. If you want to change it, you can do it on row 49 in the code, where you change number 10 to whatever you like.
If you want to change how often the data is transmitted, you have to calculate your number x 6 (comes from the time.sleep with the LED's).
### Which wireless protocols did you use (WiFi, LoRa, etc …)?
This project uses WiFi due to lack of signal from LoRa-network. LoRa would be prefered as the data from the sensors are very small.
### Which transport protocols were used?
Since we're uising Pybytes then we use the MQTT-protocol when sending data.
## Presenting the data
This figure below is the dashboard on Pybytes, to access it go to
https://pybytes.pycom.io/devices and click on your device name.


Most of these readings where taken without the humidity sensor inserted into a plant and may look bad, at the end of the graph I made sure I had a few readings.
With the rain sensor it's 3.3V when there is no rain and goes down to 0V when it's raining alot.
### How often is data saved in the database.
The data is saved as often as the chosen time, for this project it's every minute. Every time the data is sent to Pybytes it's saved in the database.
## Finalizing the design
### Show final results of the project

This is a picture of my window with everything connected, right now it's hard to see with the green and red LED if it's time to watering my plant, hold a finger to make some darkness around the LED and you should see if it's okay.

This is a picture from outside, as for right now, it was difficult to have it lay down due to short cables, but that would improve the sensor results alot!
## Things I would love to continue working on
At the beginning of this course my goal was to open/close a window based on the temperature in the greenhouse, but since this was a task from my mother that lives 150km away from me, I had too little time to come up with an idea that would work properly and easy, and to test it.
Instead I worked from home and realized I could instead get self-watering project for one of my two plants that I have. I made an order from Electrokit of a watering pump (https://www.electrokit.com/produkt/vatskepump-peristalisk-12v-silikonslang/) but it unfortunately didn't arrive in time.
I wanted to try out LoRa-network that our teacher spoke really good about, so I contacted my school to see if they had an extra LoPy4 so I could make my own gateway. They currenly have a project there, and a closed gateway (required a login), which I just recently got connectivity to. Unfortunately by the time I was testing it out, the time limit has come to an end.
I will continue this fun project to help me and my friends with the issues of watering our plants!
---