# Tutorial on building a temperature and humidity sensor Name: Filip Fröst Student credentials: ff222mr **Project overview** The purpose of this project is to IoT device to measure temperature and humidity inside an apartment. The device is connected to WiFi and displays the measured values on datacake using mqtt. **Estimated time:** 8 hours # Objective I chose this project since I was a newcomer to IoT and had not much prior experience with coding. That is why I found this project as a good starting point since there is a lot of documentation online for the used hardware and since I was not sure how much interest I would find in the area of IoT, I went with the cheaper option with an ESP32 board. The purpose of this project is as mentioned previously to measure and display temperature and humidity inside an apartment, which is accesible from anywhere. The project does have room for improvement, for example regarding automation of fans when the temperature exceeds a certain number. Hopefully, by following this tutorial one could get a better holistic understanding of IoT and start building similar projects with other configurations. # Material The needed material for this project is shown in table 1. *Table 1. Bill of materials* | Item | Description | Bought from | Price | | -------------- | ----------------------------------------------------------------------------------- | ----------------- | ------ | | ESP32 | Main board for the project. Runs connection to the internet and processes the data. | Sizable | 94 SEK | | Breadboard | Used for mounting components of the system. | Sizable | 53 SEK | | 5x jumper wire | Used for connecting ESP32 to sensors. Also mounted on breadboard. | Sizable | 3 SEK | | Micro USB-cable | Used for transferring data between the computer and the ESP32 | Sizable | 18 SEK | | DHT11 sensor | Digital sensor for reading temperature and humidity. | Kjell och Company | 20 SEK | # Computer Setup ## IDE In order to setup the computer for this project a few things need to be installed. First of all Node.js was installed from this link https://nodejs.org/en/. Secondly, Atom was installed from this link https://atom.io/. The third step is to install the pymakr plugin which is done through opening atom and the package manager from Atom>>Packages>>Install. Here you search for Pymakr and install it. ## Firmware update This section will guide you through updating the firmware on the ESP32. In order to do this you must first connect your board to the computer via the Micro USB-cable. Then you will need to install this file: https://micropython.org/resources/firmware/esp32-20220117-v1.18.bin Now, you are ready to flash the board by opening this file: https://nabucasa.github.io/esp-web-flasher/ After completing these steps you are ready for your final Atom setups. You do this by opening Atom and you should see your Pymakr plugin. From there you click Setting and remove the checkboxes from Auto connect and Safe-boot before upload. After this you enter the device port (which can be find using the command ls /dev/ in the terminal) into the Device addresses (list). Now, to begin coding you simply click on Connect device in the pymakr plug-in and chose your device port. # Putting everything together This section will focus on the connections made for the project as shown in figure 1. ![](https://i.imgur.com/6o1BGqp.jpg) *Figure 1. Connection of ESP32 with DHT11 sensor and computer.* The supplied voltage is generated from the computer via the micro USB-cable. This is in turn drawn from the 3v3 output of the VCC pin of the sensor which is the pin furthest in the middle. The GND pin of the ESP32 is then connected to the GND pin of the sensor which is the pin furthest to the right. Finally the data is being transferred using pin 05 on the ESP32 and the data pin of the sensor which is the pin furthest to the left. # Platform The platform used for this project is datacake which is a multipurpose low-code IoT platform, free to use for 2 devices. Since this project only utilizes one device, and since it was seen as a pilot-project for myself, datacake suited my needs perfectly. The connection to datacake was made using WiFi and their MQTT broker. If needed, you can add functionalities to your setup using datacake, for example having it send you an SMS if the temperature reaches a certain level. # The code This section will present the code required to perform the project. The structure of the project is shown in figure 2. ![](https://i.imgur.com/YpCWR4y.png) *Figure 2. Structure of the project.* The config library contains the essential credentials for connecting to the local WiFi and Datacake. The file init is included to avoid import errors. The boot file contains WiFi configuration according to figure 3. ![](https://i.imgur.com/pdNrIn6.png) *Figure 3. WiFi connection.* This code begins by importing the network module which is a built-in-module and then the config module from packages. Then the code tries to connect to the WiFi and if its not connected it will print "connecting to network...". This process is repeated until WiFi connection is obtained, then it will print the IP-address. The code used for transferring the data from the sensors to datacake is written in main and can be found in figure 4. ![](https://i.imgur.com/Fg5VoAr.png) *Figure 4. Data transfer.* In this section, the topic at which to be published to on datacake is firstly defined. In my case, the topics will be humidity and temperature. Then the used pin is defined followed by a while loop that sends data to datacake every 2 minutes. # Transmitting the data connectivity This project as mentioned earlier utilizes WiFi and MQTT in order to transfer the data to datacake. The MQTT setup is written first thing in main according to figure 5. ![](https://i.imgur.com/tQnXzyW.png) *Figure 5. MQTT setup.* This code connects to the MQTT broker using the MQTT credentials provided from the config file, where all the data from client can be found from datacake. # Presenting the data As for now, the data is presented as a value in datacake according to figure 6. This could however be changed to a more historical perspective using graphs if sought for. ![](https://i.imgur.com/GIIlcnV.png) *Figure 6. Data visualization.* # Finalizing the design In this chapter i refer back to figure 1 and figure 6. The device works well being connected to the computer, but it is all good for installing into a outlet at home or to connect to a set of batteries. I would however recommend using a power outlet since it is supposed to stay at home, and if you want to make it mobile, you would have to change your internet connection. Moreover, a case could have been 3d-printed for the device to give it a cleaner look, but due to lack of resources, this project did not reach that far.