# Temperature/Humidity IoT Device Tutorial

By Emma Strandberg (es225hk)
This is a tutorial on how to build your own temperature and humidity device for your home and track it's data using Pybytes.
If you have all the material and are following this tutorial, the time for building this project probably will be 1-2 hours more or less.
### Objective
This project is very beginner friendly. I myself am a beginner in this area and had a lot of fun creating this as my very first IoT project.
When I was brainstorming project ideas I was certain that I did not want to build something I already had access to on my phone or on the internet. So I started thinking; If you want to know temperature or humidity outside, it's only a short google-search away. But what I can't google, is the temperature and humidity in my apartment. And that was actually something I thought I'd really like to know, and the idea to my project was found!
This IoT-device gives me daily sensor data about the temperature and humidity changes in my apartment during all hours of the day. In the long run, I think it will give me interesting insights in how my mood or health may change because of different temperature/humidity values. It will give me some kind of self exploration on how I am feeling and how my body reacts when it's lower vs higher values. Also, it could be interesting to bring the device to friends and families apartments for comparing differences in temperature and humidity.
### Material
| Part | Price (€) | Where to buy it: |
| -------- | -------- | -------- |
| Expansion board | 17,60 | https://pycom.io/product/expansion-board-3-0/ |
| Lopy4 | 38,45 | https://pycom.io/product/lopy4/ |
| Breadboard | 5,18 | https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/ |
| DHT11 Temp/humidity sensor | 4,79 | https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/ |
| Jumper wires | 2,90 | https://www.electrokit.com/produkt/labbsladdar-100mm-hane-hane-30-pack/ |
| Micro USB cable | 1,94 | https://www.electrokit.com/produkt/usb-kabel-a-hane-micro-b-hane-30cm/ |
| TOTAL COST: | 71,16 |
#### Pictures and purpose:
Expansion board and Lopy4: This is what the project will be programmed on, what you connect everything to and where the "brain" is. If you want to know more about the lopy4 and all the different pins click [here](https://docs.pycom.io/gitbook/assets/lopy4-pinout.pdf).

Breadboard: The breadboard makes it easy to insert and connect all the electronic components without having to solder.

DHT11: Sensor used for measuring temperature and humidity.

Jumper wires: For connecting all the components.

Micro USB cable: For connecting your device to the computer or some sort of power source.

### Computer setup
#### Steps:
1. Update firmware, or should you?
You don't have to update the firmware, but it is recommended to do so. I personally had some problems after I updated my firmware. In my case, when I tried to connect my device, the device would keep connecting forever, never getting anywhere and not being able to access the REPL. So what I did was that I chose an older version in the updater tool, and then it worked perfectly normal again. After this experience I'm not entirely sure if you should update the firmware or not, I really am a beginner in this area. So it's up to you! You can find instructions on updating firmware here: https://docs.pycom.io/updatefirmware/device/.
1. Download an IDE
For this project I chose Visual Studio Code as IDE. Mostly because I am familiar with it and have worked with it before. Other IDE's like Atom would work perfectly fine with this project. The only requirement for the IDE is that the Pymakr plugin needs to be available on them.
1. Download Node.js
When you have an IDE, you should download node.js, follow this link to do so: https://nodejs.org/en/.
1. Install Pymakr
You also need the Pymakr plugin. If you use VSCode, go to "Extensions" in the sidebar, search for Pymakr and install it.
1. Create your code-documents in the IDE
This is where the code will take place. First Create a folder for the project. In that folder create a file called main.py (this is where the main code will be). Also create another folder in that project folder called lib (this is where our sensor librarys will be). In that lib-folder, create a file called library.py.
In "The Code" section of this tutorial I will explain how to write and upload the code.

### Putting everything together
Follow this circuit diagram to connect your device. The DHT11 have three pins on it and it's important to connect those to the right pins on the expansion board. Connect the "S" pin to any data input pin on the expansion board, for example P23. The "+" pin should be connected to "3V3" and the "-" pin connects to "GND".


### Platform
The platform used in this project is [Pybytes](https://pybytes.pycom.io/) which is a free cloud-based device management platform. Pybytes can be used both via the app on your smartphone or via the internet on your computer. In this project I'm using the transport protocol and database pybytes is providing. If I were to scale or add more sensors to my device I would probably consider getting some other transport protocol and database since pybytes only saves the data for one month and then it's gone. But for this small project, pybytes works great! It's really easy to learn and has a nice overview of all the data.
#### Pybytes setup
Pycom has a really good and easy-to-follow guide on how to start using pybytes, you can find it [here.](https://docs.pycom.io/pybytes/gettingstarted/)
The pybytes dashboard:

### The code
#### The library
For the DHT11 sensor, you'll need a library to get all the functionality needed. Copy the library-code and paste it into the library.py file you created earlier.
The library I used: https://github.com/JurassicPork/DHT_PyCom/blob/master/dth.py.
#### The Main Code / Core Functionalities
I have changed som small parts in this code but mostly the code is from this link: https://gitlab.lnu.se/1dt305/sensor-libs/-/blob/master/DHT11%20&%20DHT22%20-%20Humidity%20&%20Temperature%20Sensor/main.py
First we'll import some essential elements for this code to work.
```python=
# Data is sent to Pybytes. Needs to flashed with Pybyte firmware
import time
from machine import Pin
from library import DHT # https://github.com/JurassicPork/DHT_PyCom
```
Next, we'll have an infinite while-loop where the result of the temperature and humidity values are being printed. The temperature is presented in celcius and the humidity is presented in relative humidiy percent.
```python=
th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0)
time.sleep(2)
while True:
result = th.read()
while not result.is_valid():
time.sleep(.5)
result = th.read()
print()
print('Temperature:', result.temperature)
print("----------------")
print('Humidity:', result.humidity)
print()
````
Lastly, we need to send our values to the pybytes website/app. The "1" and "2" in the parentheses are which channel the signal is sending to. Then the device "sleeps" for an hour before next values are being sent. If you want to send data more often or less often, just change "3600" in time.sleep().
```python=
# Sending signal to pybytes
pybytes.send_signal(1,result.temperature)
pybytes.send_signal(2,result.humidity)
time.sleep(3600) ## 1 hour
```
Now you have all the code needed. Click on **"upload"** in your IDE for uploading the code to the lopy4. Now the code should be running and the sensor will be measuring data, printing it and sending it to pybytes.
When you have uploaded your code to the device, you no longer need to connect it to the computer, instead you can use another power source like a **powerbank** and have it running in the background all day, which was what I did in this project.
### Transmitting the data / connectivity
The sensor values is being sent to pybytes via Wi-Fi every hour. As noted earlier I didn't configure any transport protocol myself since I use pybytes and it's done atomatically using MQTT.
### Presenting the data
The data is saved in the pybytes database everytime the data is sent (in this case: every hour). Then the data is saved for one month. For longer usage you would need to transmit the data to a database that's used for longer timeperiods.
#### The Dashboard
Humidity dashboard:

Temperature dashboard:

### Final thoughts
I really loved getting into the IoT-area and being able to actually build something myself! If I were to continue working with this project (which I think I will do on my free time) I would like to add more sensors for more measurements, like an air quality sensor. I would also like to connect a screen of some sort for presenting the values directly for a quick overview, that would be awesome to learn. Also, it would be nice to try to connect the device to LoraWAN, Sigfox or TTN just for the experience and being able to get it working without using Wi-Fi.
It has been really exciting to learn so many new things and realizing that technology and IoT may seem very difficult and scary at first, but once you get into it, it's actually not that complicated to build yourself your own IoT device. It's just a matter of interest and giving it time.
Final result:
