# Iot Project "Correlation between temperature and humidity, inside and outside of the apartment" #### Sahand Poursadeghi Khiavi (SP223HF) ### **Project description:** This is a little IoT or, in better words, a semi-IoT project. The primary purpose of this project is to find the correlation between the weather inside and outside of the apartment or the place you live now. The reason behind this project is to find the weather and temperature reaction in comparison with the outside weather, so based on the data and weather forecasts in the future, you can use it for saving energy or planning purposes. The time estimation of the project can be separated into two-part. The parts are building the project, making the sensors and code ready, and the accuracy and amount of data you want to collect. The first part, project-building and getting devices prepared, wouldn’t take too much time; based on your coding knowledge or interest, it can be somewhere between 1 to 2 hours. The second part, gathering data, depends on you. This article will check and analyze the correlation between temperature and humidity inside and outside the building for 12 hours. As I mentioned above, I chose this project because, with data and insights into the measurement we are doing, we can get good outcomes in different ways. After building the project and gathering data, we can see the relation between temperature and humidity and probably have a good quality view of the ventilation and temperature of the building and place we live. We can use the data and outcome results in a lot of ways. For example, after measuring different rooms and finding the correlation, it can be helpful for the plants with special needs of temperature or humidity inside the apartment on the humid days or based on the temperature prediction so we can move them to different places. Another example can be this data, which can be in favor of smart homes. If you are not home or traveling so, on the way back home, based on the temperature outside, you know what the temperature will be inside your place, so you can remotely control your AC or increase the temperature if you have smart home devices. #### **List of materials** * pysense 2.0 for €29.65 [you can buy from here](https://pycom.io/product/pysense-2-0-x/) * Fipy, price range between €59.40 – €65.40 [you can buy from here](https://pycom.io/product/fipy//) #### A brief description of devices ##### Pysense: Pysense is a sensor shield, which means it contains numbers of sensors. Pysense 2.0 can be used with any other pycom multi-network modules. Pysense contains: - Ambient light sensor - Barometric pressure sensor - Humidity - 3 axis 12-bit accelerometer - Temperature sensor - A USB port with serial access - LiPo battery charger - MicroSD card compatibility - Ultra low power operation (~1uA in deep sleep) And it supports Location services with: - Galileo - QZSS ##### Fipy: Fipy is the multi-network module. Users can use different networks to connect and collect their sensor data. Fipy contains: - CPU - Five Networks: WiFi, BLE, cellular LTE-CAT M1/NB1, LoRa and Sigfox - 1KM Wifi Range - MicroPython enabled - Fits in a standard breadboard (with headers) - Ultra-low power usage: a fraction compared to other connected micro controllers #### Softwere Configuration befre start: In this project, I’m going to use Atom as IDE. you can download it from [here](https://atom.io) (The website will automatically recognize your OS) After downloading and installing the Atom, we have to install the Pymkr plug-in You can see in the picture how we can install it: ![](https://i.imgur.com/XHGZe0p.png) And then just Typ "Pymakr" in the search box and click n install: ![](https://i.imgur.com/vP8e7jS.png) After installing the Atom ide and Pymakr plug-in, we have to update the firmware of our device. You can easily follow the process from [Pycom documentation](https://docs.pycom.io/updatefirmware/device/) or this article on [hackmd.io](https://hackmd.io/@lnu-iot/SJ91R_jSO). This one is for Mac users, and other OS users can check both links mentioned above. First of all, you have to download the software and install it. [Download Link](https://software.pycom.io/findupgrade?product=pycom-firmware-updater&type=all&platform=macos&redirect=true) After downloading and installation you have to connect your Pycom Expansion board to your computer After opening the app, you just installed, Press on continue several time until you find yourself in this part (picture below) ![](https://i.imgur.com/WVO3lUw.png) Don’t forget to change the type to Development and activate the Show advanced setting like in the picture Again click on Continue and follow the ceck boxes exactly as in the picture you see below ![](https://i.imgur.com/xTNtucD.png) Continue Again And finally the resoults ![](https://i.imgur.com/WazinHY.png) #### Putting everything together You don’t need to worry about wiring because In this project, we are not using external sensors, Just what we have in our presence device (Temperature and humidity). So simply connect the Fipy to Pysense like in the pictures below. ![](https://i.imgur.com/5rLwCSb.jpg) ![](https://i.imgur.com/JacVnpB.jpg) #### Platform In this scenario im using the Pybytes platform, which was developed by Pycom. Pybyte is a tool for network and data management. ![](https://i.imgur.com/afNAg6S.png) To use this platform, users have to register first. But as you can see in the picture, Pycom offers network, device, project management, etc. But in our project, we will use the Pybytes platform to visualize the data we are gathering from our sensors from pysense and send it with the fipy module through the wifi network. #### The code Before starting the code part, don’t forget to check [this tutorial](https://hackmd.io/@lnu-iot/SJ8TGsUd5) and connect your device through the wifi. After connecting through the Wifi network, you can also check and finish [this tutoria](https://hackmd.io/@lnu-iot/rk8uGYo_c)l about how to use Pybytes. There are some steps before adding the code, and now we will go through those steps. First, open Atim IDE and add an empty folder (your project folder). Inside the folder you just added to Atom, make another folder and name it “lib”. We need a folder called lib because the code we are going to use needs libraries and code modules to be able to compile and run, and we are going to add the needed library inside the folder named “lib”. You can read more about python libraries [here](https://docs.python.org/3/library/). After you open the project folder and “lib,” you have to go to [this GitHub](https://github.com/pycom/pycom-libraries/) repository and download it. After downloading the repository navigate to the pycom-libraries/shields/lib/ and copy the all python files and paste it inside the folder lib you created. At this point, if you checked the links I gave you above about how to connect to wifi and use Pybyte, you probably know that we need a main.py file inside our project folder. Create the main.py file and copy this code below and paste it inside the main file. After that, you can upload your code (don’t forget to connect your device via USB for this part). ``` import time import pycom from pycoproc_2 import Pycoproc import machine from LIS2HH12 import LIS2HH12 from SI7006A20 import SI7006A20 from LTR329ALS01 import LTR329ALS01 from MPL3115A2 import MPL3115A2,ALTITUDE,PRESSURE pycom.heartbeat(False) pycom.rgbled(0x0A0A08) # white py = Pycoproc() if py.read_product_id() != Pycoproc.USB_PID_PYSENSE: raise Exception('Not a Pysense') pybytes_enabled = False if 'pybytes' in globals(): if(pybytes.isconnected()): print('Pybytes is connected, sending signals to Pybytes') pybytes_enabled = True def pysense_sensors(): mp = MPL3115A2(py,mode=ALTITUDE) # Returns height in meters. Mode may also be set to PRESSURE, returning a value in Pascals print("MPL3115A2 temperature: " + str(mp.temperature())) print("Altitude: " + str(mp.altitude())) mpp = MPL3115A2(py,mode=PRESSURE) # Returns pressure in Pa. Mode may also be set to ALTITUDE, returning a value in meters print("Pressure: " + str(mpp.pressure())) si = SI7006A20(py) print("Temperature: " + str(si.temperature())+ " deg C and Relative Humidity: " + str(si.humidity()) + " %RH") print("Dew point: "+ str(si.dew_point()) + " deg C") t_ambient = 24.4 print("Humidity Ambient for " + str(t_ambient) + " deg C is " + str(si.humid_ambient(t_ambient)) + "%RH") lt = LTR329ALS01(py) li = LIS2HH12(py) # set your battery voltage limits here vmax = 4.2 vmin = 3.3 battery_voltage = py.read_battery_voltage() battery_percentage = (battery_voltage - vmin / (vmax - vmin))*100 print("Battery voltage: " + str(py.read_battery_voltage()), " percentage: ", battery_percentage) if(pybytes_enabled): pybytes.send_signal(1, mpp.pressure()) pybytes.send_signal(2, si.temperature()) pybytes.send_signal(3, si.humidity()) print("Sent data to pybytes") while True: pysense_sensors() time.sleep(60) ``` #### Transmitting the data / connectivity As I mentioned above, if you followed the tutorial, you know that we send our data through Wi-Fi, but how often are we doing it? Your answer is here in the last part of the code i gave you. ``` while True: pysense_sensors() time.sleep(60) ``` As you can see, we have a While loop, and we are using time.sleep() function, the number 60 inside parentheses in front of the function represents the seconds we want to wait and redo the loop. You can read more about this specific function [here](https://www.programiz.com/python-programming/time/sleep) So putting all the code for reading data from the sensors and sending it to pybytes inside a function “pysense_sensors()” we can make our code more readable and easier to undrestand. #### Presenting the data At this point, if you followed all of the steps and other external links such as connecting through the wifi, using pybytes, etc., if you added widgets and probably played around a little with the pybytes platform, you probably will be able to see data like pictures below. ![](https://i.imgur.com/og76uib.png) As you can see in this example, I’m using two different graphs to represent the same data input. im also using a widget for humidity ![](https://i.imgur.com/RBLgxN2.png) #### Outcome Finalizing the design / Where is the Correlation? Up to this part of our tutorial, we were able to gather data and visualize it with the pybytes platform, but this isn’t what we were looking for, right? This part is the last part of this tutorial, requiring a bit higher level of programming ability and maybe creativity. Now we have measured the temperature and humidity inside our place, and we need to get exact data from outside the apartment/home we live in. So here is the part creativity comes in. What solution do we have for the second part? The first idea is to do the same thing we did inside our place. Use sensors and your devices. With this solution, we are going to experience some problems. To find a correlation, we need data at the same time. The best idea is to measure the temperature and humidity parallel inside and outside of your place to get more accurate results, but it will cost you more than what you spent up till now. The other solution is to check the weather apps and website and write it down in an excel file or something. Let’s be honest, its not practical, and no one wants to sit all day and check the weather app only. So what’s the solution? Using [APIs](https://en.wikipedia.org/wiki/API). With the help of APIs, we can get the outside temperature while our sensors gather data from inside our homes. When I started this project, I had the idea of adding an API call inside the main.py and just call it when I’m calling a function inside The while loop ``` while True: pysense_sensors() API_Call() #just like this time.sleep(60) ``` But the problem is using APIs requests request library, and up till now, I couldn’t find a way to add that library into our lib folder (Requests library is not a simple .py library). So I just wrote the code for the API call and ran it on my PC during the data gathering time. How? The first step is going to openweathermap.org, Create an account and generate your API key. The second step is just to take the code and paste your API key into the API key variable and run it. ``` import requests import json def weather_api(): api_key = "" #Your API key base_url = "http://api.openweathermap.org/data/2.5/weather?" city_name = "" #Dont forget to write your city name here complete_url = base_url + "appid=" + api_key + "&q=" + city_name response = requests.get(complete_url) x = response.json() if x["cod"] != "404": y = x["main"] current_temperature = y["temp"] current_pressure = y["pressure"] current_humidity = y["humidity"] z = x["weather"] weather_description = z[0]["description"] print(" Temperature (in celsius unit) = " + str(current_temperature - 273) + "\n atmospheric pressure (in hPa unit) = " + str(current_pressure) + "\n humidity (in percentage) = " + str(current_humidity) + "\n description = " + str(weather_description)) while True: weather_api() time.sleep(60) ``` resoult of running this code: ![](https://i.imgur.com/jHY9s8y.png) IF you are intrested to Visualizing wheather API just check [this Github Repository](https://github.com/Baylex/World_Weather_Analysis) and even more you can read about matplotlib library [here](https://matplotlib.org) #### Results ![](https://i.imgur.com/Uzm1wLw.png) ![](https://i.imgur.com/Krot5nE.png) ![](https://i.imgur.com/B1sXF2D.png) ![](https://i.imgur.com/RMRs8RV.png) After checking and comparing temperature and humidity diffrences inside and outside my apartment, I can say there is a correlation between temperature but not with humidity. During the day, after an increase or decrease of 1 celsius outside, I received a 0.3 - 0.5 celsius increase or decrease inside my apartment, but there wasn’t any relationship between humidity. For the humidity, part things like cooking food or boiling water for making tea can affect the results.