Introduction: Make a Dumb Device Smart With Pilight and HomeKit

Hi there! In this Instructable, you're going to learn how to turn 5 dumb devices into smart ones, all for the price of one single commercial smart switch. Ready? We'll get started soon, but first...

WHAT IS THIS?

This project aims to help anyone and everyone make a smart home with ease. It utilizes Pilight and a HomeKit server written in NodeJS.

WHY DIY?

DIY is a fun solution to anything, and the fun of making alone makes it worth it, in my opinion.

You'll also save a ridiculous amount of money, so that's a plus too.

Step 1: Find a Dumb Device!

This may sound simple, but it's actually not always easy. A dumb device must meet the following criteria:

1. It must have an on position and an off position (so a computer with an "On/Off" button won't work since it's the same switch, but a lamp with an "On/Off" switch will!)

2. It must be made more useful with the ability to control with your phone, not less!

3. It must NOT annoy your neighbors if you turn it on :)

4. IT MUST LOOK REALLY AWESOME WHEN YOU SHOW PEOPLE YOU CAN CONTROL YOUR LIGHTS WITH YOUR PHONE. Seriously. That's a must.

So, as a TL;DR: Anything with a switch or dial that can remain in an on position will work.

Step 2: Hardware/Materials

Here are your materials...

Solderless Plug-in BreadBoard, 830 tie-points, 2 Power lanes, 200PTS, 16.5 x 5.4 x 0.85 cm ($10, but on sale for $2.80 as of 3/25/2016. You can skip this if you already have a breadboard.)

50 PCS Jumper Wires 200mm M/F Male-to-Female ($12, but on sale for $3.15 inc shipping as of 3/25/2016. You can skip this if you already have jumper wires.)

Etekcity Wireless Remote Control Electrical Outlet Switch for Household Appliances (5Rx-2Tx) ($39.98, on sale for $29.98 as of 3/25/2016. If you have some switches that can be controlled by an RF remote already, you can skip this, but it's not guaranteed to be compatible if you do so. I doubt you have some of these lying around... if you do, go ahead and use it, but make sure it matches the frequency of the transmitter/receiver! That's a must! I HIGHLY RECOMMEND YOU GET THIS IF YOU'RE NOT SURE.)

433mhz Rf Transmitter & Receiver Kit for Raspberry Pi ($7.91, couldn't find a better deal, feel free to shop around! I doubt you have one of these lying around... if you do go ahead and use it, but make sure it matches the frequency of the Outlets you buy! That's a must! I HIGHLY RECOMMEND YOU GET THIS IF YOU'RE NOT SURE.)

The core of the project: A Raspberry Pi B+ (Should work with any Raspberry Pi! You probably have one of these in use or lying around, let's put it to use!)

You'll also need a micro usb cable, an sd card, type depending on which model Pi you're using, and a USB WiFi dongle if you need a wireless connection.

Personally I already had a Raspberry Pi... I had a micro usb cable... I had an sd card, and I had a WiFi dongle.

So the price of this project for me was $43.84, not including tax. (A single (1) Belkin Wemo switch is $49.99, $6.15 more than the FIVE switches we'll be building.) So we're paying $8.77per switch when we build, while if we bought a Wemo, we're paying $49.99 per switch.

The total price of the project will vary based on what you have, but the items I have listed prices for are the only ones you likely don't have.

For this tutorial, an iPhone is best for controlling your devices.

Step 3: Hardware/Setup

Before we get started downloading programs, we need to get our hardware set up.

We need to wire:

RECEIVER --> RASPBERRY PI

VCC --> 3.3v

DATA (There's two data pins, choose one, doesn't matter) --> GPIO 18

GND --> GND

TRANSMITTER --> RASPBERRY PI

VCC --> 5v

DATA --> GPIO 17

GND --> GND

Step 4: Remote Switches/Setup

This step is fairly straightforward - the switches came with 2 remotes, and the 2 remotes came with 2 batteries, one for each remote.

The only thing we need to do is put a battery into one of the remotes. We'll come back to it later, so don't lose it!

Step 5: Software/Setup - Node

We're going to install Node through adafruit.

This tutorial assumes you already installed Raspbian on your Raspberry Pi, and know how to SSH into it. If you don't know how to do that, visit this link.

SSH into your Pi and run this command to add adafruit's repo, which contains Node:

curl -sLS https://apt.adafruit.com/add | sudo bash

Then actually install Node:

sudo apt-get install node

Lastly, check to make sure it installed properly by running:

node -v

If everything looks good, let's keep going!

Step 6: Software/Setup - Dependencies

Make yourself superuser by running:

sudo su

Install some dependencies:

apt-get install python-dev python-pip libavahi-compat-libdnssd-dev

Lastly, install some modules that node needs:

npm install -g node-gyp && npm install python-shell && npm install node-persist && npm install srp && npm install mdns && npm install ed25519 && npm install curve25519 && npm install debug

That's it for the Node setup - let's go set up pilight!

Step 7: Software/Setup - Pilight

SSH into your Pi and login. Run:

sudo su

If it requests a password, type it in. Next, run:

nano /etc/apt/sources.list

You should see at least one line on this file, such as: "deb https://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi"

Below the last line of that file, add:

deb https://apt.pilight.org/ stable main

Save by pressing CTRL + X, followed by Y for "yes, save."

Update the database of packages:

apt-get update

And finally, install Pilight:

apt-get install pilight

Pilight should now be installed, and should start at boot. Some people have had trouble with auto start, so if you think that it isn't starting on boot, as root, run:

pilight-daemon

Step 8: Sniff Out RF Codes

The next step is to sniff out the RF codes.

Each button on the remote has a code that matches with the relevant switch. In the picture of the switch above, the switch has a general code of 1502. It is the 4th switch in the 5 pack, hence 1502-4. But we need the RF code, not the general code. To get this, we sniff the RF code out.

To turn that switch on with the remote, you would simply press the 4th "On" button. So we'll grab the RF code sent when the 4th "On" button is pressed.

SSH into your Pi again (or just continue your last session, if you didn't close it) and run:

sudo pilight-receive

If there is no error, skip past the underlined text. If there's an error about "no pilight-ssdp sessions found," you're going to want to read the underlined text.

The error "no pilight-ssdp sessions found" would mean that "pilight-daemon" is not running. Chances are it didn't start up at boot, and you'll need to start it manually. Do so by running the command "sudo pilight-daemon"

Grab the remote with the battery from Step 4, and bring it close to the receiver.

We'll start with Switch 1. With the pilight-receive program still running, press the "On" button for switch #1.

Look at the output of the pilight-receive program. Did it show anything?

Yes! It did! It should look like this!

"message": {

"id": "A3",

"unit": 34,

"state": "on"

},

"origin": "receiver",

"protocol": clarus_switch",

"uuid": "SOMENUMBER", "repeats": 1

}

Now, we need to understand what that means, and allow ourselves to control it.

The information we'll need for the next step is the id, the unit, and the protocol.

Step 9: Write RF Send Script

Using the (id, unit) information from the last step, we can now send RF signals.

Step 6 told me that my switch's protocol was clarus_switch, the id was A3, and the unit was 34.

With that in mind, we can write a script that will send the same code from the Raspberry Pi as the remote would send.

Go to the directory you want to save the scripts, then run:

nano Switch1_On.sh

...to create and edit the On script file. If you're using #2 switch, then you can name it Switch2_On.sh or something like that.

Then, put this in the file:

sudo pilight-send -p [protocol] -i [id] -u [unit] -t

...where [protocol] is your protocol, [id] is your id, and [unit] is your unit. "-t" means "On."

So if your ID and UNIT were the same as mine, your script would look like this:

sudo pilight-send -p clarus_switch -i A3 -u 34 -t

Save the file with CTRL + X, followed by Y for "yes, save that file."

Make the file executable by running:

sudo chmod +x Switch_On.sh

Next, we'll create the Off script. You can actually use the exact same script with one change, so we'll just copy the script over and edit it. The execute permissions will be copied as well, so we don't need to run chmod again.

cp Switch1_On.sh Switch1_Off.sh

nano Switch1_Off.sh

The file should be changed from:

sudo pilight-send -p [protocol] -i [id] -u [unit] -t

...to:

sudo pilight-send -p [protocol] -i [id] -u [unit] -f

...where -f means to turn the switch OFF.

Step 10: Test the Scripts

Congratulations, you made it to the last step of Part 1 to this tutorial. We now get to test the scripts to make sure they work.

But first, we'll need our dumb device from Step 1!

My dumb device was a fan. I switched it to the "ON" position on setting 1, so that the fan is always running while it has power.


Next, I plugged it into RF Outlet #1 (the one that we wrote the script for), and plugged the outlet into the wall. You can confirm the outlet is working normally by turning it ON and OFF with the remote.

Assuming the remote worked properly, it's finally time to try out the scripts. SSH into your Pi again, or continue your session if you haven't closed it already, then run:

sudo ./Switch1_On.sh

Did it turn your dumb device on? Yes? Your dumb device is now a little smarter.

Now, turn it off by running:

sudo ./Switch1_Off.sh

Off? Yes? Your device is now even smarter.

You can now use these scripts to turn your outlets on and off, and any service that can run your .sh files can now be used to turn your dumb device On/Off.

But what about making it even more smart?

Step 11: HomeKit/Siri Integration - Editing

HomeKit provides an excellent platform for home innovation and automation, even though it's not that popular yet.

We'll start by cloning my Git repo that is forked from KhaosT. I made a few modifications for the purpose of this tutorial, but you can always find the original here.

To clone my fork and enter the accessories directory within it, run:

git clone https://github.com/MiniguyBrendan/HAP-NodeJS.git

cd HAP-NodeJS/accessories/

There are a few changes we'll have to make.

We'll need to generate a random username (in the form of a MAC address) for each device. We can do that here.

Make sure to do the following on that site:

Set MAC address format to the one with Colons (00:10:FA:6E:38:4A)

Set Case to Uppercase

Let's edit the default Outlet_accessory.js for Outlet #1 so we can add our username:

sudo nano Outlet_accessory.js

Find the line that says outlet.username = "1A:2B:3C:4D:5E:FF";

Change the username value to whatever the website generated. For example, I might change it to read:

outlet.username = "FC:80:71:21:8A:4C";

...if that was what the generator gave me.

Also, on the lines with PythonShell.run('light1.py', function (err) { and PythonShell.run('light0.py', function (err) {

...pay close attention to the file names "light1.py" and "light0.py." You may replace "light1" or "light0" with "fan1" or "fan0", if, for example, your dumb device is a fan. If you choose to replace the default value, don't forget what you change it to!

Step 12: HomeKit/Siri Integration - Python

Now, we'll need a Python script that runs our .sh script. "Eww, that's inefficient," you're probably thinking. Yes, you're correct. Running .sh files with Node has been spotty on my Raspberry Pi, so YMMV. Because of that, we will instead be running the .sh file through a python script.

If you're still in the "accessories" folder, go back to the root of the HAP-NodeJS folder:

cd ..

Next, create a python file to call our "Switch1_On.sh" script.

sudo nano light1.py

And write the following:

import subprocess

subprocess.Popen(['sh', '[yourdirectory]/Switch1_On.sh'])

...where [yourdirectory] is the directory where you saved your .sh file.

Save the file by typing CTRL + X and Y for "yes, save this please!"

Once again, we can duplicate this file for the Off script:

sudo cp light1.py light0.py

Then edit it...

sudo nano light0.py

...and change "Switch1_On.sh"to "Switch1_Off.sh"

Save it with CTRL + X, then Yfor "yes, I really want to save. thanks."

Step 13: HomeKit/Siri Integration - Npm

To actually run the HomeKit server, we'll need to rebuild the Node project. It will take some time to complete, try to be patient! Run:

npm rebuild

And now, finally, we can try running the server itself!

sudo node BridgedCore.js

If you've set everything up correctly, it should start up the HomeKit server. We're ready to hook up our iPhone!

Step 14: HomeKit/Siri Integration - Finishing Up

We're in the home stretch! The last step is to connect your iPhone to your HomeKit server, which will allow you to control your devices with Siri.

My personal favorite of the HomeKit apps that can be used to set up a new device: Eve elgato. You can download it on your iPhone or iPad here.

Make sure to allow it access to your Home Data, and then let it search for devices. It should discover the device(s) you set up earlier. Add your device. Follow the onscreen instructions. If you want to change the name of the device, do that now.

When you finish, you should be able to control your device by the name you set.

Try it! Activate Siri, and say, "Turn on [device name]"

... where [device name] is the name you gave it.

Does it turn on? Yes? Congratulations, you've just created a smart home!