HomeKit w/ ESP8266

Demo

Lantern Demo from Sami Suteria on Vimeo.

Software Setup

On your home server (raspberry pi or similar) install and run HAP-NodeJS and a MQTT Broker like Mosca.

Install Mosca by keying npm install mosca bunyan -g then run it mosca -v | bunyan

Before running the HAP server - install the MQTT package npm install mqtt --save then install all the packages npm install

Create a new “accessory” by adding a file to the accessories folder - in my case I called it BedroomLantern_accessory.js. Mine is a copy of Light_accessory.js with the name changed and MQTT added. You need to add an MQTT connection at the top of the file and make a publish call in the setPowerOn function.

var mqtt = require('mqtt');
var options = {
  port: 1883,
  host: '192.168.1.142',
  cliendId: 'CapricaHome_BedroomLantern'
};
var client = mqtt.connect(options);
var BedroomLantern = {
  powerOn: false,

  setPowerOn: function(on) {
    var status = on ? "on" : "off"
    console.log("Lantern is " + status)
    client.publish('BedroomLantern', status)
  },
  identify: function() {
    console.log("Identify called")
  }
}

Make sure your file as the suffix _accessory.js otherwise HAP-NodeJS won’t recognize it. Now when you run node BridgedCore.js you should see:

HAP-NodeJS starting...
Parsing accessory: BedroomLantern_accessory.js

Hardware Setup

For hardware you can use whatever you want that can connect to your home network and has an MQTT library. I like to use the ESP8266 chip. You probably want to get a development board for it and I recommend the NodeMCU. You can get it off eBay/Amazon for pretty cheap ~$5 ebay.

For the ESP8266 you can program it using the ArduinoIDE.

If you’ve ever programmed using Arduino the programs (called Sketches) have a simple pattern of:

void setup() {

}

void loop() {

}

By using the libraries that come with the chip you can connect to WiFi really easily:

#include <ESP8266WiFi.h>

const char *ssid =  "Caprica";
const char *pass =  "**********";

WiFiClient espClient;

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
}

Then we want to connect to the MQTT server so we use a library called PubSubClient. You’ll need to modify your setup and loop functions now.

#include <PubSubClient.h>

const char *mqtt_server = "Caprica-Home.web-pass.com"; //my raspberry pi name

PubSubClient client(espClient);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  String message = String((char*)payload);
  Serial.println(message);

  if (message == "on") {
    Serial.println("on recieved");
    //turnLEDsOn();
  } else if (message == "off") {
    Serial.println("off recieved");
    //turnLEDsOff();
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "lantern conntected");
      // ... and resubscribe
      client.subscribe("BedroomLantern");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

Assuming all went well so far - you can now setup your “bridge” and device with the Home app on your iPhone running iOS 10.

Now for the fun part - adding the LEDs. I like to use Adafruit’s NeoPixels because each LED can have a different color and an entire strip can be controlled from 1 pin from the microcontroller. Adafruit sells these LEDs for a nice markup so I prefer to buy them from eBay or Alibaba. The NeoPixels are actually a common LED package called the WS2812B so you can just search for that and find it at a fraction of the cost.

Adafruit does have a nice library for controlling them but I actually prefer another library called FastLED. FastLED supports a lot of different LED types and has a nice abstraction layer on top of all of them. It also takes up less memory on the microcontroller.

So find a nice piece of wood or plastic or something to hold the LEDs then solder them to the microcontroller pins. The NeoPixels run anywhere from 4-6V so I just used the Vin pin of the NodeMCU board for power (USB provides 5V) and attached each strip to a different digital out pin.

Then I just stuffed everything inside a glass lantern I got from Turkey while traveling.

So to run the LEDs you need to setup the FastLED library in your microcontroller code.

#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
#include "FastLED.h"

//LED Pins
#define NUM_STRIPS 3
#define NUM_LEDS 5
CRGB leds[NUM_STRIPS][NUM_LEDS];

void setupLEDs() {
  //<Type of LEDs, Pin Number>
  FastLED.addLeds<NEOPIXEL, 8>(leds[0], NUM_LEDS);
  FastLED.addLeds<NEOPIXEL, 2>(leds[1], NUM_LEDS);
  FastLED.addLeds<NEOPIXEL, 4>(leds[2], NUM_LEDS);
}

void turnLEDsOff() {
  for(int x = 0; x < NUM_STRIPS; x++) {
    for(int y = 0; i < NUM_LEDS; i++) {
      leds[x][y] = CRGB::Black;
    }
  }
  FastLED.show();
}

void turnLEDsOn() {
  for(int x = 0; x < NUM_STRIPS; x++) {
    for(int y = 0; i < NUM_LEDS; i++) {
      leds[x][y] = CRGB::White;
    }
  }
  FastLED.show();
}

And then uncomment the turnLEDsOn() and turnLEDsOff() functions from your callback function. And thats it!

References

ESP8266 Wifi Example PubSubClient ESP8266 Example HAP-NodeJS MQTT.js Mosca NodeMCU Pinout FastLED ESP8266 NodeMCU Pins




comments powered by Disqus