Wuming 無名

Blog

From One Tiny Board to a Center and IR Launchers: Fixing My HomeKit Air Conditioner's Response Time

· Technology · English

中文版:空调接入 HomeKit:从一块小板到“中心 + 红外发射器”

My air conditioners are the old kind. No Wi-Fi, no app, just an infrared remote that goes missing in the sofa. For a while I had them in Apple Home through one tiny board per unit. It worked, mostly. Then I spent a summer tapping a tile in the Home app and watching it say “Updating…”.

This post covers what I used before, why it was slow, and the setup that replaced it: one center that speaks HomeKit, and small IR launchers that only fire infrared.

What I used before: one board doing everything

The first version was as small as it gets. An ESP-01M, an ESP8266 module about the size of a thumbnail, sold with an IR LED and an IR receiver already soldered on. One board sat near each AC and did the whole job:

  • ran a HomeKit accessory with the arduino-homekit-esp8266 library, so the AC showed up as a thermostat in the Home app;
  • turned the HomeKit state (mode, target temperature, fan) into the AC’s IR protocol (Kelvinator, Gree or Coolix, through IRremoteESP8266) and sent it;
  • kept the IR receiver listening, so when someone used the physical remote, the board decoded the frame and updated HomeKit to match.

On paper that’s a complete product for a few dollars. Wi-Fi credentials were hard-coded, because with 1 MB of flash there was no room for a Wi-Fi manager, a web page or mDNS. Flash usage sat around 800–900 KB, and free RAM was about 30 KB.

The trouble: response time

The trouble wasn’t that it never worked. It was that it worked slowly and unpredictably, and you can’t build trust in a light switch, or an AC, that only works most of the time.

What I kept seeing:

  • Tap, then wait. Turning on the AC from the Home app often took several seconds before the beep. Sometimes the tile spun on “Updating…” and then gave up with “No Response”, and the AC turned on a moment later anyway.
  • Siri said it failed when it hadn’t. “Set the bedroom to 26” would get “the device isn’t responding” while the AC happily beeped.
  • Re-pairing was a gamble. After a reset, adding the accessory back could take more than ten seconds and often timed out halfway.
  • The remote and the Home app drifted apart. If the board was busy when someone pressed the physical remote, it missed the frame, and Home showed the old state.

Digging into it, the slowness wasn’t one bug. It was the board.

HomeKit is cryptography first. Every time an iPhone or a home hub (HomePod, Apple TV) opens a session to an accessory, the two run a pair-verify handshake: Curve25519 key exchange, Ed25519 signatures, ChaCha20-Poly1305 for every message after that. An ESP8266 at 160 MHz has no crypto hardware, so all of this runs in software. Reports for this library put one Ed25519 operation at roughly 200–300 ms on an ESP8266, and a full pairing at 12 seconds or more. An ESP32 does the same signature in tens of milliseconds.

One core, everything waiting in line. The ESP8266 has a single core, and the Wi-Fi stack runs on it too. While the crypto is crunching, the radio is starved, the IR receiver isn’t being serviced, and an incoming HomeKit request just waits. Home hubs reconnect fairly often, so a “slow handshake” wasn’t a rare event. It sat right in front of many of my taps.

Not enough memory. Wi-Fi and TCP take about 25 KB, the crypto library about 20 KB, and what’s left is thin. Under load the board sometimes rebooted mid-handshake, which looked from the outside exactly like “No Response”.

The library stood still while iOS moved. arduino-homekit-esp8266 had its last release in 2021. Apple kept changing Home underneath, including the new Home architecture where the home hub sits in the middle of everything. Nobody was going to tune an abandoned library for that.

I tried the usual tricks: lower the IR receiver’s buffer, skip unnecessary work in the loop, pin the Wi-Fi channel. Each helped a little. None changed the fact that a single-core ESP8266 doing HomeKit crypto is slow by design. I also looked at porting a modern HomeKit library such as HomeSpan to the ESP8266. The memory math rules it out: HomeSpan wants something like 60 KB free during pairing, and the ESP8266 has about half of that.

The upgrade: a center and IR launchers

The fix was to stop asking one small board to do two very different jobs.

iPhone / Siri / Home hub
        │  HomeKit (encrypted, needs CPU)
        ▼
   ESP32 center  (HomeSpan)
        │  plain HTTP on the LAN, UDP for discovery
        ▼
 IR launcher(s)  (ESP-01M / ESP-12F, no HomeKit)
        │  infrared
        ▼
   air conditioner

The center is an ESP32-S3 running HomeSpan, a HomeKit library that is actively maintained. The S3 has two cores at 240 MHz, hardware crypto acceleration and 512 KB of RAM. HomeKit is the only hard thing it does, and it does it comfortably. In the Home app, each AC is still a thermostat, so nothing changed for the family.

The IR launchers are the same cheap ESP8266 boards as before, now with the HomeKit code removed. Each one is a small HTTP service:

  • /set_ac takes power, mode, temperature and protocol and fires the IR frame;
  • /status returns what the launcher last sent or heard;
  • /learn and /get_learned capture a raw IR code from any remote, for buttons the AC protocol doesn’t cover;
  • /identify blinks the board so you can tell which one is which.

With HomeKit gone, a launcher has plenty of headroom, and it can do the one thing it is good at without being interrupted.

Why it’s fast now

The HomeKit side never waits on the network. When the Home app changes a thermostat, HomeSpan calls the center’s update handler. That handler doesn’t call the launcher. It drops a command into a FreeRTOS queue and returns at once, so HomeKit gets its answer within milliseconds. A separate task on the center drains the queue and makes the HTTP call to the launcher. If a launcher is slow or offline, HomeKit doesn’t notice. No more “No Response” because a downstream board was busy.

The handshakes are cheap. Pair-verify on the ESP32 takes a fraction of the time it did on the ESP8266, so the reconnects from the home hub stopped sitting between me and the AC.

Launchers find themselves. A launcher announces itself with a UDP broadcast on port 4210 (Gravity:DISCOVER). The center lists what it hears on its web page. To bind a launcher to an AC, pick it in a dropdown, pick the protocol, and save. No IP addresses in source code, and a launcher that changes its DHCP lease is found again.

The physical remote is covered too. The launcher still listens for IR. When it decodes a frame from the real remote, it broadcasts Gravity:STATE_CHANGE:<mac>, and the center polls that launcher’s /status straight away and updates HomeKit. As a safety net, the center also polls every launcher every 15 seconds. Home and the wall unit stay in sync even when someone ignores the phone completely.

Things that came for free

Once the center had memory and CPU to spare, a few features I had given up on came back:

  • Two ACs per center. The center has slots, each bound to a launcher and an IR protocol. Each slot appears in Home as its own thermostat.
  • A sleep timer. Each slot has a timer in Home that looks like a fan: each quarter of the slider is one hour, up to four, and the center sends an OFF to that AC when it runs out. Turning the AC off by hand cancels it.
  • Learned buttons. Point any remote at a launcher, press “Learn” on the center’s page, and that code becomes a button in Home. Good for swing, light, or whatever the protocol library doesn’t model.
  • A small screen. One build of the center drives an OLED, another runs on an ESP32-C6 board with a 1.47” LCD, showing each slot’s state at a glance.
  • OTA updates for both the center and the launchers, so I don’t have to climb up to the AC with a USB-serial adapter again.

What I’d tell myself a year ago

The ESP8266 is a fine little chip. It’s just the wrong place for HomeKit. Encryption-heavy protocols want a CPU with headroom. Infrared wants a cheap board in line of sight of the AC. Splitting them along that line made each half simple, and the system as a whole fast.

The cost of the upgrade was one ESP32 board, a few dollars. The launchers are the boards I already had, with less code on them than before. And the Home app does what it says when I tap it, which was the whole point.

Comments

  1. Loading comments…

Leave a comment

Plain text; links are fine. Comments with many links wait for review.