Relay with HomeKit and NodeMCU

Adithya
2 min readJan 7, 2023

--

Initially I just wanted to setup a relay for a tubelight with my Raspberry Pi Pico W. Unfortunately, I couldn’t find any HomeKit libraries for it. However I found one for the ESP8266 which I happened to have lying around in the form of a NodeMCU.

HomeKit is a framework that let’s you control various devices like bulbs, garage doors, switches from their Apple devices using the Home app. It also allows you to sync various devices to create a scene and setup various automation with smart devices.

For those that don’t know, here goes an ELI5 of a relay: it’s used as a switch with DC control.
The one I used has 3 pins: Common, NO and NC. NO (Normally Open) and NC(Normally Closed) indicate the initial position of the relay.

I wanted to keep the existing wall switch fully functional, so I decided to keep the relay in series with the switch. As you can see from the image, I’m using the NC. This is so that when the relay is disconnected, the existing wall switch will still work as it used to. But this configuration also means that the on/off states on the microcontroller or the HomeKit end needs to be reversed which is still a WIP.

Setting up the HomeKit was simpler than I had expected. I connected the NodeMCU, fired up the Arduino IDE, configured the board and installed the library.

This library comes with a switch example that I ended up using. I only had to add my WiFi credentials and flash the NodeMCU and Bam! Just like that it showed up on the phone.

The on/off states are currently inverted, which I haven’t got around to fixing yet.
This is a whole new framework for me and it seems very interesting. Just a quick write up before I go learn more about it.

--

--