---
# System prepended metadata

title: Home temperature and humidity sensor

---

# Home temperature and humidity sensor
#### By Jonatan Nordström, jn223uz

---

## Overview
In this project we will create an internet of things sensor that publishes your home temperature and humidity to the pybytes database. This is a relatively simple project and should take about 1-2 hours.

## Objective
I chose to do this project because I wanted to monitor my home temperature over a longer duration of time. My apartment gets very warm during the summer and I wanted to have plots for this while also learning about IoT. Hopefully this data can be used to learn patterns in the home temperature that can be used to predict when to open windows and such.

## Materials
The following is a list of items needed to follow this tutorial. Prices are gathered from mouser.se since the kit i bought from electrokit.se is no longer available.

| Item     | Approx. price    |
| -------- | -------- |
| LoPy 4     | 31,51 € |
| Expansion board | 16,28 €	|
| DHT11 module with board | 8,43 €|

You will also need jumper cables to connect the sensor to the expansion board.
The LoPy 4 module is our microprocessor and is chosen due to being the preffered microprocessor of the course. 
The expansion board is needed to connect the LoPy board to the PC for uploading code, but also to power the board via micro-USB. 
The DHT11 module is our temperature and humidity sensor. Note that you can also use the DHT22 which is a similar sensor with better accuracy.

## Computer setup
To program the microprocessor we need an IDE, I use Visual Studio Code because that's what I already had on my computer. But you can use Atom as well. Note that I am using Windows 10.

- [ ] Install [python](https://www.python.org/downloads/)
- [ ] Install [VScode](https://code.visualstudio.com/)
- [ ] Install [Node.js](https://nodejs.org/en/)
- [ ] Install the [Pymakr package](https://docs.pycom.io/gettingstarted/software/vscode/) in VSCode

You may also need to [Update the firmware](https://pycom.io/downloads/) of the LoPy4 board.

Now you can start writing code for the LoPy board. Start by creating a project folder, for example called "my_project". Open this folder in VSCode (or Atom) and create the following structure.

> my_project
> |-lib
> |  |- some_library.py
> |-boot.py
> |-main.py

Where lib is another folder where we will keep our library. `main.py` is where we will write our main code and `boot.py` is the first script the LoPy runs and won't be used for anything in this project.

## Putting everything together
The following is a circuit diagram of the project.

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

Since we only have one sensor we can omit a breadboard and connect it directly to the expansion board using male to female jumpwires. 

## Platform
I chose to use the cloud-based Pybytes platform as it's both free and very simple to use with the LoPy device. It's very easy to design visualisations for the data as well. And since it's developed by Pycom you also get neat integrations like being able to change your code from the website.

## The code
I am using [JurassicPork's DHT library](https://github.com/JurassicPork/DHT_PyCom) to read the DHT11 sensor. To use this, download the library and paste it into your lib folder.

As previously stated we will leave the `boot.py` file empty. The following code is the main program in the `main.py` file. Note that if you are using the DHT22 sensor you need to change line 8 accordingly.
```python=
import time
import pycom
from machine import Pin
from dht import DHT # https://github.com/JurassicPork/DHT_PyCom

# Type 0 = dht11
# Type 1 = dht22
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()

    pybytes.send_signal(3,result.temperature)
    pybytes.send_signal(4,result.humidity)

    time.sleep(10*60)
```

## Transmitting
The data is sent every 10 minutes using the MQTT protocol. Wifi is used since the device is meant to be used in my home which has good wifi. And since the device will be connected to a power outlet the higher power consumption of wifi can be neglected.

## Presenting the data
Since the Pybytes platform comes with a database we will be using that as it is very simple to implement and has an adequate dashboard for our purposes.  And in the future you will be able to set up alerts, so that you can get a notification when it get's to hot or to cold. The data is stored everytime it gets recieved (every 10 minutes) and is stored for a month.
![](https://i.imgur.com/8gjNr7R.png)

## Finalising the design
The device is placed in the shelf in the middle of my living space. The goal is to have the sensor as close to the center of the room since this will give the most accurate readings. 
![](https://i.imgur.com/wDrxTy2.png)

This is a great beginner project for getting started with IoT as the simplicity let's you focus on the IoT aspect without worrying about the basic stuff.

I think this project went great as I now have a live graph that I can show wherever I may need. This project can easily be expanded by introducing a battery and using LoRa to save battery life. Creating a portable system for measuring wherever. 