Home First Experience with Smart Home - Apple HomeKit & Xiaomi Mijia
Post
Cancel

First Experience with Smart Home - Apple HomeKit & Xiaomi Mijia

First Experience with Smart Home - Apple HomeKit & Xiaomi Mijia

Mijia Smart Camera and Mijia Smart Desk Lamp, Homekit Setup Tutorial

[2020/04/20] Advanced Tutorial Released Experienced users please proceed directly to the advanced tutorial>> Demonstration of using Raspberry Pi as HomeBridge host to connect all Mijia appliances to HomeKit

Miscellaneous:

I recently moved; unlike my previous place where the ceiling had office-style light fixtures that were too bright, the new place has decorative reflective lights that are a bit dim for using the computer or reading. After two weeks, my eyes felt more dry and uncomfortable. Initially, I planned to shop at IKEA, but considering the light color and eye protection, I ultimately chose Xiaomi desk lamps (since I already had a Xiaomi smart camera, all part of the Mijia series).

This Article:

I didn’t particularly check if the products supported Apple HomeKit when purchasing, which is quite a failure as an iOS developer because I didn’t expect Xiaomi to support it.

So, this article will separately introduce Apple HomeKit usage, how to use third-party connections for smart home devices that do not support Apple HomeKit, and how to set up a smart home using Mijia itself (with IFTTT).

You can skip to the sections that suit your device needs.

Purchase:

I bought two desk lamps, one (Pro) for the computer desk and the other for the bedside as a reading lamp.

Mijia Desk Lamp Pro :

NT$ 1,795 supports Mijia, Apple HomeKit

NT$ 1,795 supports Mijia, Apple HomeKit

Mijia LED Smart Desk Lamp :

NT$ 995 only supports Mijia

NT$ 995 only supports Mijia

For detailed introductions, refer to the official website. Both lamps support smart control, color change, brightness adjustment, and eye protection. The Pro version supports Apple HomeKit and three-angle adjustments. So far, I am quite satisfied with the functionality of one lamp. If I had to pick a flaw, it would be that the Pro version’s angle adjustment only allows the base to rotate horizontally, not the lamp itself, which means you can’t adjust the light angle!

Ideal Smart Home Goals:

Current Devices:

  1. Mijia Smart Camera Pan-Tilt Version 1080P (supports: Mijia)
  2. Mijia Desk Lamp Pro (supports: Apple HomeKit, Mijia)
  3. Mijia LED Smart Desk Lamp (supports: Mijia)

Ideal Goals:

When returning home: Automatically turn off the camera (for privacy and to prevent false alarms, as the Mijia app has a bug where the home security alarm cannot be turned on/off according to the set time), and turn on the Pro lamp on the computer desk (to avoid fumbling in the dark). When leaving home: Automatically turn on the camera (default to home security mode) and turn off all lights.

Final Achievement in This Article:

Receive push notifications when leaving or returning home, and trigger operations with a single tap on the phone (with the current devices, it’s not possible to achieve the ideal automation goal).

Smart Home Setup Journey:

Apple HomeKit Usage

*Only for Mijia Desk Lamp Pro! Mijia Desk Lamp Pro! Mijia Desk Lamp Pro!

This is the simplest part because it’s all native functionality.

Only four steps

Only four steps

  1. Find the Home app (if not available, search for “Home” in the App Store and install it)
  2. Open the Home app
  3. Click the “+” in the upper right corner to add an accessory
  4. Scan the HomeKit QR code at the bottom of the Pro lamp to add the accessory!

After successfully adding the accessory, press hard (3D TOUCH) / long press on the accessory to adjust the brightness and color.

What about smart home devices that do not support Apple HomeKit? How to use third-party integration with HomeKit?

Apart from the smart devices that support Apple HomeKit, does it mean that devices that do not support Apple HomeKit cannot be controlled through the Home app at all? This section will guide you step-by-step on how to add unsupported devices (cameras, regular desk lamps) to the “Home” app!

Mac ONLY, Windows users please skip to the section on using Mi Home

My device is MacOS 10.14/iOS 12

Using HomeBridge:

HomeBridge uses a Mac computer as a bridge to simulate unsupported devices as HomeKit devices, allowing them to be added to the “Home” accessories.

Operation Comparison

Operation Comparison

One key point is that you need to keep a Mac computer on to maintain the bridge channel smoothly; once the computer is turned off or goes to sleep, you will not be able to control those HomeKit devices.

Of course, there are also advanced methods online where people buy a Raspberry Pi to use as a bridge; however, this involves too much technical detail and will not be covered in this article.

If you are aware of the drawbacks and still want to try, you can continue reading or skip to the next section on using Mi Home directly.

Step 1:

Install node.js: Click here to download and install it.

Step 2:

Open “Terminal” and enter

1
sudo npm -v

Check if the node.js npm package manager is installed successfully: if the version number is displayed, it means success!

Step 3:

Install the HomeBridge package via npm:

1
sudo npm -g install homebridge --unsafe-perm

After the installation is complete… the HomeBridge tool is installed!

As mentioned earlier, “HomeBridge uses a Mac computer as a bridge to simulate unsupported devices as HomeKit devices,” HomeBridge is just a platform, and each device needs to find additional HomeBridge plugin resources to be added.

It’s easy to find, just google or search on GitHub for “Mi Home product English name homebridge” and you will find many resources; here are two resources for devices I use:

1. Mi Home Camera Pan-Tilt Version Resource: MijiaCamera

Cameras are relatively tricky devices, and I spent some time researching and organizing this; I hope it helps those in need!

First, use “Terminal” to install the MijiaCamera npm package with the command

1
sudo npm install -g homebridge-mijia-camera

After installation, we need to obtain the camera’s network IP address and Token information.

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

Token information is more troublesome and requires you to connect your phone to the Mac:

Open iTunes Interface

Open iTunes Interface

Select backup Do not check Encrypt local backup, and click “Back Up Now.”

After the backup is complete, download and install the backup viewing software: iBackupViewer

Open “iBackupViewer”. The first time you launch it, you will need to go to Mac “System Preferences” -> “Security & Privacy” -> “Privacy” -> “+” -> Add “iBackupViewer”. *If you have privacy concerns, you can disable the network while using this software and remove it after use.

Open “iBackupViewer” again. After successfully reading the backup file, click the top right corner to switch to “Tree View” mode.

On the left side, you will see all the installed apps. Find the Mi Home app “AppDomain-com.xiaomi.mihome” -> “Documents”.

In the document list on the right, find and select the file “number_mihome.sqlite”.

Click the top right corner “Export” -> “Selected”.

Drop the exported sqlite file into https://inloop.github.io/sqlite-viewer/ to view the content.

You can see all the device information fields on the Mi Home app. Scroll to the far right end to find the ZTOKEN field. Double-click to edit, select all, and copy.

Finally, open http://aes.online-domain-tools.com/ to convert ZTOKEN into the final Token.

  1. Paste the copied ZTOKEN into “Input Text” and select “Hex”.
  2. Enter “00000000000000000000000000000000” (32 zeros) in the Key field and select “Hex”.
  3. Click “Decrypt!” to convert.
  4. Select all, copy the blue box in the bottom right corner, and remove spaces to get the final Token.

Token: I tried using “miio” to sniff directly, but it seems that the Mi Home camera firmware has been updated, and this method no longer works to quickly and conveniently obtain the Token!

Back to HomeBridge! Edit the config file config.json

Use “Finder” -> “Go” -> “Go to Folder” -> Enter “~/ .homebridge” to go.

Open “config.json” with a text editor. If this file does not exist, create one yourself or click here to download and place it directly.

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":""
      }
   ]
}

Add the above content to config.json, and input the IP and Token obtained earlier.

Then, go back to the “Terminal” and enter the following command to start HomeBridge:

1
sudo homebridge start

If you have already started it and then changed the config.json content, you can use:

1
sudo homebridge restart

Restart

At this point, a HomeKit QRCode will appear for you to scan and add accessories (steps as mentioned above, the way to add Apple HomeKit devices).

Below will also have status messages: [2019–7–4 23:45:03] [Mi Camera] connecting to camera at 192.168.0.100… [2019–7–4 23:45:03] [Mi Camera] current power state: off

If you see these and no error messages appear, it means the setup is successful!

The most common error is usually an incorrect Token. Just check if there are any omissions in the above process.

Now you can turn the Mi Home Smart Camera on and off from the “Home” APP!

2. Mi Home LED Smart Desk Lamp HomeBridge Resource: homebridge-yeelight-wifi

Next is the Mi Home LED Smart Desk Lamp. Since it does not support Apple HomeKit like the Pro version, we still need to use the HomeBridge method to add it. Although the steps do not require a cumbersome process to obtain IP and Token, it is relatively simpler than the camera, but the desk lamp has its own pitfalls. You need to use another YeeLight APP to pair it and then turn on the local network control setting:

I have to complain about this poor integration; the native Mi Home APP cannot make this setting. So please search for the “ Yeelight “ APP in the APP Store to download and install it.

Open the APP -> Log in directly using the Mi Home account -> Add device -> Mi Home Desk Lamp -> Follow the instructions to rebind the desk lamp to the Yeelight APP.

After the device is bound, go back to the “Device” page -> Click “Mi Home Desk Lamp” to enter -> Click the bottom right “△” Tab -> Click “Local Network Control” to enter the settings -> Turn on the button to allow local network control.

The desk lamp setup is complete here. You can keep this APP to control the desk lamp or rebind it back to Mi Home.

Next is the HomeBridge setup; similarly, open the “Terminal” and enter the command to install the homebridge-yeelight-wifi npm package

1
sudo npm install -g homebridge-yeelight-wifi

After installation, follow the same steps as the camera, go to the ~/.homebridge folder, create or edit the config.json file, and this time just add the following inside the last }:

1
2
3
4
5
6
"platforms": [
   {
         "platform" : "yeelight",
         "name" : "yeelight"
   }
 ]

That’s it!

Finally, combine the above camera config.json file as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
 "bridge": {
  "name": "Homebridge",
  "username": "CC:22:3D:E3:CE:30",
  "port": 51826,
  "pin": "123-45-568"
 },

 "accessories": [
  {
   "accessory": "MijiaCamera",
   "name": "Mi Camera",
   "ip": "",
   "token": ""
  }
 ],

 "platforms": [
   {
         "platform" : "yeelight",
         "name" : "yeelight"
   }
 ]
}

Then go back to the “Terminal” and enter:

1
sudo homebridge start

or

1
sudo homebridge restart

You will see the unsupported Mi Home LED Smart Desk Lamp added to the HomeKit “Home” APP!

And it also supports color and brightness adjustment!

All HomeKit accessories are added, how to make them smart?

After adding and bridging everything, open the “Home” APP again.

Follow the steps to add a scene scenario, here using “Going Home” as an example:

Click the “+” in the upper right corner -> Add Scenario -> Custom -> Enter the accessory name yourself (EX: Going Home) -> Click “Add Accessory” at the bottom -> Select the HomeKit accessories that have been connected -> Set the accessory status for this scene (Camera: Off / Desk Lamp: On) -> You can click “Test Scenario” to test -> Click “Done” in the upper right corner!

Now the scene is set! At this point, clicking the scene on the homepage will execute the settings for all the accessories inside!

There is also a quick tip, which is to directly click the house-shaped button in the pull-up control menu to quickly operate HomeKit/execute scenarios (you can switch modes in the upper right corner)!

Now that we have the intelligence, how do we automate it?

Now that we have the intelligence, I want to achieve the ultimate goal: automatically turn off the camera and turn on the lights when I get home; automatically turn on the camera and turn off the lights when I leave home.

Switch to the third tab “Automation” to set it up. Unfortunately, I don’t have any of the aforementioned devices (iPad/Apple TV/HomePod) to act as a Home Hub, so I haven’t researched this part.

The principle seems to be that when you get home, the “Home Hub” detects your phone/watch and triggers it accurately!

Here I found a tricky method: (GPS sensing)

By using a third-party app to connect to “Home” and add automation settings, you can use your phone’s GPS to achieve automation and unlock the “Automation” tab’s functionality.

p.s. GPS has an error margin of about 100 meters.

The third-party app I used is: myHome Plus

Download & install the app -> Open the app -> Allow access to “Home Data” -> You will see the data configuration of “Home” -> Click the “Settings button” in the upper right corner -> Click “My Home” to enter -> Scroll down to the “Triggers” area -> Click “Add Trigger”

Select “Location” as the trigger type -> Enter a name (EX: Going Home) -> Click “Set Location” to set the location area -> Then in REGION STATUS, you can set whether to enter or leave the area -> Finally, in SCENES, you can choose the corresponding “scenario” to execute (created above).

After clicking “Done” in the upper right corner to save, go back to the “Home” app, and you will see that the “Automation” tab is now available!

At this point, you can click the “+” in the upper right corner to directly add automation scripts using the “Home” app!

The steps are similar to the third-party app, but with better integration! After creating the automation using the native “Home” app, you can also swipe to delete the one created with the third-party app.

!! Just note that you need to keep at least one; otherwise, the tab will revert to its original locked state!!

Siri Voice Control:

Compared to the Mi Home introduced below, HomeKit has a high level of integration and can directly use voice control for the set accessories and execute scenes without additional settings.

This concludes the introduction to HomeKit settings. Next, let’s explain how to use Mi Home’s native smart home features.

Using Mi Home to build a smart home:

Here I encountered a confusing point: I couldn’t find the same Mi Home desk lamp in the list of new devices in Mi Home. The answer is:

Just look at the text, this is it

Just look at the text, this is it

Other devices: For the camera and Pro desk lamp, just follow the official instructions to add them, no need to elaborate here.

Scene Scenario Settings:

Similar to the “Home” setup -> Switch to the “Smart” tab -> Select “Manual Execution” -> Choose device operation at the bottom (since it’s native, you can choose more functions) -> Continue to add other devices (desk lamp) -> Click “Save” to complete!

Someone might ask why not just choose “leave or arrive at a place”? Because this function is useless, the app is not optimized for Taiwan’s GPS, which is wrong, and its positioning can only be set on landmarks. If your location has that, you can directly use this function. You can skip the rest of the article!

Fun fact: All maps of China in Google Maps are wrong!

For the quick switch part, you can set the widget from “My” -> “Widgets”!

This way, you can quickly execute scenes and devices from the notification center!

You can also control the widget from Apple Watch! *If the watch app keeps showing blank, please delete and reinstall the watch or phone app. This app has quite a few bugs.

Now that we have the intelligence, how do we automate it?

Here, we still need to use the GPS sensing method. If the scene added above is “leave or arrive at a place”, you can skip the following settings!

* * * * *

[2019/09/26] Update iOS ≥ 13 to achieve automation using only the built-in Shortcuts app:

iOS ≥ 13.1 Use the “Shortcuts” automation feature with Mi Home smart home, click to view»

* * * * *

iOS ≥ 12, iOS < 13 Only:

Use the built-in Shortcuts app with IFTTT

First, go to “My” -> “Experimental Features” -> “iOS Shortcuts” -> “Add Mi Home scenes to Shortcuts”

Open the system-built “ Shortcuts “ app (if you can’t find it, please search and download it from the App Store)

Click the “+” in the upper right corner to create a shortcut -> Click the settings button below the upper right corner -> Name -> Enter a name (it is recommended to use English, because you will use it later)

Return to the new shortcut page -> Enter “Mi Home” in the search menu below -> Add the corresponding scene set in Mi Home, and turn off “Show When Run” otherwise it will open the Mi Home app after execution.

*If you can’t find Mi Home, please go back to the Mi Home app and try to toggle “My” -> “Experimental Features” -> “iOS Shortcuts” -> “Add Mi Home scenes to Shortcuts”, and restart the “Shortcuts” app.

At this time, we need to use a third-party app again. We use IFTTT to create a GPS entry and exit background trigger. Search for “ IFTTT “ in the App Store to download and install.

Open IFTTT, log in to your account, switch to the “My Applets” tab, click the “+” in the upper right corner to add -> Click “+this” -> Search for “Location” -> Choose whether to enter or leave

Set the location -> Click “Create trigger” to confirm -> Then click “+that” below -> Search for “notification”

Choose “Send a rich notification from the IFTTT app”:

Title = Notification title, Message = Notification content

Link URL, please enter: shortcuts://run-shortcut?name= Shortcut name

So it’s recommended to set the shortcut name in English

-> Click “Create action” -> You can click “Edit title” to set the name

-> “Finish” save completed!

You will receive a triggered notification the next time you leave/enter the set area range (with an error range of about 100 meters). Clicking the notification will automatically execute the Mi Home scene!

Clicking the notification will automatically execute the scene in the background

Clicking the notification will automatically execute the scene in the background

For Siri voice control:

Since Mi Home is not an Apple built-in app, you need to set it up separately to support Siri voice control:

In the “Smart” Tab -> “Add to Siri” -> Select “Target Scene” and click “Add to Siri”

-> Click the red record command (EX: turn off the light) -> Done!

You can directly call and control the scene execution in Siri!

Summary

To summarize the above setup steps:

For a good experience, you need to spend a lot of money to buy appliances with the HomeKit logo (so you don’t need to keep a Mac running HomeBridge, and it integrates perfectly with the native Apple Home function). You also need to buy a HomePod or Apple TV, or iPad as the home hub; both HomeKit appliances and home hubs are not cheap!

If you have technical skills, you can consider using third-party smart devices (such as Mi Home) with a Raspberry Pi to run HomeBridge.

If you are an ordinary person like me, it is still most convenient to use Mi Home directly. Currently, my usage habit is to execute scene operations from the notification center shortcut widget when coming home or leaving home; the Shortcuts app with IFTTT is only used for notification reminders, in case I forget sometimes.

Although the current experience has not reached the ideal goal, it has already taken a step closer to a “smart home”!

Advanced

Demonstration of using Raspberry Pi as a HomeBridge host to connect all Mi Home appliances to HomeKit

Further Reading

  1. New additions to Xiaomi smart home (AI speaker, temperature and humidity sensor, scale 2, DC inverter fan)
  2. iOS ≥ 13.1 using “Shortcuts” automation function with Mi Home smart home (directly using the built-in Shortcuts app in iOS ≥ 13.1 to complete automation operations)
  3. Mi Home APP / Xiao Ai speaker region issues

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

===

本文中文版本

===

This article was first published in Traditional Chinese on Medium ➡️ View Here


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

AirPods 2 Unboxing and Hands-On Experience

Apple Watch Case Unboxing Experience (Catalyst & Muvit)