1
0
mirror of https://github.com/balkian/balkian.github.com.git synced 2024-11-05 03:21:42 +00:00
balkian.github.com/controlling-zigbee-devices-with-mqtt.html

269 lines
14 KiB
HTML
Raw Normal View History

2019-01-08 17:22:33 +00:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>balkian.com - Controlling Zigbee devices with MQTT
</title>
<meta name="viewport" content="width=device-width">
<!-- syntax highlighting CSS -->
<link rel="stylesheet" href="/theme/css/solarized.css">
<!--<link href="/css/bootstrap.css" rel="stylesheet">-->
<link rel="stylesheet" href="/theme/font-awesome/css/font-awesome.min.css">
<!--<link rel="stylesheet" href="/css/bootstrap-responsive.min.css">-->
<!-- Custom CSS -->
<link rel="stylesheet" media="only screen" href="/theme/css/main.css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300|Comfortaa' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="container" class="container">
<div id="contentwrapper">
<div id="content">
<header id="header">
<ul class="navbar" id="navbar">
<a href="/">
<li >
<i class="fa fa-home fa-large"></i>
</li></a>
<a href="/category/cheats.html"><li >
Cheats
</li></a>
<a href="/category/misc.html"><li class="active"
>
Misc
</li></a>
<a href="/tags.html"><li >
Tags
</li></a>
<a href="/pages/projects.html"><li >
Projects
</li></a>
<a href="//jfernando.es" target="_blank"><li>
CV
</li></a>
</ul>
</header>
<!--Body content-->
<div class="postnav">
<span class="previouspost"><i class="fa fa-chevron-left"></i> <a href="hdmi-vga-adapter-issues-with-raspberry-pi.html">HDMI-VGA adapter issues with Raspberry Pi</a></span>
</div>
<div class="posthead">
<h2 class="title">Controlling Zigbee devices with MQTT</h2>
<span class="meta date">2019-01-06</span>
<span class="tag"><a href="/tag/mqtt.html" class="label label-default">mqtt</a></span>
<span class="tag"><a href="/tag/iot.html" class="label label-default">iot</a></span>
<span class="tag"><a href="/tag/zigbee.html" class="label label-default">zigbee</a></span>
</div>
<div class="post">
<p>This is a short tutorial on connecting a zigbee device (an Aqara cube) to an MQTT server, so you can control your zigbee devices from the network.</p>
<p>If you're anything like me, you're probably a sucker for IoT devices.
For a long time, I've been using WiFi-enabled lights, and Amazon dash buttons to control them.
To keep these (cheap Chinese) internet enabled devices away from your network and their respective cloud services, you'll probably want to set up a dedicated network in your router (more on this on a future post, maybe).
Another disadvantage of WiFi devices is that they're relatively power hungry.</p>
<p>A popular alternative is using ZigBee for communication.
It is a dedicated protocol similar to bluetooth (BLE), with lower power requirements and bitrate.</p>
<p>Take the (super cute) aqara cube as an example.
It is a small cube that detects rotation on all of its axes, and tapping events.
Here's a video:</p>
<div class="youtube youtube-16x9"><iframe src="https://www.youtube.com/embed/5YtqG1wEnng" allowfullscreen seamless frameBorder="0"></iframe></div><p>To connect to zigbee devices you will need a zigbee enabled gateway (a.k.a. hub), which connects to your WiFi network and your zigbee devices.
Once again, this means adding an internet-enabled device to your home, and probably a couple of cloud services.</p>
<p>As an alternative, you can set up your own zigbee gateway, and control it to your home automation platform of choice (e.g. home assistant).
We will cover how to set up a zigbee2mqtt gateway that is also connected to an MQTT server, so you can use MQTT to control your devices and get notifications.</p>
<p>What you need:</p>
<ul class="simple">
<li><a class="reference external" href="https://www.aliexpress.com/item/Original-Xiaomi-Mi-Aqara-Cube-Smart-Home-Controller-6-Action-Operation-Fr-Home-Device-Zigbee-Version/32892947622.html?spm=a2g0s.9042311.0.0.3da24c4dXV8sBI">Aqara cube</a>.</li>
<li><a class="reference external" href="https://www.aliexpress.com/item/Wireless-Zigbee-CC2531-CC2540-Zigbee-Sniffer-Bluetooth-BLE-4-0-Dongle-Capture-Module-USB-Programmer-Downloader/32907587711.html?spm=a2g0s.9042311.0.0.3da24c4dXV8sBI">CC2531 zigbee sniffer</a>.</li>
<li><a class="reference external" href="https://www.aliexpress.com/item/CFSUNBIRD-CC-DEBUGGER-Debugger-and-Programmer-for-RF-System-on-Chips-TI-ORIGINAL-Fast-hipping/32813122315.html?spm=a2g0s.9042311.0.0.3da24c4dXV8sBI">CC-debugger</a>.</li>
</ul>
<p>You will need to flash your sniffer.
For that, you only need to follow the instructions from the <a class="reference external" href="https://koenkk.github.io/zigbee2mqtt/">zigbee2mqtt documentation</a>.</p>
<p>Once you're done flashing, you're ready to set up the zigbee2mqtt server.
For convenience, I wrote a simple docker-compose to deploy a zigbee2mqtt server and a test mosquitto server:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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</pre></div></td><td class="code"><div class="highlight"><pre><span></span>version: &#39;2.1&#39;
services:
zigbee2mqtt:
image: koenkk/zigbee2mqtt
container_name: zigbee2mqtt
restart: always
volumes:
- ./z2m-data/:/app/data/
devices:
- &quot;/dev/ttyACM0&quot;
networks:
- hass
mqtt:
image: eclipse-mosquitto
ports:
- 1883:1883
- 9001:9001
networks:
- hass
volumes:
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf
hass:
image: homeassistant/home-assistant
ports:
- &quot;8123:8123&quot;
networks:
- hass
volumes:
- ./hass-config:/config
- &quot;/etc/localtime:/etc/localtime:ro&quot;
networks:
hass:
driver: overlay
</pre></div>
</td></tr></table><p>You can test your installation with:</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6</pre></div></td><td class="code"><div class="highlight"><pre><span></span> mosquitto_sub -h localhost -p <span class="m">1883</span> -t <span class="s1">&#39;zigbee2mqtt/#&#39;</span>
online
<span class="o">{</span><span class="s2">&quot;battery&quot;</span>:17,<span class="s2">&quot;voltage&quot;</span>:2925,<span class="s2">&quot;linkquality&quot;</span>:149,<span class="s2">&quot;action&quot;</span>:<span class="s2">&quot;rotate_right&quot;</span>,<span class="s2">&quot;angle&quot;</span>:12.8<span class="o">}</span>
<span class="o">{</span><span class="s2">&quot;battery&quot;</span>:17,<span class="s2">&quot;voltage&quot;</span>:2925,<span class="s2">&quot;linkquality&quot;</span>:141,<span class="s2">&quot;action&quot;</span>:<span class="s2">&quot;slide&quot;</span>,<span class="s2">&quot;side&quot;</span>:2<span class="o">}</span>
<span class="o">{</span><span class="s2">&quot;battery&quot;</span>:17,<span class="s2">&quot;voltage&quot;</span>:2925,<span class="s2">&quot;linkquality&quot;</span>:120<span class="o">}</span>
<span class="o">{</span><span class="s2">&quot;battery&quot;</span>:17,<span class="s2">&quot;voltage&quot;</span>:2925,<span class="s2">&quot;linkquality&quot;</span>:141,<span class="s2">&quot;action&quot;</span>:<span class="s2">&quot;wakeu</span>
</pre></div>
</td></tr></table><p>zigbee2mqtt supports the following events for the aqara cube: shake, wakeup, fall, tap, slide, flip180, flip90, rotate_left and rotate_right.
Every event has additional information, such as the sides involved, or the degrees turned.</p>
<p>Now you are ready to set up home assistant support in zigbee2mqtt following <a class="reference external" href="https://koenkk.github.io/zigbee2mqtt/integration/home_assistant.html">this guide</a>.</p>
</div>
</div>
<div id="sidebar" >
<div id="badge" class="flip-container" ontouchstart="this.classList.toggle('hover');">
<div class="flipper sticky">
<div class="front">
<!-- front content -->
<img id="avatar" src="/theme/img/me.png">
</div>
<div class="back">
<!-- back content -->
<img id="picture" src="/theme/img/me-bat.png">
</div>
</div>
</div>
<div class="entries">
<h2 class="title">Latest entries</h2>
<dl>
<dt><a href="/controlling-zigbee-devices-with-mqtt.html">Controlling Zigbee devices with MQTT</a></dt>
<dd class="tag"><a class="label" href="/tag/mqtt.html">mqtt</a></dd>
<dd class="tag"><a class="label" href="/tag/iot.html">iot</a></dd>
<dd class="tag"><a class="label" href="/tag/zigbee.html">zigbee</a></dd>
<dt><a href="/hdmi-vga-adapter-issues-with-raspberry-pi.html">HDMI-VGA adapter issues with Raspberry Pi</a></dt>
<dd class="tag"><a class="label" href="/tag/rpi.html">rpi</a></dd>
<dt><a href="/pptp-connections.html">PPTP connections</a></dt>
<dd class="tag"><a class="label" href="/tag/openwrt.html">openwrt</a></dd>
<dd class="tag"><a class="label" href="/tag/linux.html">linux</a></dd>
<dd class="tag"><a class="label" href="/tag/router.html">router</a></dd>
<dt><a href="/progress-bars-in-python.html">Progress bars in python</a></dt>
<dd class="tag"><a class="label" href="/tag/python.html">python</a></dd>
<dt><a href="/arch-linux-fixes.html">Arch Linux Fixes</a></dt>
<dd class="tag"><a class="label" href="/tag/arch.html">arch</a></dd>
<dd class="tag"><a class="label" href="/tag/linux.html">linux</a></dd>
</dl>
</div>
<div class="about" style="clear:both;">
<!-- <h2 class="title">About me</h2> -->
<div class="icons">
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa-stack-2x" ></i><i class="fa fa-stack-1x fa-linux"></i></span><span class="explanation">Linux lover</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-android"></i></span><span class="explanation">Android dev</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-github-alt"></i> </span><span class="explanation">GitHub user</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-stack-exchange"></i> </span><span class="explanation">StackExchange fan</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-music"></i> </span><span class="explanation">Music lover</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-film"></i> </span><span class="explanation">Movie fan</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-comments"></i> </span><span class="explanation">Always on IM</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-laptop"></i> </span><span class="explanation">Always on a PC</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-pencil"></i> </span><span class="explanation">Occassional writter</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-moon-o"></i> </span><span class="explanation">Night owl</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-terminal"></i></span> <span class="explanation">CLI user</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-flag"></i></span> <span class="explanation">Linguist wannabe</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-code"></i> </span> <span class="explanation">Love to code</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-book"></i></span> <span class="explanation">Keen reader</span></span>
<span class="coolicon"><span class="fa fa-stack"> <i class="fa fa-square-o fa fa-stack-2x" ></i><i class="fa fa-quote-right"></i></span> <span class="explanation">Quotes on demand</span></span>
</div>
</div>
<!--Sidebar content-->
</div> <div class="clear"></div>
</div>
<footer role="contentinfo">
<div class="contact">
<p>
J. Fernando Sánchez Rada | balkian
</p>
</div>
<ul id="social">
<li><a href="http://github.com/balkian"><i class="fa fa-github"></i></a></li>
<li><a href="http://bitbucket.com/balkian"><i class="fa fa-bitbucket"></i></a></li>
<li><a href="http://twitter.com/balkian"><i class="fa fa-twitter"></i></a></li>
<li><a href="https://plus.google.com/u/0/111897020957944410316"><i class="fa fa-google-plus"></i></a></li>
<li><a href="http://linkedin.com/in/jfsanchezrada"><i class="fa fa-linkedin"></i></a></li>
<li><a href="http://facebook.com/balkian"><i class="fa fa-facebook"></i></a></li>
</ul>
<p>
<i class="fa fa-creative-commons"></i> Creative Commons A-SA-NC
</p>
</footer>
</div>
<script src="/theme/js/jquery-2.0.2.min.js"></script>
<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//piwik.sinpapel.es/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', 2]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="//piwik.sinpapel.es/piwik.php?idsite=2" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->
<!--<script src="/js/bootstrap.min.js"></script>-->
</body>
</html>