# Tutorial on how to build a temperature and humidity application using a LoPy4 and DTH11 sensor
### Patrik Sylve
### Student credentials: ps223dy
## Overview
In this tutorial I will explain how to create a temperature and humitidy monitoring application using LoPy4 and DHT11 sensor. The collected data is later displayed with the PyBytes dashboards. The project takes approximately 1 hour to set up.
## Objective
The tutorial will give some insights in how to set up a simple sensor application with a LoPy4 device and display the collected data in a dashboard on the PyBytes platform. The purpose is to create a small application that through nice graphs in a dashboard will let you know the weather conditions outside the door. I wanted to do this project to test and get knowledge how to create simpler IoT applications.
## Material
In this tutorial I haves used the following materials:
| Material | Link | Price |
| -------- | -------- | -------- |
| 1x LoPy4 | https://pycom.io/product/lopy4/?gclid=EAIaIQobChMIsbLkr4K-6gIVwoXVCh1S4Q-tEAAYASAAEgIiAvD_BwE | 35€ |
| 1x Extension board 3.0 | https://pycom.io/product/expansion-board-3-0/ | 16€ |
| 1x Breadboard | https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklabb/kopplingsdack-400-hal-p36285 | 9€ |
| 3x Wires | https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklabb/delbar-kopplingskabel-40-pol-15-cm-hona-hona-p87075 | 7€ |
| 1x micro usb | https://www.kjell.com/se/produkter/mobilt/kablar-adaptrar/micro-usb-kablar/micro-usb-kabel-1-m-p68687 | 9€ |
| 1x DTH11 sensor | https://www.kjell.com/se/produkter/el-verktyg/arduino/moduler/playknowlogy-lilla-modul-paketet-for-arduino-p87292 | 5€ |
#### LoPy4 and extension board
The LoPy4 is a MicroPython development board that has support for Wifi, BLuetooth, SigFox and LoRa. Together with the expansion board it is possible to connect the device to a computer.
#### DTH11
DTH11 is a sensor that collects temperature and humidity data. You can read the datasheet for the sensor [here](http://www.micro4you.com/files/sensor/DHT11.pdf)
I got the DHT11 sensor by buying the sensor bundle from Kjell & Co. But it can be bought seperatly on Kjell & Co for around 5€.
#### Wires and breadboard
These are used to connect the sensor to the LoPy4 device. Only three wires are needed for this project.
## Computer
In this section I will describe how to setup the device and connect it to PyBytes using the IDE plugin PyMakr.
### Provisioning the device
First we will need to activate our pycom device with the firmware updater. To do this, start off by making an account on [PyBytes](https://pybytes.pycom.io/). When registered, go to the homepage and click on "Add device" and select to add it via USB. Next, select the model of your pycom device which should be LoPy4. Then, select WiFi as method of connection and configure the WiFi settings.
With the device added to PyBytes, you will se a box with the heading "Activate your device with Firmware updater". Download and install the updater for your OS. 
Connect your device to the computer via USB and start the updater and follow the steps presented on the [Pycom docs](https://docs.pycom.io/pybytes/connect/quick/).
#### Setting up the Pymakr plugin for VSC
For this project I am using Visual Studio Code (VSC) together with the PyMakr extension. With the PyMakr extension you can easily communicate with the LoPy4 device from VSC through the REPL console interface.
First make sure that you have Visual Studio Code and [NodeJS](https://nodejs.org/en/ "Title") installed on your computer. Then open up VSC and go to the "Extensions" tab and search for "Pymakr" and install it.

With your device connected and the pymakr extension console up and running, paste the activation token in the console. This can be found on Pybytes platform when navigating to the device previously added.

## Putting everything together
Now it is time to connect our DTH11 sensor to the LoPy4. This is done simply by attatching the sensor to the breadboard and wire it according to below scheme. Keep a note on which pin you connect the data wire (green) as this must be defined later in the code.
**OBS** My sensor came attatached to a little board that changes the wiring a bit from the original sensor. If you don't have a board attateched the wiring should probably be:
Vcc - Data - Gnd

*Circuit diagram*
## Platform
[The Pybytes platform](https://pybytes.pycom.io/) is a free to use cloud platform and works for Pycom devices. It comes with nice dashboards to easily present the data. Furthermore it has an online IDE that can be used to code and upload the documents to the device.
## The Code
To read the sensor data I used the DTH library found on https://github.com/JurassicPork/DHT_PyCom. Download the dth.py file and include it in the "lib"-folder. Then create a main.py file and include the following code:
```python=
#main.py
import pycom
import time
import machine
from dth import DTH
# Here select the pin which you send data to.
# Set the third argument to '0' to tell the library that we are using DTH11
th = DTH(machine.Pin('P23', mode=machine.Pin.OPEN_DRAIN),0)
time.sleep(2)
while True:
result = th.read()
# If we get a vlid result, send data to Pybytes
if result.is_valid():
print("Temperature: %d C" % result.temperature)
print("Humidity: %d %%" % result.humidity)
# send temperature data on singal 1 and humidity on 2
pybytes.send_signal(1, result.temperature)
pybytes.send_signal(2,result.humidity)
#Send data every 30 seconds
time.sleep(30)
```
To upload the code to your device, click on the "upload" button in VSC. It should be located on the bottom panel.

When the files are uploaded you should see the temperature and humidity values being printed in the console.

*Screenshot from the console in VSC when running the code*
## Transmitting the data / connectivity
When setting up the project (See Computer chapter) I choose WiFi to be the method of transfering data. So when the data is sent on line 19 and 20, it will send over WiFi using MQTT protocol (this is done by Pybytes so the user won't need to configure anything).
On line 23 the program waits for 30 seconds before running the code again. So data will only be sent to Pybytes every 30 seconds.
## Presenting the data
Now if we go into the Pybytes website and select the device that we previously setup, we will see on the dashboard that data is being sent. In the navigation, click on "Signals" and the two signals are visible.

When clicking on the 'write'-icon next to the signals in the frame to the right of the window, we can define what units the recived data have as well as naming the signals. I named signal one "Temperature" and set the unit to 'C'. Signal to was named "Humidity" and got the unit '%'.

When clicking on a signal a table is shown where all sent signals are presented. On the top there is a button "Create new display". When clicking on this one we can choose how we want the data to be presented. For both signals I though line charts were fitting.
A bunch of alternatives can now be choosen to tweak the appearence of the graph, such as timescale, area under graph etc.. Then we can fill in the 'show on dashboard'-checkbox. Now we will see our data displayed on the dashboard tab.
When data is sent to Pybytes it will be stored for 1 month.

*Graph showing humidity data*
## Finalizing the design
This is a simple IoT application that tells the current humidity and temperature levels. It is possible to integrate more sensors to gather and present more data for this one.
As I don't have a battery for my LoPy4, I only send data when it is connected to my computer. But with a battery connected and maybe a nice casing, this could be placed outside on the balcony or in the back yard to get the newest local weather updates!

*The hardware setup*

*Screenshot from the dashboard*