# Indoor Temperature and Humidity Sensor with Fan control Christopher Gustafsson, cg222qs This tutorial will show you how to create a IoT device that monitors indoor temperature and controls a fan that will be mounted in your window. **Time: (approx) 2-3 hrs** ## Objective I have chosen to build this device with the main goal to monitor the indoor temperature and humidity in my appartment over the summer, as an added bonus i have also connected a fan to the device wich will be mounted in the window with the goal of lowering the indoor temperature by using outdoor air during colder periods of the day ( i.e. during the night ). I hope this device will give me more information of how the temperature in my appartement changes during the day, and how it changes over longer periods of time. I also hope that by utilizing the fan i can bring down the temperatures indoor to a more comfortable level. ## Material Here i will briefly discuss all of the materials needed for this project. ### List of materials | Thing | Why | Costs | Where to buy | | ----- | ------------------ | ------ | ------------ | | LoPy4 | ESP32 based IoT Development platform | ~35 EUR | https://pycom.io/product/lopy4/ | | Expansion board | To be able to connect your LoPy4 to your computer over USB | 16 EUR | https://pycom.io/product/expansion-board-3-0/ | | USB A - Male to Micro B male cable | To connect your Lopy4 to your computer | ~39 SEK | https://www.electrokit.com/produkt/usb-kabel-a-hane-micro-b-5p-hane-1-8m/ | | Breadboard | Nice to have to connect your sensors | 39 SEK | https://www.electrokit.com/produkt/kopplingsdack-270-anslutningar/ | | DHT11 | Digital Temperature and humidity sensor | 49 SEK | https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/ | | Photo resistor | Gives an indication of light conditions (optional) | 39 SEK | https://www.electrokit.com/produkt/ljussensor/ | | Wires | To connect everything to LoPy4 | 36 SEK | https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/ | | CPU/Computer fan | To push cold air from outside into the room! | Free? | I used an old fan that i had lying around| | ~9-12 V DC adaptor | To provide proper current/voltage to the fan | ~50-300 SEK | https://www.electrokit.com/produkt/batterieliminator-12v-0-5a/ | | **Total** | | ~600 SEK | | I have chosen a Pycom LoPy4 beacuse it's easy to setup and get running with IoT dashboards and offer several ways to connect it to the cloud. The sensors are cheap and readily available, I have chosen to buy versions that is presoldered on a PCB with proper resistances aldready mounted. ![LoPy4](https://i.imgur.com/MxOBHZZ.jpg) Fig. 1. LoPy4 mounted on expansion board with Micro Usb cable attached. ![Sensors](https://i.imgur.com/5SH7fvx.jpg) Fig. 2. Light sensor (left) and DHT11 sensor (right) mounted on breadboard. ## Computer setup ### Getting the LoPy4 up and running: * The first step to get your device running is to update the firmware of the expansion board, follow the instructions [here.](https://docs.pycom.io/pytrackpysense/installation/firmware/) * Next follow [these](https://docs.pycom.io/gettingstarted/connection/lopy4/) instructions to get your LoPy4 mounted on the expansion board. * I recommend also to update the firmware of the LoPy4, [follow these instructions.](https://docs.pycom.io/gettingstarted/installation/firmwaretool/) ### Setting up your IDE: I have chosen to use VS Code, the following guide assumes you will be using it too. * If not already installed, [download and install VS Code from here.](https://code.visualstudio.com) * Next you will need Node.js to run the Pymakr plugin we will use, [download and install Node.js from here.](https://nodejs.org/) * * When all that is set up, start up VS Code and locate the extensions button on the left hand side, or press CTRL+SHIFT+X * * Search for the Python plugin, install the one from Microsoft. * * Next install the Pymakr plugin from pycom. Now you should be all setup and ready to go! When you plug in the LoPy4 to your computer, it should now be automatically detected by Pymakr and you should be able to observe messages in the Pymakr Console in VS Code. To flash your code to the LoPy4, just press the "Upload" button in the lower left-to-middle corner. ## Putting everything together Now it's time to connect everything, follow the circuit diagram below! The PCBs for the sensors have two markings, "s" is the sensor output (green lines in the circuit diagram) and "-" which is ground (black lines in diagram.). These sensors already have proper resistors mounted on the PCB, perfect for development and prototype testing, for deployment of a final product i would use custom made PCBs with space for all the components. ![Circuit Diagram](https://i.imgur.com/ea9XTtD.png) Fig. 3. Circuit diagram for the project. ### Platform For this project i have chosen Ubidots as my platform, they provide an easy to use dashboard free of charge for non-commersial users. The dashboard provides widgets for the most common types of data presentation, including time-series. With the limited amount of data this project will need the convenience is nice. For a larger project or more time consuming project i would look into creating a local installation. ### The code You will need two extra libraries for this project, download and add these to your lib folder: | Library | Why |Link | | -------- |---- |-------- | | DHT_Pycom | Library for the Temperature and humidity sensor |https://github.com/JurassicPork/DHT_PyCom/tree/pulses_get | | Urequest | To connect to Ubidots | https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py | Here's the code for the project, it will read values from the sensors and send them to Ubidots. It will also turn on the fan if the temperature is at or below 24 C. #### boot.py {%gist chrgu931/54adf0965cb9983a1ed13dca8e401e19 %} #### main.py {%gist chrgu931/0e74a5b3557285236a75061d265fddb3 %} ### Transmitting the data / connectivity The LoPy4 is connected to the internet using a local WiFi connection. The data is then transported to Ubidots using HTTPS in an JSON anotation format. The data, ie temperature, humidity, light and fan pwm setting, is sent every 10 minutes. Wich will provide good enough data while preserving data size. ### Presenting the data ![Ubidots Dashboard](https://i.imgur.com/rAmC6Dv.jpg) Fig. 4. Ubidots dashboard. Fig. 4 shows how i have configured my dashboard in Ubidots, i have chosen to display time-series and histogram for humidity and temperature. The PWM signal of the fan is displayed with a guage and a light indicator giving an approximate light condition is provided (light sensor data sadly missing in Fig. 4.). The last read temperature value is also displayed in large readable digits. Ubidots saves and preserves my data for a month, giving me ample time to review values for this project, with a paid account you can get longer storage. ## Finalizing the design ![Final design](https://i.imgur.com/r9sIrUv.jpg) Fig. 5. Final design of the project, with fan connected. Fig. 5 shows the end result of my project with the fan connected and running. Thus far i am happy with the result, i get temperature readings and can study them over time, wich was my main goal. The fan is running rather slow, so it doesn't push much air into the appartement. This has to do with my DC power supply, since my budget was constrained I had to rely on an old DC power supply I had lying around, it only provides ~8.5 V DC. To improve the project i would add a beefier DC power supply and a larger fan. I would also add one or two more temperature sensors, that would measure outside air temperature and the temperature further into the apartement. Hope you enjoyed reading this tutorial, I surely enjoyed the course and will create bigger projects when I have more free time on my hands! # The end