---
# System prepended metadata

title: Tutorial on building a internet connected home thermometer for temperature and humidity sensing

---

# Tutorial on building a internet connected home thermometer for temperature and humidity sensing

###### tags: Pycom, Pybytes, MicroPython    

**Holger Lindh**
**Student Cresidentials: kl223cc**
**Contact:** lindh.holger@gmail.com

# Project overview 
This project involves the using of Pycom hardware together with a DHT-11 sensor to create a Internet of things (IoT) enabled  home thermometer. The devices is connected over WiFi sending collected data over to Pycoms own PyBytes dashboard making the temperature and humidity data avalible anyware. 

---

# Time required
The project itself took a few days to complete after learing the basics of Pycom hardware and sensor technology. The time needed for a project like this will depend heavily on previous experience using same or similar hardware or programming knowledge in general. As for me, I had no earlier experience using IoT hardware and only very limited experience with programming, with no experience at all in MicroPython or Python. For only the project itself I spent an approximate of 15-20 hours. If including the time it took to learn the basics probably around 40-50 hours was spent. This includes updating the hardware, reading and watching tutorials and so on. But as said before, previous experience or overall effectiveness can influence time needed. I'm sure I could have done it faster if time was better spent. 

# Objective

**Why did I choose this project?**
With no earlier experience in this particular field, I wanted to create a project that was both relatively easy to start off with but that would at the same time be somewhat useful. I felt like a temperature and humidity sensing device could actually be something that I would use at home (Especially since I don't have any similar device at home).

**What is the purpose?** 
During the winter months, my apartment uses central heating that I myself have limited control over. A thermometer is useful to measure if, for example, the apartment actually is **cold** today or if it's just me being colder then normal. 

Also, my mother recently complained that my apartments air is dry (?), in which I will now have a way to measure and compare. Perfect!


# List of material
**Here is what you need for this project**

- [ ] LoPy4 with headers
- [ ] Pycom Expansion Board 3.0
- [ ] LoRa & Sigfox antenna kit (**In this case, not required**)
- [ ] Breadboard
- [ ] Connection wires
- [ ] DHT11 temperature and humidity sensor

---
As for the DHT11 sensor, a large kit was bought with 15 sensors from Kjell & Company (Link: [:link:][Kjell]) where the sensor was included. Single sensor price is therefore an estimation. 

|#| Item                         | Price   | Bought at|
|--------| --------                     | --------| -------- |
|1.| LoPy4 with headers           | €34,95  |Pycom store    :link:|
|2.| Pycom expansion board 3.0    | €16,00  |Pycom store    :link:|
|3.| Breadboard                   | €5,63   |Electrokit [:link:][Electrokit-Bread]|
|4.| Connection wires             | €3,43   |Electrokit [:link:][Electrokit-Cable]|
|5.| DHT-11 sensor                |€6.00   |Kjell & Company :link:|
|6.| LoRa & Sigfox antenna kit    | €9,00   |Pycom store    :link:|
||**Total:** | **€75,01*** | *Shipping not included



[Electrokit-Bread]: https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/
[Electrokit-Cable]: https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/

[Kjell]: https://www.kjell.com/se/produkter/el-verktyg/arduino/moduler/playknowlogy-lilla-modul-paketet-for-arduino-p87292


**1. LoPy4 with headers**
A MicroPython programmable board with plenty of connection options making it very suitible for IoT usecases. This device is compatible with WiFi, LoRa, Sigfox and Bluetooth connections. 

![](https://i.imgur.com/bjzNqXB.png)

**2.Expansion Board 3.0**
The expansionboard is used to get USB-power and connectivity to the LoPy4 device. It is also handy for making connections into the breadboard. 
![](https://i.imgur.com/UChrlbv.png)

**3. Breadboard**
A breadboard comes in handy for easier connection to one or multiple sensors.
![](https://i.imgur.com/jh24eSy.png)

**4. Cables**
Cables are good to have in order to connect the sensors to the Expansion Board.
![](https://i.imgur.com/Ih32asC.png)

**5. DHT-11 Temperature and humidity sensor**
A cheap and simple temperature and humidity sensor with a temperature range of 0°C to 50°C which can be operated using between 3v to 5v of voltage. The accuracy of these sensors are usually around ±1°C (temp) and ±1% (humidity).
![](https://i.imgur.com/g63M0ZP.png)

**6. A LoRa or Sigfox antenna**
If you need to transfer data over longer ranges and with less power consumption (running on battery for example) the using of LoRa the LoRa network could be useful. When buying a LoPy4 device 1 year of free usage of LoRa  is included. 

In my usecase, powerdrain was not an issue and I have therefore not used WiFi instead of LoRa or WiFi. 
![](https://i.imgur.com/sTDWqkv.png)

---

# Computer setup 

Windows 10 has been used as operating system for the project. 

**Picking a text editor** 
Pycom recommends using either Atom or MS Visual Studio Code as text editors when working with their projects. The main reason for this is that both these IDEs are compatible with their own Pybyes software plugin. This plugin makes it easier to get started using your Pycom device. 

Because I have used Atom before and feel familiar using it, liking its simplicity, I picked it as my IDE for this project. The plugin has worked without issues and has made creating and uploading the code a nice experience. If you have previous experience with MS Visual Code I am sure it will work just as well (Atleast acording to Pycom), but I have no experience using it and can therefore not comment on how well it works. 

**The first thing** you is to install node.js on your computer which is needed to get the Pymakr plugin to work (see below). Installation can be found here: [[:link:]][nodejs] 

[nodejs]:https://nodejs.org/en/

**The second thing** you want to do is to install the Pymakr plugin for your chosen IDE. This plugin helps you to make use of the repl interface and running/uploading code to your Pycom device. 

You can find the plugin by searching for "Pymakr" in avalible packages. Installing is as simple as pressing "install"

![](https://i.imgur.com/rP7G74j.png)

# Setting up your Pycom device

First of all, make sure the firmware is updated on both your LoPy4 device as well as your Expansion Board to mitigate any eventual issues. 

I recommend using this official tutorial for help updating the firmware as it can vary depending on your operating system and hardware: https://docs.pycom.io/gettingstarted/installation/firmwaretool/

---

# Connecting the components

For this project a DHT-11 sensor was used. Please mind that different manufacturers of these sensors could have different pinouts (Something I had to find out the hard way). The sensor used for this project has the following pinout: 

![](https://i.imgur.com/vSkfZAl.png)

Connect the DHT11 sensor to your breadboard and then connect cables through the breadboard to the respective connectors on your Pycom Expansion board. 

The sensor can handle 3.3 - 5 volts, no resistors should be needed as the output of the Pycom device is 3.3v. 

I chose to color code my cables respectively to make it easy for myself, but is not absoluteley necessary, but recommended.

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

# Platform

In order to make the data avalible anyware, Pybytes is a easy way to get started. 

Create an account att https://pybytes.pycom.io/ and add a new device. The website will help you getting started and adding your wifi cresidentials. 

Next up, flash the Pybytes software for your Pycom device via USB to your computer using the firmware updating tool. 

![](https://i.imgur.com/bkIJJf2.png)
![](https://i.imgur.com/p3ehT6k.png)


**The firmware updater** will ask you for a token which can be found on your newly created device page on Pybytes under "provisioning".

![](https://i.imgur.com/NP98Wer.png)

# The code

When your done with setting up Pybytes, it's time to get the code up and running.


In order to start reading the DHT-11 sensor data, I have relied on a DHT library that can be found here: https://github.com/JurassicPork/DHT_PyCom.:link: 
Thanks to JurassicPork for creating this library. 

This library is also compatible with DHT-22 sensors if you'd rather use one of those. 

**Next up** we need to write our main file of code to utalize the library and start sending the data from the device over to pybytes. The code is presented below with and without comments. 

![](https://i.imgur.com/HXseHok.png)

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

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

while True:
    result = dataRead.read()
    print('Temperatur:', result.temperature)
    print('Luftfuktighet:', result.humidity)
    pybytes.send_signal(1, result.temperature)
    pybytes.send_signal(2, result.humidity)
    time.sleep(60)
```

# Transmitting the data and connectivity

The data is transmitted via Pybytes and can be read via the Pybytes website. I chose to not use any third-party softare as the Pybytes implementation felt easy to use and fufilled all the needs that I required from the project.

The data-transfer size is relatively large at 8 bytes which can be a disadvantage if for example energy consumption is a critical factor. As for me, the device will be used with a constant wired connection via USB from a wall-outlet, which means that power consumption is not an issue. 

The data is transmitted using WiFi for convenience. It is not the most long range or energy efficent way to transfer data, but as mentioned before energy is not a factor and range is limited to my apartment. Pybytes uses MQTT transport protocol which is perfect for sending small packets of data to and from IoT devices over WiFi. 

I have opted to send the data every **60 seconds** which for me feels like a reasonable amount of time for updates. 

**If** I would expand the project, I would probably somehow try to lower the data-size, maybe reduce the time between updating the data as well as start using the LoRa network. With a lower consumption I could make the system more portable together with a battery/powerbank. 

# Presenting the data

The data presentation is done mainly through the Pybytes dasbhoard. It can also be presented individually with different types of charts using the signals menu. 

---

![](https://i.imgur.com/4wqt5p4.png)

---

**Signal menu**
![](https://i.imgur.com/Eg7cOAH.png)

---

**Data presented in a table**
![](https://i.imgur.com/IMmaCf8.png)

---

**The data is saved** for one month on the Pybytes platform. Additional time and database synchronization can be done using external service integrations. I have not felt the need to do this for my project. 

---

![](https://i.imgur.com/KlO74Ws.png)

# Final results

The project has intended and the temperature and humidity data is being transfered over WiFi to Pybytes making it accessible on multiple devices independent of position. 

The project could as mentioned above be extended to incorperate a more energy efficient data-transfer such as LoRa or LTE protocols and maybe a water-proof enclosure making it useful to have at remote locations (as a weather station).

**This was my first ever** IoT project and first time working with such sensor and transmitting hardware. Although the project itself is relatively simple I am very happy with the results I have manged to achieve. 


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


---
