---
# System prepended metadata

title: IoT Temperature and Humidity Sensor

---

# IoT Temperature and Humidity Sensor

#### Author 
Samuel Andersson sa224kr

### Introdution
This tutorial describes how you can build a weather station that measures the temperature and humidity, sends the data over Wifi and display the data on a dashboard. Estimated time to make the weather station is 1-4 hours, depending on prior experience of programming.

### Objective
The reason I chose to make this project is that I thought that it, as this is my first IoT-project, would be easy enough for me to get started and finish within the timeframe of the summer course that this project is part of, but it is also something that I can continue to work with and make more advanced as I get more experinced with IoT. For instance, it is possible for me to further develop the project in the future and by adding more sensors, some manipulation of the data or features like mobile application which can display the data. 

The purpose of this project is to serve as an introduction to making IoT-applications as well as making something that I can use to check the weather. I think that the project will give me insights on how all the different elements of an IoT-application, like the sensor, protocols, databases, and internet are connected and give a base knowledge from which one can develop more complex projects in the future.

### Material
To be able to make this project, the following material is needed:


| Material        | Price  | Bought at                                             |
|:--------------- | ------ |:-------------------------------------------------- |
| Pycom LoPy4     | 365 kr | [X](https://pycom.io/product/lopy4/)               |
| Expansion board | 167 kr | [X](https://pycom.io/product/expansion-board-3-0/) |
|DHT11 Sensor | 49 kr | [X](https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/)|
|Jumper cables M/F | 29 kr  | [X](https://www.electrokit.com/produkt/labsladd-1-pin-hane-hona-150mm-10-pack/)|
|Micro USB cable | 99 kr | [X](https://www.kjell.com/se/produkter/mobilt/kablar-adaptrar/micro-usb-kablar/linocell-micro-usb-kabel-svart-025-m-p93423?gclid=CjwKCAjw88v3BRBFEiwApwLevZzQZzopLKChwViVHMMx4CzDfxVnFD7eDxAQWviZOUHTEKF0QF2tjxoC5rMQAvD_BwE&gclsrc=aw.ds)

#### Specification
**LoPy4** The LoPy4 is a Micropython development platform with several wireless communcation protocols (WiFi, Bluetooth, LoRa, SigFox) and both analog and digital outputs which makes it a good device for developing IoT-projects. 

**Expansion board** The expansion board is the device that handles all the code.

**DHT11 sensor** The DHT11 is a temperature and humidity sensor, which measures temperature in °C and humidity in %. The sensor has three pinouts, 3-5V, GND and Data.

**Jumper Cables** Cables that will be sed to connect the sensor with the pycom device.

**Micro USB cable** A cable that you connect with the Pycom device in order to power it. I had an old mobile charger at home with which I power the pycom device from a wall socket. 

Here is a picture of all the material needed for this project
![](https://i.imgur.com/h5WcWFU.jpg)



### Computer setup
I used the IDE *Atom* for writing the code in the project. As of my understanding, you can use either Atom or VSCode, as these are the two IDEs with PyMakr plugin. I tried both but felt that ATom was more simple, looked more slick and was easier to navigate which was why I chose it. The Atom IDE can be downloaded at https://atom.io.

When you have chosen your IDE you will need to install the PyMakr plugin. In Atom, you go to settings > packages > search for *PyMakr* in the search bar > click the install icon.

You will also have to flash the hardware. To do that, you first need to download the Pycom firmware update from [this link.](https://docs.pycom.io/gettingstarted/installation/firmwaretool/) You then run the program, with the Pycom device connected to your computer and Atom closed. The only thing you might want to change when running the firmware updater is "type". For this project I chose *Pybytes* since it is the easiest and contains the pybytes libraries.
Other than this, I did not download or change anything on my computer (PC with Windows 10). You, might alos need Node.js for Pymakr to work in Atom, but since I did not need to install it, I guess I already had it installed.

I uploaded the code to my Pycom device by using the PyMakr plug-in in Atom and clicking *upload project to device* while the Pycom device is connected to the computer.


### Putting everything together
The sensor is connected to the Pycom device by connecting the left pin to GND, the middle pin to power (3V3) and the right pin (signal) to P23 on the Pycom device. I have made a very simple illustration of how everything is connected:
![](https://i.imgur.com/B3pNql0.jpg)

I would say that this is mostly a development setup and if I would want to make the project more advanced and add more sensors, I would probably use a breadboard to connect everything.

### Platform
I present the data on the Pybytes platform, which is a platform developed by Pycom. The data is stored automatically in the cloud and the platform communicates through MQTT protocol. Pybytes is a free, simple and easy to use platform that connects automatically to your device through the Pycom library. Since it is very user-friendly, I thought that it would be a good platform to use for an introductory project like this which was why I chose it. 
I also tried to create a local TIG-stack, which is a bit more complex but also gives you more authority to control and see what happens in the database, but was not able to get it to work properly. Therefore I chose to stick with the Pybytes platform but in a future more advanced project, I would probably want to try a solution with more authority.

### The code

```python=
# Data is sent to Pybytes. Therefore, the device needs to flashed with Pybyte firmware
import time
import pycom
from machine import Pin
from dht import DHT #https://raw.githubusercontent.com/JurassicPork/DHT_PyCom/pulses_get/dth.py


#Disable pycom rgbled heartbeat function
pycom.heartbeat(False)

#Create an object called res which is an instance of DTH using P23
#Type 0 = DHT11 sensor (the same library can be used for dht22 sensor)
res = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0)
time.sleep(3)

while True:
    result = res.read()
    if result.is_valid():
        print('Temperature: ', result.temperature) #Prints the temperature in console
        print('Humidity: ', result.humidity) #Prints humidity in console
        pybytes.send_signal(1, result.temperature) #send temperature value to pybytes
        pybytes.send_signal(2, result.humidity) #send humidity value to pybytes

        #Blink green when sending data to pybytes, mostly for testing purposes
        pycom.rgbled(0x000000) #no light
        time.sleep(0.2)
        pycom.rgbled(0x007f00) #green light
        time.sleep(1)
        pycom.rgbled(0x000000)
        time.sleep(0.2)

    time.sleep(7200) #Sleep for two hours, as that is how often I want to measure the temperature and humidity
```

### Transmitting the data / connectivity
The data is transfered to the internet over WiFi. I would have liked to try to send data with the LoRa protocol, but I do not live close enough to a LoRa gateway it to be possible to connect to it. I send data every other hour, since that is how often I want to read the temperature and humidity. Since I use the very user-firendly Pybytes platform, I can't really see/influence which transport protocols are used and how, but Pybytes uses MQTT as a transport protocol.

### Presenting the data
The data is presented through Pybytes application, which looks like this:

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

In Pybytes, you can make easy and simple charts for presenting your data, which is suitable for this easy project but, as mentioned before, if you would want to devleop something more advanced, Pybytes is probably a bit too simple. The dashboard contains two graphs, one for the temperature and one for the humidity and a map widgets showing the location. The data is preseved in Pybytes, and thereby the database, forever.

### Finalizing the design
My prior knowledge of programming was some very basic Java knowledge, so I did not plan on maiking the most advanced project. All in all, I am pretty pleased with my project. The intention of this project was to get an introduction to IoT technology and programming and to get starting making something. I believe I have done that and even if I might would have hoped that I could have developed something more advanced, I think that it is possible for me to further develop this project in the future, intergrating more sensors, more manipulation and use of the data and try other solutions for presenting and sending the data, instead of just using Pybytes. I have at least got started and learned the basic things of developing an IoT-project. By conducting this project and yaking this course, I have learned a lot of things about the IoT technology, programming and problem solving.

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