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.
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
#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="";
}
}
}
}
}
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!
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?
I suppose that in order to add more devices you should add them to the array in the raspberry pi?
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
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
You would need to have a switch that is contactable via the network to poll its current status, e.g I am on I am Off.