I have always wanted to get involved in a hardware project. And I love home automation. So automating my home fan seems to be the perfect project to start with.
I use HomeKit for my door locks and lights. When I leave, HomeKit will lock my door and turn off all lights. But I always have to search for my fan remote to turn it off manually. It is not how I imagine my smart home should be.
Having had no experience with hardware, starting on my first project was quite intimidating. I did some research on what I needed, then purchased a Raspberry Pi starter kit and a pair of 433Mhz receiver/transmitter.
Setting up Raspberry Pi 3 was a lot easier than I thought. Canakit Ultimate Starter Kit comes with the OS preloaded in a microSD card. It also comes with jumper wires, a breadboard, a GPIO to breadboard interface board, LEDs and resistors.
The next thing I did, I went through an LED tutorial, and figured out how to use the breadboard. I was not sure if I would fry my Raspberry Pi if it was not connected properly, so I turned off the device during setup, turned it on when I was done. When connected wrongly, the device simply did not boot. I was lucky, the LED test went fine. That is equivalent to the ‘Hello World’ of coding.
I suspected that my fan remote uses RF because it can work from a different room, but I was not sure that a 433Mhz receiver/transmitter was the right hardware to buy. It was a guess, but it worked out for me.
Left: 433Mhz RF receiver. Right: 433Mhz RF transmitter.
My first task was to read signals with the RF receiver. I followed this guide closely—installed wiringPi and 433Utils, connected the data-pin of the RF receiver to GPIO27, GND to GND, VCC to 5V, run RFSniffer and pressed my remote. It worked!
Next, I connected the 433Mhz RF transmitter following the instructions here. I was able to test that the transmitter was working fine using codesend to transmit a number, and received by the RF receiver running RFSniffer.
Everything went very smoothly until this point. However, the signal received from my remote using RFSniffer were not able to control my fan when re-transmitted with codesend. There must be something I misunderstood about how RF remote works. Looks like the hardware was the easy part.
Then I came across this post that uses pilight.
RF Transmitter connected to pin 17, and the RF receiver connected to pin 27
After installing pilight, I configured it to work with my setup. The photo above shows that RF transmitter was connected to pin 17 and RF receiver to pin 27.
GPIO numbering table from http://wiringx.org
The sender and receiver value in the config.json file can be determined by referencing the GPIO table above. Sender value at pin 17 should be 0, and receiver value at pin 27 should be 2.
pilight configuration file: sender=0 and receiver=2, by referencing the GPIO table above
Then I used pilight-debug to read and output raw information from my remote. Remember to kill pilight-daemon before running pilight-debug.
pi@raspberrypi:~/ $ sudo killall pilight-daemon
pi@raspberrypi:~/ $ pilight-debug
Run pilight-debug and press the buttons in the remote to capture the signals.
The raw code from pilight-debug is a series of numbers, with each single number measuring the time difference between a HIGH-LOW or LOW-HIGH transition. There were a lot of other noise signals, so I had to filter manually for the correct signal that originated from my remote. It seems the trick is to look out for series of numbers that only contain three unique numbers. These ‘cleaner’ numbers are only ones that worked for me. I had to keep pressing my remote until I get a code that looks ‘clean’ like this.
209 627 627 209 209 627 209 627 209 627 209 627 209 627 627 209 209 627 209 627 209 627 209 627 209 627 209 627 209 627 209 627 627 209 627 209 209 627 209 627 209 627 209 627 209 627 209 627 209 7106
Then I used pilight-send to send the raw code (If you have previously killed pilight-daemon, remember to start it again sudo pilight-daemon
). Viola! It worked. I repeated that for all buttons on the remote to get the different code for turning off, and changing speed. The signals are different for different remotes as well, so I have to repeat the same steps for the fan in my bedroom even though it is the same model.
pi@raspberrypi:~/ $ sudo pilight-daemon
pi@raspberrypi:~/ $ pilight-send -p raw -c “209 627 627 209 209 627 209 627 209 627 209 627 209 627 627 209 209 627 209 627 209 627 209 627 209 627 209 627 209 627 209 627 627 209 627 209 209 627 209 627 209 627 209 627 209 627 209 627 209 7106”
Now that I have managed to control my fan with command line, the next step is to get it working with HomeKit. HomeBridge is an open-sourced NodeJS server to emulate iOS HomeKit API. You can find the instruction to install HomeBridge in Raspberry Pi here.
nfarina/homebridge_homebridge - HomeKit support for the impatient_github.com
pilight has a HomeBridge plugin. However, I had to create a custom plugin instead because I could not figure out how to set up pilight to do this. I found an open-source HomeBridge plugin to control a 3-speed TOSR0x fan, homebridge-tosr0x-fan via API calls. I used that as a template for my plugin.
I created a REST API using Python/Flask that executes pilight-send with command line.
honcheng/rfremote-fan-api_rfremote-fan-api - API to control RF fan using pilight in Raspberry Pi_github.com
Then I modified the plugin to call the REST API, and removed temperature sensor, which is not supported by my fan.
honcheng/homebridge-rfremote-fan_homebridge-rfremote-fan - HomeBridge plugin to replace RF remote control for fans_github.com=
I used the command below to install the plugin, though it is slower compared to setting up a development folder.
pi@raspberrypi:~/ $ npm install -g homebridge
After installing the plugin, I added my fans to HomeBridge by editing the configuration file in ~/.homebridge/config.json. The key ‘accessory’ has to be identical to the name declared in the plugin.
Restart HomeBridge for changes to take effect.
pi@raspberrypi:~/ $ sudo killall homebridge
pi@raspberrypi:~/ $ homebridge
Fan controls in iOS Control Center
Changing fan speed in HomeKit
Not bad for my first hardware hack, done in a weekend.
If you are trying to replicate the same setup for your RF remote controlled fan, you will have to identify the signals again because it will be different from mine. Good luck!