After making a lot of ESP8266 HomeKit samples, it might be time to explain how to add more than one accessory to HomeKit. Take the ESP8285 – Power Plug for example. Suppose I want to add more than one power plug, how do you do that? I will explain hot to do multiple HomeKit Accessory Pairing.

HomeKit Accessory Pairing

First we need to know how HomeKit Accessory Pairing is working. Once you have your iOS device and your HomeKit hardware, you’ll need to add it to your Home app. To do that, you’ll be asked to enter an eight-digit code or scan the HomeKit QR code that’s included with your device.

To be able to use a QR Code you nee to add some lines of code to your original code.So you can use a QR code to pair with the accessories. To enable that feature, you need to configure accessory to use static password and set some Setup ID:

homekit_server_config_t config = {
.accessories = accessories,
.password = "123-45-678",
.setupId="1QJ8",
};

The last piece of information you need is accessory category code. You can find it in mDNS announcement in accessory logs

mDNS announcement: Name=Sample LED-1692md=MyLEDpv=1.0id=16:92:CE:D4:EE:7Ac#=1s#=1ff=0sf=1ci=5 Port=5556 TTL=4500

Notice “ci=5” this is accessory category code. Here below you find a table with all category type numbers.

CategoryType number
HomeKit Accessory other 1
HomeKit Accessory bridge 2
HomeKit Accessory fan 3
HomeKit Accessory garage 4
HomeKit Accessory lightbulb 5
HomeKit Accessory door lock 6
HomeKit Accessory outlet 7
HomeKit Accessory switch 8
HomeKit Accessory thermostat 9
HomeKit Accessory sensor 10
HomeKit Accessory security system 11
HomeKit Accessory door 12
HomeKit Accessory window 13
HomeKit Accessory window covering 14
HomeKit Accessory programmable switch 15
HomeKit Accessory range extender 16
HomeKit Accessory ip camera 17
HomeKit Accessory video door bell 18
HomeKit Accessory air purifier 19
HomeKit Accessory heater 20
HomeKit Accessory air conditioner 21
HomeKit Accessory humidifier 22
HomeKit Accessory dehumidifier 23
HomeKit Accessory apple tv 24
HomeKit Accessory speaker 26
HomeKit Accessory airport 27
HomeKit Accessory sprinkler 28
HomeKit Accessory faucet 29
HomeKit Accessory shower head 30
HomeKit Accessory television 31
HomeKit Accessory target controller 32

Then you need to generate QR code using the information you gathered above, more information about generating QR code read: ESP HomeKit SDK – QR Code

tools/gen_qrcode 5 443-22-549 1QJ8 qrcode.png
QR code example

Multiple HomeKit Accessory Pairing

Now that you have added your HomeKit Accessory for example a Power Plug or a Lamp. We want to add another one. Then it will be a problem. HomeKit has made a connection to this device as will trow an error saying this device is already added. Firts you will think you need to make a new and unique QR code. Actually this is not necessary. HomeKit registers the device on mDNS level. This means that in this case it looks at the name value.

mDNS announcement: Name=Sample LED-1692md=MyLEDpv=1.0id=16:92:CE:D4:EE:7Ac#=1s#=1ff=0sf=1ci=5 Port=5556 TTL=4500

So how can we solve this problem, we need a unique identifier.

Chip ID

By adding a unique identification, we can solve this problem. A solution would be to add the mac address of the ESP module. In this case, I choose the Chip ID of the processor. You can get the ESP Chip ID by calling ‘sdk_system_get_chip_id()‘. 

Add this code to generate a unique mDSN name.

void create_accessory_name() {
        int serialLength = snprintf(NULL, 0, "%d", sdk_system_get_chip_id());
        char *serialNumberValue = malloc(serialLength + 1);
        snprintf(serialNumberValue, serialLength + 1, "%d", sdk_system_get_chip_id());
        int name_len = snprintf(NULL, 0, "%s-%s", DEVICE_NAME, serialNumberValue);
        if (name_len > 63) {
                name_len = 63;
        }
        char *name_value = malloc(name_len + 1);
        snprintf(name_value, name_len + 1, "%s-%s", DEVICE_NAME, serialNumberValue);
        name.value = HOMEKIT_STRING(name_value);
}

After the ‘void user_init(void)‘ add the line ‘create_accessory_name();

void user_init(void) {
        uart_set_baud(0, 115200);
        create_accessory_name();

Problem Solved!


Note: To produce and sell HomeKit compatible accessories, your company need to be certified for that (https://developer.apple.com/homekit/, If you’re interested in developing or manufacturing a HomeKit accessory that will be distributed or sold, your company must enroll in the MFi Program.) Espressif have their implementation of HomeKit framework, but it will give you it only if you have MFi certification (notice this text at the bottom of page you mentioned: Please note that the Espressif HomeKit SDK is available to MFi licensees only, and you need to provide the Account Number for verification purposes when requesting the SDK.).This project is a non-commercial implementation of HAP protocol, not meant for commercial use.


REFERENCE

Maxim Kulkin,  esp-homekit (2019), Apple HomeKit accessory server library for ESP-OPEN-RTOS , https://github.com/maximkulkin/esp-homekit Maxim Kulkin,  esp-homekit-demo (2019), Demo of Apple HomeKit accessory server library , https://github.com/maximkulkin/esp-homekit-demo

Scroll to Top