---
# System prepended metadata

title: Temperature & Humidity Reader with OLED Display

---

# Temperature & Humidity Reader with OLED Display
**Philip Viklund / pv222fp**

This project is about creating an IoT weather station. This station is able to read humitity and temperature with the capability of displaying it on an OLED display and publishing it to a cloud dashboard.
**Approximate time 1-3 hrs**

## Objective

This is my first IoT project that I did in order to familiarize myself with IoT. 
I chose this project becuase it seemed like it could be useful since I lacked a thermometer at home. The OLED screen of the Heltec board also seemed very useful so I wanted to test it out. This project has granted me some insight about the possibilities of IoT and sparked an intrest that I hope to continue exploring with future projects. 

## Material

To get started with this project som materials are required. The main things we will need is a controller that can do all the computing and handle the communications and a sensor that can read the temperature and humidity. Lastly we will need some components to connect these two together.  

| Item  | Price | Description |
| -------- | -------- | -------- |
| Heltec Wifi LoRa 32 v2| 492 SEK (hitechchain.se)| This is a controller that is based on  the ESP32 chip and has an integrated OLED display. For communication it supports bluetooth, WiFi and LoRaWan. 
| DHT11 Sensor| 99 SEK (kjell.com)| Sensor that can measure temperature and humidity
Micro USB cable||Needed to connect the controller to a PC
3x Jumper cables (male-male)| 72 SEK (kjell.com)| Used to connect the sensor to the controller
|Breadboard| 89 SEK (kjell.com)| Used to connect all the components
Soldering kit||Required to attach the connection pins to the controller

## Computer setup
Now that we have all the parts needed we can start to prepare and setup all of the software. This includes flashing a new firmware into the controller, setting up a development environment on our PC and lastly, familierizing ourselves with how we upload the code to the controller.

### IDE setup
Let's start with setting up our IDE which will be ATOM with the pymakr plugin.
1. The first step is to install [Node js](https://nodejs.org/en/) (v.16.15.1 LTS was used during this project) go to the website and follow the installation instructions.![](https://i.imgur.com/ScDIn7V.png =500x300)

2. Now it´s time to install [atom](https://atom.io). Go to their website and follow the installation instructions![](https://i.imgur.com/wCNOapl.png =500x300)
3. The last software we need is the plugin pymakr: 
start atom -> click on file -> go to settings
 -> go to the tab install -> search for pymakr -> click install
 ![](https://i.imgur.com/w45lm36.png)
 
 All the pc software is now installed, let's continue with flashing the firmware

### Flashing firmware
Before we can flash the hardware we will need to install the drivers [CP210x](https://www.silabs.com/documents/public/software/CP210x_Windows_Drivers.zip)

Now we can install the micro python firmware, [this](https://github.com/H-Ryan/Heltec/blob/main/PyCom%20MicroPython/Heltec%20PyCom%20MicroPython.zip?raw=true) was firmware used and it was installed via the the web tool available [here](https://nabucasa.github.io/esp-web-flasher/)

### How to upload the code

We also need to know how to upload code to the controller:
1. Start up Atom and start a new project.
2. Upload the files linked later on in this tutorial into that project folder.
3. In the pymakr window click connect device -> select COM X, it was 3 in my case. 
4. Make sure you save all the files
5. Click on upload to 
## Putting everything together

Now it's time to assemble all the pieces! 
The first thing to do is to solder the connection pins to the heltec board. Make sure you avoid damaging the ribbon cable to the screen and don't have the device connected to power. I had never soldered before but watching some youtube tutorials beforehand was really helpful!

With the board soldered we can now connect all the pieces (see the circuit diagram). The sensor have three pins, labeled S, + and - and these need to go a unique slot on the board. Connect the following:
* S goes to P23
* \+ goes to 3v3
* \- goes to ground
![](https://i.imgur.com/jzv1W99.png)

## Platform

For the IoT platform Datacake will be used. Datacake is a cloud based platform that can be accessable via many different networks such as helium or TTN. The platforms have lots of features such as widgets where you can create dashboards or make smaller applications. Datacake includes a timeseries database where your data is stored. The service has a free option where you can add two devices and make 500 transmissions per day.
## The code
The code is very simple. We will make use of three premade libraries, MQTT, DHT11 and ssd1306. We file have one file for the boot.py which will connect to the wifi network. The last file is the main.py which contains the main program of this project which is the following:

```python= #main loop
while True:

    #sensor readings
    payload = th.read()
    while not payload.is_valid():
        time.sleep(10)
        payload = th.read()

    #oled screen print
    oled.fill(0)
    oled.text("Humidity {} %".format(payload.humidity), 0, 0)
    oled.text("Temp: {} C".format(payload.temperature), 0, 25)
    oled.show()

    #send data to the cloud
    client.publish(topic=ttemp, msg=str(payload.temperature))
    client.check_msg()
    client.publish(topic=thumid, msg=str(payload.humidity))
    client.check_msg()
    #time until next reading
    time.sleep(120)
```
some notworthy parts is that the sensor needs to be converted to string for it to be printed and the screen need to be cleared before displaying a new message.

All the code is available on [GitHub](https://github.com/phvi0487/IoT-weather-sensor)

## Transmitting the data
Once the controller has done a sensor reading the data is first transmitted via WiFi. The transport protocol is MQTT where two topics have been setup in datacake, one for humidity and one for temperature. The data is being sent every two minutes 
## Presenting the data
Once the data have arrived to the database it can be viewed via a dashboard. The dashboards consists of the most recent value for each reading and is updated every two minutes 

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

## Finalizing the design
The project is now complete! We now have a fully functional weather station. Some areas of improvements would be to make use of LoRa WAN, get both sensor reading into the same transmission or add a battery pack. Overall the project went very well but due to parts arriving very late the project was a bit rushed. 
![](https://i.imgur.com/9NB6FUZ.jpg)







