Friday, June 5, 2015

Home Automation with a Raspberry Pi, IFTTT, and an Air Purifier

Finding the right air purifier


I wanted to get an air purifier since my Hello Sense sleep tracker would tell me that the air quality in my room was not the greatest. I also didn't want an air purifier that I would have to remember to turn on and turn off throughout the night. I began searching online for what "smart" air purifiers existed, and the only one I found was the Holmes Smart WeMo air purifier. Not only was it a bit expensive at $200, but I was not particularly fond of the way it looked.

My next thought was to get any standard air purifier and use a WeMo Switch to power the air purifier on or off. But after doing a bit of research, it seems most air purifiers will not turn back on automatically or remember your settings when powered.



Finally I concluded that my best bet was to get an air purifier that can be remote controlled, and then use an IR blaster to control it. After searching and comparing models, I ended up buying the Hoover Air Purifier 600 for $120. It was big enough to handle my room, uses HEPA filters, automatic fan speed, includes a remote control, and has good Amazon reviews.

Building the IR blaster


So now I had the air purifier. Next I needed a way of controlling it. Abandoned in a drawer somewhere I had a Rapsberry Pi (RPi), which is essentally just a small computer powered by smartphone-level components. With just a bit of googling, you can find tons of documentation on making your own IR blaster. Since I was too lazy to buy specific components, and I did not want to leave the setup on a breadboard, I bought this IR LED Kit from SparkFun.


One mistake I did was solder the IR diode right onto the board. If I could go back I would make it long enough so that I can bend it in different directions. After soldering the IR Kit together, I soldered a wire from the VCC pad to the 3v3 pin on the RPi. Another wire from the CTL pad to GPIO pin 22. And then the last wire from the GND pad to any GND pin. The following image will help show the pin layout:

Setting up LIRC


After hooking everything up, the hardware part was done. All that is left is the software. I installed Raspbian as the OS for my RPi. We will use LIRC in order to interface with the blaster. You can download it with the following shell command:

sudo apt-get install lirc

Next we will edit the file /boot/config.txt and add in the following line at the very end:

dtoverlay=lirc-rpi,gpio_out_pin=22

I used my HP MCE Tranceiver in order to record the control keys from the air purifier. The configuration below should be placed in /etc/lirc/lircd.conf :

#
# To find out how to get a proper configuration file please read:
#
# /usr/share/doc/lirc/README.Debian
 
begin remote
  name  hoover
  bits           16
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100
 
  header       9434  4662
  one           600  1773
  zero          600   606
  ptrail        586
  repeat       9402  2341
  pre_data_bits   16
  pre_data       0x42BD
  gap          111626
  toggle_bit_mask 0x0
 
      begin codes
          KEY_POWER                0x50AF
          KEY_SPEED                0x10EF
          KEY_TIME                 0xB04F
          KEY_T                    0x42BD609F
      end codes
 
end remote

The file /etc/lirc/hardware.conf should look like so:

# /etc/lirc/hardware.conf 
# 
# Arguments which will be used when launching lircd 
LIRCD_ARGS="" 

#Don't start lircmd even if there seems to be a good config file #START_LIRCMD=false 

#Don't start irexec, even if a good config file seems to exist. 
#START_IREXEC=false 

#Try to load appropriate kernel modules LOAD_MODULES=true 
# Run "lircd --driver=help" for a list of supported drivers. 
DRIVER="default" 
# usually /dev/lirc0 is the correct setting for systems using udev 
DEVICE="/dev/lirc0" 

#MODULES="lirc_dev mceusb" 
MODULES="lirc_rpi" 

# Default configuration files for your hardware if any 
LIRCD_CONF=""
LIRCMD_CONF=""

Make sure lirc is started with sudo service lirc start. You can test to see if it works by using a digital camera pointing at the IR diode and notice it blinks with the command irsend SEND_ONCE hoover "KEY_POWER". Note that your cellphone camera may NOT capture the IR blinking. My iPhone 6 did not.

Not working?


To debug the diode not blinking, enter the following shell commands in order to permanently turn the pin on which should keep the IR diode visible:

# Enable pin 22
echo "22" > /sys/class/gpio/export
# Set direction to out
echo "out" > /sys/class/gpio/gpio22/direction
# Set value to one
echo "1" > /sys/class/gpio/gpio22/value

If it is still not on, I would use a multimeter to check that 3.3V is reaching the board. Use the AC mode of the multimeter to check the voltage spiking when sending commands to the CTL pin. Also, you can short the VCC and CTL pins together to turn on the diode and verify you soldered the board correctly. If it still does not work, check that you put the diode in the right direction and the transistor is inserted correctly.

Bash script with Dropbox


After having the hardware working and configured on the Raspberry Pi, we now need to create a script that can be run every 5 minutes with a cron job. This script will scan a Dropbox directory for a file that will tell it to either turn on or off.

There is a Dropbox script called Dropbox Uploader available on Github thanks to Andrea Fabrizi. Download the file dropbox_uploader.sh in your /opt directory:

sudo wget https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh -O /opt/dropbox

Next you will download the script I created in the /opt directory as well, available on my Github. With your /opt directory having both files, remember to make them executable with the chmod +x FILE_NAME command. When you first execute the script sh /opt/dropbox it will run through the steps you need to connect it to your Dropbox account. I decided to make a directory called Home_Automation in my home directory /home/pi/ that will by used for syncing. You can edit my script air_purifier.sh to change the directory name and what the files will be called.

My script can be called with on or off as arguments to control the air purifier. If you do not pass any arguments to it, it will look for the files called air_purifier_on.txt or air_purifier_off.txt to also turn the air purifier on or off respectively inside the directory called Home_Automation. My script also turns off the UV light and sets the fan speed to automatic. I turn off the UV light because the bright blue LED on top to show the UV light is powered happens to bother me while I sleep. You can remove that line from my script if you wish. I mapped the UV control key to KEY_T.

Now we will set the cron job to run my script every 5 minutes with the command crontab -e and the following two lines:

PATH=/opt:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

*/5 * * * * cd /home/pi && sh /opt/air_purifier.sh >> /var/log/automation.log 2>&1

The first line sets the PATH to /opt so that our scripts can be found. The second line moves you to the home directory /home/pi, then it executes my script which will first call the Dropbox script to sync the Home_Automation folder and then check to see if the unique files are in there before deleting them. Then it transfer any text output to the file /var/log/automation.log

Dropbox with IFTTT


In order to automate the air purifier, I will be using Dropbox along with IFTTT. IFTTT will create a file in a specific folder within Dropbox at times you wish, and then the RPi script will continuously sync with this Dropbox folder looking for unique files.

If you have not heard of IFTTT, it stands for "IF This Then That", which allows the communication of different kinds of home automation products. I created one recipe to create the file air_purifier_on at 10:00pm, and then a file called air_purifier_off at 7:30am inside the Dropbox directory /Apps/RaspberryPi_dp/Home_Automation. This is assuming when you ran the Dropbox script for the first time, and followed the instructions, you set it to use its own app directory called Home_Automation.


And that was it! Now my Hoover air purifier automatically turns on and off. It looks kind of hacky, but it is hiding underneath my bed with direct line of sight to the air purifier in front of my bed. If you want to make it even cleaner, you can buy an IR repeater and sneak the long wired IR diode to anywhere you want. Just make sure it points directly to the front of the air purifier.

Update


So I kept getting this anxiety that I might be sitting on the side of my bed blocking the IR blaster and the air purifier would not turn on. I had an IR repeater that was broken, so I decided to rip the diode cable and just solder it onto where the current diode is on the board (in parallel). Then I placed the diode in the corner of my bed discretely and I am much happier with its professional look compared to a circuit board taped to the side of my bed.


I also created a php file on my web server that I can call in order to toggle the air purifier. The code is pretty simple: 

<?php

$pass = $_GET['pass'];

if($pass != 'my_super_secret_pass') {
  header('Location: http://www.google.com/');
} else {
  $output=shell_exec("ssh pi@192.168.1.10 '/opt/air_purifier.sh on'");
  echo "<script>window.close();</script>";
}

?>

All this does is grab the password from the address argument. If it is correct, it will ssh into my RPi and execute my script to turn the air purifier on and then close the page window/tab. If the password is wrong, it will redirect you to Google. The reason for the SSH part is because my web server is on a different machine from my RPi. In order to make the SSH smooth, I also made sure to transfer my RSA keys. If you are hosting your web server on the RPi as well, you can remove the SSH part.

Next I have Launcher which I use to have shortcuts on my Notification Center. I added a new Web Launcher which would go to https://mywebsite.com/home_automation/air_purifier_toggle.php?pass=my_super_secret_pass and use the Hoover logo. Now I can easily press a button on my iPhone or iPad to turn on the air purifier if I need to. And just in case, I made my script send KEY_TIME once in order to place a one hour timer by default since I cannot know if the air purifier is on by accident.

3 comments:

  1. I am using logitech harmony hub to have same solution as yours. a bit more expansive (not much if you buy second hand one), but easier for end user like me. just need to teach the harmony hub and trigger via ifttt

    ReplyDelete

  2. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...

    Home Automation in Chennai
    smart home in Chennai
    Home security in Chennai
    Burglar alarm in Chennai
    Door sensors Chennai

    ReplyDelete
  3. Charged Media Filters work the same way as electrostatic precipitators, but they collect particles on fiber filters instead of plates. airpurifiersmoke

    ReplyDelete