# Synking Temperature and Humidity Sensor with Fan Through IFTTT **Alexander Heikinaho *(ah222td)*** heikinaho@gmail.com This is a simple project that sets up a temperature and humidity sensor (DHT11) and syncs it with IFTTT using webhooks. Since I've am quite unfamialiar with IoT-devices and Pycom it took about 4-5 days with research and everything. But it should only take a few hours to complete on your own ## Objective ### Why / Purpose / Insights There have been some unbearable heat in the summer and my which makes it hard for me to sleep at night. The goal with this project is to turn on the fan in the bedroom when it is to hot. This will also help me understand when on the day my room is at it warmest temperature. ## Materials ### List of the matieral This project doesn't use that many materials | Material | Price SEK | Where | | -------- | -------- | -------- | | pycom lopy4 | 350 | [elektrokit.com ](https://https://www.electrokit.com/) | | pycom extension board | 160? | [elektrokit.com ](https://https://www.electrokit.com/) | | DHT11 - temperature and humidity sensor | 100 | [kjell.com](https://kjell.com) | | HS110 - TP-link Kasa smartplug | 300 | [kjell.com](https://kjell.com) | LiPo-battery 1200mAh | 100 | [kjell.com](https://kjell.com) | | a fan | no clue | wherever you can find one | Both the lopy and the extension where bought from elektrokit, however as of writing they seem to be out of stock so the prices com from pycom own website. Also you can get DHT-sensor for at least half the price than what i got if you start to look around. ## Computer Setup Before using any code I updated the firmware on the lopy following the documentation on their [website](https://docs.pycom.io/gettingstarted/installation/firmwaretool/). I used Visual Studio Code as my chosen IDE with the pymakr extension. All the code was uploaded using this extension. You will also need NodeJS for it to function properly. ## Assembly ### Circuit setup ![](https://i.imgur.com/JaL5W9J.png) The wiring is rather simple. set it up according to the image above. ## Platform I decided to use pybytes to display the data and use IFTTT to sync the fan with the lopy [**Pybytes**](https://pybytes.pycom.io/) is a cloud-based device management platform created by pycom. It helps to sync up with the pycom and visualize the data. [**IFTTT**](https://ifttt.com) is a service that syncs up other webservices using simple if statements. It's a very simple but very powerful tool. ## The Code Lets go through the code chunk by chunk. ``` import pycom import time from machine import Pin from dth import DTH # https://github.com/JurassicPork/DHT_PyCom import urequests import keys ``` Nothing too fancy here. One thing to note is that I'm using the DTH_Pycom library from JurassicPorc. urequest and keys are used for the IFTTT webhook. ``` SLEEP = 60*15 # 15 minutes TOO_WARM = 28 TOO_COLD = 21 ``` Global variables that are used. i find that 22 C is too cold, at least in the summer. and 28 C is unbearable. ``` pycom.heartbeat(False) pycom.rgbled(0x00FFFF) # blue # DHT11 th = DTH(Pin('G10', mode=Pin.OPEN_DRAIN), 0) time.sleep(2) ``` Reads in the DHT11 sensor and sleeps for 2 seconds since the sensor is rather slow so it takes a while to get new input. ``` def read_values(): result = th.read() return result def send_values(val): pybytes.send_signal(0, val.temperature) pybytes.send_signal(1, val.humidity) def fan_toggle(): urequests.get(keys.HOOK_TOGGLE) def fan_off(): urequests.get(keys.HOOK_FAN_OFF) def fan_on(): urequests.get(keys.HOOK_FAN_ON) ``` I believe that the functions are self-explanatory by their name. *readvalues()* reads the sensor value, *sendvalues()* sends the data to pybyte. *fantoggle/off/on()* sends the webhook to IFTTT ``` def update(): result = read_values() if result.is_valid(): pycom.rgbled(0x00FF00) # green print("---------") print("Temperature: %d C" % result.temperature) print("Humidity: %d %%" % result.humidity) print("---------") print() if (result.temperature >= TOO_WARM): fan_on() if (result.temperature <= TOO_COLD): fan_off() send_values(result) else: pycom.rgbled(0xFF0000) # red print("no valid input") while True: update() time.sleep(SLEEP) ``` This is the main loop of the program. On each update read the value, check if it is valid. check if it is needed to turn on/off the fan and send the data to pybytes. ## Transmitting the data I decided to send the data to pybytes every 15 minutes since there is no need for constant updates. It takes a long time for the temperature to change after all. Since this is all in my bedroom i only need to use the my home WiFi to connect to the IFTTT and their webhooks. ### Dashboard ![](https://i.imgur.com/EFcO7Nr.png) This is how it looks like on the dashboard with temperature and humidity on the to whith the default data from pybytes at the bottom. ## Finilizing the Design ![](https://i.imgur.com/PVdU0kE.jpg) This is how it looks now with everythin assembled. Next step would be to make a proper case for the lopy, either through 3D-printing or by buying a case. I think that the result was rather good. The fan turns on and off depending on temperature without me having to do it which is nifty.