Using a Raspberry Pi Zero W to Add a Camera and a Xiaomi Air Purifier 2 to HomeKit via Homebridge

· · 10 Comments

I recently purchased a Xiaomi Air Purifier 2, to avoid having dirty air in my apartment. Unfortunately, this product does not integrate with HomeKit. I am generally averse to having my “connected” home accessible from the internet, but I did want to use the HomeKit automation features. Setting this whole thing up just for one air purifier did seem like a lot of hassle, until I found out (from Steven Troughton-Smith) that you can get a Raspberry Pi Zero W with a camera module, which also works under HomeKit. Two-in-one? No need to ask me twice.

Please note that the instructions below are specific to the Raspberry Pi Zero W. You will need to adjust them if you are using a different model.

Ingredients

  1. Raspberry Pi Zero W Camera Pack [Adafruit / Botland] or Raspberry Pi Zero W Camera NoIR Pack [Adafruit / Botland ] — 1x.
  2. Xiaomi Air Purifier 2 [Amazon.de] — at least 1x.
  3. microSD card — a fast one is preferable, 8 GB or more (if you want to consider storing images and video).

Downloads

Unless you purchased a microSD card with your Raspberry Pi Zero W Camera Pack, which comes pre-installed with NOOBS1, then you will need to download Raspbian to your Mac or PC:

You will need to unzip the first file after it’s downloaded.

Burning Raspbian to Your microSD Card

Etcher screenshot

  1. Install Etcher by mounting the DMG file and then copying over the program to your /Applications folder.
  2. Launch Etcher.
  3. Press the big plus symbol and select 2017-11-29-raspbian-stretch.img (or equivalent).
  4. Choose your microSD card under the middle icon — please make sure that you are not erasing the wrong card/drive or any critical data!
  5. Press Flash!
  6. After the process finishes, remove the microSD card and re-insert it into your card reader, to mount it again.

Configuring SSH and Wi-Fi

Since we’re not going to be connecting the Raspberry Pi Zero W to a display or keyboard, you need to setup Wi-Fi and SSH beforehand.

  1. Create a new file in the root directory of the microSD card with Raspbian, named wpa_supplicant.conf.
  2. Edit that file with a text editor (I use BBedit):

    Please replace COUNTRY_CODE with your country’s code, e.g. US for USA, PL for Poland, and so on. Replace YOUR_WI-FI_SSID with whatever your SSID is — mine is CIA Surveillance Van 12. Finish off with replacing YOUR_PASSWORD with your Wi-Fi password.
  3. Save the file.
  4. Create another file in the root folder of your microSD card and call it ssh (without an extension). Leave the file empty. This will enable SSH (it’s disabled by default).

Ignition!

We now need to:

  1. Insert the microSD card into the proper slot in the Raspberry Pi.
  2. Connect the Raspberry Pi Camera cable to the Raspberry Pi Zero W.
  3. Insert the Raspberry Pi into the case — it should make a satisfying click.
  4. Insert the camera module into the top half of the case.
  5. Put both halves together.
  6. Connect the Raspberry Pi to the included power adapter.

The RPi Zero W should log onto your Wi-Fi network within a minute or so. You can connect to your router to find its IP address. The next step, is to connect to it via SSH:

  1. Launch Terminal.
  2. SSH to the RPi: ssh pi@xxx.xxx.xxx.xxx (please input the RPi’s IP address).
  3. Enter the password: raspberry.

This should get you in. The first thing you need to do is change the default password:

  1. Enter passwd into Terminal.
  2. Create a new password and save it somewhere safe, like 1Password.

We need to update/upgrade everything now by entering the following commands in Terminal:

  1. Type in sudo apt-get update into Terminal, press Enter and wait for it to finish.
  2. Type in sudo apt-get upgrade, press Enter and wait.
  3. Type in sudo apt-get dist-upgrade.
  4. Type in sudo apt-get clean.
  5. Type in raspi-update.
  6. And now it’s time to reboot: sudo reboot.

This might take a minute or three — be patient.

Installing node 8.9.4

At the time of writing this, I was informed that I will specifically need node 8.9.4. I tried running version 9.x.x and it didn’t work. If you know why this specific version is required for Homebridge, please let me know.

  1. Type node -v to check your current node version — it will probably be 4.x.x.
  2. cd to return to your home folder.
  3. wget https://nodejs.org/dist/v8.9.4/node-v8.9.4-linux-armv6l.tar.xz to download node 8.9.4.
  4. You will need to unpack node now: tar -xvf node-v8.9.4-linux-armv6l.tar.xz.
  5. cd node-v8.9.4-linux-armv6l/.
  6. Type in whereis node to discover where node is installed. The result will be similar to this: node: /usr/bin/node /usr/local/bin/node /usr/share/man/man1/node.1.gz Make note of the first item, /usr/bin/node in this case.
  7. If the first item was /usr/bin/node then type in: sudo cp -R * /usr/. If /usr/local/bin/node then type in: cp -R * /usr/local/.
  8. Now check if the installed version is correct: node -v. The result should be v8.9.4.

Install Homebridge and Add Your Xiaomi Air Purifier 2

Homebridge is a service which makes non-HomeKit devices visible under HomeKit.

  1. You need to install a few libraries first: sudo apt-get install libavahi-compat-libdnssd-dev.
  2. And then Homebridge itself: sudo npm install -g --unsafe-perm homebridge.

We will need to edit a few files soon. I personally use nano, but feel free to use your editor of choice.

  1. sudo nano /etc/default/homebridge to edit that file.
  2. Paste the following into the file:
  3. Type ^X (Control + X), press y and Enter to save the file.
  4. sudo nano /etc/systemd/system/homebridge.service to edit another file.
  5. Paste:
  6. Type ^X, y and Enter to save.
  7. You’ll want to automate Homebridge, to start automatically if/when the Raspberry Pi is restarted: sudo useradd --system homebridge.
  8. sudo mkdir /var/homebridge.
  9. cp /usr/local/lib/node_modules/homebridge/config-sample.json ~/.homebridge/config.json.
  10. We need to install one more tool, which is required for the Xiaomi Air Purifier 2: sudo npm install -g homebridge-mi-airpurifier miio.
  11. Type in miio discover sync to find your Xiaomi Air Purifier 2 on the network. The result of the search will look like this:
  12. Please copy your Token and Address from the result — we’ll require this information soon.
  13. Press ^C to close miio.
  14. We now need to prepare our configuration file: nano ~/.homebridge/config.json.
  15. Please generate a random MAC address using this tool. You will need it to comply to this format: XX:XX:XX:XX:XX:XX, e.g. F2:4A:25:C1:87:57.
  16. Since the file is empty, please paste the following into it:
  17. Please replace XX:XX:XX:XX:XX:XX in the file above with the MAC address you generated in step 15.
  18. Please add the IP address of your Xiaomi Air Purifier 2 from step 11 under ip.
  19. Please add the token of your Xiaomi Air Purifier 2 from step 11 under token.
  20. Save the file: ^X, y and Enter.
  21. Launch Homebridge, to make sure the configuration file works correctly: homebridge. In case of any errors, you can verify (and format) your JSON using this tool.
  22. If everything works correctly, you should see a QR code, a PIN number and some text, stating that everything loaded correctly. If it does, close Homebridge by typing in ^C.
  23. You will now complete automating Homebridge, to launch by itself: cp ~/.homebridge/config.json /var/homebridge.
  24. sudo cp -r ~/.homebridge/persist /var/homebridge.
  25. sudo chmod -R 0777 /var/homebridge.
  26. sudo systemctl daemon-reload.
  27. sudo systemctl enable homebridge.
  28. sudo systemctl start homebridge.
  29. sudo systemctl status homebridge.

You can press q to exit the command from step 29 and repeat it, until you see that everything loaded correctly — it only displays the last few lines of text of it loading.

Installing the Raspberry Pi Camera

You will now proceed to installing the Raspberry Pi camera itself.

  1. Type in sudo raspi-config.
  2. Go to Interfacing options.
  3. Go to P1 Camera.
  4. Set it to Yes.
  5. Exit the configuration program.
  6. Type in sudo modprobe bcm2835-v4l2.
  7. We will need to install ffmpeg: sudo apt install ffmpeg.
  8. And the camera plugin: sudo npm install -g homebridge-camera-rpi.
  9. cd /opt.
  10. sudo mkdir homebridge-camera-rpi.
  11. sudo chown pi homebridge-camera-rpi.
  12. git clone https://github.com/moritzmhmk/homebridge-camera-rpi.
  13. cd homebridge-camera-rpi.
  14. npm install.
  15. Create a new file: sudo nano /etc/systemd/system/hap-camera-rpi.service.
  16. Paste the following into it:
  17. Save the file: ^X, y and Enter.
  18. Create another new file: sudo nano /etc/homebridge-camera-rpi.conf.json.
  19. Paste the following, replacing XX:XX:XX:XX:XX:XX with a MAC address generated from this tool:

    Please note that you can change rotation to 180, should you mount your Raspberry Pi Zero W upside down. You can also change verticalFlip to true.
  20. Save the file: ^X, y and Enter.
  21. sudo systemctl enable hap-camera-rpi.
  22. sudo systemctl start hap-camera-rpi.
  23. sudo systemctl restart homebridge.
  24. To complete the camera service launching automatically, we need to edit a file: sudo nano /etc/modules.
  25. Please paste bcm2835-v4l2 into a new line, at the end of the file.
  26. Save the file: ^X, y and Enter.

Configuring HomeKit

Do you remember that QR code from earlier? Normally we would use that to configure HomeKit — it’s much quicker that way — but since Homebridge is running as a service, it will be easier just to enter the PIN code that you used in your configuration file. If you didn’t change it from my examples above, it should be 031-45-154.

  1. Launch HomeKit on your iPad or iPhone.
  2. Find the option to add your device via a PIN code (it should be somewhere towards the bottom of the screen).
  3. Enter your PIN code.
  4. When adding all your devices, you can change their names in HomeKit, e.g. I renamed MiAirPurifier2 to Air Purifier.

If you would like to use the automation features of HomeKit, you will need to have and Apple TV 4, Apple TV 4K or iPad running iOS 11. You will find more information on the subject here and here.


All your devices — the camera and air purifier — should be visible in HomeKit, either in the room you assigned them too, or in the list of favourites. You can of course control them via Siri, should you wish to.

And if you’re feeling a bit more adventurous, you can try to add a few more virtual devices to your Homebridge/HomeKit setup. I use Weather Underground to display the weather forecast for my street, as an example2.

Let me know if you got everything up and running!

  1. New Out Of Box Software.
  2. Please note that the Living Room temperature is actually the outside temperature — I guess I should move it to another “room”.

Chcesz zwrócić mi na coś uwagę lub skomentować? Zapraszam na @morid1n.

10 Comments

  1. Hi Wojtek,

    Thanks for your great post!

    I can see my Homebridge in de Home.app, but no accessories (PiCam) shows up.
    What goes wrong?

  2. I get the following error:
    raspberrypi systemd[1]: homebridge.service: Failed with result ‘exit-code’

  3. Hello, I have two Xiaomi air purifier 2, how can I add the second one ?

    Thanks

    • Not sure how that would work exactly, but in step 16 you would have to add it twice, including unique IP & MAC addresses. I’m not sure if it will need a unique name instead of MiAirPurifier2, but it could. If you figure it out, please do let me know.

Leave a Reply