---
# System prepended metadata

title: Monitoring system for indoor temperature & humidity

---

The following project regards the development of a temperature and humidity indoor station which notifies the user visually and audibly by using a microcontroller, a temperature and humidity sensor, an lcd display, an led and an active piezo(buzzer). In addition, the data that is gathered are sent online (through an MQTT broker) to a platform for visualization.

Name: Nikolaos Moskoglou
Student ID: nm222ti

The implementation of the project is quite simple and it may take approximately between 3 to 5 hours in order to complete it depending on the level of the user's experience.

## Objective

I decided to build this indoor temperature and humidity station because I consider it as a very useful and feasible solution in order to monitor the indoor climate conditions of a space.

The purpose of the project is to provide data for the indoor climate of an apartment to the user by first displaying them on the lcd screen and afterward sending them online to a platform for visualisation as well as to notify the user audibly and visually in case the temperature exceeds a certain threshold. 

The indoor temperature and humidity station is able to provide insights regarding how the temperature and humidity alter throughout the day which in turn can help the user adjust different factors for achieving more optimal indoor climate conditions.

## Materials

The materials that were required for the project were purchased from [Electrokit](https://www.electrokit.com/en/)-[Amazon.se](https://www.amazon.se/) and are listed below (the names of the items are clickable links):

|   Item     | Description |   Cost (in SEK)   |
|:----------:|:-----------:|:-----------------:|
| [Raspberry Pi Pico W](https://www.electrokit.com/en/product/raspberry-pi-pico-w/)    |  Microcontroller      |   98     |
| [Solderless Breadboard](https://www.electrokit.com/en/product/solderless-breadboard-840-tie-points-2/)     |   840-point breadboard      |   69   |
|   [DHT11](https://www.electrokit.com/en/product/digital-temperature-and-humidity-sensor-dht11/)   | Digital temperature and humidity sensor |   49   |
|    [LED](https://www.electrokit.com/en/product/led-module-rgb-smd/)  | Module RGB SMD |   19    |
|   [Sound module](https://www.electrokit.com/en/product/piezo-alarm-active/)   | Piezo active(buzzer) |   39   |
|   [LCD Display](https://www.amazon.se/Freenove-Display-Compatible-Arduino-Raspberry/dp/B0B76YGDV4?language=en_GB)   | I2C LCD 1602 Module |   99   |
|   [USB cable](https://www.electrokit.com/en/product/usb-cable-a-male-microb-male-1-8m/)   | Cable for data transfer |   39   |
|   [Jumper wires](https://www.electrokit.com/en/product/piezo-alarm-active/)   | Wires with connectors in both ends for use with breadboards         |   49   |
||   **Total Cost (in SEK)**   |        472    |

In this project, I worked with the Raspberry Pi Pico W microcontroller (Fig. 1). This device offers wifi and bluetooth connection and it can be programmed by using MicroPython. Also, it provides many digital and analog inputs and outputs and is suitable for an IoT project regardless level of experience.

![Raspberry Pi Pico W](https://hackmd.io/_uploads/H1jlCIlK3.jpg)

**Fig.1 - Raspberry Pi Pico W**

In order to connect the microcontroller to my laptop, I used a USB cable that is capable for data transfer which is also serves as power supplier from the laptop. (Fig. 2)

![USB cable](https://hackmd.io/_uploads/rJuz0Lgth.jpg)

**Fig.2 - USB Cable**

A solderless breadboard (Fig.3) was used in order to provide a stable base for connecting the differents parts/sensors of the project to the microcontroller by using male-to-male jumper wires(Fig.4)

![Breadboard](https://hackmd.io/_uploads/HycQAIgF3.jpg)

**Fig.3 - Breadboard**


![Jumper wires](https://hackmd.io/_uploads/SkN7kDeF2.jpg)

**Fig.4 - Jumper wires**

For the presentation of the actual temperature and humidity values as well as for the presentation of a message when the temperature exceeds a certain threshold, I used an LCD display 1602 (Fig.5). This screen offers I2C connection capability and features the representation of 16 characters on each one of its 2 raws.

![LCD display](https://hackmd.io/_uploads/S1_8kvxY2.jpg)
**Fig.5 - LCD display**

In order to get visual (red LED light) and audible (short beep sound) notifications when the temperature exceeds a certain threshold, I used an LED module (Fig.6) and an active piezo(buzzer) (Fig.7), respectively.

![LED module](https://hackmd.io/_uploads/BJDcJDxtn.jpg)
**Fig.6 - LED module**

![Active piezo](https://hackmd.io/_uploads/Sy8akwlK2.jpg)
**Fig.7 - Active piezo**

## Computer setup

In my case I used a macbook pro which runs on MacOS Catalina(10.15.7) so the instructions given below regard this operating system.

Concerning the development tools that I used during the project, I chose to work with **VSCode IDE** because it is quite easy to work with as well as because I thought that my prior experience with it would help me significantly. Alternatively, **Thonny IDE** may be considered as a good alternative option.

Below I  explain step by step the installation process of the necessary tools in order to accomplish the implentation of the project:

- Step 1: Download and install **[Node js](https://nodejs.org/en/download)**.
- Step 2: Download and install **[VScode](https://code.visualstudio.com/Download)**.
- Step 3: Open VScode and install the **Pymakr** extension from the corresponding panel on the left side of the screen.
- Step 4: In order to get the Raspberry Pi Pico W started, it is needed to download and update its micropython firmware. In order to do that we need the usb cable for the connection with the computer:
    - Step 1: Download the micropython firmware from **[here](https://micropython.org/download/rp2-pico-w)**.(Download the latest one (it is a uf2 file) from the Releases category and not Nightly Builds.)
    - Step 2: Connect the micro-usb end of your cable into the Raspberry Pi Pico W and while holding the BOOTSEL key down on the board, connect the big end of the cable into the computer’s USB port. After connecting it to the computer release  the BOOTSEL button.
    - Step 3: A new drive will open in the file system that is named RPI-RP2. This is the Raspbbery Pi Pico W storage where the uf2 file needs to get pasted.
    - Step 4: The pico wil automatically disconnect from the computer and will reconnect.
    - Step 5: By completing the steps above, the pico is all set and it can be tested by writing in the terminal something after the **>>>** signs e.g. print("Hello!"). If by pressing <kbd>enter</kbd> you are able to see **Hello!**, it means that the pico is working as it should.

## Putting everything together

The following circuit diagram (Fig.8), that was drawn by using **[Fritzing](https://fritzing.org/download/)**, illustrates the connections between the LED module/LCD display/DHT11 sensor/active piezo and the Raspberry Pi Pico by using male-to-male jumper wires. All the components were placed on the breadboard. 

:::info
Be careful when it comes to the connectivity of the power supply pins and always check the documentation of the components because wrong voltage can cause damage to them.
:::

![Circuit Diagram](https://hackmd.io/_uploads/rJJ2h-A_3.jpg)
**Fig.8 - Circuit Diagram**

## Platform

When it comes to the choice of platform in my project, I decided to use the cloud based platform [Adafruit IO](https://io.adafruit.com/) because it is free, it is quite simple to utilize for a beginner's level project as well as it offers various data visualisation options. Specifically, this platform consists of the feeds, which include the data sent from the IoT device to the platform through a broker (**MQTT** in my case), and the dashboards, where the feeds get visualised by using graphs, gauges or other forms. In order to get started with the setup of the platform, the following steps should be followed:

- Step 1: Go to the website [Adafruit IO](https://io.adafruit.com/) and create an account. Sign in to the account and go to the control panel.
- Step 2: Go to the **feeds** and create two (2) new feeds by using the names **temperature** and **humidity**.
- Step 3: Go to the **dashboard** and create a new dashboard by naming it **Temperature_Humidity**.
- Step 4: Click on the settings button in the **dashboard** section and link the dashboard with the feeds **temperature** and **humidity**. At this point you can choose the form of visualisation for two (2) feeds (e.g. graphs, gauges).
- Step 5: Save somewhere in your computer the username, active key, the entire path name of the humidity feed and the entire path name of the temperature feed. They are needed in order to be used later in the code so the data can be sent to **Adafruit IO**.

## Code

:::info
The entire code for the project can be found on Github **[(click here)](https://github.com/nikolaos4136/Monitoring-system-for-indoor-temperature-humidity-IoT_Project_/tree/main/src)**.
:::

To begin with, I created an empty project by clicking on the **Pymakr** extension on **VSCode**.

The structure of the project as seen on **VSCode** is the following:

![Structure of the project](https://hackmd.io/_uploads/rJ5u_N1Y3.png)

The **lib** folder contains the necessary libraries for the LCD display (**[lcd_api.py](https://github.com/nikolaos4136/Monitoring-system-for-indoor-temperature-humidity-IoT_Project_/blob/main/src/lib/lcd_api.py)**, **[pico_i2c_lcd.py](https://github.com/nikolaos4136/Monitoring-system-for-indoor-temperature-humidity-IoT_Project_/blob/main/src/lib/pico_i2c_lcd.py)**) that were downloaded from this [website](https://www.circuitschools.com/interfacing-16x2-lcd-module-with-raspberry-pi-pico-with-and-without-i2c/), the library for the MQTT protocol (**[mqtt.py](https://github.com/nikolaos4136/Monitoring-system-for-indoor-temperature-humidity-IoT_Project_/blob/main/src/lib/mqtt.py)**) that was downloaded from [here](https://github.com/iot-lnu/applied-iot/blob/master/Raspberry%20Pi%20Pico%20(W)%20Micropython/network-examples/N2_WiFi_MQTT_Webhook_Adafruit/lib/mqtt.py) and the file that contains the credentials for the wifi connection and Adafruit (**[secrets.py](https://github.com/nikolaos4136/Monitoring-system-for-indoor-temperature-humidity-IoT_Project_/blob/main/src/lib/secrets.py)**).

The file **[boot.py](https://github.com/nikolaos4136/Monitoring-system-for-indoor-temperature-humidity-IoT_Project_/blob/main/src/boot.py)** contains the code (it was downloaded from [here](https://github.com/iot-lnu/applied-iot/blob/master/Raspberry%20Pi%20Pico%20(W)%20Micropython/network-examples/N1_WiFi_Connection/lib/wifiConnection.py)) so that the Raspberry Pi Pico W can establish connection to the wifi network (by using the credentials from the corresponding file as stated above) as long as it gets supplied with power.

The file **[main.py](https://github.com/nikolaos4136/Monitoring-system-for-indoor-temperature-humidity-IoT_Project_/blob/main/src/main.py)** contains the core part of the functionality in the project and those are the important parts in the code: 

- **Importing the libraries**:

```
import time                   # Allows use of  time.sleep() for delays
import ubinascii              # Conversions between binary data and various encodings
import machine                # Interfaces with hardware components
import dht
from mqtt import MQTTClient   # For use of MQTT protocol to talk to Adafruit IO
from machine import I2C, Pin
from pico_i2c_lcd import I2cLcd
from secrets import credentials  # Importing the credentials for Adafruit IO
```


- **All the sensors/components are initialised based on their connection with Raspberry Pi Pico W**:

```
onboard_led = Pin("LED", Pin.OUT)
tempSensor = dht.DHT11(machine.Pin(21))

buzzer = Pin(22, Pin.OUT)

LED_Pin_Red = Pin(26, Pin.OUT)
LED_Pin_Green = Pin(27, Pin.OUT)
LED_Pin_Blue = Pin(28, Pin.OUT)


# LCD setup
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
celsiusSymbol = bytearray([0x0C, 0x12, 0x12, 0x0C, 0x00, 0x00, 0x00, 0x00])
lcd.custom_char(0, celsiusSymbol)
```

- **Adafruit IO configuration/Connection to Adafruit with MQTT protocol**:

```
AIO_SERVER = "io.adafruit.com"
AIO_PORT = 1883
AIO_USER = credentials["AIO_USERNAME"]
AIO_KEY = credentials["AIO_KEY"]
AIO_CLIENT_ID = ubinascii.hexlify(machine.unique_id())  # Can be anything
AIO_humidity_FEED = "nikolaosmoskoglou/feeds/humidity"
AIO_temperature_FEED = "nikolaosmoskoglou/feeds/temperature"

client = MQTTClient(AIO_CLIENT_ID, AIO_SERVER, AIO_PORT, AIO_USER, AIO_KEY)
client.connect()
```

- **The temperature and humidity values are measured by the DHT11/If temperature exceeds 23 degrees, the LED module blinks red, the active piezo beeps and a message is displayed on the LCD screen/The temperature and humidity are displayed on the LCD screen and they are sent to the Adafruit IO platform/The onboard LED is on when publishing the data(the data are sent to the platform every 40 seconds)**:


```
publish_interval = 40

while True:
    try:
        if ((time.time() - last_publish) < publish_interval):
            lcd.clear()
            onboard_led.value(1)
            tempSensor.measure()
            temperature_value, humidity_value = tempSensor.temperature(), tempSensor.humidity()
            if temperature_value > 23:
                LED_Pin_Red.value(0)
                LED_Pin_Green.value(1)
                LED_Pin_Blue.value(0)
                time.sleep(2)
                LED_Pin_Red.value(0)
                LED_Pin_Green.value(0)
                LED_Pin_Blue.value(0)
                buzzer.value(1)
                time.sleep(1)
                buzzer.value(0)
                lcd.putstr("Temperature")
                lcd.move_to(0, 1)
                lcd.putstr("exceeded 23" + chr(0) + "C")
                time.sleep(5)
                lcd.clear()
            lcd.putstr(f"Temperature:{temperature_value}{chr(0)}C")
            lcd.putstr(f"Humidity:   {humidity_value}%")
            print(
                f"Publishing: {temperature_value} to {AIO_temperature_FEED}... ", end='')
            client.publish(topic=AIO_temperature_FEED,
                           msg=str(temperature_value))
            print("Done")
            print(
                f"Publishing: {humidity_value} to {AIO_humidity_FEED}... ", end='')
            client.publish(topic=AIO_humidity_FEED,
                           msg=str(humidity_value))
            print("Done")
    except Exception as e:
        print("Error occured: " + str(e))
    finally:
        onboard_led.value(0)
        last_publish = time.time()
        time.sleep(15)
```

## Transmitting data

I used my local wifi network in order to get the Raspberry Pi Pico W connected to the Internet. That was the only feasible solution for Internet connection because I could not have any coverage when it comes to LoraWan which would be my preferable choice otherwise. 

In order to send the data to the Adafruit IO platform, I utilized the MQTT protocol. The data is sent on a constant basis every 40 seconds.

## Presenting the data

In order to present the data (temperature & humidity values), I used separate graphs and gauges on the dashboard section of Adafruit IO. As explained above (section **Platform**) the configuration of the data visualisation is very simple as it is only needed to "connect" the feeds (incoming data) to the dashboard.

As shown below the temperature and humidity graphs represent the corresponding measured values in eight (8) hours intervals:

![Data Visualisation](https://hackmd.io/_uploads/HycTgIgth.png)

When it comes to the visualization of temperature & humidity values, they are presented as well on the LCD screen that I used. Specifically, if the temperature exceeds 23°C, then the LED module blinks red, the active piezo beeps and a message is presented on the screen before showing the actual temperature & humidity values.

## Finalizing the design

Overall, I can summarize that working on this project, it was a very interesting and useful experience as a beginning in the world of IoT. Without doubt many things could get improved and therefore my future plan is to use LoraWan connectivity (creating my own gateway etc.) as well as trigerring the activation of my A\C (depending on the indoor temperature) by using a smart plugg.

Below I present my project during function:

![Led on](https://hackmd.io/_uploads/BJmQU8xYn.jpg)
![Message on Lcd](https://hackmd.io/_uploads/ryXXULgF3.jpg)
![Temp & Humidity values on Lcd](https://hackmd.io/_uploads/r1Xm8LxYh.jpg)













