Well after a long time I’ve picked up where I left my homebridge project. Its time to go to the next level; accessories!

The first logical step is in my opinion switching a light ( or relay )  on and off though the homebridge with Siri.

large

First, you must install HomeBridge on a Raspberry Pi, and configure it as a HomeKit bridge device. For who did’t read my previous  post about the Raspian Pi update, there are a few thing changed! first read this post https://www.studiopieters.nl/raspberry-pi-a-security-update-for-raspbian-pixel/

So once again we have to alter a few things in the installation process. For a headless installation we need to add the ssh file to the root of the SD-Card. This in order to connect to the Raspberry Pi. When we have don this we can install Xrdp as described in this post https://www.studiopieters.nl/raspberry-pi-remote-desktop-connection/

But then I stumbled on to a new problem. It has to do with the fact that the Raspbian Jessie with pixel, which officially stands for “Pi Improved Xwindows Environment, Lightweight”, now comes pre-packaged with RealVNC, which conflicts with both xrdp (which uses vnc4server) and tightvncserver.

So I needed a solution: Google me this google me that….

The fix:

Let’s assume you already did sudo apt-get update and sudo apt-get install xrdp.

Now, let’s disable the RealVNC install. I have tested it and followed these steps to disable the RealVNC install and allow xrdp to work:

sudo apt-get remove xrdp vnc4server tightvncserver

sudo apt-get install xrdp

Remote logins using xrdp now work. Here after you can make a remote connection and follow the steps as described in this post https://www.studiopieters.nl/raspberry-pi-apple-homebridge/

When you have installed the homebridge you need to install the homebridge-http package in order to let the homebride communicate with your ESP8266 over http.

Edit the HomeBridge /var/homebridge/config.json file on the Raspberry Pi HomeBridge. the file may alternatively be in /home/.homebridge or /root/home/./homebridge.

add this code to the config.json file.

sudo nano ~/.homebridge/config.json

mind the last comma, you may or may not need it if you have other accessories, or Homebridge is crashing on load.




{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:22:3D:E3:CE:30",
        "port": 51826,
        "pin": "031-45-154"
    },

    "description": "This is an example configuration file with all supported devices. You can use this as a template for creating your own configuration file containing devices you actually own.",

    "platforms": [
        
    ],

    "accessories": [
        {    "accessory": "Http",
            "name": "PowerTail",
            "on_url": "http://192.168.1.110/gpio/1",
            "off_url": "http://192.168.1.110/gpio/0",
            "http_method": "GET"
        },
       
    ]
}

The Hardware

Once you have HomeKit / HomeBridge working on your Pi and your iPhone, we can build a wireless power switch that can be controlled by Siri and the HomeKit app.

At first we need to setup the FTDI programmer to program the ESP8266 with the Arduino IDE.

lamp001_bb

upload the program below to your ESP8266.

/*
 *  Project name: Apple Homebridge - Relays & Lights
 *  Project URI:  https://www.studiopieters.nl/apple-homebridge-relays-lights/ 
 *  Description: Apple Homebridge - Relays & Lights
 *  Version: Apple homebridge - Relays & Lights 3.2.3
 *  License: GNU General Public License V2 or later
 */

#include <ESP8266WiFi.h>

const char* ssid = "your-ssid";
const char* password = "your-password";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(2, val);
  
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}
Now we can modify our setup so we can see if everything is working. In the fritting digram below you can see that now I have added a LED and keeping the RX and TX connected so we can see in the monitor from the Arduino IDE if everything is working as we want to.

lamp002_bb

Now we have to connect our iPhone to the home bridge and add the new accesoires.

Hooray!it works!
200

Now you can remove the FTDI programmer and you have a standalone Apple homekit accesoirs.

lamp003_bb

To switch a Light you need to replace the LED with a Relay module.

lamp004_bb

ios-badge-works-with-apple-homekit-750x400

Download all Files for this project on github

Do you have any questions? Leave a comment down below!

Pin on Pinterest1Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Buffer this pageDigg thisEmail this to someone
Flattr the authorShare on Reddit0Share on StumbleUpon0Share on Tumblr0Share on VKShare on Yummly0
  • John_Maree

    Hi there,

    How would you assign two accessories to one esp module in this code. ie, having who gpio HTTP get requests being monitored?

  • @ John_maree
    That’s possible then you have to identify two gpio’s and recode the whole arduino code, so it knows when to activate one or the other.

  • John_Maree

    Thank you for your quick reply.

    I tried changing the code and basically just adding in another set of the “req.indexOf” code and telling it to listen for another gpio, however the code loads with no errors but only listens to one and says invalid requests to another… I fear I don’t know enough of the homebridge coding to understand how to do this. If you have done this or know what I should add, assistance would be greatly appreciated. I have an ESP-13, so IO is not an issue.

    Regards,

    John

  • @ John_maree
    In that case you need to recode de plugin, you can find it here:
    https://www.npmjs.com/package/homebridge-http

    Regarding homebridge coding, this is for most people a issue, there are different coding used to setup plugins and harde ware code in c+.
    For each plugin and hardware I make i need to open all registers to get my head around it. If you really serious about making a custom peace of hardware with a plugin for homebridge, I suggest to start small and experiment. Step by step you will see the big picture, as i did.
    Please share your knowledge here so others can learn from it. Good luck!