1
0
mirror of https://github.com/balkian/balkian.github.com.git synced 2024-12-27 02:38:13 +00:00
balkian.github.com/post/2019-01-06-zigbee2mqtt/index.html

312 lines
17 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Balkian&#39;s site - WELCOME</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="../../font-awesome/css/all.css">
<link rel="stylesheet" media="only screen" href="../../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="../../post/"><li class='' >Blog
</li></a>
<a href="../../tags/"><li class='' >Tags
</li></a>
<a href="../../cheatsheet/"><li class='' >Cheatsheets
</li></a>
<a href="../../project/"><li class='' >Projects
</li></a>
<a href="../../page/todo/"><li class='' >To-do
</li></a>
<a href="//jfernando.es" target="_blank"><li>
CV
</li></a>
<a href="../../search"><li>
<i class="fa fa-search"></i>
</li></a>
</ul>
</header>
<h2>Controlling Zigbee devices with MQTT</h2>
<span class="date">2019/01/06</span>
<span>
<a class="tag" href='../../tags/mqtt'><span class="label">mqtt</span></a>
<a class="tag" href='../../tags/iot'><span class="label">iot</span></a>
<a class="tag" href='../../tags/zigbee'><span class="label">zigbee</span></a>
</span>
<h3></h3>
<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&rsquo;re anything like me, you&rsquo;re probably a sucker for IoT devices.
For a long time, I&rsquo;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&rsquo;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&rsquo;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&rsquo;s a
video:</p>
<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
<iframe src="https://www.youtube.com/embed/5YtqG1wEnng" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" allowfullscreen title="YouTube Video"></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>
<li><a 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 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 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 href="https://koenkk.github.io/zigbee2mqtt/">zigbee2mqtt
documentation</a>.</p>
<p>Once you&rsquo;re done flashing, you&rsquo;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>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">24
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-yaml" data-lang="yaml"><span style="color:#66d9ef">version</span>: <span style="color:#e6db74">&#39;2.1&#39;</span>
<span style="color:#66d9ef">services</span>:
<span style="color:#66d9ef">zigbee2mqtt</span>:
<span style="color:#66d9ef">image</span>: koenkk/zigbee2mqtt
<span style="color:#66d9ef">container_name</span>: zigbee2mqtt
<span style="color:#66d9ef">restart</span>: always
<span style="color:#66d9ef">volumes</span>:
- ./z2m-data/:/app/data/
<span style="color:#66d9ef">devices</span>:
- <span style="color:#e6db74">&#34;/dev/ttyACM0&#34;</span>
<span style="color:#66d9ef">networks</span>:
- hass
<span style="color:#66d9ef">mqtt</span>:
<span style="color:#66d9ef">image</span>: eclipse-mosquitto
<span style="color:#66d9ef">ports</span>:
- <span style="color:#ae81ff">1883</span>:<span style="color:#ae81ff">1883</span>
- <span style="color:#ae81ff">9001</span>:<span style="color:#ae81ff">9001</span>
<span style="color:#66d9ef">networks</span>:
- hass
<span style="color:#66d9ef">volumes</span>:
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf
<span style="color:#66d9ef">networks</span>:
<span style="color:#66d9ef">hass</span>:
<span style="color:#66d9ef">driver</span>: overlay
</code></pre></td></tr></table>
</div>
</div><p>You can test your installation with:</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">6
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"> mosquitto_sub -h localhost -p <span style="color:#ae81ff">1883</span> -t <span style="color:#e6db74">&#39;zigbee2mqtt/#&#39;</span>
online
<span style="color:#f92672">{</span><span style="color:#e6db74">&#34;battery&#34;</span>:17,<span style="color:#e6db74">&#34;voltage&#34;</span>:2925,<span style="color:#e6db74">&#34;linkquality&#34;</span>:149,<span style="color:#e6db74">&#34;action&#34;</span>:<span style="color:#e6db74">&#34;rotate_right&#34;</span>,<span style="color:#e6db74">&#34;angle&#34;</span>:12.8<span style="color:#f92672">}</span>
<span style="color:#f92672">{</span><span style="color:#e6db74">&#34;battery&#34;</span>:17,<span style="color:#e6db74">&#34;voltage&#34;</span>:2925,<span style="color:#e6db74">&#34;linkquality&#34;</span>:141,<span style="color:#e6db74">&#34;action&#34;</span>:<span style="color:#e6db74">&#34;slide&#34;</span>,<span style="color:#e6db74">&#34;side&#34;</span>:2<span style="color:#f92672">}</span>
<span style="color:#f92672">{</span><span style="color:#e6db74">&#34;battery&#34;</span>:17,<span style="color:#e6db74">&#34;voltage&#34;</span>:2925,<span style="color:#e6db74">&#34;linkquality&#34;</span>:120<span style="color:#f92672">}</span>
<span style="color:#f92672">{</span><span style="color:#e6db74">&#34;battery&#34;</span>:17,<span style="color:#e6db74">&#34;voltage&#34;</span>:2925,<span style="color:#e6db74">&#34;linkquality&#34;</span>:141,<span style="color:#e6db74">&#34;action&#34;</span>:<span style="color:#e6db74">&#34;wakeup&#34;</span><span style="color:#f92672">}</span></code></pre></td></tr></table>
</div>
</div>
<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 href="https://koenkk.github.io/zigbee2mqtt/integration/home_assistant.html">this
guide</a>.</p>
</div>
<div id="sidebar" class="fixed">
<div id="badge" class="flip-container" ontouchstart="this.classList.toggle('hover');">
<div class="flipper sticky expand">
<div class="front">
<img id="avatar" class="expandx" src="../../img/me.png">
</div>
<div class="back">
<div id="about">
<div class="icons vertical-center">
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-linux"></i></span><span class="explanation">Linux user</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-android"></i></span><span class="explanation">Android dev and user</span></span>
<a href="http://github.com/balkian" target="_blank"><span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-github"></i> </span><span class="explanation">Github user</span></span></a>
<a href="http://gitlab.com/balkian" target="_blank"><span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-gitlab"></i> </span><span class="explanation">GitLab user</span></span></a>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fab fa-inverse fa-stack-1x fa-stack-exchange"></i> </span><span class="explanation">StackExchange fan</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-music"></i> </span><span class="explanation">Music lover</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-film"></i> </span><span class="explanation">Movie fan</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-laptop"></i> </span><span class="explanation">Always on a PC</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-moon"></i> </span><span class="explanation">Night owl</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-terminal"></i></span> <span class="explanation">CLI user</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-flag"></i></span> <span class="explanation">I love languages</span></span>
<span class="coolicon"><span class="fa-stack"> <i class="fas fa-square fa-stack-2x" ></i><i class="fas fa-inverse fa-stack-1x fa-code"></i> </span> <span class="explanation">I love programming</span></span>
</div>
</div>
</div>
</div>
</div>
<div class="entries">
<h2 class="title">Latest entries</h2>
<dt><a href="../../post/2021-10-29-logitech-mx-keys-mac/">Logitech MX Keys for Mac on Linux</a></dt>
<a class="tag" href='../../tags/linux'><span class="label">linux</span></a>
<a class="tag" href='../../tags/logitech'><span class="label">logitech</span></a>
<a class="tag" href='../../tags/keyboard'><span class="label">keyboard</span></a>
<dt><a href="../../post/2019-05-01-surface-go/">Linux on the Microsoft Surface Go</a></dt>
<a class="tag" href='../../tags/linux'><span class="label">linux</span></a>
<a class="tag" href='../../tags/surface-go'><span class="label">surface go</span></a>
<a class="tag" href='../../tags/config'><span class="label">config</span></a>
<dt><a href="../../post/2019-01-06-zigbee2mqtt/">Controlling Zigbee devices with MQTT</a></dt>
<a class="tag" href='../../tags/mqtt'><span class="label">mqtt</span></a>
<a class="tag" href='../../tags/iot'><span class="label">iot</span></a>
<a class="tag" href='../../tags/zigbee'><span class="label">zigbee</span></a>
<dt><a href="../../post/2016-09-28-tqdm/">Progress bars in python</a></dt>
<a class="tag" href='../../tags/python'><span class="label">python</span></a>
</div>
</div>
<div class="clear"></div>
</div>
<footer class="pagefooter" 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="fab fa-github"></i></a></li>
<li><a href="http://lab.gsi.upm.es/balkian"><i class="fab fa-gitlab"></i></a></li>
<li><a href="http://twitter.com/balkian"><i class="fab fa-twitter"></i></a></li>
<li><a href="http://git.sinpapel.es/balkian"><i class="fab fa-git"></i></a></li>
<li><a href="http://linkedin.com/in/jfsanchezrada"><i class="fab fa-linkedin"></i></a></li>
</ul>
<p>
<i class="fab fa-creative-commons"></i> Creative Commons A-SA-NC
</p>
</footer>
</div>
<script src="../../js/jquery-2.0.2.min.js"></script>
</body>
</html>