Xiaomi wireless devices in the smart home ioBroker

Greetings to all home automation lovers. I decided to share the experience of using Xiaomi wireless devices with ZigBee interface. I, frankly, oppose the use of any wireless devices in any automation, from serious large-scale process control systems to small automation systems like fire alarm systems or smart homes, but Xiaomi solutions bribed with low cost, availability, excellent design and many positive reviews from users, I decided to try.
 3r33655.
 3r33655. This post should be taken as step-by-step instructions for integrating ZigBee devices into the infrastructure of a smart home. Described here is by no means an axiom and you can find many other ways to connect ZigBee devices. If you still skip the detailed description, you can make an impression of the complexity or ease of combining devices from different manufacturers into one local platform using the example of ZigBee and ioBroker (more on this later). I will tell in this article how to connect devices to a smart home, display information from them on a tablet or simply in a browser and send messages via telegrams about changing the state of devices. If I am interested in you, then I ask for cat.
 3r33655. github Download the firmware (the file is called CC2530ZNP-Pro-Secure_LinkKeyJoin.hex for this device) and the flash-programmer, after which the necessary drivers are added to the system.
 3r33655. When a debugger is connected to a USB port of a computer, the program will immediately display the connected device. You only need to specify the path to the firmware file and click the “Perform Actions” button
 3r33655.
 3r33655. Xiaomi wireless devices in the smart home ioBroker
 3r33655.
 3r33655. The ZigBee module with P03 (Rx) and P02 (Tx) ports is connected to UART4 (in OS as ttyS4) cubietruck boards, 3V3 power supply, GND picked up on neighboring pin. For stable operation, it is still necessary to pull the ports of the coordinator P2? P? P5 to the ground. As I wrote above, I use the Armbian OS, the UART port is activated very simply, using the command armbian-config in section 3r3488. System [/b] - 3r3488. Hardware [/b] need to activate the desired port and reboot the system.
 3r33655.
 3r33655.
 3r33655.
 3r33655. The zigbee driver is added from the admin with one click.
 3r33655.
 3r33655.
 3r33655.
 3r33655. In my case, the coordinator is connected to port /dev /ttyS4 (as I wrote above), we specify it in the settings.
 3r33655.
 3r33655.
 3r33655.
 3r33655. The remaining settings can be left as default. After the first launch of the driver, you need to pair (add) devices. Complete instructions on github , pairing through this driver is a bit more complicated than through a native application, but I have not had any problems.
 3r33655.
 3r33655. So, for example, let's add the Xiaomi button (Mijia series), to do this, press the green button in the driver settings and, following the instructions, first hold down the pairing button on the back side with a paper clip until the LED starts blinking, then click this button for about 2 seconds, see mating progress.
 3r33655.
 3r33655.
 3r33655.
 3r33655. My apartment is not large, the connection with all devices is stable, even with the door opening sensor on the landing (RC wall 100 mm and distance 5 m in a straight line). The problems started when I decided to add a temperature and humidity sensor for outdoor air, which I installed on the outer wall of the house from the side of the insulated loggia. The signal of a weak sensor, which was also on the street, did not reach the automation cabinet. You can solve the problem simply - you need to add a router to the ZigBee network and place it closer to the sensor. Some wireless devices, for example, the Xiaomi socket can work as a router, but I did not have such devices. I didn’t want to buy an expensive outlet or a controlled light bulb only for “forwarding” data from a sensor on the street. As it turned out, for the same terminal devices based on the CC25xx chip there is a special firmware that allows them to be used as a router in the system. As a result, I added a router based on the CC2531 chip with a USB connection. I will not dwell on the firmware process in detail, the diagram and the firmware file itself can be found on page github project.
 3r33655.
 3r33655. As a matter of fact, there is no communication with the module via the USB port, I temporarily plugged it into charging with the USB port and plugged it into a power outlet in the kitchen. In the near future I plan to locate permanently in a normal building on the loggia and with normal power from a home UPS.
 3r33655.
 3r33655.
 3r33655.
 3r33655. The process of adding a router to the system is simple: we press the pairing button in the driver and on the switched on router we press the S2 button several times until the devices are connected.
 3r33655.
 3r33655.
 3r33655.
 3r33655. For example, let's add a temperature /humidity sensor that I placed on the loggia outside and that should work through a router.
 3r33655.
 3r33655. You can simply do the pairing through the driver and the sensor should connect through the router, if it is closer. But you can forcibly indicate that the pairing goes through a specific router. To do this, in the list of devices on the router's icon, press the green pairing button.
 3r33655.
 3r33655. 3r33333.
 3r33655.
 3r33655. Make sure that the sensor is correctly connected - let's see the network map.
 3r33655.
 3r33655.
 3r33655.
 3r33655. The map shows device pairing lines with an indication of the signal quality between network segments. As I wrote above, I have a small odnushka of 35 square meters, so the map is rather modest. With the permission of other users, I will publish possible options with a larger map.
 3r33655.
 3r33655.
 3r33655.
 3r33655.
 3r33655.
 3r33655.
 3r33655.
 3r33655. The functionality of some Xiaomi devices through this adapter is even slightly larger than through the native application and the mihome driver. In the future I want to expand the network a little and try new devices, in particular, a smart drive for curtains and a sensor cube.
 3r33655.
 3r33655.

Driver Material


 3r33655. We obtained the data, the executive mechanisms were tied, what now to do with them? To begin, let's display in a beautiful interface. I have a big project in the VIS driver, which exists in several versions for different resolutions, but there is enough material for a separate article. Maybe she will be next.
 3r33655.
 3r33655. For simple and quick display of data and control of various devices, I use the driver material. Installation as usual in a couple of clicks. Interface tiles display data based on category settings, which are added to the interface admin window of the same name. The list of categories is limited only by fantasy, I use the “room-function” scheme, and grouping takes place according to them. Added all rooms (kitchen, corridor, room, balcony, etc.) and functions (lighting, sensors, system, etc.).
 3r33655.
 3r33655. 3r33360.
 3r33655.
 3r33655. Now in the settings window of the system objects, it is necessary for the variables that will be displayed on the screen, specify the room, the function and assign the role (for the correct display). An example is a Xiaomi room wall temperature and humidity sensor connected via Bluetooth.
 3r33655.
 3r33655.
 3r33655.
 3r33655. Based on these settings, the tile will display the data, the icon will be pulled, and objects of a certain type will have control available.
 3r33655.
 3r33655. Example 1. Display all information on one category - functions. Lighting throughout the apartment.
 3r33655.
 3r33655.
 3r33655.
 3r33655. Example 2. The output of all the information on the room - Bathroom.
 3r33655.
 3r33655. 3r33395.
 3r33655.
 3r33655. Example 3. Voltage and battery level of all Xiaomi wireless devices.
 3r33655.
 3r33655. 3r3406.
 3r33655.
 3r33655. This driver works for me for local control of the system from a mobile phone, a stationary tablet on the wall. Sometimes I connect via VPN. To control and view the states remotely, receive notifications, I use the telegram driver.
 3r33655.
 3r33655.
Driver Telegram 3r34040.
 3r33655. I will not describe the installation, just go over the settings. I use the mode of operation through periodic polling (by default, 300ms) and connection through a proxy server. To get the token, you need a little "talk" with the bot creator - BotFather. The process is simple - find the search for this bot, give the command to create a new one, specify its unique name and your key, specify it in the driver settings and, for security, be sure to specify the “welcome” password. His bot will ask you when communicating with a new user.
 3r33655.
 3r33655. 3r33434.
 3r33655.
 3r33655. Now you need to configure communication cases through the bot. For this you can use the text2command driver or jаvascript. So historically, I use jаvascript in both text and Blockly blocks. Installing the JS driver should not cause difficulties, the setting in this case is not needed. After installation and launch, you need to enable the display of the menu to create and edit scripts.
 3r33655.
 3r33655. 3r33434.
 3r33655.
 3r33655. Example 1. Alerts.
 3r33655.
 3r33655. To begin, let's try to send a notification of the opening, for example, the front door. I have a sensor-reed wireless Xiaomi on the front door. Monitor as a wife walks with small while I'm at work. Create a new script in the common group.
 3r33655.
 3r33655.
 3r33655.
 3r33655. Specify what will “draw” in blockly and call it “telegram_bot”.
 3r33655.
 3r33655.
 3r33655.
 3r33655. In the "Events" group, we take the block of reaction to changing a variable and drag it to the working field.
 3r33655.
 3r33655. 3rr3465.
 3r33655.
 3r33655. Next, we select the object ID for which we subscribe, we insert the object check through “if” - “otherwise if” to the value true /false. In the end, you should get something like the following.
 3r33655.
 3r33655. 3r37474.
 3r33655.
 3r33655. Great, now run, open the door - close the door and see the messages in the telegram.
 3r33655.
 3r33655.
 3r33655.
 3r33655. Example 2. Lighting control through the menu.
 3r33655. An example is more complicated, let's make the lighting control using the menu buttons in the telegram. Here you’ll have to give a little bit of time When creating a script, you need to select JS. The task is something like this: make the buttons in the form of a menu with the status of the lights immediately in the text of the buttons. Still try to make sure that when you press the button, the state of lighting is inverted and the status is immediately updated in the text of the button by the appearance /disappearance of the light bulb. Plus, if the menu has already been started and manually turned on /off the light, it is necessary that the menu is also updated with the status of the lights.
 3r33655.
 3r33655. I’ve got the following code:
 3r33655.
 3r33655. 3r?500. 3r3501. //Signs of the menu buttons and the ID of the lighting control channels 3r3655. const LIGHT1 = 'Room night', CH_LIGHT1 = 'mqtt.0.PLC55_Lighting.lighting.MainRoom_main1'; 3r33655. const LIGHT2 = 'Room base', CH_LIGHT2 = 'mqtt.0.PLC55_Lighting.lighting.MainRoom_main2'; 3r33655. const LIGHT3 = 'Room Sub', CH_LIGHT3 = 'mqtt.0.PLC55_Lighting.lighting.MainRoom_sec'; 3r33655. const LIGHT4 = 'Kukh light', CH_LIGHT4 = 'mqtt.0.PLC55_Lighting.lighting.Kitchen_main'; 3r33655. const LIGHT5 = 'Hot sub 1', CH_LIGHT5 = 'mqtt.0.PLC55_Lighting.lighting.Kitchen_sec_top'; 3r33655. const LIGHT6 = 'Hot sub 2', CH_LIGHT6 = 'mqtt.0.PLC55_Lighting.lighting.Kitchen_sec_bottom'; 3r33655. const LIGHT7 = 'Bath base', CH_LIGHT7 = 'mqtt.0.PLC55_Lighting.lighting.BathRoom_main'; 3r33655. const LIGHT8 = 'Core DSP', CH_LIGHT8 = 'mqtt.0.PLC55_Lighting.lighting.Hall_main'; 3r33655. const LIGHT9 = 'Balcony ’, CH_LIGHT9 =' mqtt.0.PLC55_Lighting.lighting.Balcon_main '; 3r33655. 3r33655. //Sending the menu itself to the telegrams
function sendLightMenu (message_text) {3r3655. //Get the current status of the luminaires
var l1 = getState (CH_LIGHT1) .val? '': ''; 3r33655. var l2 = getState (CH_LIGHT2) .val? '': ''; 3r33655. var l3 = getState (CH_LIGHT3) .val? '': ''; 3r33655. var l4 = getState (CH_LIGHT4) .val? '': ''; 3r33655. var l5 = getState (CH_LIGHT5) .val? '': ''; 3r33655. var l6 = getState (CH_LIGHT6) .val? '': ''; 3r33655. var l7 = getState (CH_LIGHT7) .val? '': ''; 3r33655. var l8 = getState (CH_LIGHT8) .val? '': ''; 3r33655. var l9 = getState (CH_LIGHT9) .val? '': ''; 3r33655. //Send the menu
sendTo ('telegram.0', {3r336565. text: message_text, 3r336565. reply_markup: {3r33655. keyboard:[
[LIGHT1+l1, LIGHT2+l2, LIGHT3+l3],
[LIGHT4+l4, LIGHT5+l5, LIGHT6+l6], 3r33655.[LIGHT7+l7, LIGHT8+l8, LIGHT9+l9]
true
}
}); 3r33655.}
//Changing the state of lighting
function changeLight (room, channel) {
//Check the availability of the lighting controller
var alive = getState ("mqtt.0.info.connection"). val; 3r33655. if (alive.includes ("PLC55_Lighting")) {3r336565. if (getState (channel) .val) {3r336565. setState (channel, false, false, function () {3r3655. sendLightMenu (room + 'Off'); 3r3655.}); 3r33655.} else {
setState (channel, true, false, function () {3r3655. sendLightMenu (room + 'ON'); 3r3655.}); 3r33655.}
} else {
sendLightMenu ('Lighting Controller OFFLINE'); 3r33655.}
}
//Subscribe to the request variable of the telegram driver, any changes without confirmation
on ({id: "telegram.0.communicate.request", ack: false, change: 'any'}, function (obj) {3r3655.5. var msg = obj.state.val; 3r3655.5 var command = obj.state .val.substring (obj.state.val.indexOf (']') + 1);
var user = obj.state.val.substring (obj.state.val.indexOf ('[')+1,obj.state.val.indexOf(']'));
. var chat_id = getState ("telegram.0.communicate.requestChatId"). val; 3r3655. var message_id = getState ("telegram.0.communicate.requestMessageId"). val; 3r3655. var handled = false; 3r3655. ////Displaying menus by typical initial commands
if (command == '/start' || command == '/menu' || command == 'menu' || command == 'Menu') {
SendLightMenu ("Menu "); 3r33665. Handled = true; 3r336655.} 3r33665. //####################### Lighting menu and commands ###### ##########
If (command == LIGHT1 || command == LIGHT1 + '') {
ChangeLight (LIGHT? CH_LIGHT1);
Handled = true;
}
if (command == LIGHT2 || command == LIGHT2 + '') {
changeLight (LIGHT? CH_LIGHT2); 3r33655. handled = true; 3r33655.}
if (command == LIGHT3 || command == LIGHT3 + '') {
changeLight (LIGHT? CH_LIGHT3); 3r33655. handled = true; 3r33655.}
if (command == LIGHT4 || command == LIGHT4 + '') {
changeLight (LIGHT? CH_LIHT4); 3r33655. handled = true; 3r33655.}
if (command == LIGHT5 || command == LIGHT5 + '') {
changeLight (LIGHT? CH_LIGHT5); 3r33655. handled = true; 3r33655.}
if (command == LIGHT6 || command == LIGHT6 + '') {
changeLight (LIGHT? CH_LIGHT6); 3r33655. handled = true; 3r33655.}
if (command == LIGHT7 || command == LIGHT7 + '') {
changeLight (LIGHT? CH_LIGHT7); 3r33655. handled = true; 3r33655.}
if (command == LIGHT8 || command == LIGHT8 + '') {
changeLight (LIGHT? CH_LIGHT8); 3r33655. handled = true; 3r33655.}
if (command == LIGHT9 || command == LIGHT9 + '') {
changeLight (LIGHT? CH_LIGHT9); 3r33655. handled = true; 3r33655.}
//If the command did not match any of the options, then pass it to the text2command
driver. if (! handled) {
sendTo ('text2command.0', {
text: command.replace (///g, '#'). replace (/_ /g, ''),
id: chat_id, 3r33655. user: user
.}, function (response) {
if (response && response.response) {
sendTo ('telegram.0', {user: user, text: response.response});
}
}); 3r33655.}
}); 3r33655. //If you manually switched the lights, just refresh the menu 3r33655. on ({id: /^mqtt.0.PLC55_Lighting.lighting*$/, ack: true, change: 'ne'}, function (obj) {3r3655.5 sendLightMenu ("Manual control"); 3r3655.}) ; 3r33655. //When restarting a driver or script, immediately send the menu 3r33655. sendLightMenu ("Menu"); 3r33655.
 3r33655. The result should be something like this:
 3r33655.
 3r33655. 3r3634.
 3r33655.
 3r33655.
Conclusion 3r34040.
 3r33655. The article turned out to be a long one, but I hope it is useful, and maybe it will make life easier for some users or encourage someone to create their smart home.
 3r33655. At the moment, my ioBroker-based smart home system has been “spinning” for 4 years now and I’m quite happy with it. In addition to ZigBee, it also has several self-made MQTT and HTTP controllers for lighting, ventilation and other systems, a 1-wire temperature sensor network, an RS485 electrical network parameter monitoring device and a modbus RTU protocol, and much more.
 3r33655.
 3r33655. In my piggy bank of solutions for a smart home, many ideas have accumulated, although they probably should be perceived rather as a piggy bank of problems for a smart home (those in the subject will understand what I mean). Leave your wishes in the comments to help me decide on the topic of the next article. 3r3656. 3r33655. 3r33655. 3r33655.
! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r3654. 3r33655. 3r3656.
+ 0 -

Add comment