How to make friends Siri and Xiaomi
This article presents a guide to setting up collaboration of Xiaomi fixtures working via Wi-Fi and Bluetooth with Siri using Homebridge. And also described the rake on which I managed to step.
Winter. Cold. Darkness. Getting up in the morning is hard.
They said that dummy lighting could help wake up.
Well then what? I took a nightlight from Xiaomi, or rather Yeelight . He played a little, was upset by the crookedly working software. But bright and colorful lighting added positive, the workplace was transformed, it became more comfortable.
I looked through the catalog, found an LED strip with Wi-Fi connectivity. Compared with analogues, it turned out to be brighter and cheaper. I take it!
And again, software problems. The device falls off, you have to reconnect when you enter the program. Grouping also did not always work correctly, and reaching for the phone to set it up and turn it on is not a thankful task. I did not begin to search and buy additional hubs, I postponed everything until better times.
Colleagues presented a “raspberry” for their birthday, in common language - the Raspberry Pi 3 Model B. It is important that the device has both Wi-Fi and a Bluetooth module. Since I already wrote and studied a lot of materials for working with HomeKit devices, I remembered that you can include unsupported devices into this infrastructure via a homebridge server written in NodeJS that emulates the HomeKit API.
Step 1. Installing the OS
We start by installing the OS. I chose Raspbian as the distribution.
- We take a memory card of 8 GB or more (I came across 16 GB from the company% # & @).
- Format and copy the installer
At this stage, the first problems arose. For some reason, the files were not copied to the card correctly, although formatting using the proprietary SD Memory Card Formatter utility was successful. I was going to take the card to the store and take another, but decided to try before this an external card reader. And, lo and behold - it all worked!
It turned out that the problem was in Mac mini, which did not support SDHC format cards.
- We insert the card into the "raspberry", connect the keyboard, mouse and monitor via HDMI and turn on the power.
The installer is very simple, and the download is fast enough. If you have problems, check the official documentation .
Step 2. Initial OS setup
- We change passwords and set up accounting.
For remote work with Rasperry Pi
- Enable access via SSH and VNC . It is possible through a graphical interface.
Initially, the "Malinka" was powered by a USB port, but at the same time an indication appears on the screen that the power is absolutely not enough. The charger from the iPhone + micro USB-cord solved this problem.
All manipulations were carried out in the office due to the lack of a monitor for the initial setup of the house.
By the weekend, "Malinka" reached the apartment, and her hands were already scratching her hands as soon as possible to connect smart bulbs.
Launched Raspberry ...
Attempt to connect via VNС ... Black screen .
Attempting to connect via SSH ... No reaction .
Waiting for Monday and getting back to work. I connect the keyboard, mouse and monitor.
The system does not stop. Perhaps the problem arose due to damage to the SD card or divine intervention.
Reinstalled the system. I recalled the old quote: “People are divided into two categories:
those who still do not make backups, and those who already do them,” and made a strong-willed decision.
Step 0. Make a backup
There are many ways. I chose the most, in my opinion, simple. Full copy of the SD card image to the USB flash drive.
Create an image
- Find the SD card in the list:
diskutil list
- Save the image to disk:
sudo dd if=/dev/disk2 of=~/Desktop/raspberrypi.dmg
Restore the image
Find the SD card in the list:
diskutil list
Unmount the SD card:
diskutil unmountDisk /dev/disk2
Format the card. It is possible through the utility, it is possible through Terminal:
sudo newfs_msdos -F 16 /dev/disk2
- Restore the previously saved image:
sudo dd if=~/Desktop/raspberrypi.dmg of=/dev/disk2
The disadvantage is a long time to restore the image. Depends on the volume of the card, its speed and the characteristics of the card reader. The process took me about 4 hours.
The advantage is simplicity and reliability.
By the way, you can try to speed up this process. Detailed instructions on
StackExchange .
After a couple of days, I had to perform recovery from backup again. When performing any operation, a "segmentation fault" error occurred. Most likely, the problem was due to a faulty memory card. I bought a new one, this time from Transcend, so far the flight is normal.
Step 3. Installing components
We start with the components that are needed to start the server:
As always, we start by updating the system:
sudo apt-get update
sudo apt-get upgrade
After the upgrade, check the g ++ version:
g++ -v
I had gcc version 6.3.0 , to install homebridge version 4.9+ is needed.
Next, install NodeJS. I installed version 9:
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
sudo apt-get install -y nodejs
The next step is to install Avahi, the library for working with Apple Bonjour services:
sudo apt-get install libavahi-compat-libdnssd-dev
At this stage, the preliminary preparation is completed.
Step 4. Installing homebridge
Now you can follow the basic instructions in the project repository .
Using the npm install -g homebridge command, the installation failed, so we follow the instructions of the author of the project and run the installer with the sudo flag and -g --unsafe-perm homebridge :
sudo npm install -g --unsafe-perm homebridge
Homebridge installed. If you have any problems, you can read the
wiki .
We proceed to configure and connect devices:
- Xiaomi Yeelight Smart Light Strip
- Xiaomi Yeelight Bedside Lamp
Step 5. Yeelight Smart Light Strip
Light Strip is connected via WiFi, its setup is easier. We start by installing the plugin :
sudo npm install -g homebridge-re-yeelight
sudo npm install -g miio@0.14.1
Limit miio version 0.14.1, with a newer plugin it will not work at the moment.
Now we search for devices on the local network. Before that, of course, you need to connect the Yeelight Smart Light Strip via WiFi:
miio --discover
We get approximately the following output to the console:
INFO Discovering devices. Press Ctrl+C to stop.
Device ID: xxxxxxxx
Model info: yeelink.light.strip1
Address: 192.168.1.xxx
Token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx via auto-token
Support: At least basic
We write it down on a piece of paper and proceed to setting up the configuration file.
Step 6. Creating config.json
Create a configuration file. Go to the ~ / .homebridge directory :
touch config.json
nano config.json
Add the hub and the devices we want to control to the configuration file. Typically, device-specific configurations are listed on the plug-in project page, in the config.json file .
In the config, add the following information about homebridge and the device that we received in the previous step.
{
"bridge": {
"name": "Yeebridge",
"username": "18:00:27:40:BC:1B",
"port": 51825,
"pin": "031-45-154"
},
"platforms": [
{
"platform": "ReYeelightPlatform",
"deviceCfgs": [{
"type": "ColorLEDStrip",
"ip": "192.168.1.253",
"token": "5d0b94f872b4372164b6e61d472d424b",
"Name": "LED Strip",
"updatetimer": true,
"interval": 5
}]
}
]
}
- name - the name under which the hub is displayed in the Home application.
- pin - pin code, you will need to connect homebridge to the Home application.
The following is a description of the connected platforms and accessories.
As a platform for WiFi devices from Yeelight, we use ReYeelightPlatform.
Device configuration for this platform:
- type - usually on the project page types are indicated for each supported device.
- ip and token - received in the previous step.
- name - the name to display in the Home application.
Step 7. Connecting Xiaomi Yeelight Bedside Lamp
The device is connected via BLE. That is, you need an installed Bluetooth module with BLE support. To work with such devices, you need another platform - ReYeelightBLEPlatform
Install libraries for working with the Bluetooth stack:
sudo apt-get install libbluetooth-dev libudev-dev
We find the folder with installed node modules:
npm -g root
I got it at / usr / lib / node_modules . We install the necessary rights and perform the installation.
If you do not install the rights and install under sudo, problems may arise in the future:
sudo chown -R $(whoami) /usr/lib/node_modules/
npm install noble
sudo apt-get install libcap2-bin
sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
sudo hciconfig hci0 up
Now install the HomeBridge plugin to support Xiaomi Yeelight Bedside Lamp :
sudo npm install -g homebridge-re-yeelight-ble
Be careful when installing other modules that control devices via Bluetooth, compatibility issues may occur. For example, for a long time I could not figure out why I constantly had the lamp turn off. It turned out that the problem is with the installed homebridge-yeelight . After its removal, normal operation was restored. You might also find this command useful:
sudo npm uninstall -g homebridge-yeelight
We are looking for the device address using:
sudo hcitool -i hci0 lescan
The output might look something like this:
LE Scan ...
8C:26:A6:3D:67:40 (unknown)
8C:26:A6:3D:67:40 (unknown)
FF:B6:36:78:96:A6 RK-M171S
F6:24:41:E8:6B:14 XMCTD_
F6:24:41:E8:6B:14 XMCTD_
76:79:01:46:86:5A (unknown)
76:79:01:46:86:5A (unknown)
D6:64:11:49:E6:8D RK-G200S
F6:BC:36:68:96:A3 (unknown)
D6:66:11:49:E6:8D (unknown)
The desired device is marked as XMCTD_ .
In my case, F6: 24: 41: E8: 6B: 14 XMCTD_
Add a new ReYeelightBLEPlatform and the necessary device with the specified address to config.json .
The resulting config.json will look something like this:
{
"bridge": {
"name": "Yeebridge",
"username": "18:00:27:40:BC:1B",
"port": 51825,
"pin": "031-45-154"
},
"platforms": [
{
"platform": "ReYeelightBLEPlatform",
"defaultValue": {
"F6:24:41:E8:6B:14": "Bedside Lamp"
}
},
{
"platform": "ReYeelightPlatform",
"deviceCfgs": [{
"type": "ColorLEDStrip",
"ip": "192.168.1.253",
"token": "560696f676b4672164b6e61d472d424b",
"Name": "LED Strip",
"updatetimer": true,
"interval": 5
}]
}
]
}
Step 8. Starting homebridge
Launch Homebridge for verification using the homebridge command in the console.
A QR code appears on the screen to add the hub to the Home application. And a little lower - the PIN code from the config.json file , it is used for manual connection.
If we see the coveted line [ReYeelight] [BLE] Timer Started , then most likely you configured the BLE device correctly.
Your Xiaomi Yeelight Bedside Lamp will flash, the connection is established. You must confirm it by pressing the MODE button on the lamp.
At this stage, we have a configured hub and 2 devices connected to it.
- Launch the Home app.
- Click to add an accessory.
- We scan the QR code and agree to connect an uncertified device.
home
For each of the connected devices, you can select a name by which it can be accessed through Siri, and the room in which the device is located.
When configuring / reconfiguring devices, there may be a problem re-adding homebridge to the Home application. To eliminate it, we perform the following operations in the homebridge settings folder.
- Stop the homebridge .
- Delete the accessories and persist directories :
rm -r accessories
rm -r persist
Launch homebridge and add the hub to the Home app again.
For more information on customizing accessories, facilities and scenarios, read on the Apple website .
Step 9. Launch Homebridge when Raspberry Pi boots
After rebooting the device, you must automatically resume the work of Homebridge. To do this, run as a service .
Download two files, homebridge and homebridge.service from the link . We place the homebridge file in / etc / default . The homebridge.service file is placed in / etc / systemd / system .
We make changes to the homebridge.service file . The path ExecStart = / usr / local / bin / homebridge $ HOMEBRIDGE_OPTS is changed to ExecStart = / usr / bin / homebridge $ HOMEBRIDGE_OPTS .
Add the user under which the service will be launched:
sudo useradd --system homebridge
Create a folder for storing the homebridge configuration, copy the configs and set the rights:
sudo mkdir /var/homebridge
sudo cp ~/.homebridge/config.json /var/homebridge/
sudo cp -r ~/.homebridge/persist /var/homebridge
sudo cp -r ~/.homebridge/accessories /var/homebridge
We start the service:
sudo systemctl daemon-reload
sudo systemctl enable homebridge
sudo systemctl start homebridge
Check his status:
systemctl status homebridge
Step 10. Siri. Translation difficulties
Siri is not the easiest girl to talk to.
For example, LED strip should not be called "Table illumination." Siri is trying to perform "Under the Light Has Been," while responding that she cannot close the program.
Also, the lighting device should not be called a "table lamp." If you say “Turn on the desk lamp”, all the lights in the room turn on. When executing the “Turn off the table lamp” command, only the table lamp turns off. Most likely, this is the name for the group, but the lack of sane documentation on Siri does not help to figure it out.
Changing the color of lighting is also a non-trivial task. For example, in English the team sounds and pronounces very simply: “Change light color to green”. In Russian we get: "Set the color of light to green."
Change or change , instead of installing it will not work. Instead of “color of light,” Siri constantly recognizes the command as “light of light,” or “color of color." It's time for a speech therapist. It’s easier to swap words in this phrase, then Siri will have fewer mistakes. As a result, the “stop phrase” sounds like “Set the Light color to blue.” Yes, Sveta in the request will be capitalized!
A small cheat sheet for working with Siri:
Привет, Siri, включи свет.
Привет, Siri, выключи свет.
Привет, Siri, установи яркость света в спальне на 50%.
Привет, Siri, установи цвет света на красный.
A small demo in the end.
Conclusion
For 2 weeks, the server has been working stably and is pleased with the ability to easily manage devices through the Home application. It’s a little frustrating that the work on the plugins is abandoned by the developers, but the source code is on github, and Homebridge has documentation that describes the process of creating your own plugin. In the future, I plan to add new devices and sensors to the Home, as well as automate the process of starting devices at the entrance to the apartment. If you have any questions or suggestions, write in the comments, I will be happy to help and answer your questions.
All good and warm spring! ^ _ ^