What is Homekit2MQTT and why should you use it?
Homekit2MQTT is a utility that a wonderful genius called Sebastian Raff wrote to allow you to tie together all of the various self made home devices into Apples HomeKit framework for Siri to control.
If you want to learn more about Siri and HomeKit then I would suggest you start reading Apple’s website for some examples and details. Now that you are familiar with Siri and HomeKit we can move onto using Homekit2MQTT.
Homekit2MQTT is a utility that needs to be run on a device that can stay powered on all the time in the background. Ideally you will want something that doesn’t consume lots of power or resources. That’s where the Raspberry Pi Zero W comes in.
Ok I’m convinced. Now what?
Now you will need to get everything together and assembled. Some of the things I’ll be covering will be optional. Such as you can optionally connect your server via Ethernet or WiFi, depending if you use a Raspberry Pi 3 Model B or a Raspberry Pi Zero W. In my case I use the Raspberry Pi Zero W. You may also want to protect your Raspberry Pi by installing it into a case, You can find a case that I made here and here depending on your Raspberry model.
- Raspberry Pi Zero W ( I bought mine here )
- SanDisk Ultra 32GB microSDHC
- USB 3.0 Card Reader
- Raspberry Pi Case (Optional)
- RJ45 Ethernet Patch Cable (Optional, only need one if you use a Raspberry Pi 3 Model B)
Once you have acquired all the necessary and desired pieced you will need to download the operating system to install on your raspberry pi.
Installing your Raspberry Pi Zero W.
First we need to download the Raspberry image and a tool to write the image to a SD card.
On time of writhing this blog it’s Raspbian Stretch Lite, Release date: 2017-08-16, Kernel version: 4.9.
Install pibakery as described in my previous blog here.
When you have installed pibakery, put the SD card into your USB 3.0 Card Reader. Open Pibakery and click on startup en select On Every Boot. Hereafter click on Network and select Setup Wifi.
Now you can fill in your network credentials. Once you’ve created the perfect setup for your Raspberry Pi, you’re ready to write it to an SD card to use. Click on the Write button in the top right corner of PiBakery. This will bring up the SD card chooser. Select your SD card.
Choose your SD card from the drop-down menu making sure that you choose the right one. The drive that you choose will be erased and then written over, meaning that any data on it currently will be deleted permanently. Once you’ve chosen your SD card, click on Start Write and choose Yes in the warning dialog that pops up. Once the write has finished, you’ll see a green tick with the Write Successful message.
Once PiBakery has finished writing to your SD card, you can remove it from your computer, insert it into your Raspberry Pi and power it on. Your scripts will be running after a few seconds, customizing Raspbian to your requirements.
Setting Up your Raspberry Pi Zero W.
Open the Terminal app. Click the Finder icon in your dock. Click Go. Click Utilities. Double-click Terminal. Alternatively, you can press Shift + Option + U. We start with setting up your ssh connection.
Type the following line where your IP address, is the one you provided when installing your Raspberry Pi Zero W.
and press enter
Note: If you get this error because you already made a connection with your Raspberry Pi Zero W in the past.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that the RSA host key has just been changed. The fingerprint for the RSA key sent by the remote host is 5c:9b:16:56:a6:cd:11:10:3a:cd:1b:a2:91:cd:e5:1c. Please contact your system administrator. Add correct host key in /home/user/.ssh/known_hosts to get rid of this message. Offending key in /home/user/.ssh/known_hosts:1 RSA host key for ras.mydomain.com has changed and you have requested strict checking. Host key verification failed.
rm -f ~/.ssh/known_hosts
and try to reconnect.
Configure your Raspberry Pi Zero W.
To configure your Raspberry Pi Zero W for further use, I recommend to follow these steps before installing the actual Homekit2MQTT software.
To open the program called Raspi-config, which you use to change some of the settings on your Raspberry Pi, type:
First of all expand the capacity of the SD-card for your operating system.
When you flash your SD card, an exact copy of the operating system and its disk formatting is copied to the card. As a result, the SD card can look like it has less capacity than it really does, which means you can quickly run out of space, even on a higher capacity card.
One of the first things you should do is use this option in Raspi-config to ensure your Raspberry Pi can use all the space available on your SD card.
When you press Enter with this option highlighted, it runs straight away. When you next start up your Raspberry Pi, your Raspberry Pi resizes the file system, which can take a few minutes, during which the screen won’t update. The new capacity then becomes available to you.
Select change_pass (strongly advised!)
This option enables you to change the password for the default user on your Raspberry Pi, which is the user called pi. You can leave this setting alone, unless you’re particularly security-conscious or are using your Raspberry Pi for particularly sensitive activities. If you change your password, don’t forget it!
Provide your Raspberry Pi Zero W with a Static IP Address (strongly advised!) you can read how in my previous bloge here.
Update your Raspberry Pi Zero W.
Update your Raspberry Pi Zero W? I just installed it fresh? It’s always a good thing to update you Raspberry Pi Zero W to the latest software to prevent bugs and security issues. You may have installed a fresh copy, but there can be updates that aren’t slipstreamed into your image yet.
To update your Raspberry Pi Zero W type:
sudo apt-get update
When the updates are downloaded type:
sudo apt-get upgrade
Now we are ready to install Homekit2MQTT.
Installing Homekit2MQTT Prerequisites
Before installing the actual Homekit2MQTT software you need two Prerequisites:
Node.js 6 or higher
To be sure you have the latest version of Node.js type.
sudo apt install nodejs
sudo wget https://nodejs.org/dist/v6.9.2/node-v6.9.2-linux-armv6l.tar.gz
sudo tar -xvf node-v6.9.2-linux-armv6l.tar.gz
opy to / usr / local / using the following command to execute :
sudo cp -R * /usr/local/
When the installation is done type
sudo node –v
This will show you the version installed of node.js on your system.
Now reboot your Raspberry Pi Zero W.
If you’re running on Linux ( Yes, your raspberry Pi runs on Linux), you’ll need to make sure you have the libavahi-compat-libdnssd-dev package installed. You can do this by typing:
sudo apt-get install libavahi-compat-libdnssd-dev
Now, you need to add the Mosquitto repository. Enter the following commands.
sudo wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
sudo rm mosquitto-repo.gpg.key
Next, make the repository available to apt-get. First we need to change to the apt sources list directory.
Now install the packages list for your version of Raspbian.
For Jessie (old version)
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
For Stretch (latest)
sudo wget http://repo.mosquitto.org/debian/ mosquitto-stretch.list
Now update apt information and install Mosquitto.
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients
That is it. Mosquitto is now installed and running on your Raspberry Pi.
Now reboot your Raspberry Pi Zero W.
Where almost there…
Now that you have installed all the Prerequisites you’re ready for the real deal, installing Homekit2MQTT. Type:
sudo npm install -g homekit2mqtt --unsafe-perm
Installation can take quite some time. Wait quietly until the completion of the installation.
When the installation is complete, we can start Homekit2MQTT for the first time. Enter the following command:
Now that you have homekit2mqtt up and running you can add sensors. homekit2mqtt needs a JSON file that defines devices and mappings from MQTT-topics and payloads to HomeKit-characteristics. You can either create this manually or use the Web UI to configure homekit2mqtt.
Connect you iPhone to homekit2mqtt
Now we have to connect our iPhone to the homekit2mqtt and add the new accessories.
On your iOS device launch the Home app.
Tap the Add button in the top right corner of the screen (looks like a plus sign).
Tap Add Accessory.
Tap on the MQTT accessory (mine’s just called MQTT Bridge).
You will see an alert that says, “This accessory is not certified and may not work reliably with HomeKit.” tap Add Anyway.
Tap Enter Code Manually at the bottom of the screen.
Enter the HomeKit code for your MQTT Bridge accessory.
Add your Homebridge accessory and tap Next.
Add the accessories (The are a few preconfigured) and tap Done.
To add the new accessories, homekit2mqtt has a build-in webserver. This allows you that easily add new MQTT accessories for homekit. So to connect to your homekit2mqtt webserver, open your web browser and type:
Now you can login with the username:
Congratulations! You’ve successfully linked a non-HomeKit-enabled smart home accessory to HomeKit. Now it’s time to make some sensors for homekit2mqtt so we can test if everything is working. But that will be in a next blog…