Post

Smart WFH Home Setup|Control All Appliances with HomeKit & Raspberry Pi

Discover how to transform your WFH environment by integrating Xiaomi appliances with HomeKit using Raspberry Pi as a HomeBridge server, enabling seamless smart home control at your fingertips.

Smart WFH Home Setup|Control All Appliances with HomeKit & Raspberry Pi

点击这里查看本文章简体中文版本。

點擊這裡查看本文章正體中文版本。

This post was translated with AI assistance — let me know if anything sounds off!


Creating a Comfortable WFH Smart Home Environment, Control Appliances at Your Fingertips

Demonstration of Using Raspberry Pi as a HomeBridge Host to Connect All Mijia Appliances to HomeKit

photo by [picjumbo.com](https://www.pexels.com/zh-tw/@picjumbo-com-55570?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels){:target="_blank"}

photo by picjumbo.com

About

Due to the pandemic, people have been spending more time at home; especially when working from home, it’s best if all home appliances can be smartly controlled via an app. This way, you don’t have to keep getting up to turn on the lights or the rice cooker, which wastes a lot of time.

I previously wrote an article titled “First Experience with Smart Home — Apple HomeKit & Xiaomi Mijia,” where I tried using HomeBridge to connect Xiaomi appliances to HomeKit. It proved theoretically feasible, but practical applications were rarely mentioned. This article is an advanced, comprehensive version of the previous one, including how to set up a Raspberry Pi as the host, with a step-by-step tutorial from start to finish.

The reason is that I recently switched to an iPhone 11 Pro, which supports NFC automation in Shortcuts for iOS ≥ 13. This means the phone can execute corresponding shortcuts when it senses an NFC tag. Although you can directly use an old EasyCard as an NFC tag, it takes up too much space and I don’t have that many cards. I asked around Guanghua but couldn’t find NFC tag stickers for sale. Finally, I found some on Shopee for $50 each and bought 5 to try out. The seller was thoughtful enough to separate them by color.

NFC automation features are device-specific and only supported on iPhone XS/XS Max/XR/11/11 Pro/11 Pro Max. Earlier models like iPhone 8 do not have the NFC option.

After testing a bit, I found an issue: when running the Mi Home app shortcut, you must enable the “Show When Run” option (otherwise it won’t actually execute). When the Tag is detected and the shortcut needs to run, you still have to unlock the iPhone, and the shortcut opens visibly during execution, so it can’t run silently in the background. Additionally, I tested that if the shortcut uses native Apple services (like HomeKit appliances), it can run directly in the background without unlocking. Also, HomeKit’s response speed and stability are much better than Mi Home.

This makes a big difference in user experience, so I further explored connecting the Mi Home smart home series products to HomeKit. For those that support HomeKit, just bind them directly as this article does not repeat; for those that don’t support it, follow the tutorial in this article to bind them as well!

My Mi Home Smart Home Project

  1. Mi Home Smart Camera Pan-Tilt Version 1080P

  2. Mi Home DC Inverter Electric Fan

  3. Mijia LED Smart Desk Lamp

  4. Xiaomi Air Purifier 3

  5. Mijia Desk Lamp Pro (supports HomeKit natively)

  6. Mi Home LED Smart Bulb Color Version * 2 (natively supports HomeKit)

How It Works

Here is a simple reference diagram: if a smart appliance supports HomeKit, it can connect directly. For smart appliances that do not support HomeKit, setting up a “HomeBridge” server (which must stay powered on) can bridge and connect them; within the same network environment (e.g., the same WiFi), the iPhone can freely control all HomeKit devices. However, when outside the local network, such as using a 4G mobile network, an Apple TV, HomePod, or iPad must act as the home hub and stay powered on at home to control HomeKit devices remotely. Without a home hub, opening the Home app outside will show “No Response.”

*If it is Xiaomi, the home appliances are controlled through Xiaomi’s servers. To be clear, there are security concerns as all data passes through Mainland China.

Required Environment

So, there are two devices that must always be on standby: one is the Apple TV/HomePod or iPad acting as the Home Hub. Currently, there is no solution to simulate this with other methods; you have to obtain these devices. Without them, you can only use HomeKit at home.** **

Another computer that can be on standby 24 hours a day (such as your iMac/MacBook), an idle host (old iMac, Mac Mini), or a Raspberry Pi will work.

*The Windows series has not been tested, but it should work as well!

Or if you want to try it out, you can directly use your current computer (can be combined with the previous article).

This article will demonstrate using a Raspberry Pi (Raspberry Pi 3B) operated on a MacBook Pro (MacOS 10.15.4), starting from setting up the Raspberry Pi environment from scratch. If you are not using a Raspberry Pi, you can skip directly to the HomeBridge integration with HomeKit section (the steps are the same there).

Raspberry Pi 3B (special thanks to [Lu Xun Huang](https://medium.com/u/b32ce1b681f8){:target="_blank"} )

Raspberry Pi 3B (special thanks to Lu Xun Huang )

If using a Raspberry Pi, you will also need a micro SD card (not too large, I use 8GB), a card reader, an Ethernet cable (for setup, later you can connect via WiFi); and the necessary software for the Raspberry Pi:

  1. Raspberry Pi Desktop Operating System (Easy for Beginners, GUI Version)

  2. Etcher burning software

Raspberry Pi Environment Setup

Burning the Operating System

After downloading the two required software, first insert the memory card into the card reader and connect it to the computer; then open the Etcher program (balenaEtcher)

First, select the recently downloaded Raspberry Pi OS file "xxxx.img". Second, choose your memory card device, then click "Flash!" to start writing!

For the first option, select the newly downloaded Raspberry Pi OS image file “xxxx.img”. For the second option, choose your memory card device, then click “Flash!” to start writing!

At this point, a prompt will appear asking you to enter your **MacOS password**. After entering it, click "Ok" to continue.

At this point, a prompt will appear asking you to enter your MacOS password. After entering it, click “Ok” to continue.

Burning... Please wait....

Burning in progress… please wait….

Verifying… Please wait….

Verifying… please wait….

Burn Successful!

Burning successful!

*If a red Error appears, try formatting the memory card and burning it again.

Reconnect the card reader to the computer, and create an empty file named “ssh” ( or download here ) in the memory card directory. The file should be blank, with no extension, just a plain “ssh” file; this allows us to connect to the Raspberry Pi using Terminal.

ssh

ssh

Setting up Raspberry Pi

Eject the memory card, insert it into the Raspberry Pi, connect the network cable, and power it on; ensure the MacBook and Raspberry Pi are on the same network.

Check the IP Address Assigned to the Raspberry Pi

The assigned IP address of the Raspberry Pi is: 192.168.0.110 (Please replace all IP addresses mentioned in this article with the ones you have found)

It is recommended to set the Raspberry Pi to a static/reserved IP; otherwise, the IP address may change after rebooting and reconnecting, requiring you to check it again.

Use SSH to Access the Raspberry Pi for Operations

Open Terminal and enter:

1
ssh pi@your_raspberry_pi_IP_address

If prompted, enter yes. The default password is: raspberry

**Connection Successful!**

Connection Successful!

*If you encounter a WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED error message, open /Users/xxxx/.ssh/known_hosts with a text editor and clear its contents.

Basic Raspberry Pi Tools Installation and Setup

  1. Enter the following command to install the Vim editor:
1
sudo apt-get install vim

2. Fix the following language warnings:

1
2
3
4
5
6
7
8
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_LANG = "zh_TW.UTF-8",
    LANG = "zh_TW.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

Input

1
vi .bashrc

Press “Enter” to proceed

Press “i” to enter insert mode

Move to the very bottom of the file and add a line: export LC_ALL=C

Press “Esc” and type “:wq!” to save and exit.

Then run source .bashrc to update.

3. Install nvm to manage nodejs/npm:

1
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh \\| bash

4. Install the latest version of nodejs using nvm:

nvm install 12.16.2

*Here, choose to install version “12.16.2”

5. Confirm Environment Installation Completion:

Enter the following command

npm -v

and

node -v

Confirmation

No error message means success!

No error messages!

6. Establish nodejs connection

Enter the following command

1
which node

Get the path information of nodejs location

Enter Again

1
sudo ln -fs paste the path found by which node here (without "double quotes") /usr/local/bin/node

Create Links

Setup Complete!

Enable Raspberry Pi VNC Remote Desktop Function

Here, although we are installing the GUI version, you can of course connect the Raspberry Pi to a keyboard and HDMI and use it like a regular computer. However, for convenience, we will control the Raspberry Pi via remote desktop.

Input:

1
sudo raspi-config

Enter Settings:

Select the fifth item "**Interfacing Options**"

Select the fifth item, “Interfacing Options

Select the third option "**P3 VNC**"

Choose the third option “P3 VNC

Use " **←** " to select " **Yes** " to open

Use “ “ to select “ Yes “ to turn on

**VNC Remote Desktop Feature Enabled Successfully!**

VNC Remote Desktop Function Enabled Successfully!

Use " **→** " to directly jump to " **Finish** " and exit the settings interface.

Use “” to go directly to “Finish” to exit the settings interface.

Add VNC Remote Desktop Service to Startup Items

We want the VNC remote desktop service to start automatically when the Raspberry Pi boots up.

Input

1
sudo vim /etc/init.d/vncserver

Press “Enter” to proceed

Press “i” to enter insert mode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/sh
### BEGIN INIT INFO
# Provides:          vncserver
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop vncserver
### END INIT INFO

# More details see:
# http://www.penguintutor.com/linux/vnc

### Customize this entry
# Set the USER variable to the name of the user to start vncserver under
export USER='pi'
### End customization required

eval cd ~$USER

case "$1" in
  start)
    su $USER -c '/usr/bin/vncserver -depth 16 -geometry 1024x768 :1'
    echo "Starting VNC server for $USER "
    ;;
  stop)
    su $USER -c '/usr/bin/vncserver -kill :1'
    echo "vncserver stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/vncserver {start\\|stop}"
    exit 1
    ;;
esac
exit 0

Press “Command” + “C” and “Command” + “V” to copy and paste the above content, then press “Esc” and type “:wq!” to save and exit.

Input again:

1
sudo chmod 755 /etc/init.d/vncserver

Modify file permissions.

Input again:

1
sudo update-rc.d vncserver defaults

Add to startup items.

Final Input:

1
sudo reboot

Restart the Raspberry Pi.

  • After the restart is complete, reconnect via SSH following the previous steps.

Connecting Using a VNC Client:

Here, the Chrome app “VNC® Viewer for Google Chrome™” is used. After installation and launch, enter Raspberry Pi IP address:1. Please note that the Port:1 must be added!

*I cannot connect using the Mac’s built-in VNC://, unsure of the reason.

Click "**Connect**".

Click “Connect”.

Click "**OK**".

Click “OK”.

**Enter login username and password**, same as SSH connection, username `pi` default password `raspberry`.

Enter login username and password, same as SSH connection, username pi with default password raspberry.

**Successfully Connected!**

Successfully connected!

Complete Raspberry Pi Initialization Setup:

Next are all graphical interfaces! Very easy!

Set language, region, and time zone.

Set Language, Region, and Time Zone.

Change the default Raspberry Pi password by entering the password you want to set.

Change the default Raspberry Pi password by entering the password you want to set.

Directly click " **Next** ".

Directly proceed to the “Next” step.

Set up WiFi connection, so you don't need to plug in the cable anymore.

Set up WiFi connection, so you don’t need to plug in the cable afterward.

*Note that the Raspberry Pi’s IP address may change, so check the router again.

Do you want to update the current operating system? If you're not in a hurry, choose "**Next**" to update!

If you’re not in a hurry, choose “Next” to update the current operating system!

*The update takes about 20 to 30 minutes (depending on your internet speed)

After the update is complete, click "**Restart**" to reboot.

After the update is complete, click “Restart” to reboot.

Raspberry Pi Environment Setup Complete!

HomeBridge Installation

Officially entering the main event: installing and using HomeBridge.

Use Terminal to SSH into the Raspberry Pi or directly use the Terminal within the VNC remote desktop.

Input:

1
npm -g install homebridge --unsafe-perm

^( Without sudo )

Install HomeBridge

Installation Complete!

Create/Modify Configuration File (config.json):

To facilitate editing, use VNC remote desktop to connect to the Raspberry Pi (or directly use commands) :

Click the top-left corner to open “File Manager” -> Go to “/home/pi/.homebridge

If you don’t see the “config.json” file, right-click on the blank area, select “New File” -> enter the file name “config.json

Right-click on “config.json” and open it with “Text Editor”.

Paste the following basic configuration content:

1
2
3
4
5
6
7
{
   "bridge": {
  "name": "Homebridge",
  "username": "CC:22:3D:E3:CE:30",
  "port": 51826,
  "pin": "123-45-568"
}

No need to make special changes to the content, just copy it as is!

Remember to save your work!

Done!

Binding HomeBridge to Homekit

Input:

1
homebridge start

^( Without sudo )

Enable

*If you encounter an error like “Error: Service name is already in use on the network” or the port is occupied, try deleting the service, using homebridge restart to restart, or rebooting the device.

  • If you see an error like “was not registered by any plugin,” it means you haven’t installed the corresponding homebridge plugin yet.

If you make changes to the configuration file (config.json) during startup, you need to update it accordingly:

sudo homebridge restart

Restart HomeBridge

  • Press “Control” + “C” to close and exit the HomeBridge service in Terminal.

Take out your iPhone and open the “Home” app. Tap the “+” in the top right corner of “Home,” select “Add Accessory,” and scan the QR code that appears.

At this point, you should see “Accessory Not Found.” Don’t worry! This is because we haven’t added any accessories to the HomeBridge bridge yet. It’s okay, let’s continue.

At least one accessory is required to scan and add! ! ! (Using a camera as an example) :
At least one accessory is required to scan and add! ! ! (Using a camera as an example) :
At least one accessory is required to scan and add! ! ! (Using a camera as an example) :

The first time you scan, a warning window will appear. Just click “Force Join” to proceed!

After adding once, you don’t need to scan additional accessories again; they will update automatically!

Add HomeBridge Service to Raspberry Pi Startup Items Automatically

Like VNC remote desktop service, we also want the HomeBridge service to start automatically when the Raspberry Pi boots up. Otherwise, we would have to manually connect and enable it again after each reboot.

Input:

1
which homebridge

Get homebridge Path Information

Remember this path.

Enter again:

1
sudo vim /etc/init.d/homebridge

Press “Enter” to proceed

Press “i” to enter edit mode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/sh
### BEGIN INIT INFO
# Provides:
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO


dir="/home/pi"
cmd="DEBUG=* Paste the path found by which homebridge here"
user="pi"


name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"


get_pid() {
cat "$pid_file"
}


is_running() {
[ -f "$pid_file" ] && ps -p `get_pid` > /dev/null 2>&1
}


case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
cd "$dir"
if [ -z "$user" ]; then
sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
else
sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
fi
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
if is_running; then
echo -n "Stopping $name.."
kill `get_pid`
for i in 1 2 3 4 5 6 7 8 9 10
# for i in `seq 10`
do
if ! is_running; then
break
fi


echo -n "."
sleep 1
done
echo


if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start\\|stop\\|restart\\|status}"
exit 1
;;
esac
exit 0

To:

cmd="DEBUG=* Paste here the path you found with which homebridge"

Replace with the path information you found (no quotation marks needed)

Type “Commend” + “C” and “Commend” + “V” to copy and paste the above content, then press “Esc” and type “:wq!” to save and exit.

Enter again:

1
sudo chmod 755 /etc/init.d/homebridge

Change file permissions.

Final Input:

1
sudo update-rc.d homebridge defaults

Add to startup items.

Done!

You can directly use sudo /etc/init.d/homebridge start to start the homebridge service.

You can also use: tail -f /var/log/homebridge.err to view startup error messages, and tail -f /var/log/homebridge.log to view logs.

Preparation Before Connecting Mijia Smart Appliances

After Homebridge is up and running, we can start adding all Mi Home appliances to Homebridge and connect them to HomeKit one by one!

First, we need to add all Mijia smart appliances to the “Mijia APP, as we will obtain the information to connect to HomeBridge from it.

After Smart Appliances Are Added to the Mi Home App:

Connect the iPhone to the Mac, open the Finder/iTunes interface, and select the connected phone.

Select backup to “This Computer”, do not check “Encrypt local backup”, then click “Back Up Now”.

After completing the backup, download and install the backup viewer software: iBackupViewer

Open “iBackupViewer

At first launch, you will be prompted to go to Mac “System Preferences” - “Security & Privacy” - “Privacy” - “+” - add “iBackupViewer”

**If you have privacy concerns, you can disable internet access for this software and remove it after use*

After reopening iBackupViewer and successfully loading the backup file, click on the “Recently Backed Up Phone.”

Select the "**App Store**" Icon

Select the “App Store” Icon

On the left, find "MiHome APP (MiHome.app)" -> On the right, find the "**number_mihome.sqlite**" file and "**select**" it -> Top right corner "**Export**" -> "**Selected Files**"

On the left, find “Mi Home APP (MiHome.app)” -> on the right, locate the “digital_mihome.sqlite” file and “select” it -> top right corner “Export” -> “Selected Files

*If there are two “number_mihome.sqlite” files, choose the one with the newest Created time.

Drag the exported 數字_mihome.sqlite file into this website to view its contents:

You can change the query syntax to:

1
SELECT `ZDID`,`ZNAME`,`ZTOKEN` FROM 'ZDEVICE' LIMIT 0,30

Only display the required field information (if specific appliance kits need additional field information, you can add those for filtering).

  1. ZDID: Device ID

  2. ZNAME: Device Name

  3. ZTOKEN: Install ZToken

ZTOKEN cannot be used directly; it must be converted to “Token” before use.

Here is an example of converting a camera’s ZToken to a Token:

First, we get the content of the camera’s ZToken field from the list above.

1
7f1a3541f0433b3ccda94beb856c2f5ba2b15f293ce0cc398ea08b549f9c74050143db63ee66b0cdff9f69917680151e

But the TOKEN obtained here cannot be used yet; we still need to convert it.

Open the website http://aes.online-domain-tools.com/:

  1. Paste the copied ZTOKEN into the “Input Text” field and select “Hex”.

  2. Enter the key “00000000000000000000000000000000” with 32 zeros, still select “Hex”.

  3. Then press “Decrypt!” to convert.

  4. Select all and copy the output content of the two lines at the bottom right & remove the spaces to get the result we want Token.

“**6d304e6867384b704b4f714d45314a34**” is the Token result we want!

6d304e6867384b704b4f714d45314a34” is the Token result we need!

*For obtaining the Token, I tried using “miio” to sniff it directly, but it seems the Mi Home firmware has been updated, making this method no longer a quick and convenient way to get the Token!

Finally, we also need to know the device’s IP address (using the camera as an example here):

Open the Mi Home APP → Camera → Top right corner “…” → Settings → Network Information to get the IP address!

Record ZDID/Token/IP information for later use.

Integrate Mijia Smart Appliances into HomeBridge One by One

According to the packages and connection information required by each device, install and configure them one by one, then add them to HomeBridge.

Next, open Terminal to SSH into the Raspberry Pi or directly use the Terminal in the VNC remote desktop to continue the following tasks….

1. Mijia Camera PTZ Version:

Install the MijiaCamera homebridge plugin via Terminal ( without sudo ):

1
npm install -g homebridge-mijia-camera

Referring to the previous tutorial on modifying the configuration file (config.json), add the accessories section to the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
   "bridge":{
      "name":"Homebridge",
      "username":"CC:22:3D:E3:CE:30",
      "port":51826,
      "pin":"123-45-568"
   },
   "accessories":[
      {
         "accessory":"MijiaCamera",
         "name":"Mi Camera",
         "ip":"",
         "token":""
      }
   ]
}

accessories: Add the Mi Home camera settings, with ip set to the camera’s IP and token set to the token from the previous tutorial.

Remember to save your work!

Then, follow the Homebridge section instructions to start/restart/scan and add Homebridge; you will be able to see the camera controls in the “Home” app.

Controllable item: Camera On/Off

2. Mijia DC Inverter Electric Fan

Run the command below in Terminal to install the homebridge-mi-fan homebridge plugin (without sudo):

1
npm install -g homebridge-mi-fan

Referring to the previous tutorial on modifying the configuration file (config.json), add the platforms section in the file (if it already exists, add a new subsection within it separated by a comma):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
   "bridge":{
      "name":"Homebridge",
      "username":"CC:22:3D:E3:CE:30",
      "port":51826,
      "pin":"123-45-568"
   },
   "platforms":[
      {
         "platform":"MiFanPlatform",
         "deviceCfgs":[
            {
               "type":"MiDCVariableFrequencyFan",
               "ip":"",
               "token":"",
               "fanName":"room fan",
               "fanDisable":false,
               "temperatureName":"room temperature",
               "temperatureDisable":true,
               "humidityName":"room humidity",
               "humidityDisable":true,
               "buzzerSwitchName":"fan buzzer switch",
               "buzzerSwitchDisable":true,
               "ledBulbName":"fan led switch",
               "ledBulbDisable":true
            }
         ]
      }
   ]
}

platforms: Add Mi Home fan configuration info, with ip set to the camera’s ip, token set to the token from the previous tutorial, and humidity/temperature controlling whether to link and display humidity and temperature meter info.
type must include the corresponding model text, supporting four different fan models:

  1. ZhiMi DC Variable Frequency Floor Fan: ZhiMi DC Variable Frequency Floor Fan

  2. ZhiMi Natural Wind Fan: ZhiMiNaturalWindFan

  3. Mi Home DC Inverter: MiDCVariableFrequencyFan (Sold in Taiwan)

  4. Mijia Fan: DmakerFan

Please enter your own fan model.

Remember to save your work!

Then, follow the Homebridge chapter to start/restart/scan and add Homebridge; you will be able to see the camera controls in the “Home” app.

Controllable items: Fan on/off, wind speed adjustment

3. Xiaomi Air Purifier 3

Run the following command in Terminal to install the homebridge-xiaomi-air-purifier3 homebridge plugin (without sudo):

1
npm install -g homebridge-xiaomi-air-purifier3

Refer to the previous tutorial on modifying the configuration file (config.json), add the accessories section to the file (if it already exists, add a new subsection within it separated by a comma) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
   "bridge":{
      "name":"Homebridge",
      "username":"CC:22:3D:E3:CE:30",
      "port":51826,
      "pin":"123-45-568"
   },
   "accessories":[
      {
         "accessory":"XiaomiAirPurifier3",
         "name":"Xiaomi Air Purifier",
         "did":"",
         "ip":"",
         "token":"",
         "pm25_breakpoints":[
            5,
            12,
            35,
            55
         ]
      }
   ]
}

accessories: Add Mi Home fan configuration info, with ip set to the camera’s ip, token set to the token from the previous tutorial, and did set to zdid

Remember to save your work!

Then, follow the Homebridge section instructions to start/restart/scan and add Homebridge; you will be able to see the camera controls in the “Home” app.

Controllable items: Air purifier power switch, fan speed adjustment
Viewable items: Current temperature and humidity

4. Mijia LED Smart Desk Lamp

Install the homebridge-yeelight-wifi homebridge plugin via Terminal (without sudo):

1
npm install -g homebridge-yeelight-wifi

Referring to the previous tutorial on modifying the configuration file (config.json), add the platforms section in the file (if it already exists, add a new subsection within it separated by a comma) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
   "bridge":{
      "name":"Homebridge",
      "username":"CC:22:3D:E3:CE:30",
      "port":51826,
      "pin":"123-45-568"
   },
   "platforms":[
      {
         "platform":"yeelight",
         "name":"Yeelight"
      }
   ]
}

No need to pass any special parameters! For more detailed settings, refer to the official documentation (such as brightness/color temperature…).

Remember to save your work!

The smart desk lamp also needs to be linked to the Yeelight app, and then enable “Local Network Control” to allow Homebridge to control it.

  1. Download and install the “Yeelight” app on your iPhone

App Store search for "Yeelight" installation

Search “Yeelight” on the App Store and install it

After installation, open the Yeelight APP -> "Add Device" -> Find "Mi Home Desk Lamp" -> Re-pair and bind

After installation, open the Yeelight APP -> “Add Device” -> find “Mi Home Desk Lamp” -> re-pair and bind.

Last step, remember to enable "**Local Network Control**"

The final step is to enable “Local Network Control”.

If you accidentally didn’t open it, go to the “Devices” page -> select the desk lamp device -> tap the “△” tab at the bottom right -> tap “Local Network Control” to enter settings -> turn on Local Network Control

Honestly, this is really bad. The Mi Home app itself doesn’t have this toggle feature; you have to bind it to the Yeelight app, and you can’t unbind or rebind it back to Mi Home… otherwise, it will stop working.

Then, follow the Homebridge section instructions to start/restart/scan and add Homebridge; you will be able to see the camera controls in the “Home” app.

Controllable Items: Light switch, color temperature adjustment, brightness adjustment

Other Mijia smart home appliances homebridge packages:

My final config.json looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{
   "bridge":{
      "name":"Homebridge",
      "username":"CC:22:3D:E3:CE:30",
      "port":51826,
      "pin":"123-45-568"
   },
   "accessories":[
      {
         "accessory":"MijiaCamera",
         "name":"Mi Camera",
         "ip":"192.168.0.105",
         "token":"6d304e6867384b704b4f714d45314a34"
      },
      {
         "accessory":"XiaomiAirPurifier3",
         "name":"Xiaomi Air Purifier",
         "did":"270033668",
         "ip":"192.168.0.108",
         "token":"5c3eeb03065fd8fc6ad10cae1f7cce7c",
         "pm25_breakpoints":[
            5,
            12,
            35,
            55
         ]
      }
   ],
   "platforms":[
      {
         "platform":"MiFanPlatform",
         "deviceCfgs":[
            {
               "type":"MiDCVariableFrequencyFan",
               "ip":"192.168.0.106",
               "token":"dd1b6f582ba6ce34f959bbbc1c1ca59f",
               "fanName":"room fan",
               "fanDisable":false,
               "temperatureName":"room temperature",
               "temperatureDisable":true,
               "humidityName":"room humidity",
               "humidityDisable":true,
               "buzzerSwitchName":"fan buzzer switch",
               "buzzerSwitchDisable":true,
               "ledBulbName":"fan led switch",
               "ledBulbDisable":true
            }
         ]
      },
      {
         "platform":"yeelight",
         "name":"Yeelight"
      }
   ]
}

For everyone’s reference!

The Mi Home appliances I used are as shown in the tutorial above. For others I don’t have, I didn’t try them. You can check yourself on npm (homebridge-plugin XXX English name), then follow the similar installation and setup steps as above to integrate them!

Here are a few Homebridge plugins I found but haven’t tried (no guarantee they work):

  1. Xiaomi Air Purifier 1st Gen: homebridge-mi-air-purifier

  2. Mi Home Smart Socket Series: homebridge-mi-outlet

  3. Xiaomi Robot Vacuum: homebridge-mi-robot_vacuum

  4. Mi Home Smart Gateway: homebridge-mi-aqara

Tips

  1. It is recommended to set all Mi Home appliances to static/reserved IPs on the router. Otherwise, the IP addresses may change, requiring you to update the config.json settings again.

  2. If you find that all the steps are correct but errors still occur or HomeKit keeps showing “No Response,” try again; if the issue persists, it may mean the plugin is no longer valid and you need to find another plugin to connect. (You can check the GitHub issues)

  3. Functionality failure and slow response; this is also unresolved. You can submit an issue to notify the author and wait for an update. Since it is an open-source project, you can’t expect too much!

  4. After binding each appliance, you can start Homebridge once, then check on your iPhone if it works. If it does, you can press “Control” + “C” to stop it; after all appliances are bound, restart the Raspberry Pi so it automatically starts the Homebridge service in the background after reboot. This is what we want.

Conclusion

You can also go to "Settings" -> "Control Center" -> "Customize Controls" and add the "Home" app to quickly control HomeKit from the Control Center pull-down!

You can also go to “Settings” -> “Control Center” -> “Customize” and add the “Home” app to quickly access HomeKit from the Control Center dropdown!

After connecting everything to HomeKit, there’s only one word: “Awesome”! The switches respond faster; the only downside is I don’t have a home hub to enable remote control. This concludes the advanced Homebridge guide. Thank you for reading.

Back to the beginning of the article, once everything is added to HomeKit, we can seamlessly use the automation features of iOS ≥ 13 Shortcuts.

Later, I want to study how the Homebridge plugin works. It seems very interesting! So if a Homebridge plugin doesn’t meet your needs or a plugin breaks and you can’t find a replacement, just wait for me to research it!

Home assistant

There is another smart home platform called Homeassistant that can be installed on a Raspberry Pi (but note: a 2A power supply is required to start it); I have also installed Homeassistant to try it out. It offers a full GUI with graphical operations, allowing you to connect home appliances with just a few clicks. I plan to explore it further later. It feels like an alternative to the Mi Home platform, and it is more suitable if you have many IoT devices from different manufacturers.

References

  1. https://www.domoticz.cn/forum/viewtopic.php?t=52

  2. https://or2.in/2017/07/02/Homekit-and-MiJia-with-pi/#3-%E5%8F%B7%E5%A4%96-%E5%BC%80%E5%90%AF%E5%8F%AF%E8%A7%86%E5%8C%96VNC

Further Reading

  1. Xiaomi Smart Home New Purchases (AI Speaker, Temperature and Humidity Sensor, Scale 2, DC Inverter Fan)

  2. iOS ≥ 13.1 Automation with “Shortcuts” for Mijia Smart Home (Using the built-in Shortcuts app in iOS ≥ 13.1 to complete automation)

  3. Mi Home APP / Xiao Ai Speaker Region Issues

  4. Smart Home First Experience — Apple HomeKit & Xiaomi Mijia (Mijia Smart Camera and Mijia Smart Desk Lamp, HomeKit Setup Tutorial)

If you have any questions or feedback, feel free to contact me.


Buy me a beer

This post was originally published on Medium (View original post), and automatically converted and synced by ZMediumToMarkdown.

Improve this page on Github.

This post is licensed under CC BY 4.0 by the author.