bookmark_borderSpelunking into actual IoT Development – Part 1 – The Lamp in the Corner

So you may have read my previous post about my adventures in controlling my ceiling fan with my phone, computer, watch, etc using HomeKit. I decided my second project would a simple lamp.

For my controller, I chose the ESP-32 originally. It’s cheap, has wifi, and has a good reputation. All of the reliable libraries that implement the HomeKit protocol are proprietary and expensive. I’m on a student budget, so that’s not an option. However, there is a pretty good library built by maximkulkin hosted on Github.

Now, even with that good reputation, I had some difficulties with the ESP-32 that I’ll get to later. Because I am going to be putting this in a lamp which gets its power from mains, I needed a way to get a 5V power supply. So, I took apart one of those USB wall adapters and pulled out the circuitry so that I could use it as my power source. Then, I spliced the wires of the lamp on the inside, and connected them to that circuit. Then I got a relay, and wired the live wire from the wall into the common terminal of the relay and the live wire going to the lamp to the normally open terminal. Then, I wired the 5V from the power supply to the ESP-32’s voltage input GPIO pin and to the VCC pin of the relay.

I found the esp-homekit-demo repository on GitHub from which I modified the LED example to work as a lamp, and U connected that GPIO pin to the input on the relay.

Et voila! I had a lamp controllable from my phone!
Wait. It stopped working exactly two minutes later? Okay, okay.
It turns out that the library I’m using has an issue on the ESP-32 where the MDNS broadcasts don’t work quite right. Since then, I’ve only been using ESP-8266s as they do not share this issue and are actually cheaper. The projects later in this series will all use the ESP-8266.
Thanks for reading!

bookmark_borderDIY Ceiling Fan IoT HomeKit Control

I had seen and sort of dismissed the whole smart home concept long ago because it’s all so expensive, but a few weeks ago, I had an idea. I would try to make my ceiling fan remotely controllable with HomeKit. (HomeKit because I primarily use Apple devices) If I was successful, I would give smart home stuff a shot. Spoiler: I was successful. Soon, I intend write about my adventures making other smart home stuff on the cheap like smart plugs.

How I did it

First, I wanted to get the hardware working without the fuss of working with Apple’s HomeKit protocol. With a multi-meter and testing using some wire I had on hand, I worked out which parts of the button pads I needed to pull low in order to emulate a button press.

Shows which contacts are current sinks or sources on the remote's PCB

Then, I soldered wires to each of the portions of the button pads that source current. (The current sinks are the ones that are connected to ground while the sources are the other ones.)

One thing I haven’t mentioned up to this point is that the remote runs on 12V while the Pi runs on 5V, so if I used the Pi to power the remote PCB, it would not have enough power to send a strong enough signal. I did find that it did sort of work, but wasn’t anything close to reliable, so I cobbled together a small battery holder for the button PCB, and put it’s original battery in it for power. However, in order for the pi to successfully communicate with the remote PCB, they have to share a ground potential. Basically, I had to connect their grounds together. Then I discovered that once connected to the Raspberry Pi, the remote would think that all of the buttons are being perpetually pressed even if the Pi was trying to source current (outputting a 1 on the GPIO pin). So, I soldered some 68k ohm resistors between the Pi and the remote, and…….. Success! I could send button presses to the remote by outputting 0s to the GPIO pin!

Now, I needed to figure a way to control the remote with HomeKit. I found a project online called homebridge. It allows you to write Javascript plugins to control non-homekit devices from HomeKit. I initially tried a couple of plugins designed to control the Pi’s GPIO, but I didn’t end up getting any to work. So, I wrote my own plugin which can be found on Github. I did have some challenges writing the plugin, however. You can order the to a specific state(off, low, medium, high), but the remote has only a single button for the light which makes it difficult to order a specific state. Despite that, after some experimentation, I found that if you hold down the light button for 5 seconds, you can guarantee that the light is on because the dimming function begins after 5 seconds. So, if the light is off, it will turn on on the rising-edge of the button press, and if the falling-edge resides 5 or more seconds later, the light will begin to change its brightness. If the light is already on, it won’t do anything on the rising-edge of the button press, and upon 5 seconds after the rising-edge, the light will begin to dim. You may expect that this would cause the brightness of the light to be erratic, but only after 5 seconds will the brightness begin to change slowly, so it should remain very close to constant, and this only happens when the controller is rebooted.

Future Improvements

I intend to move away from using a Raspberry Pi as an entire operating system is not necessary and quite inefficient to interact with HomeKit. Eventually, I think I’ll transition to using an ESP-8266 as they are super cheap and efficient.