# Temperature and Humidity measurment in the home Sol Granbom (sg223hv) --- In this project a temperature and humidity meter in the home has been built. The IoT-device has been built by using Heltec WIFI LoRa 32 V2 Microcontroller and a breadboard connecting the DHT11 sensor through 3 jumperwires (male-male). The code is written in MicroPython in the IDE Atom and the Data thats sent through the device goes through Wifi to Adafruit IO. With my pre-requisites the project took approximately 1 week to make all the connections and get everything to work. But if you have a knowledge since before the project can approximately been built in some hours. --- ## Objective I chose to make a temperature and humidity meter because I don't own one since before. The interest in knowing how the indoor climate in the home was therefore evident and I also considered it a good beginner's project in building an IoT device. Since I already believed that the indoor climate in my home is not approved, this project make it possible to verify if this is the case. The insight the project will provide is an understanding of how an IoT device can be enabled with hardware, software and cloud service. --- ## Material | Material | Specification | | -------------------------------------------------- | ------------- | | Heltec WIFI LoRa 32 V2![](https://i.imgur.com/SubvJI8.png)| ESP32 based Microcontroller with Wifi, LoRa and OLED screen | | Breadboard ![](https://i.imgur.com/hsJvRSI.png) | The breadboard is a neat way of connecting the sensor with the jumperwires | | 3 units Jumperwires (Male-Male) ![](https://i.imgur.com/eToM1Hy.png) | Wires to connect the sensor to the Heltec WIFI LoRa 32 V2 Microcontroller| | Micro USB-C cable ![](https://i.imgur.com/WBlNlYr.png) | Cable to get power to the Microcontroller | | DHT11 sensor ![](https://i.imgur.com/MyUZWmU.png) | A sensor that measures the temperature and humidity | Breadboard, Jumperwires, Micro USB-C cable, and DHT11 sensor were purchased at Electrokit.com. Breadboard, Jumperwires and Micro USB-C cable were bought in a kit (LNU – 1DT305 Tillämpad IoT – Sensors only bundle) and the price was 129kr (~12,05€) together. The price for the DHT11 sensor was 49kr (~4,58€). Heltec WIFI LoRa 32 V2 was purchased on Amazon.se (AZDelivery NodeMCU ESP32 Heltec med 0,96 tum OLED-skärm och SX1276 868MHz LoRa inklusive e-book) and the price was 329,99 kr (~30,83€). --- ## Computer setup The chosen IDE for this project was Atom and it was used to upload the code. It was downloaded from the Atom website (https://atom.io/). A link to download the IDE for 64-bit Windows 7 or later was provided on the main page. To flash the firmware a hackmd guide (https://hackmd.io/@lnu-iot/By5ZUqvOq) to update firmware on Heltec was followed. After the update the code below was tested to make sure everything worked as it should. ``` from machine import Pin import time led = Pin('P22', mode=Pin.OUT) while True: led.value(1) time.sleep(1) led.value(0) time.sleep(1) ``` A pycom package, Pymakr, was installed as a plug-in in Atom in order to program and compiling the code using MicroPython. The code for the sensor used for this project the DHT11 was downloaded from Github (https://github.com/JurassicPork/DHT_PyCom). --- ## Putting everything together The microcontroller pins were soldered in place and the microcontroller was then placed on the breadboard and pressed into place. The DHT11 sensor was then placed on the breadboard by pressing the three pins into place. The DHT11 sensor was then connected to the microcontroller by 3 male-male jumperwires. One cable for power (3.3v), one for ground (GND) and one for data (P6). The jumperwires were connected from the DHT11 sensor to the Microcontroller through the breadboard as shown below in **Figure 1**. ![](https://i.imgur.com/dwmOgUS.png) **Figure 1 - Circuit Diagram** The DHT11 sensors usually require external pull-up resistor of 10KΩ but is not needed in the project because the module (The DHT11-sensor) has a built-in pull-up resistor. **Figure 1** shows the circuit diagram which is connected as followed: The pin on the left of the DHT11-sensor for data is plugged into the breadboard at P6 with a white jumperwire. The middle pin on the DHT11-sensor is for power and is plugged into the breadboard at 3v3 with a red jumperwire. The pin on the far right of the sensor, GND, is for ground and is plugged into the breadboard at GND with a black jumperwire. --- ## Platform The platform used is Adafruit IO which is a cloud service for iot. The decision of platform was made because it is free to use, and because it seemed user friendly. I have no previous experience with any platform since before, so the platform was chosen according to the two mentioned advantages. The platform has different layouts for meters, graphs, and you can create up to five dashboards with the free account which was used for this project. The free account on Adafruit.io gives the user 30 data points per minute, 30 days of data storage, and 10 feeds, among other things as seen in the picture below (**Figure 2**). ![](https://i.imgur.com/FVP5q3j.png) **Figure 2 - Adafruit IO free plan** In this project I am using two feeds: Temperature and Humidity. Should the project scale up to a monitoring station with over 10 metrics or if usage exceeds the free account maximum, a paid account (IO+) will be required. A paid account can be purchased for $10 per month, which gives the user an unlimited number of dashboards as well as feeds, and a usage of 60 data points per minute and 60 days of data storage. --- ## The code Import core functions of your code here, and don’t forget to explain what you have done! Do not put too much code here, focus on the core functionalities. Have you done a specific function that does a calculation, or are you using clever function for sending data on two networks? Or, are you checking if the value is reasonable etc. Explain what you have done, including the setup of the network, wireless, libraries and all that is needed to understand. ``` import pycom import time from dth import DTH from network import WLAN from mqtt import MQTTClient import ubinascii import machine import micropython ``` The code above shows the library imports needed to make the program work. The library "time" makes the "sleep" commands work. The library "dth" is for the DHT11-sensor. The "WLAN" part of the "network" library is needed to make Wifi work. The "MQTTClient" part was taken from the "mqtt" library which is needed for the MQTT protocol to work and talk to the Adafruit IO. The library "ubinascii" is used to convert the machine's ID to hexadecimal. The library "machine" interfaces with hardware components. The library "micropython" is needed to run any MicroPython code. The library "dth" and "mqtt" were manually added to /lib. **Wifi and Adafruit IO:** ``` WIFI_SSID = ["Your_WiFi_Name"] WIFI_PASS = ["Your_WiFi_Password"] AIO_SERVER = "io.adafruit.com" AIO_PORT = 1883 AIO_USER = ["Your_Adafruit_User_Name"] AIO_KEY = ["Your_Adafruit_Application_Key"] AIO_CLIENT_ID = ubinascii.hexlify(machine.unique_id()) AIO_CONTROL_FEED = ["Your_lights_Feed_Address"] AIO_RANDOMS_FEED = ["Your_randoms_Feed_Address"] ``` Abow is the code needed to make the program work with WiFi and Adafruit IO. (The square brackets are not used in the code but only indicate the data to be entered.) Data needed from Adafruit includes the username, the key and the name of the feeds which is retrieved from the Adafruit account. **Method for reading sensor value:** ``` def readSensor(): result = th.read() if result.is_valid(): return result.temperature, result.humidity ``` The code abow shows the method written to read the sensor. If the result of the sensor-reading is valid the method will return the result of the temperature and humidity. **Method for sending data to Adafruit IO:** ``` def sendData(datapoint, feed): print("Publishing: {0} to {1} ... ".format(datapoint, feed), end='/n') try: client.publish(topic=feed, msg=str(datapoint)) print("Sent data to %s" % AIO_SERVER) except Exception as e: print("Failed") ``` The code abow shows the method for sending the data to Adafruit IO. The datapoint is the result (temperature and humidity) given from the sensor-reading. Feed is the named feed on Adafruit IO where the result will be seen. If everything is correct the "datapoint" will be published to the feed. **Connect to Wifi:** ``` wlan = WLAN(mode=WLAN.STA) wlan.connect(WIFI_SSID, auth=(WLAN.WPA2, WIFI_PASS), timeout=5000) while not wlan.isconnected(): machine.idle() print("Connected to Wifi") ``` The code abow is showing the code written in main.py file to connect to Wifi. The method for connecting Wifi is in the boot.py file. **While Loop that executes statements as long as they are true:** ``` while True: th = DTH(Pin('P6', mode=Pin.OPEN_DRAIN),0) temperature, humidity = readSensor() client = MQTTClient(AIO_CLIENT_ID, AIO_SERVER, AIO_PORT, AIO_USER, AIO_KEY) client.connect() try: if None not in (temperature, humidity): time.sleep(0.1) sendData(temperature, AIO_TEMPERATURE_FEED) client.disconnect() client = None client = MQTTClient(AIO_CLIENT_ID, AIO_SERVER, AIO_PORT, AIO_USER, AIO_KEY) client.connect() time.sleep(0.1) sendData(humidity, AIO_HUMIDITY_FEED) else: print('Got none values from sensor') finally: client.disconnect() client = None print("Disconnected from Adafruit IO") time.sleep(1200) ``` Abow is the code that executes the statements as long as they are true. The sensor is defined ("th" in the code) with the correct pin. The data is then read with the "readSensor()" method, and sent to the Adafruit IO server with the "sendData()" method. The device is put to sleep for 20 minutes, then the iteration continues. --- ## Transmitting the data / connectivity The data from the IoT-device is connected trough WiFi. When the IoT-device reads values through the DHT11-sensor, the data is sent to the Adafruit IO-server using the MQTT protocol. Every 20 minutes, the value is read and forwarded to the Adafruit IO. --- ## Presenting the data ![](https://i.imgur.com/asEXWZ9.png) **Figure 3** ![](https://i.imgur.com/mHBJoHS.png) **Figure 4** The two pictures (**Figure 3, Figure 4**) is showing the Dashboard in Adafruit IO, "Temperature and humidity measurment in the home", which is used to display the temperature as well as the humidity in the home. In Adafruit IO you can choose between different gauges and what the two gauges shows in the first picture (**Figure 3**) is that the temperature in the apartment is 25 degrees celcius and it is 30% humidity at the time. In the second picture (**Figure 4**) the diagram shows the development in time of the temperature and humidity in the home. For apartments, the relative humidity should be between 40 and 60%. You are putting your immune system, and therefore your health, at risk if the humidity is not kept within this range. If the humidity is below 40 % or abow 60 %, the meter will change colour and "warn" that the humidity is too high or too low. Which is the case in the picture in gauge one (**Figure 3**) which shows red as a warning and that the humidity is to low. The Public Health Agency recommends that the temperature in an apartment should be between 20 and 23 degrees Celsius. If the temperature is below 20 degrees or abow 23 degrees, the meter will change colour and "warn" that the temperature is too high or too low. Which is the case in the picture in gauge two (**Figure 3**) which shows red as a warning and that the temperature is to high. High indoor temperatures can cause nausea, fatigue and headaches, but can also lead to more serious health effects. Too cold indoors can affect blood pressure and is thought to increase the risk of cardiovascular and lung-related diseases. --- ## Finalizing the design ![](https://i.imgur.com/qGoJeEB.jpg) **Figure 5** ![](https://i.imgur.com/37hT9hF.jpg) **Figure 6** ![](https://i.imgur.com/NqDsWp8.jpg) **Figure 7** ![](https://i.imgur.com/dRzXiR8.jpg) **Figure 8** The pictures (**Figure 5, Figure 6, Figure 7, Figure 8**) shows the result of the project in different angles. The IoT-device is assembled on the living room table. The project went well except for trouble in getting the parts that was needed to fullfill the project, aswell as assembling the parts and get the code to function. Which caused some stress in getting everything done in the project on time. I could have refined and polished the code further with nicer methods of connecting to wifi among other things if I had the time. It would also have been nice to have nicer stationing and decoration of the iot device with, for example, a box to hide the wires. It would have been fun to also measure light to get more values and feeds on the dashboard and also I was interested in using a Pycom-device which I will take with me to my next project.