[TOC]
# Project report - ChiliNet
Jacob Welander - jw223ft
## Tutorial on how to combine a light, temperature and humidity sensor with a LoPy4
* This IoT-project aims to set up a basic device collecting data from 3 things light, temperature and humidity. The LoPy4 will be connected by wifi.
* Time depends of expertise but 3~5 hours to complete this project following this guide. Personally I've put about 2-3 work weeks into this project, exploring different options.
## Objective
Why I chose the project was because of the need for a sensor for my chili located on my porch.
The purpose were mainly getting more information about IoT and data collection but also a interest in gardening.. and so was ChiliNet created.
Gardening requires many different conditions to be right such as water, temperature, light and humidity. In this project we will focus on **temperature**, **light** and **humidity**.
Analyzing the data from the location of the chili will give insights about the conditions of the chili 24/7 and reveal a pattern of night and day..
## Material
| Thing | Where to Buy | Cost |
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------:| ------ |
| LoPy4 | https://pycom.io/product/lopy4/ | ~370kr |
| Expansion board 3.0 | https://pycom.io/product/expansion-board-3-0/ | ~170kr |
| DHT11 - Sensor | https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/ | 49kr |
| KY-018 Sensor | https://www.electrokit.com/produkt/ljussensor/ | 39kr |
| Li-Po Battery | https://www.kjell.com/se/produkter/el-verktyg/arduino/tillbehor/luxorparts-li-po-batteri-37-v-med-kontakt-1200-mah-p87924 | 100kr |
| Pins Female-Male | https://www.electrokit.com/produkt/labbsladd-40-pin-30cm-hane-hane/ | 99kr |
| Breadboard | https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/ | 59kr |
| Pins Male-Male | https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/ | 36kr |
| LoRa & Sigfox Antenna Kit | https://pycom.io/product/lora-868mhz-915mhz-sigfox-antenna-kit/ | ~95kr |
| Box | https://www.clasohlson.com/se/Förvaringslåda-SmartStore-Home/p/34-1552-4 | 50kr |
| | Summa: | ~1067kr |
In the picture below a LoPy4 is shown. This enable us to run MicroPython and can handle WiFi, Bluetooth, LoRa and Sigfox for connectivity. And accepts both digital and analog inputs and outputs. The LoPy4 is made by Pycom.

The expansionboard connects to the LoPy4 to make more accessible for a IoT project enable more features such as a battery. Also alot of jumpers to enable/disable features.

The DHT11 sensor measures humidity and temperature. The Pins are from left to right: **SIGNAL - 3V3 - Ground**

The DHT11 sensor measures and give a analog output of light, the values goes to 0-1024 bits where 0 is much light and 1024 is dark. The Pins are from left to right: **SIGNAL - 3V3 - Ground**

## Computer setup
This is my first time programming in Python / MicroPython, I'm used to the language R. But for this I chose Atom as an IDE, where the code is uploaded from a WiFi connection and connected by the usb-port.
Im running Atom on a macOS Catalina (10.15.5) where there seems to be a problem with flashing the expansion board.
But the expansion board seems to work fine anyway without the update.
## Putting everything together
The wiring to are connected as in the picture below:

The yellow cables are passing information/signal from the sensors to PIN 16 and PIN 23. The red cables provides electricity to the sensors from the 3V3 from the expansion board. The green cables are for ground.
As this is my first IoT project this is **not** suitable for production.. but as a proof of concept for myself.
The actual wiring does not look as neat as the previous picture as shown in the pictures below when it's fitted inside the box.



As shown in the picture I drilled a hole on the side of the box for the antenna and carved out two ports for the sensors on the bottom of the box, a simple solution when you lack a 3d-printer..
The antenna that can be used for more projects in the future such as LoRa and sigfox connections, but the lack of LoRa connection I decided to use Wifi instead.
The battery will connect as shown in the drawn picture over the circuits. It will provide 3.7v to the board when fully charged.
## Platform
Im using the platform Pybytes (https://pybytes.pycom.io/), it's a free cloud-based platform that are build by Pycom that has its own library with full of function for the pycom device such as send_signal() and deepsleep().
To me this was a user-friendly platform for a beginner that made it simple to display data from the sensors for my first project.
I am a statistican, so in the future I will try something else that can handle more calculations like Shiny or something else that can connect with R or Python.
## The Code
The code for the DHT can be found: https://github.com/iot-lnu/applied-iot-20/blob/master/sensor-examples/DHT11-22/lib/dth.py
``` python
# Load every library needed
from machine import ADC
from machine import Pin
from dht import DHT
import time
# Set up pin and information for the light sensor
# KY-018
LightSensorPin = 'P16'
lightPin = Pin(LightSensorPin, mode=Pin.IN)
adc = ADC(bits=10) # Gives a value from 0-1024,where 0 is light and 1024 dark.
apin = adc.channel(attn=ADC.ATTN_11DB, pin=LightSensorPin)
# Set up pin and information for the temperatur and humidity sensor
# DHT11
th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0) # 0 = DHT 11 , 1 = DHT 22
# Set a pass of True to read values from pin P16
# and P23 with apin() and th.read()
if True:
val = apin()
result = th.read()
time.sleep(1) # Sleep 1 second
while not result.is_valid():
time.sleep(.5) # Sleep 0.5 second
result = th.read()
print('Temperatur:', result.temperature) # print result
print('Luftfuktighet:', result.humidity)
print("Ljus:", (1 - val/1024)) # Transform 10 bit output to 0-1 as
pybytes.send_signal(1,result.temperature) # Send all signals to Pybytes
pybytes.send_signal(2,result.humidity)
pybytes.send_signal(3,(1 - val/1024))
time.sleep(10) #IMPORTANT!Need to sleep for some seconds before sending data
# Set deepsleep with X minutes, after sleep the code repeats
# X * (60 * 1000 minutes)
pybytes.deepsleep(15 * 60 *1000)
```
## Transmitting the data / connectivity
For this project I'm using WiFi as the wireless protocols, the data are sent each ~15 minutes. The function send_data() from pybytes is used to send data to 3 different channels via Pybytes MQTT. When using the 3.7v LiPo-Battery with 1200 mAh battery you need to think about how often you need to receive the data, otherwise the battery life will only last about 9 hours (when sending every 5 seconds), the function deepsleep() puts the device in a less battery consuming stage and only count the time left to "wake up", then the code repeats and loops around. This saves the battery-life alot up to days instead of hours!
## Presenting the data
The data is presented from Pybytes platform.
Here we can see device location, how much data is received and the 3 sensor values in form of a linechart of the output.


Here we can see that the last 2 days there have been alot of humidity and a temperature of 16-32.
In the graph with the light sensor can a pattern be identified where less light can be observed on nights 10pm to 5am.
## Finalizing the design
As my first IoT-project im proud of the outcome, it may not be a pretty design but ey! At least I got some data to analyze.
To improve this IoT-project a soil-humidity sensor could be intergrated together with a water system. Also a more robust casing and perhaps another platform and wireless protocols such as LoRa.
One concern I got.. is heat from the sunlight, hopfully nothing will start to burn..
Picture time, final product:
Front:

Bottom:
