Introduction: Keeping Your Baby Cool This Summer - Controlling Dumb Fans With Smart Things!

About: 28, technical consultant by day, geeky dad by night Loves breaking things, partially hates fixing things ha...

As of two weeks ago at the time of writing this, I became a dad to an incredible baby boy!

With the seasons changing, the days getting longer and the temperatures getting warmer, I thought it would be good to have some kind of monitor about in the nursery to check how hot it was getting up there!

I haven’t tinkered with much python over the years but wanted to get back into it since my first year in university nearly 5 years ago now! Because of this I thought it would be better to document what I did, to help me later, also help anyone else wanting to do the same thing!

Using a Raspberry pi, Enviro pHAT and a smart TP-link kasa plug, you can control the plugs using the IFTTT framework! it's really simple to do and you can even modify this walkthrough to control anything through different sensors and different devices! Hope you find this useful.

If you like this instructable, please don't forget to vote for my submission in the sensors contest! :D (sorry for the asking), it's my first time doing this and would like to have a possible chance of winning something for all the time I put into this

** EDIT **

Over the next few days I am going to look at pushing this data to a Grafana board, if you think this would be good, let know! :D

Let's get right into it…


  1. Any Raspberry pi will do but I chose the Zero W as it was what I had to hand in my draw, it also has the smallest form factor too with WiFi enabled by default! - Raspberry PI Zero w
  2. SD card, everyone has different preferences on these, but I used one of these! - Micro-SD card
  3. If you are stuck, here is the official supported documentation for SD cards -official supported SD-cards
  4. Jumper wires (will explain later) - Jumper wires
  5. Enviro pHAT for all the glorious sensors! - Envrio-pHAT
  6. A smart plug, I have gone with TP-Link as you don't need any fancy hubs to get it working out the box! - Smart Plug!
  7. A dumb fan to control, you can use any fan, I just found a fan on amazon, but any wall socket fan will work fine - Dumb fan
  8. A soldering iron, but if you dont wish to use a soldering iron, you can get click in headers for your PI and pHAT - GPIO-hammer-header

In order for a connection between TP-links kasa and IFTTT to work, you will need to have a TP-link kasa and IFTTT account set up prior to starting this instructable. When you receive your TP-link smart plug the instructions that are supplied with the smart plug will guide you through the setup process including how to name your plug and it's basic functions.

Once you have all your shopping list it’s time to get assembling it all!

Step 1: Setting Up Your Raspberry Pi

Imaging your micro-SD card
Get the latest and greatest Raspbian image, the lite version will do fine, if not better for the zero w! -

Write this image to the SD card, you will need a reader for this and an imager, I use etcher -

Setup Wifi (optional)
If your PI has a built in WIFI, why not set it up now to save a lot of messing about with consoles later!

Once your pi has been imaged, the drive has been ejected. Disconnect and connect the SD card so it gets detected by your machine again. A boot partition should appear.

Create a blank file called wpa_supplicant.confwithin the boot partition of your pi, within here place the below in this new file:


Enabling SSH on your PI of choice (optional)

By default, a clean Raspbian image will have it's ssh disabled. You would need to boot the system, connect a keyboard and display to the Pi and then enable ssh. This step will enable ssh at first boot.
In the boot partition, create a new file called ssh. An empty file with exactly that name.

After completing them two optional steps, your PI will automatically connect to your WIFI and have SSH enabled by default.

Once you have imaged and configured the micro-SD card, you can then insert it to your PI or choice and power it up!

Updating your PI
Once you have booted your PI, you can find your IP address of you PI within your wireless routers settings.
Most routers are different so this would be hard to write a guide for this unfortunately!

Once your have your IP of your PI, connect through SSH and login with the following:

Username: pi
Password: raspberry

Once you have an active SSH session with your PI, you will want to update it, simply run the below with the corresponding Y or N throughout updating it:

sudo apt-get update
sudo apt-get upgrade

its always good to reboot a PI after any new updates have been installed to make sure it's ran afresh with the latest updates, this can be achieved with

sudo reboot -n

Now that's the PI ready, time to move on to the next step!

Step 2: Setting Up Your Enviro PHAT

Wiring up your Enviro pHAT

Once you have your pi all up to date, you can connect your Enviro pHAT to the PI of your choice!

***To make sure you don’t damage your PI or your pHAT make sure you turn it off first***

Here I used the jumper leads because you get can get thermal washout from the CPU getting hot under the Enviro pHAT. This extra heat can alter the temperature readings you would get later on. Because of this I have used 5 jumper leads on the below pins:

  • Pin 3 – GPIO2 (SDA I2C)
  • Pin 5 – GPIO3 (SCL I2C)
  • Pin 7 – GPIO4
  • Pin 6 – Ground
  • Pin 4 - 5V

Please check the wiring diagram at the top of this step for visual reference or the below link:

(sorry about the shoddy paint job).

When wiring up your pHAT, make sure you have it set the right way around or else you could potentially damage it!!! To do this, I had mine setup so the ANALOG IN headers are towards the SD card slot, this has later been turned around in my pictures to make it fit snugger into my "carrier".

Installing the dependencies

Power your PI back up and SSH back in.

Once back in your PI run the following to install required packages and software for your Enviro pHAT:

curl | bash

Make sure to hit Y or N when needed to.

After the install is complete, make sure to restart your PI...

sudo reboot -n

Testing the pHAT

To make sure that your pHAT is wired up right, you can drop into a python module and run a few commands by puttying back to your PI and typing python, once in a python module type the below:

from envirophat import weather

This should show you a readout like below:

>>> from envirophat import weather
>>> print(weather.temperature()) 


If you get something along the lines of an error below:

Traceback (most recent call last):<br>  File "", line 1, in 
  File "/usr/lib/python2.7/dist-packages/envirophat/", line 134, in temperature
  File "/usr/lib/python2.7/dist-packages/envirophat/", line 169, in update
    if self._read_byte(REGISTER_CHIPID) == 0x58: # check sensor id 0x58=BMP280
  File "/usr/lib/python2.7/dist-packages/envirophat/", line 116, in _read_byte
    return self.i2c_bus.read_byte_data(self.addr, register)
IOError: [Errno 121] Remote I/O error

You can then run:

sudo i2cdetect -y 1

This will run a diagnostic check on your connections/wiring and you would be looking for something like below:

pi@raspberrypi:~ $ sudo i2cdetect -y 1<br>     
0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- 1d -- -- 
20: -- -- -- -- -- -- -- -- -- 29 -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- 49 -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77

If not, then you will need to go back and check your wiring again, please check the wiring diagram and try again!

Installing the script

If all is well and you were successful on getting a temperature readout then we can go ahead and install the package!

Due to how instructables handles links, I have attached the .py script in the above step header... To copy this over to your PI I recommend using something like WinSCP:

WinSCP is a popular SFTP client and FTP client for Microsoft Windows! Copy file between a local computer and remote servers using FTP, FTPS, SCP, SFTP, WebDAV or S3 file transfer protocols.

WinSCP is simple to use, simply connect to your PI using your PI's IP address then drag and drop the .py file to where you would like to run it from for me this is from /home/pi.

Now the .py script is on your PI, it's time to move on to setting up your IFTTT account / connections

Step 3: Setting Your IFTTT Acount/connection

Next up we will look at getting a IFTTT token, this will be used when calling the hot / cold function within the script to connect to the kasa plug and turn on/off the fan.

Creating the Webhooks

Head over to and create an account if you don't already have one.

Once you have logged in, within the search bar, search: TP-link kasa

Once that has loaded, click services then TP-link kasa. You will then need to connect you kasa account to your IFTTT account, login to your kasa account through IFTTT to make a connection.

After linking your account click "My Applets" then new applet, then click the "+this" and search webhooks.

Click, "Receive a web request", within the event name type hot (this can be anything you wish but you must update this later within the script or this will not work, I have used hot to turn the fan on and cold to turn the fan off. Then click create trigger.

Next up we need to link your new trigger to your Kasa account, click "+that" and search kasa, click the TP-link kasa then click "turn on", after this select the device you wish to turn on, then click create action. Review the action and click finish if correct.


Remember if you have chosen another Event name other than "hot" and "cold", you will need to make a note of these for later and update the script! Otherwise the .py script wont work.


After we have created our first web hook, repeat the process for "cold", but this time you are selecting turn off instead of turn on when selecting the +that action.

Once we have two event webhooks within your my applets account section, click services and search webhooks then click that. Within here you will see a documentation button (top right), click that. This is where you will find your IFTTT token key. whilst in here make a note of your key (you will need it later), I have edited mine within the picture otherwise anyone would be able to turn my smart plugs on or off.

Testing the webhooks

Now we have a created two webhooks which are connected to our kasa account, we can test whether they work or not!

Within your PI, you can enter the following:

curl -X POST

Where it says "hot" you need to add your event name, if you left it as "hot" leave this alone. Where it says /you... you need to replace this with your IFTTT token key. Hit enter and you should then hear the click of your smart plugs relay turning the plug on. If you couldn't hear it then you can check the Kasa app to check the status of your plugs.

Now we have the dependencies for our Enviro pHAT installed and now have IFTTT setup, time to finish off with modifying the code.

Step 4: Editing the Script

Event name and IFTTT TOKENS
Back on your pi, escalate to the location of your script, for me its in /home/pi (my home folder) and edit the file using nano:


Within finder where it says def turn_off()

def turn_off():
# Set your trigger word - e.g. "cold" - and the IFTTT Webhook token below.
TOKEN = "YOUR IFTTT TOKEN GOES HERE""{trigger_word}/with/key/{token}".format(trigger_word=TRIGGER_WORD, token=TOKEN))
print("Fan off!"): 

This is the function to turn off the fan. It does this by sending a webhook to IFTTT which triggers the tplink HS100 to turn off. If you have kept the trigger work within your IFTTT account the same as "cold", you can leave that in, and just replace the token that you got from the webhooks documentation earlier. If you did choose a different event name then replace this word here.

After pasting in your IFTTT token, go to the next section where it says def turn_on()

def turn_on():
# Set your trigger word - e.g. "hot" - and the IFTTT Webhook token below. TRIGGER_WORD = "hot" TOKEN = "YOUR IFTTT TOKEN GOES HERE""{trigger_word}/with/key/{token}".format(trigger_word=TRIGGER_WORD, token=TOKEN)) print("Fan on!")

This function is the reverse of the off, it turns on the TP-link smart plug!

If you have kept the trigger work within your IFTTT account the same as "hot", you can leave that in, and just replace the token that you got from the webhooks documentation earlier. If you did choose a different event name then replace this word here.

Everything else is already coded in, so you shouldn't have to make any changed to the code other the TRIGGER_WORD, TOKEN and a few adjustments in temperatures which is next!

Changing the temperatures

Next up, find where it says # Local variables.

# Local variables. state = 0 # Sets the state for the switch low = 20 # Low value for temperature level (celcius). high = 24 # High value for temperature level (celcius). period = 120 # Delay, in seconds, between calls.

The state variable is how the pi knows if the plug is on or not, this is updated later if it gets too hot (24+*c then the state is changed later to 1), this would also fire off to the webhook and turn the smart plug on!

The low variable is how cool I want the room to get before the plug is turned off. If you would like the room warmer, simply change this to a higher value. If you would like it cooler, lower the value.

The high variable is the max temperature I want the room getting before I want the fan to kick in and start to cool the room down. If you would like the fan to come on before this, then simply lower the value, or if you would like it to be warmer then higher the value.

The period variable is the duration in seconds between the temperature checks, if you would like the pi script to check less frequently then higher the number if you would like to shorten the time, lower the number.

Once you have made your changes save and exit nano

CTRL + x

Step 5: Using the Script

Running the script locally

There are a few ways that you can run this script, you can connect via SSH and type the following:

sudo python /

This will run the script on demand, this will only work for as long as there is an active SSH session to the PI.
The other way is to use screen, screen is not installed by default I don't think, so an sudo apt install screen will have to be used.

Here is a great tutorial on how to use screen properly:

Screen or GNU Screen is a terminal multiplexer. In other words, it means that you can start a screen session and then open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run when their window is not visible even if you get disconnected.

Automating to run on boot

The other way is to edit the boot file in /etc/rc.local to do this use nano.

sudo nano /etc/rc.local

Editing this file will make the script run on boot. meaning that it will always be running when the PI in on, this is also how I run 90% of my scripts on other PIs.

If you have used the rc.local, then you can run the command below to check to see what is currently running on the PI. This is handy to troubleshoot later to see if the script is working at all or not:

ps -aef | grep python

Step 6: Finished!

Well... Thanks if your still here and reached this point!

Sorry it's been quite long, this was my first ever instructable and wasn't quite sure what to write down.

As I mentioned at the start, this instructables has many different applications with it using heat to trigger a smart plug! you could cool any room in the house/flat/apartment/garage/shed. You can even modify the IFTTT to trigger any smart device that is controllable through IFTTT, it doesn't just have to be TP-link kasa devices.

I would love to hear if you have tried this instructables out or even if you found it interesting please leave me a comment below!

If you have any questions be sure to leave them below and I shall try my best to answer them!

Sensors Contest

Runner Up in the
Sensors Contest