---
# System prepended metadata

title: Tutorial IoT

---

# Tutorial IoT
By Patrik Hasselblad (ph222ue)
-----------

## Tutorial on how to build a temperature and humidity sensor
  
  
The project is about how to build a temperature and humidity sensor using a Pycom LoPy4, extension board and sensors. The device will be connected to Pybytes cloud platform where the data will be saved.
  
If all goes without issues, this project would not take more than 1 hour to set up.

## Objective
#### Why this project?
I chose this project because an indoor temperature and humidity sensor helps me and my family to arrange our cooling devices based on temperature, the humidity in the air also requires some special attention from our machines.

#### The purpose
The purpose of this device is to create a nice and levelled indoor climate. For example; when it is 30 degrees celcius outdoor and the sun is burning through our kitchen window, normally the temperature would be agonizing, but with an indoor thermometer connected to the cloud, the computer can direct our attention to where cooling is needed.

#### Insights
With the data I will be able to read what room that usually requires most cooling and with help of the data I could figure out what needs to be done with the room. It will probably give me the insight on how to reduce and perfect the usage of our cooling devices.

## Material
For this device to work you will need a Pycom LoPy 4 device, temperature sensor DHT11, a breadboard and 3 cables.

As seen in the picture below, My connected device (LoPy 4, DHT11, breadboard and cables) is at the bottom. I bought a sensor kit for this course to be able to choose between sensors that I feel appropriate.

![](https://i.imgur.com/QcIwZna.jpg)


| IoT Thing | Purpose | Bought at | Price |
| -------- | -------- | -------- | ------- |
| Pycom LoPy 4     | Connection     | Electrokit.se    | LoPy 4 kit = 796 kr
| Temperature Sensor DHT11 | Temperature reading    | Electrokit.se    | Sensor kit = 299 kr
| Breadboard | Sensor connection    | Electrokit.se    | LoPy 4 kit = 796 kr
| Cables | Connection between LoPy 4 and sensors    | Electrokit.se    | LoPy 4 kit = 796 kr


In this project I have chosen to work with the Pycom LoPy4 device that is programmed with MicroPython and can connect to different bands. I also used the DHT11 temperature Sensor that I pressed down onto my breadboard. I connected the sensor to my LoPy 4 device with 3 cables that I attached firstly on the breadboard and in port GND, 3V3 and P23 on my LoPy 4 device. Detailed instructions will follow in a section below.

## Computer setup
Most of the software I already had installed due to regular school, but things I needed for this course was node.js, the latest Python language installed and appropriate plugins to make it easier to write code.

I tried to use VSCode from the get go, but ran into issues when trying to connect to the device. I therefore quickly changed to ATOM. I then installed the PyMakr plugin and went on from there.  

To initialize the device I created an account on [pycom.io](https://pycom.io) and added my LoPy device through USB connection, I flashed it and upgraded the firmware through the online-tool and simply followed the instructions.

When the device was upgraded and connected through USB, with help of the PyMakr plugin, I downloaded everything that was on the device to get the appropriate files and thereafter added the code for my project. When this was done I simply clicked the "upload project to device" icon.

## Putting everything together

![](https://i.imgur.com/yoPHTuD.jpg)

In the picture above I display how the cables are connected in my LoPy 4 device. Red cable goes in the GND port, The green cable goes in 3V3 and the data transfer uses a blue cable and is connected in port P23.

![](https://i.imgur.com/XMi05NS.jpg)

On the breadboard, as seen above, I have connected the sensor in E1, E2 & E3, to give some space between cables and sensor. The Blue cable is put in A1, green in A2 and red in A3.

At the moment this is as far as my device has been developed, but after the course I will continue my development. I have ordered a few led monitors that will be able to display the current temperature & humidity, but I didn't get the items in time so the device is at the moment very basic.

## Platform
I have chosen the Pybytes cloud-based platform, mainly because it was the first I tried and I understood it quite instantly. I use a pycom device and also thought that I would get some extra benefit from staying on their platform. I can simply connect my device to the platform and it tells me if there are new firmware updates and I found that neat.

Pybytes also offers an easy-to-use mobile app that is a nice addition to the platform and offers a more flexible solution to my device. I also found the built-in Pymakr that made it really easy to make small changes without having to open up ATOM and connect through there.

## The code
#### Imports
The code below shows how my device works. I import **time** and use it so the device know when it should take a reading and when to remain idle. The **Pin** import uses the library from the device and is used to tell the device where to look for data. The **DHT** import is the actual sensor and is used to recieve the data required.

#### The code block
**th** is a variable where the current value is stored, P23 is the pin where the temperature sensor data is transmitted through and the number 0 at the end tells us what device I am using, there are 2 different sensors for this library and depending on what sensor you have, this might have to be changed.

**def send_env_data():** is a function that is used to print out the result in the terminal aswell as send the result to my pybytes cloud. The function will send it if the condition is True, which in this case means, if data is recieved from the sensor, then it is True, otherwise it will retry until I disconnect the device.

```
import time
from machine import Pin
import _thread
from dht import DHT

th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0)
time.sleep(2)

def send_env_data():
    while True:
        result = th.read()
        while not result.is_valid():
            time.sleep(.5)
            result = th.read()
        print('Temp:', result.temperature)
        print('RH:', result.humidity)
        pybytes.send_signal(1,result.temperature)
        pybytes.send_signal(2,result.humidity)

        time.sleep(5)


_thread.start_new_thread(send_env_data, ())
```

## Transmitting the data / connectivity
To transmit the data to the internet and platform I used WiFi. The data package is sent with the help of the code strip **pybytes.send_signal()**. The pybytes_config.json file is then read and in that my credentials is stored so that the device knows where to send the data. The data is sent every **60** seconds and is sent with help of the **MQTT** protocol.

## Presenting the data
The dashboard is built according to Pybytes standard settings. I have a device location, data recieved the past hour and data recieved the past day (Picture below, I hid my location for security reasons). The data is saved at the cloud for 30 days, unless I start to pay for the services.
![](https://i.imgur.com/PQPsdo2.png)


#### Temperature diagram
![](https://i.imgur.com/8TrDdW4.png)
The temperature the day of the test was solid on 29 degrees.

#### Humidity diagram
![](https://i.imgur.com/aB1MOco.png)
The humidity, however, changed a bit during a small workout close to the sensor, just to display that it actually worked. Once I stopped the workout the humidity dropped a few values. A few days later I did a few more tests and the humidity had dropped a little bit further, which was expected.

The sensor readings are being made once every 60 seconds and sent to my pybytes platform. On the platform the data is saved for 30 days before it is deleted.

## Finalizing the design

![](https://i.imgur.com/uOn1wpn.png)
The final result of my device is shown in the picture above. It would have been grander and more impressing if I had recieved my items before this deadline, but luck would not have it this time. However, the plan is to add a display that will show the current temperature or humidity, it should change with the help of a button that also will be added.

What would have made this project better is a clear plan from the start so I could have the right equipment, but since I at the start was quite clueless, I thought I had everything I needed, but as it turns out, I didn't. So, to make it better for future projects; plan early and have a clear idea.

But I am fairly satisfied with my results as I have a working device connected to a platform. I would have wanted to try more but time runs past when you are having fun and dig into problems and problem solving.

I hope this tutorial has been enjoyable, thank you for reading it.

---

Patrik Hasselblad 2020-06-28