Native Apple HomeKit accessory implementation for the ESP8266 Arduino core.
中文说明 | Português Brasileiro | Русский
This Arduino library is a native Apple HomeKit accessory implementation for the ESP8266 Arduino core, and works without any additional bridges.
This project is mainly based on esp-homekit for ESP-OPEN-RTOS.
I ported the RTOS-based implementation of esp-homekit to the pure Arduino environment, aimed at easy and fast building project using Arduino IDE (or Eclipse with sloeber, PlatformIO).
Enjoy the “one-key” build, “one-key” upload, and work to link various other Arduino libraries with Apple HomeKit!
Here is a discussion about the RTOS is required for running Apple HomeKit, and this project is a proof of concept that Apple HomeKit can be implemented and work fine without the RTOS.
This library is built with ESP8266 Arduino Core 2.6.3. Lower versions may compile with errors.
For ESP32, see Arduino-HomeKit-ESP32. The HomeKit running on ESP32 has a GREAT PERFORMANCE which is 10x faster than ESP8266.
111-11-111
homekit_accessory_t *accessories[] = ...
homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111",
//.on_event = on_homekit_event, //optional
//.setupId = "ABCD" //optional
};
In your sketch
#include <arduino_homekit_server.h>;
//access the config defined in C code
extern "C" homekit_server_config_t config;
void setup() {
WiFi.begin(ssid, password);
arduino_homekit_setup(&config);
}
void loop() {
arduino_homekit_loop();
}
Done.
Notice: You should set the ESP8266 CPU to run at 160MHz (at least during the pairing process), to avoid the tcp-socket disconnection from iOS device caused by timeout.
All pairing process takes ~14s after you input the setup-code on your iPhone. Notice that Preinit require ~9s before you can start to pair.
The heap is critical for ESP8266 with full TCP/IP support. ESP8266 easily crashes when the memory is lower than ~5000.
I tried to make WolfSSL crypto work safely on ESP8266 with better performance and lower memory or a trade-off. See details in next section.
Here are the free heap values of running the example sketch:
After memory optimization in v1.1.0:
CURVE25519_SMALL
and ED25519_SMALL
: ESP8266 can not directly run without SMALL
defined since the memory is not sufficient. But the NO SMALL
version is faster. I mark the big ge_precomp base[32][8]
with PROGMEM to store it in Flash (around 70KB). Also the ge_double_scalarmult_vartime
can not run caused by lack of heap. I define ESP_GE_DOUBLE_SCALARMULT_VARTIME_LOWMEM
in user_settings.h
to use LOWMEM version of ge_double_scalarmult_vartime
in ge_low_mem.c
. This is a trade-off of performance and memory. If you want more Flash space, you should define CURVE25519_SMALL
and ED25519_SMALL
and undefine ESP_GE_DOUBLE_SCALARMULT_VARTIME_LOWMEM
in user_settings.h
(this will lead the Pair Verify Steps to take 1.2s + 0.9s).integer.c
(big integer operations): MP_16BIT
and ESP_FORCE_S_MP_EXPTMOD
are defined for better performance in ESP8266. ESP_INTEGER_WINSIZE
(value is 3) is defined to avoid crash caused by memory exhaust and the values of {3, 4, 5} are of similar performance.EEPROM
address in ESP8266 Arduino core.EEPROM
library with data-cache to reduce memory use (directly call flash_read and write). EEPROM
is 4096B in ESP8266, this project uses max [0, 1408B).storge.c
and ESP8266-EEPROM-doc.EEPROM
of [1408, 4096) is safe for you to use. FS(file system)
, so you can use FS
freely.Preinit
and Pair Setup Step 2/3
.WolfSSL
section if you want a smaller sketch) All Flash Contents
when you first uploadESP8266WiFi
(WiFiServer and WiFiClient) is used for tcp connection.ESP8266mDNS
is used for advertising (Bonjour) yield()
while crypto computing, to prevent WiFi disconnection. The idea is from BbIKTOP-issues80RODATA
section of bin
is only 4672. Extra ~20K free-heap is available compared with v1.0.1.nossl
and noleak
version of the official ESP8266WiFi
library of Arduino Core 2.6.3. Removed all codes of SSL
to save memory (extra ~3K) since the HomeKit does not require SSL. Fix the memory-leak in WiFiClinet.stop()
by adding tcp_abandon(_pcb, 0)
in stop()
, based on the idea of esp8266/Arduino/pull/2767.winsize
from 3
to 2
(same performance) to lower the heap required. Pairing can be done with low free-heap of ~14000.HTTP_METHOD
(s) in http_parser.h
to avoid multi-definition errors when using ESP8266WebServer
together.