Arduino HomeKit integration – How to set it up

So today I was looking at trying to get an iOS device to talk with an arduino to control some home appliances using HomeKit. I already had the majority of things that were needed, mainly a solid state relay and ethernet shield.

It turns out there’s a great NodeJS server called HomeBridge to get things started. This will allow native support for Apples HomeKit. I installed this on RaspberryPi on the latest release of Raspbian. This was relatively pain free. Follow this guide here to get going:

Once this running you need to install a plugin to support HTTP based devices (this will be how we interface with the arduino:

npm install homebridge-http
Now for the code on the arduino, this is really basic and full of security related issues but I found some useful and simple HTTP relay control code online to get started. A basic GET request to the arduino’s IP with either /?on or /?off will control the pin. You can test it with wget before going any further:
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 201 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  //enable serial data print 
  Serial.begin(9600); 
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == 'n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Simple Arduino button</H1>");
          
          client.println("<a href="/?on">ON</a>"); 
          client.println("<a href="/?off">OFF</a>"); 

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on") != -1)//checks for on
          {
            Serial.println(readString.indexOf("on"));
            
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");

          }
          if(readString.indexOf("off") != 1)//checks for off
          {
            Serial.println(readString.indexOf("off"));
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
} 
Load this up onto your board and then edit your config.json file with the following:
{
 
“bridge”: {
 
“name”: “Homebridge”,
 
“username”: “CC:22:3D:E3:CE:30”,
 
“port”: 51826,
 
“pin”: “031-45-154”
 
},
 
 
“platforms”: [
 
 
],
 
 
“accessories”: [
 
{
 
“accessory”: “Http”,
 
“name”: “Living Room Lamp”,
 
“on_url”: “http://192.168.1.201:/?on”,
 
“off_url”: “http://192.168.1.201:80/?off”,
 
“http_method”: “GET”
 
}
 
 
]
 
}
 

You can now start homebridge, type homebridgeThe final step is to get an App to control all of this. I didn’t realise such an App isn’t included. The one I opted for was Elgato Eve – its free!

Add the Homebride using the pin 031-45-154 and you should be able to start controlling the relay!

The plan is to shrink this down and use something like a teensy board with WiFI to embed this into a light switch. Also need to look at power options that will fit behind a light switch!

Also need to look at a more reliable / secure method of communicating!

5 comments
  1. Hello … I build the circuit and test.
    I've found follow troubles:
    1- led is random high or low or high for a while and soon down (flash).
    I solved adding in void loop a digitalwrite low.
    After I swapped the set of commands about ON and OFF.
    The problem seem finished.
    Form web page all is working fine.
    2-homekit integration not seem working.
    clearly added plugin homebridge-http and config.
    Siri send command (light on) homebridge receive it, but nothing happen on Arduino.

    finally. How I can add more than 1 relays?

  2. Can someone contact me on Facebook messenger if they feel like helping me out. I’m new to this and am wondering how many relays can the home bridge server and a pi support. If fewer than 5 is there something that supports more? I want to be able to control a relay to open/close my garage door, a relay to trigger the garage door controlled lights, a relay to control the primary lights in the garage, a relay for the exterior lights and finally a relay to the flood light outside the garage.

    Again I’m new to home kit and pi but have experience with home automation.

    Facebook messenger: m.me/murphwi

  3. Hello, the tutorial is very helpfull and easy to understand. But I have a problem and can’t find a solution.
    I have a Lamp. I want to switch it on and off with homekit(is working) and with a hardware switch(like normal house installation) both are working. BUT homekit doesn’t get the information if I turn off the light with the button(still shows on).
    Is there any posibillity that homebridge ask for the statues of the lamp? Thank you for helping

Leave a Reply

Your email address will not be published. Required fields are marked *