Skip to content
My Account

HomeKit

ESP8266 HomeKit Dual Lamps

Two lamps in one light fitting as one HomeKit accessory: a top light and a bottom light, two relays and a button that steps through the combinations. A test setup on an ESP8266.

  • ESP8266
  • HomeKit
  • Safety notes
  • 4 min read
No ratings yet, be the first.

Two lamps in one light fitting, controlled as one HomeKit accessory. The top lamp and the bottom lamp show up as two separate lights in the Home app, so you can switch each one on its own, and a button on the device steps through the combinations.

This is a test setup. The build uses two relay modules and no mains voltage, and was made to check the code and the hardware.

What it does

FeatureDetails
One accessory, two lightsA single “Dual Lamp” accessory with a Top Light and a Bottom Light in the Home app
Two relaysRelay 0 switches lamp 1 (top), relay 1 switches lamp 2 (bottom)
ButtonSteps through the four combinations of the two lamps
IdentifyThe top lamp stays on and the bottom lamp flashes, so you can find the accessory
UpdatesOver the air, through Life Cycle Manager 2
No cloudControl stays on your own network

The accessory name gets the last three bytes of the MAC address added, for example Dual Lamp-A1B2C3, so several of them can live in one home.

Hardware

ComponentNotes
ESP8266 moduleAn Ai-Thinker ESP8266 module of the ESP-12 type. See the ESP8266 pinout
2 relay modulesKeyes SRD-05VDC-SL-C, one per lamp, each with NC, C and NO terminals
3 push buttonsBUTTON, RESET and PROGRAM
USB-serial (FTDI) adapterFor flashing and for power in this test setup. Set it to 3.3 V logic
470 µF capacitorAcross the supply, needed for stable operation when powering from the FTDI adapter
100 nF ceramic capacitorAt the module, as drawn
ResistorsPull-ups, as drawn

In this test setup the module is powered from the FTDI adapter, which is not recommended for long-term use. The lamps are the two lamps of one light fitting, lamp 1 on top and lamp 2 below. Only the relay modules are connected in the test build, with no mains voltage.

Wiring and pins

The Fritzing diagram at the top of this page shows the build on a breadboard, with the pinout of the module in the corner. The pins the firmware uses are set at the top of main.c:

FunctionESP8266 pin
Relay 0, lamp 1 (top)GPIO12
Relay 1, lamp 2 (bottom)GPIO5
ButtonGPIO4, active low
LED (defined in the firmware, not drawn)GPIO13

The relays are switched active-high. Use an FTDI adapter set to 3.3 V, because a 5 V adapter can damage the module. The Fritzing source of the diagram is in the repository as Resources/dual_lights.fzz.

Mains voltage

The test build has no mains connected. If you use the relays to switch lamps that run on mains voltage, disconnect the power first and only work on mains wiring if you know how.

How the button and the lamps behave

Button pressTop lampBottom lamp
At start-upOnOn
1st pressOffOff
2nd pressOnOff
3rd pressOffOn
4th pressOnOn

After that the sequence repeats. Switching a lamp from the Home app updates the state, and the button then continues from there. When the state changes from the button, HomeKit is notified, so the Home app always shows what the lamps are doing.

Flash and install

The installation is the same as for the other ESP8266 HomeKit accessories on this site. The firmware runs on ESP-Open-RTOS with Life Cycle Manager 2, which sets up the WiFi and installs updates over the air. The full steps, from installing esptool to flashing, joining the LCM- WiFi network and pairing, are in ESP8266 HomeKit Blinds.

In the portal, use these values:

FieldValue
OTA repositoryAchimPieters/ESP8266-HomeKit-Dual-Lamps
OTA binary filemain.bin

When the installation is done, scan the QR code the device generates in the Home app to add it.

Build from source

This repository includes the firmware source in the Dual_lamp folder: main.c, button.c and toggle.c. It builds with ESP-Open-RTOS. The Makefile uses the http-parser and dhcpserver extras, plus the wifi_config, cJSON, wolfssl and homekit components, with FLASH_SIZE set to 8, the dout flash mode at 40 MHz, and the HomeKit data stored at flash address 0x7A000.

The HomeKit setup code is set in main.c. Change it to your own before you use the device for real.

Firmware release

VersionDateNotes
0.0.125 October 2019Initial release

The release has a main.bin and a signature file, main.bin.sig.

Good to know

  • This is a test setup and not a finished product.
  • The HomeKit Accessory Protocol specification the firmware follows is the non-commercial version, for accessories you build yourself and do not distribute or sell.
  • The repository is under the MIT licence.

Everything is in the ESP8266-HomeKit-Dual-Lamps repository on GitHub.

Advertisement
All HomeKit

More HomeKit

Keep building.

Switches, sensors, plugs and blinds: more accessories that you build yourself and add to the Home app.

All HomeKit