# Tutorial on how to build a temperature and humidity sensor ## Project overview #### Temperature and humidity sensor ##### Author: Eric Haga (eh223ub) :::info This guide describes how to build a simple temperature and humidity sensor. You will learn how to transmit and present the sensor data, allowing you to monitor and track changes in temperature and humidity from anywhere online. ::: #### Time estimation: 2-4 hours. ## Objective #### Why you chose the project I have chosen this project for two main reasons: 1. I have an interest in monitoring climate. 2. It's simple and unexpensive to put together. #### What purpose does it serve In this case, the sensor will only be used indoors to monitor air temperature and humidity levels. I will be able to track the conditions and fluctuations when I'm not home or when I'm sleeping. I will also combine these results to draw a "real feel" temperature value using the [heat index formula](https://en.wikipedia.org/wiki/Heat_index). The purpose is to gain a better understanding of the indoor conditions, and in the future use this data to optimize the apartment (eg. by automating a fan, dehumidifier, or air conditioning system). #### What insights you think it will give I'm currently living in a relatively warm area of Spain. The high humidity often makes the temperature feel warmer than it actually is, and I'm interested in seeing how that affects the indoor conditions. I also hope this project gives me a better understading of IoT as a whole. Given my time constraints, I have limited the scope of the project. That said, I think it can be a good foundation to build upon. ## Material ### List of materials | Qty. | Component | Price | Purchased | | -------- | -------- | -------- | -------- | | 1 | LoPy 4.0 | 31.51 € | [Mouser](https://eu.mouser.com/ProductDetail/pycom/lopy-40/?qs=MLItCLRbWsz2Ys%252bwjFa7JA==&countrycode=DE&currencycode=EUR) | | 1 | Expansion Board V3 | 16.28 € | [Mouser](https://eu.mouser.com/ProductDetail/pycom/expansion-board-v3/?qs=MLItCLRbWswyAU64Xj3CwQ%3D%3D&countrycode=DE&currencycode=EUR) | | 1 | DHT11 sensor | 3.39 € | [Electan](https://www.electan.com/modulo-sensor-humedad-temperatura-dht11-p-7211.html) | | 1 | Dupont Cables (M/F) | 3.50 € | [Amazon](https://www.amazon.es/Dupont-Cable-puente-macho-hembra/dp/B00D7SDDLU/ref=sr_1_6?__mk_es_ES=ÅMÅŽÕÑ&dchild=1&keywords=dupont+mf&qid=1627043707&s=industrial&sr=1-6) | | 1 | Micro-usb cable | 5.99 € | [Amazon](https://www.amazon.es/AmazonBasics-Cable-macho-micro-Paquete/dp/B0711PVX6Z/ref=sr_1_35?__mk_es_ES=ÅMÅŽÕÑ&dchild=1&keywords=micro%2Busb%2Barduino&qid=1627044070&sr=8-35&th=1) | In total, the materials for this project cost 60.67 € + shipment, but it's possible you already have a micro-usb cable laying around (as was my case) - just make sure it's capable of transmitting data. ### Short specifications #### LoPy4 This project uses a Pycom LoPy4 device, a development board that is programmed by MicroPython and has several wireless connectivity options (WiFi, BlueTooth, LoRa, Sigfox), making it suitable for IoT. In this case, the project only uses WiFi, so it does not need an external antena. ![](https://i.imgur.com/DoCUjMm.jpg) #### Expansion Board V3 It allows us to program the LoPy4 and power it through a micro-usb connection. #### DHT11 Temperature and Humidity sensor Measures temperatures in the range 0-50° with a precision of +/-2ºC and relative humidity range 20-80% with +/-5% precision. These specifications make it a good choice for indoor measurements, which rarely, if ever, exceed those ranges. ![](https://i.imgur.com/CIwshkd.jpg) #### Dupont M/F Cables Used to connect the sensor to the device. Instead of connecting the sensor directly to the board, we use the cables to separate it from the device's own heat, and get a better temperature reading. ![](https://i.imgur.com/0GsNVkk.jpg) ## Computer Setup ### NodeJS Download and install [latest version of NodeJS](https://nodejs.org/en/) if you don't already have it on your PC. ### VSCode For this project I used VSCode as my IDE of choice, but you could also use Atom. [Download and install VSCode](https://code.visualstudio.com). ### Pymakr Open the application and navigate to the "extensions" page. Then, search for the **Pymakr** plugin and install it. ![](https://i.imgur.com/5VJ1DSC.png) Once the extension is installed, press the reload button. ### Update Pycom firmware Access [Pybytes](https://pybytes.pycom.io) and register an account if you haven't already. #### Set your network settings Navigate to network settings to add the credentials of you WiFi network. #### Provision a device Go back to "Getting Started" and select "Add Device via USB". ![](https://i.imgur.com/Z8zACO3.png) Select your device (in this case, LoPy4) and set the network type as WiFi. Give a name to your device and assign it the WiFi network that you configured earlier. Now you will get a link to download and run the firmware update tool in your computer. *(If for some reason you don't get the link, you can [find the firmware here](https://pycom.io/downloads)).* You will also get an Activation Token that you will use during the update. Copy token and proceed with the update. #### Run firmware update tool Follow the updater's instructions, and make sure your device is plugged in. On the "Communication" section, check the ``Force Update PyBytes registration`` option. ![](https://i.imgur.com/nhtn6yX.png) On the next screen, paste the activation token. ![](https://i.imgur.com/jISUlLr.png) Finally, in advanced settings check the option``Erase during update``. ![](https://i.imgur.com/FPNi8ZW.png) This process will download the firmware from the Pycom server and flash your device. ### Run and upload your code Now you should be able to use the commands at the bottom of VSCode to run your code and upload it to your board. ![](https://i.imgur.com/uKwfPaD.png) For more details, check [PyCom's documentation](https://docs.pycom.io/index.html). ## Putting everything together 1. Insert the LoPy4 pins into the expansion board's headers. 2. Connect the DHT11 sensor to the board, using the Dupont M/F cables, as shown in the diagram below. 3. To power the device, plug the Expansion Board to your computer with the micro-usb. After uploading your code to the device, you can connect it to a 5v phone charger (that way you can leave it online at all times and move it to another area of the house). The final setup should look like this: ![](https://i.imgur.com/5zBvBF9.png) Connections between the LoPy4 and the DHT11 sensor: | LoPy4 pins | DHT11 pins | | -------- | -------- | | GND | GND | | 3V3 | VCC | | P23 | S (Signal) | ## Platform This project uses the [PyBytes](https://pybytes.pycom.io) platform, which is specially aimed for PyCom devices. The platform lets you store the data in the cloud free of charge, allowing you to present the results in a user-friendly way. If the project scaled, it could benefit from using a more configurable solution and have access to the database. An interesting alternative would be to use [InfluxDB](https://www.influxdata.com) and [Grafana Cloud dashboards](https://grafana.com/products/cloud/features/#cloud-dashboards-grafana), which would provide more functionality. However, I find Pycom's own solution more manageable for a beginner project. ## The code The code of the project is structured like this: ``` -- lib -- dht.py -- heatindex.py main.py ``` ``dht.py`` is a library that reads and handles the data outputs from the temperature and humidity sensor. I used the sensor's library code from the course repository ([available here](https://gitlab.lnu.se/1dt305/sensor-libs/-/blob/master/DHT11%20&%20DHT22%20-%20Humidity%20&%20Temperature%20Sensor/lib/dht.py)), which is under GPL-3.0 License. ``heatindex.py`` applies a [heat index formula](https://en.wikipedia.org/wiki/Heat_index) to extract the results from combining the temperature and humidity values: ```python def calculate_heat_index(temperature, humidity): T = temperature RH = humidity HI = round(sum([ -8.78469475556, 1.61139411 * T, 2.33854883889 * RH, -0.14611605 * T * RH, -0.012308094 * T**2, -0.0164248277778 * RH**2, 0.002211732 * T**2 * RH, 0.00072546 * T * RH**2, -0.000003582 * T**2 * RH**2, ])) return HI ``` ``main.py`` imports both ``dht.py`` and ``heatindex.py``. It reads the temperature and humidity results from the sensor and uses those values to calculate the heat index. These results are then sent as signals to PyBytes in a repeating 60s interval: ```python while True: result = th.read() heatIndex = calculate_heat_index(result.temperature, result.humidity) while not result.is_valid(): time.sleep(.5) result = th.read() pybytes.send_signal(1, result.temperature) pybytes.send_signal(2, result.humidity) pybytes.send_signal(3, heatIndex) time.sleep(60) ``` ## Transmitting data / connectivity The data is transmitted to the internet, using WiFi as the wireless protrocol, and MQTT as the transport protocol. The device is only used in my apartment, so WiFi is sufficient for this purpose. The PyBytes platform makes it easy to transmit data with very little code. The data is sent on 1 minute intervals, using the ``pybytes.send_signal()`` function for each type of measured value. For instance,``pybytes.send_signal(1, result.temperature)`` will send a temperature measurement as signal number 1. ## Presenting the data For the presentation, I used PyBytes own interface functionality, which can display the received signals as different types of charts. ![](https://i.imgur.com/EfBtsW2.png) I created one graph for each type of signal: ![](https://i.imgur.com/AZgA5wu.png) Without integrations to external services, **the data will be preserved in PyBytes for one month.** That said, it's possible to persist your data for longer, by adding services such as AWS, Google Cloud, Azure, or by using a Webhook, but we will keep it as simple as possible in this case. ## Finalizing the design This is the final design of the project: ![](https://i.imgur.com/jYdEmZ9.jpg) It was fun and simple to build, making it a nice introduction to IoT. In the future, it might be interesting to do something more ambitious, using several sensors and trying to automate a fan or perform some other type of home climate automation.