938 lines
52 KiB
XML
938 lines
52 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||
|
||
<title><![CDATA[Category: MQTT | Home Assistant]]></title>
|
||
<link href="https://home-assistant.io/blog/categories/mqtt/atom.xml" rel="self"/>
|
||
<link href="https://home-assistant.io/"/>
|
||
<updated>2016-03-21T19:41:07+00:00</updated>
|
||
<id>https://home-assistant.io/</id>
|
||
<author>
|
||
<name><![CDATA[Paulus Schoutsen]]></name>
|
||
|
||
</author>
|
||
<generator uri="http://octopress.org/">Octopress</generator>
|
||
|
||
|
||
<entry>
|
||
<title type="html"><![CDATA[Smarter SmartThings with MQTT and Home Assistant]]></title>
|
||
<link href="https://home-assistant.io/blog/2016/02/09/smarter-smart-things-with-mqtt-and-home-assistant/"/>
|
||
<updated>2016-02-09T07:44:00+00:00</updated>
|
||
<id>https://home-assistant.io/blog/2016/02/09/Smarter-Smart-Things-with-MQTT-and-Home-Assistant</id>
|
||
<content type="html"><![CDATA[<p><em>This is a guest post by Home Assistant users <a href="https://github.com/jer">Jeremiah Wuenschel</a> and <a href="https://github.com/stjohnjohnson">St. John Johnson</a>.</em></p>
|
||
|
||
<p>So you own a <a href="http://smartthings.com">SmartThings</a> Hub. You probably bought it when you were looking to get into the whole Home Automation hobby because it worked with pretty much everything and offered you the ability to automate <strong>anything.</strong> After a week of ownership, you realized that building dashboards and automating required writing way more Groovy then you expected. Then one day you were browsing <a href="https://www.reddit.com/r/homeautomation">reddit</a> and discovered the amazingness that is Home Assistant! A solution that offered dashboards, graphs, working support for Nest, and REAL EASY automation!</p>
|
||
|
||
<p>You spent your weekend getting everything set up, showing it off to your significant other, but in the end you got stumped when it came to integrating with all your existing SmartThings toys. What do I do now? Should I buy another hub? Should I just buy a Z-Wave stick?</p>
|
||
|
||
<p>That’s where we came in. We wanted a solution that can bridge the awesomeness of Home Assistant with the SmartThings hub that works with almost everything.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2016-02-smartthings/splash.png" />
|
||
</p>
|
||
|
||
<!--more-->
|
||
|
||
<h2>Glossary</h2>
|
||
|
||
<p>This is going to be a pretty detailed tutorial on setting up our SmartThings bridge. However, there are a couple key terms that <em>might</em> be new to you:</p>
|
||
|
||
<ul>
|
||
<li><a href="https://en.wikipedia.org/wiki/MQTT">MQTT</a>: A lightweight message protocol for listening and publishing events that happen. Many home automation platforms have built in support for this <a href="/components/mqtt/">(especially Home Assistant)</a>.</li>
|
||
<li><a href="https://www.docker.com/">Docker</a>: A tool for running applications that are self-contained. No need for installing any dependencies or worrying about conflicts. Installs easily on Linux and OSX.</li>
|
||
</ul>
|
||
|
||
<h2>Setting up the Bridge</h2>
|
||
|
||
<h3>MQTT</h3>
|
||
|
||
<p>Assuming that you already have Home Assistant and Smart Things running, you will first want to get an MQTT broker running. There are a handful of <a href="http://mosquitto.org/">MQTT</a> <a href="https://github.com/emqtt/emqttd">brokers</a> available in Open Source land. We chose <a href="http://www.mosca.io/">Mosca</a> for its simplicity.</p>
|
||
|
||
<p>There is very little you need to do to get Mosca running. The easiest approach is to use <a href="https://www.docker.com/">Docker</a>, and run a command like the following:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ docker run \
|
||
-d \
|
||
--name="mqtt" \
|
||
-v /opt/mosca:/db \
|
||
-p 1883:1883 \
|
||
matteocollina/mosca
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>This will start Mosca up inside of a docker container, while keeping persistent storage for Mosca in <code>/opt/mosca</code>. The default configuration is the only thing we need to get things up and running.</p>
|
||
|
||
<p>If you don’t want to mess with Docker and can get node.js installed without trouble, the <a href="https://github.com/mcollina/mosca#standalone">standalone</a> instructions are all you need.</p>
|
||
|
||
<h3>MQTT Bridge</h3>
|
||
|
||
<p>This is the small piece of magic that bridges the gap between MQTT and SmartThings. It is a node.js app, and like Mosca it is probably easiest to install with Docker:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ docker run \
|
||
-d \
|
||
--name="mqtt-bridge" \
|
||
-v /opt/mqtt-bridge:/config \
|
||
-p 8080:8080 \
|
||
stjohnjohnson/smartthings-mqtt-bridge
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>The code for this bridge is <a href="https://github.com/stjohnjohnson/smartthings-mqtt-bridge">on Github</a> if you want to start it up independently.</p>
|
||
|
||
<p>The MQTT Bridge only needs to know where your MQTT broker lives. If you are using these docker commands as-is, edit <code>/opt/mqtt-bridge/config.yml</code> to look like this:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="head"><span class="head">---</span></span>
|
||
<span class="key">mqtt</span>:
|
||
<span class="key">host</span>: <span class="string"><span class="content"><IP of the host></span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Restart the bridge, and you are ready to go:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ docker restart mqtt-bridge
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<h3>SmartThings Device</h3>
|
||
|
||
<p>The next step (and possibly the most confusing) is the device type. Go to the <a href="https://graph.api.smartthings.com/ide/devices">Smart Things Device IDE</a> and <code>Create New Device Handler</code>. Choose <code>From Code</code> and paste in the <a href="https://github.com/stjohnjohnson/smartthings-mqtt-bridge/blob/master/devicetypes/stj/mqtt-bridge.src/mqtt-bridge.groovy">MQTT Bridge Device Code</a>. Click <code>Save</code>, <code>Publish</code>, and then <code>For Me</code>.</p>
|
||
|
||
<p>Now to install your new Device Handler. Go back to <code>My Devices</code> in the IDE, and click <code>New Device</code>. Enter a name, and pick any random set of characters for the Device Network Id (this will automatically update later). For Type, scroll to the bottom of the list and find your newly created <code>MQTT Bridge</code>. Fill in the other boxes however you like.</p>
|
||
|
||
<p>Go back to <code>My Devices</code>, and click on your new device in the list. This will bring up a page that allows you to edit your device’s Preferences. Click <code>edit</code> and fill in the 3 pieces of information it asks for.</p>
|
||
|
||
<ul>
|
||
<li>MQTT Bridge IP Address: <IP address of the MQTT Bridge from the previous step></li>
|
||
<li>MQTT Bridge Port: <8080 if you have changed nothing in the previous commands></li>
|
||
<li>MQTT Bridge MAC Address: <Mac address of machine running the Bridge code></li>
|
||
</ul>
|
||
|
||
<p>This will create the link between SmartThings and the MQTT Bridge.</p>
|
||
|
||
<h3>SmartThings App</h3>
|
||
|
||
<p>The last step is to setup the SmartApp. After this, any registered devices will start sending their events to MQTT.</p>
|
||
|
||
<p>Go to the <a href="https://graph.api.smartthings.com/ide/apps">Smart App IDE</a>. Click <code>New SmartApp</code>, followed by <code>From Code</code>. Paste in the <a href="https://github.com/stjohnjohnson/smartthings-mqtt-bridge/blob/master/smartapps/stj/mqtt-bridge.src/mqtt-bridge.groovy">MQTT Bridge SmartApp code</a> and click <code>Save</code>. Click <code>Publish</code> and then <code>For Me</code>. In the SmartThings mobile app, add the new SmartApp and configure it with your devices and MQTT Bridge device. Clicking <code>done</code> will subscribe SmartThings to your MQTT broker and begin 2-way propagation of events.</p>
|
||
|
||
<h3>Configure Home Assistant</h3>
|
||
|
||
<p>To add SmartThings devices to Home Assistant over MQTT, first enable MQTT in Home Assistant:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="key">mqtt</span>:
|
||
<span class="key">broker</span>: <span class="string"><span class="content">localhost</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Replace <code>localhost</code> with the location of the running MQTT Broker. Devices from the MQTT Bridge are published to the path <code>smartthings/<Device Name>/<Atribute></code></p>
|
||
|
||
<p>For example, my Dimmer Z-Wave Lamp is called “Fireplace Lights” in SmartThings. The following topics are published:</p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>Topic</th>
|
||
<th>Description</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>smartthings/Fireplace Lights/level</td>
|
||
<td>Brightness (0-99)</td>
|
||
</tr>
|
||
<tr>
|
||
<td>smartthings/Fireplace Lights/switch</td>
|
||
<td>Switch State (on/off)</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p>Here is an example Home Assistant config:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="key">switch</span>:
|
||
<span class="key">platform</span>: <span class="string"><span class="content">mqtt</span></span>
|
||
<span class="key">name</span>: <span class="string"><span class="delimiter">"</span><span class="content">Fireplace Lights</span><span class="delimiter">"</span></span>
|
||
<span class="key">state_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">smartthings/Fireplace Lights/switch</span><span class="delimiter">"</span></span>
|
||
<span class="key">command_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">smartthings/Fireplace Lights/switch</span><span class="delimiter">"</span></span>
|
||
<span class="key">brightness_state_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">smartthings/Fireplace Lights/level</span><span class="delimiter">"</span></span>
|
||
<span class="key">brightness_command_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">smartthings/Fireplace Lights/level</span><span class="delimiter">"</span></span>
|
||
<span class="key">payload_on</span>: <span class="string"><span class="delimiter">"</span><span class="content">on</span><span class="delimiter">"</span></span>
|
||
<span class="key">payload_off</span>: <span class="string"><span class="delimiter">"</span><span class="content">off</span><span class="delimiter">"</span></span>
|
||
<span class="key">retain</span>: <span class="string"><span class="content">true</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>We recommend <code>retain: true</code> for every MQTT device in order to keep states in sync when things become disconnected.</p>
|
||
|
||
<p>Start digging through the <a href="/components/mqtt/">MQTT Components</a> in Home Assistant to find which components map to the new events being published to MQTT.</p>
|
||
|
||
<h3>Configuring with Docker-Compose</h3>
|
||
|
||
<p>Our personal preference for starting the whole suite of software is to use a single Docker-Compose file. Just create a file called <code>docker-compose.yml</code> like this:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="key">mqtt</span>:
|
||
<span class="key">image</span>: <span class="string"><span class="content">matteocollina/mosca</span></span>
|
||
<span class="key">ports</span>:
|
||
- <span class="string"><span class="content">1883:1883</span></span>
|
||
|
||
<span class="key">mqttbridge</span>:
|
||
<span class="key">image</span>: <span class="string"><span class="content">stjohnjohnson/smartthings-mqtt-bridge</span></span>
|
||
<span class="key">volumes</span>:
|
||
- <span class="string"><span class="content">./mqtt-bridge:/config</span></span>
|
||
<span class="key">ports</span>:
|
||
- <span class="string"><span class="content">8080:8080</span></span>
|
||
<span class="key">links</span>:
|
||
- <span class="string"><span class="content">mqtt</span></span>
|
||
|
||
<span class="key">homeassistant</span>:
|
||
<span class="key">image</span>: <span class="string"><span class="content">balloob/home-assistant</span></span>
|
||
<span class="key">ports</span>:
|
||
- <span class="string"><span class="content">80:80</span></span>
|
||
<span class="key">volumes</span>:
|
||
- <span class="string"><span class="content">./home-assistant:/config</span></span>
|
||
- <span class="string"><span class="content">/etc/localtime:/etc/localtime:ro</span></span>
|
||
<span class="key">links</span>:
|
||
- <span class="string"><span class="content">mqtt</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>This will start home-assistant, MQTT, and the Bridge, in dependency order. All config can reference the name of the docker container instead of using IP addresses (e.g. mqtt for the broker host in Home Assistant).</p>
|
||
|
||
<h3>How it works</h3>
|
||
|
||
<p><strong>HTTP Endpoint</strong>: There are really only 2 ways to communicate with the SmartThings hub that we could find. The easiest approach is to create a RESTful SmartApp authenticated with OAuth that provides state changes via HTTP directly. This approach is pretty straightforward to implement, but it requires communication with the SmartThings cloud service, and can’t be done entirely on your LAN. We hoped to keep all communication internal, and came up with a second approach.</p>
|
||
|
||
<p><strong>Custom Device Type:</strong> SmartThings custom device types allow developers to define handlers for HTTP events received directly over the local network by the SmartThings hub. Messages received are authenticated by MAC address, and can contain arbitrary strings in their payload. Since a Device Type is only ever tied to a single device, we need to add a SmartApp to the mix in order to translate events between individual devices and our special Home Assistant Bridge device. Here is what we have so far:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>Z-Wave Switch |
|
||
Zigbee motion sensor |<---> Bridge App <---> Bridge Device Type <---> <Local network>
|
||
Z-Wave light bulb |
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>On the Home Assistant side, there is a powerful platform available based on the MQTT lightweight message bus protocol. Everything from lights to switches to temperature sensors can be defined in Home Assistant as an MQTT component, so it makes for a convenient integration point. This requires an MQTT broker for handling the message bus, and one last piece to translate between the HTTP that SmartThings supports and MQTT.</p>
|
||
|
||
<p>Here is the final sequence of events:</p>
|
||
|
||
<p class="img">
|
||
<a href="https://home-assistant.io/images/blog/2016-02-smartthings/SmartThings-HomeAssistant.png">
|
||
<img src="https://home-assistant.io/images/blog/2016-02-smartthings/SmartThings-HomeAssistant.png" alt="SmartThings Bridge Sequence" />
|
||
</a>
|
||
SmartThings Bridge Sequence
|
||
</p>
|
||
|
||
<p>There are a lot of stops along the way for these events, but each piece is a simple translation layer to shuttle the events between systems.</p>
|
||
|
||
<h3>Future Improvements</h3>
|
||
<ul>
|
||
<li><strong>Raspberry pi</strong>: There is a lot of interest in getting this running on the Raspberry Pi. It only requires binaries compiled for ARM, so we plan to get ARM-compatible versions of the containers going at some point.</li>
|
||
<li><strong>Authentication for MQTT</strong>: At the moment, the MQTT bridge doesn’t understand how to authenticate to MQTT, so only unauthenticated MQTT is supported. This is mitigated to some degree if you use our Docker Compose config, because MQTT’s port is not actually shared publicly.</li>
|
||
<li><strong>Authentication for MQTT Bridge</strong>: Right now the bridge expects that anyone subscribing is the SmartThings hub. This could use proper authentication.</li>
|
||
</ul>
|
||
|
||
]]></content>
|
||
</entry>
|
||
|
||
<entry>
|
||
<title type="html"><![CDATA[Report the temperature with ESP8266 to MQTT]]></title>
|
||
<link href="https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/"/>
|
||
<updated>2015-10-11T19:10:00+00:00</updated>
|
||
<id>https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt</id>
|
||
<content type="html"><![CDATA[
|
||
<p>I recently learned about the ESP8266, a $5 chip that includes WiFi and is Arduino compatible. This means that all your DIY projects can now be done for a fraction of the price.</p>
|
||
|
||
<p>For this tutorial, I’ll walk through how to get going with ESP8266, get the temperature and humidity and report it to MQTT where Home Asssistant can pick it up.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-10-esp8266-temp/setup.png" />
|
||
Picture of the final setup (+ 2 LED for decoration)
|
||
</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-10-esp8266-temp/ha-sensor.png" />
|
||
Home Assistant will keep track of historical values and allow you to integrate it into automation.
|
||
</p>
|
||
|
||
<!--more-->
|
||
|
||
<h3>Components</h3>
|
||
|
||
<p>I’ve been using Adafruit for my shopping:</p>
|
||
|
||
<ul>
|
||
<li><a href="http://www.adafruit.com/product/2471">Adafruit HUZZAH ESP8266 Breakout</a> (<a href="https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout/assembly">assembly instructions</a>)</li>
|
||
<li><a href="http://www.adafruit.com/product/2635">Adafruit HDC1008 Temperature & Humidity Sensor Breakout Board</a> (<a href="https://learn.adafruit.com/adafruit-hdc1008-temperature-and-humidity-sensor-breakout/assembly">assembly instructions</a>)</li>
|
||
<li><a href="/components/mqtt/#picking-a-broker">MQTT server</a></li>
|
||
</ul>
|
||
|
||
<p><em>Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, soldering iron + wire, Serial USB cable.</em></p>
|
||
|
||
<h3>Connections</h3>
|
||
|
||
<p>On your breadboard, make the following connections from your ESP8266 to the HDC1008:</p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>ESP8266</th>
|
||
<th>HDC1008</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>GND</td>
|
||
<td>GND</td>
|
||
</tr>
|
||
<tr>
|
||
<td>3V</td>
|
||
<td>Vin</td>
|
||
</tr>
|
||
<tr>
|
||
<td>14</td>
|
||
<td>SCL</td>
|
||
</tr>
|
||
<tr>
|
||
<td>#2</td>
|
||
<td>SDA</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p><em>I picked <code>#2</code> and <code>14</code> myself, you can configure them in the sketch.</em></p>
|
||
|
||
<h3>Preparing your IDE</h3>
|
||
|
||
<p>Follow <a href="https://github.com/esp8266/Arduino#installing-with-boards-manager">these instructions</a> on how to install and prepare the Arduino IDE for ESP8266 development.</p>
|
||
|
||
<p>After you’re done installing, open the Arduino IDE, in the menu click on <code>sketch</code> -> <code>include library</code> -> <code>manage libraries</code> and install the following libraries:</p>
|
||
|
||
<ul>
|
||
<li>PubSubClient by Nick ‘O Leary</li>
|
||
<li>Adafruit HDC1000</li>
|
||
</ul>
|
||
|
||
<h3>Sketch</h3>
|
||
|
||
<p>If you have followed the previous steps, you’re all set.</p>
|
||
|
||
<ul>
|
||
<li>Open Arduino IDE and create a new sketch (<code>File</code> -> <code>New</code>)</li>
|
||
<li>Copy and paste the below sketch to the Arduino IDE</li>
|
||
<li>Adjust the values line 6 - 14 to match your setup</li>
|
||
<li>Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.</li>
|
||
<li>To have the ESP8266 accept our new sketch, we have to put it in upload mode. On the ESP8266 device keep the GPIO0 button pressed while pressing the reset button. The red led will glow half bright to indicate it is in upload mode.</li>
|
||
<li>Press the upload button in Arduino IDE</li>
|
||
<li>Open the serial monitor (<code>Tools</code> -> <code>Serial Monitor</code>) to see the output from your device</li>
|
||
</ul>
|
||
|
||
<p>This sketch will connect to your WiFi network and MQTT broker. It will read the temperature and humidity from the sensor every second. It will report it to the MQTT server if the difference is > 1 since last reported value. Reports to the MQTT broker are sent with retain set to <code>True</code>. This means that anyone connecting to the MQTT topic will automatically be notified of the last reported value.</p>
|
||
|
||
<div class="highlighter-coderay"><table class="CodeRay"><tr>
|
||
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
|
||
<a href="#n2" name="n2">2</a>
|
||
<a href="#n3" name="n3">3</a>
|
||
<a href="#n4" name="n4">4</a>
|
||
<a href="#n5" name="n5">5</a>
|
||
<a href="#n6" name="n6">6</a>
|
||
<a href="#n7" name="n7">7</a>
|
||
<a href="#n8" name="n8">8</a>
|
||
<a href="#n9" name="n9">9</a>
|
||
<strong><a href="#n10" name="n10">10</a></strong>
|
||
<a href="#n11" name="n11">11</a>
|
||
<a href="#n12" name="n12">12</a>
|
||
<a href="#n13" name="n13">13</a>
|
||
<a href="#n14" name="n14">14</a>
|
||
<a href="#n15" name="n15">15</a>
|
||
<a href="#n16" name="n16">16</a>
|
||
<a href="#n17" name="n17">17</a>
|
||
<a href="#n18" name="n18">18</a>
|
||
<a href="#n19" name="n19">19</a>
|
||
<strong><a href="#n20" name="n20">20</a></strong>
|
||
<a href="#n21" name="n21">21</a>
|
||
<a href="#n22" name="n22">22</a>
|
||
<a href="#n23" name="n23">23</a>
|
||
<a href="#n24" name="n24">24</a>
|
||
<a href="#n25" name="n25">25</a>
|
||
<a href="#n26" name="n26">26</a>
|
||
<a href="#n27" name="n27">27</a>
|
||
<a href="#n28" name="n28">28</a>
|
||
<a href="#n29" name="n29">29</a>
|
||
<strong><a href="#n30" name="n30">30</a></strong>
|
||
<a href="#n31" name="n31">31</a>
|
||
<a href="#n32" name="n32">32</a>
|
||
<a href="#n33" name="n33">33</a>
|
||
<a href="#n34" name="n34">34</a>
|
||
<a href="#n35" name="n35">35</a>
|
||
<a href="#n36" name="n36">36</a>
|
||
<a href="#n37" name="n37">37</a>
|
||
<a href="#n38" name="n38">38</a>
|
||
<a href="#n39" name="n39">39</a>
|
||
<strong><a href="#n40" name="n40">40</a></strong>
|
||
<a href="#n41" name="n41">41</a>
|
||
<a href="#n42" name="n42">42</a>
|
||
<a href="#n43" name="n43">43</a>
|
||
<a href="#n44" name="n44">44</a>
|
||
<a href="#n45" name="n45">45</a>
|
||
<a href="#n46" name="n46">46</a>
|
||
<a href="#n47" name="n47">47</a>
|
||
<a href="#n48" name="n48">48</a>
|
||
<a href="#n49" name="n49">49</a>
|
||
<strong><a href="#n50" name="n50">50</a></strong>
|
||
<a href="#n51" name="n51">51</a>
|
||
<a href="#n52" name="n52">52</a>
|
||
<a href="#n53" name="n53">53</a>
|
||
<a href="#n54" name="n54">54</a>
|
||
<a href="#n55" name="n55">55</a>
|
||
<a href="#n56" name="n56">56</a>
|
||
<a href="#n57" name="n57">57</a>
|
||
<a href="#n58" name="n58">58</a>
|
||
<a href="#n59" name="n59">59</a>
|
||
<strong><a href="#n60" name="n60">60</a></strong>
|
||
<a href="#n61" name="n61">61</a>
|
||
<a href="#n62" name="n62">62</a>
|
||
<a href="#n63" name="n63">63</a>
|
||
<a href="#n64" name="n64">64</a>
|
||
<a href="#n65" name="n65">65</a>
|
||
<a href="#n66" name="n66">66</a>
|
||
<a href="#n67" name="n67">67</a>
|
||
<a href="#n68" name="n68">68</a>
|
||
<a href="#n69" name="n69">69</a>
|
||
<strong><a href="#n70" name="n70">70</a></strong>
|
||
<a href="#n71" name="n71">71</a>
|
||
<a href="#n72" name="n72">72</a>
|
||
<a href="#n73" name="n73">73</a>
|
||
<a href="#n74" name="n74">74</a>
|
||
<a href="#n75" name="n75">75</a>
|
||
<a href="#n76" name="n76">76</a>
|
||
<a href="#n77" name="n77">77</a>
|
||
<a href="#n78" name="n78">78</a>
|
||
<a href="#n79" name="n79">79</a>
|
||
<strong><a href="#n80" name="n80">80</a></strong>
|
||
<a href="#n81" name="n81">81</a>
|
||
<a href="#n82" name="n82">82</a>
|
||
<a href="#n83" name="n83">83</a>
|
||
<a href="#n84" name="n84">84</a>
|
||
<a href="#n85" name="n85">85</a>
|
||
<a href="#n86" name="n86">86</a>
|
||
<a href="#n87" name="n87">87</a>
|
||
<a href="#n88" name="n88">88</a>
|
||
<a href="#n89" name="n89">89</a>
|
||
<strong><a href="#n90" name="n90">90</a></strong>
|
||
<a href="#n91" name="n91">91</a>
|
||
<a href="#n92" name="n92">92</a>
|
||
<a href="#n93" name="n93">93</a>
|
||
<a href="#n94" name="n94">94</a>
|
||
<a href="#n95" name="n95">95</a>
|
||
<a href="#n96" name="n96">96</a>
|
||
<a href="#n97" name="n97">97</a>
|
||
<a href="#n98" name="n98">98</a>
|
||
<a href="#n99" name="n99">99</a>
|
||
<strong><a href="#n100" name="n100">100</a></strong>
|
||
<a href="#n101" name="n101">101</a>
|
||
<a href="#n102" name="n102">102</a>
|
||
<a href="#n103" name="n103">103</a>
|
||
<a href="#n104" name="n104">104</a>
|
||
<a href="#n105" name="n105">105</a>
|
||
<a href="#n106" name="n106">106</a>
|
||
<a href="#n107" name="n107">107</a>
|
||
<a href="#n108" name="n108">108</a>
|
||
<a href="#n109" name="n109">109</a>
|
||
</pre></td>
|
||
<td class="code"><pre><span class="preprocessor">#include</span> <span class="include"><ESP8266WiFi.h></span>
|
||
<span class="preprocessor">#include</span> <span class="include"><Wire.h></span>
|
||
<span class="preprocessor">#include</span> <span class="include"><PubSubClient.h></span>
|
||
<span class="preprocessor">#include</span> <span class="include"><Adafruit_HDC1000.h></span>
|
||
|
||
<span class="preprocessor">#define</span> wifi_ssid <span class="string"><span class="delimiter">"</span><span class="content">YOUR WIFI SSID</span><span class="delimiter">"</span></span>
|
||
<span class="preprocessor">#define</span> wifi_password <span class="string"><span class="delimiter">"</span><span class="content">WIFI PASSWORD</span><span class="delimiter">"</span></span>
|
||
|
||
<span class="preprocessor">#define</span> mqtt_server <span class="string"><span class="delimiter">"</span><span class="content">YOUR_MQTT_SERVER_HOST</span><span class="delimiter">"</span></span>
|
||
<span class="preprocessor">#define</span> mqtt_user <span class="string"><span class="delimiter">"</span><span class="content">your_username</span><span class="delimiter">"</span></span>
|
||
<span class="preprocessor">#define</span> mqtt_password <span class="string"><span class="delimiter">"</span><span class="content">your_password</span><span class="delimiter">"</span></span>
|
||
|
||
<span class="preprocessor">#define</span> humidity_topic <span class="string"><span class="delimiter">"</span><span class="content">sensor/humidity</span><span class="delimiter">"</span></span>
|
||
<span class="preprocessor">#define</span> temperature_topic <span class="string"><span class="delimiter">"</span><span class="content">sensor/temperature</span><span class="delimiter">"</span></span>
|
||
|
||
WiFiClient espClient;
|
||
PubSubClient client(espClient);
|
||
Adafruit_HDC1000 hdc = Adafruit_HDC1000();
|
||
|
||
<span class="directive">void</span> setup() {
|
||
Serial.begin(<span class="integer">115200</span>);
|
||
setup_wifi();
|
||
client.setServer(mqtt_server, <span class="integer">1883</span>);
|
||
|
||
<span class="comment">// Set SDA and SDL ports</span>
|
||
Wire.begin(<span class="integer">2</span>, <span class="integer">14</span>);
|
||
|
||
<span class="comment">// Start sensor</span>
|
||
<span class="keyword">if</span> (!hdc.begin()) {
|
||
Serial.println(<span class="string"><span class="delimiter">"</span><span class="content">Couldn't find sensor!</span><span class="delimiter">"</span></span>);
|
||
<span class="keyword">while</span> (<span class="integer">1</span>);
|
||
}}
|
||
|
||
<span class="directive">void</span> setup_wifi() {
|
||
delay(<span class="integer">10</span>);
|
||
<span class="comment">// We start by connecting to a WiFi network</span>
|
||
Serial.println();
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">Connecting to </span><span class="delimiter">"</span></span>);
|
||
Serial.println(wifi_ssid);
|
||
|
||
WiFi.begin(wifi_ssid, wifi_password);
|
||
|
||
<span class="keyword">while</span> (WiFi.status() != WL_CONNECTED) {
|
||
delay(<span class="integer">500</span>);
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">.</span><span class="delimiter">"</span></span>);
|
||
}
|
||
|
||
Serial.println(<span class="string"><span class="delimiter">"</span><span class="delimiter">"</span></span>);
|
||
Serial.println(<span class="string"><span class="delimiter">"</span><span class="content">WiFi connected</span><span class="delimiter">"</span></span>);
|
||
Serial.println(<span class="string"><span class="delimiter">"</span><span class="content">IP address: </span><span class="delimiter">"</span></span>);
|
||
Serial.println(WiFi.localIP());
|
||
}
|
||
|
||
<span class="directive">void</span> reconnect() {
|
||
<span class="comment">// Loop until we're reconnected</span>
|
||
<span class="keyword">while</span> (!client.connected()) {
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">Attempting MQTT connection...</span><span class="delimiter">"</span></span>);
|
||
<span class="comment">// Attempt to connect</span>
|
||
<span class="comment">// If you do not want to use a username and password, change next line to</span>
|
||
<span class="comment">// if (client.connect("ESP8266Client")) {</span>
|
||
<span class="keyword">if</span> (client.connect(<span class="string"><span class="delimiter">"</span><span class="content">ESP8266Client</span><span class="delimiter">"</span></span>, mqtt_user, mqtt_password)) {
|
||
Serial.println(<span class="string"><span class="delimiter">"</span><span class="content">connected</span><span class="delimiter">"</span></span>);
|
||
} <span class="keyword">else</span> {
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">failed, rc=</span><span class="delimiter">"</span></span>);
|
||
Serial.print(client.state());
|
||
Serial.println(<span class="string"><span class="delimiter">"</span><span class="content"> try again in 5 seconds</span><span class="delimiter">"</span></span>);
|
||
<span class="comment">// Wait 5 seconds before retrying</span>
|
||
delay(<span class="integer">5000</span>);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="predefined-type">bool</span> checkBound(<span class="predefined-type">float</span> newValue, <span class="predefined-type">float</span> prevValue, <span class="predefined-type">float</span> maxDiff) {
|
||
<span class="keyword">return</span> newValue < prevValue - maxDiff || newValue > prevValue + maxDiff;
|
||
}
|
||
|
||
<span class="predefined-type">long</span> lastMsg = <span class="integer">0</span>;
|
||
<span class="predefined-type">float</span> temp = <span class="float">0</span><span class="float">.0</span>;
|
||
<span class="predefined-type">float</span> hum = <span class="float">0</span><span class="float">.0</span>;
|
||
<span class="predefined-type">float</span> diff = <span class="float">1</span><span class="float">.0</span>;
|
||
|
||
<span class="directive">void</span> loop() {
|
||
<span class="keyword">if</span> (!client.connected()) {
|
||
reconnect();
|
||
}
|
||
client.loop();
|
||
|
||
<span class="predefined-type">long</span> now = millis();
|
||
<span class="keyword">if</span> (now - lastMsg > <span class="integer">1000</span>) {
|
||
lastMsg = now;
|
||
|
||
<span class="predefined-type">float</span> newTemp = hdc.readTemperature();
|
||
<span class="predefined-type">float</span> newHum = hdc.readHumidity();
|
||
|
||
<span class="keyword">if</span> (checkBound(newTemp, temp, diff)) {
|
||
temp = newTemp;
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">New temperature:</span><span class="delimiter">"</span></span>);
|
||
Serial.println(String(temp).c_str());
|
||
client.publish(temperature_topic, String(temp).c_str(), <span class="predefined-constant">true</span>);
|
||
}
|
||
|
||
<span class="keyword">if</span> (checkBound(newHum, hum, diff)) {
|
||
hum = newHum;
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">New humidity:</span><span class="delimiter">"</span></span>);
|
||
Serial.println(String(hum).c_str());
|
||
client.publish(humidity_topic, String(hum).c_str(), <span class="predefined-constant">true</span>);
|
||
}
|
||
}
|
||
}
|
||
</pre></td>
|
||
</tr></table>
|
||
</div>
|
||
|
||
<h3>Configuring Home Assistant</h3>
|
||
|
||
<p>The last step is to integrate the sensor values into Home Assistant. This can be done by setting up Home Assistant to connect to the MQTT broker and subscribe to the sensor topics.</p>
|
||
|
||
<div class="highlighter-coderay"><table class="CodeRay"><tr>
|
||
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
|
||
<a href="#n2" name="n2">2</a>
|
||
<a href="#n3" name="n3">3</a>
|
||
<a href="#n4" name="n4">4</a>
|
||
<a href="#n5" name="n5">5</a>
|
||
<a href="#n6" name="n6">6</a>
|
||
<a href="#n7" name="n7">7</a>
|
||
<a href="#n8" name="n8">8</a>
|
||
<a href="#n9" name="n9">9</a>
|
||
<strong><a href="#n10" name="n10">10</a></strong>
|
||
<a href="#n11" name="n11">11</a>
|
||
<a href="#n12" name="n12">12</a>
|
||
<a href="#n13" name="n13">13</a>
|
||
<a href="#n14" name="n14">14</a>
|
||
<a href="#n15" name="n15">15</a>
|
||
<a href="#n16" name="n16">16</a>
|
||
<a href="#n17" name="n17">17</a>
|
||
<a href="#n18" name="n18">18</a>
|
||
</pre></td>
|
||
<td class="code"><pre><span class="key">mqtt</span>:
|
||
<span class="key">broker</span>: <span class="string"><span class="content">YOUR_MQTT_SERVER_HOST</span></span>
|
||
<span class="key">username</span>: <span class="string"><span class="content">your_username</span></span>
|
||
<span class="key">password</span>: <span class="string"><span class="content">your_password</span></span>
|
||
|
||
<span class="key">sensor</span>:
|
||
<span class="key">platform</span>: <span class="string"><span class="content">mqtt</span></span>
|
||
<span class="key">name</span>: <span class="string"><span class="delimiter">"</span><span class="content">Temperature</span><span class="delimiter">"</span></span>
|
||
<span class="key">state_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">sensor/temperature</span><span class="delimiter">"</span></span>
|
||
<span class="key">qos</span>: <span class="string"><span class="content">0</span></span>
|
||
<span class="key">unit_of_measurement</span>: <span class="string"><span class="delimiter">"</span><span class="content">ºC</span><span class="delimiter">"</span></span>
|
||
|
||
<span class="key">sensor 2</span>:
|
||
<span class="key">platform</span>: <span class="string"><span class="content">mqtt</span></span>
|
||
<span class="key">name</span>: <span class="string"><span class="delimiter">"</span><span class="content">Humidity</span><span class="delimiter">"</span></span>
|
||
<span class="key">state_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">sensor/humidity</span><span class="delimiter">"</span></span>
|
||
<span class="key">qos</span>: <span class="string"><span class="content">0</span></span>
|
||
<span class="key">unit_of_measurement</span>: <span class="string"><span class="delimiter">"</span><span class="content">%</span><span class="delimiter">"</span></span>
|
||
</pre></td>
|
||
</tr></table>
|
||
</div>
|
||
]]></content>
|
||
</entry>
|
||
|
||
<entry>
|
||
<title type="html"><![CDATA[Using MQTT with Home Assistant]]></title>
|
||
<link href="https://home-assistant.io/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant/"/>
|
||
<updated>2015-09-11T09:19:38+00:00</updated>
|
||
<id>https://home-assistant.io/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant</id>
|
||
<content type="html"><![CDATA[
|
||
<p><img src="https://home-assistant.io/images/supported_brands/mqtt.png" style="border:none; box-shadow: none; float: right;" height="80" /> <a href="https://en.wikipedia.org/wiki/MQTT">MQTT</a> support was added to Home Assistant recently. The <a href="https://home-assistant.io/components/mqtt/">MQTT component</a> will enable you to do all sort of things. Most likely you will use it to communicate with your devices. But Home Assistant doesn’t care where the data is coming from or is limited to real hardware as long as there is MQTT support. This means that it doesn’t matter if the data is coming from a human, a web service, or a device.</p>
|
||
|
||
<p>A great example is shown in a <a href="https://home-assistant.io/blog/2015/08/26/laundry-automation-with-moteino-mqtt-and-home-assistant/">Laundry Automation</a> post in this blog.</p>
|
||
|
||
<p>This post will give you a small overview of some other possibilities on how to use MQTT with Home Assistant.</p>
|
||
|
||
<!--more-->
|
||
|
||
<h3><a class='title-link' name='manual-usage' href='#manual-usage'></a> Manual usage </h3>
|
||
|
||
<p>The simplest but not the coolest way as a human to interact with a Home Assistant sensor is launching a command manually. Let’s create a “Mood” sensor. For simplicity Home Assistant and the MQTT broker are both running on the same host. The needed configuration snipplets to add to the <code>configuration.yaml</code> file consists of two parts: one for the broker and one for the sensor.</p>
|
||
|
||
<div class="highlighter-coderay"><table class="CodeRay"><tr>
|
||
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
|
||
<a href="#n2" name="n2">2</a>
|
||
<a href="#n3" name="n3">3</a>
|
||
<a href="#n4" name="n4">4</a>
|
||
<a href="#n5" name="n5">5</a>
|
||
<a href="#n6" name="n6">6</a>
|
||
<a href="#n7" name="n7">7</a>
|
||
</pre></td>
|
||
<td class="code"><pre><span class="key">mqtt</span>:
|
||
<span class="key">broker</span>: <span class="string"><span class="content">127.0.0.1</span></span>
|
||
|
||
<span class="key">sensor</span>:
|
||
- <span class="string"><span class="content">platform: mqtt</span></span>
|
||
<span class="key">name</span>: <span class="string"><span class="delimiter">"</span><span class="content">Fabian's Mood</span><span class="delimiter">"</span></span>
|
||
<span class="key">state_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">home-assistant/fabian/mood</span><span class="delimiter">"</span></span>
|
||
</pre></td>
|
||
</tr></table>
|
||
</div>
|
||
|
||
<p>After a restart of Home Assistant the “Mood” sensor will show up in the frontend. For more details about the configuration of MQTT itself and the sensor, please refer to the <a href="https://home-assistant.io/components/mqtt/">MQTT component</a> or the <a href="https://home-assistant.io/components/sensor.mqtt/">MQTT sensor</a> documentation.</p>
|
||
|
||
<p>Now we can set the mood. The commandline tool (<code>mosquitto_pub</code>) which is shipped with <code>mosquitto</code> is used to send an MQTT message.</p>
|
||
|
||
<div class="highlighter-coderay"><table class="CodeRay"><tr>
|
||
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
|
||
</pre></td>
|
||
<td class="code"><pre>$ mosquitto_pub -h 127.0.0.1 -t "home-assistant/fabian/mood" -m "bad"
|
||
</pre></td>
|
||
</tr></table>
|
||
</div>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-09-mqtt/mood.png" />
|
||
The Mood sensor
|
||
</p>
|
||
|
||
<p>This is a really bad example. Don’t do this in the real world because you won’t be able to create diagrams of historical data. Better use a numerical value.</p>
|
||
|
||
<h3><a class='title-link' name='python-mqtt-bindings' href='#python-mqtt-bindings'></a> Python MQTT bindings </h3>
|
||
|
||
<p>The last section was pretty boring, I know. Nobody wants to send MQTT messages by hand if there is a computer on the desk. If you are playing the lottery this section is for you. If not, read it anyway because the lottery is just an example :-).</p>
|
||
|
||
<p>This example is using the <a href="https://eclipse.org/paho/clients/python/">Paho MQTT Python binding</a> because those binding should be available on the host where Home Assistant is running. If you want to use this example on another machine, please make sure that the bindings are installed (<code>pip3 install paho-mqtt</code>).</p>
|
||
|
||
<p>The first step is to add an additional MQTT sensor to the <code>configuration.yaml</code> file. The sensor will be called “Lottery” and the unit of measurement will be “No.”.</p>
|
||
|
||
<div class="highlighter-coderay"><table class="CodeRay"><tr>
|
||
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
|
||
<a href="#n2" name="n2">2</a>
|
||
<a href="#n3" name="n3">3</a>
|
||
<a href="#n4" name="n4">4</a>
|
||
</pre></td>
|
||
<td class="code"><pre> - <span class="string"><span class="content">platform: mqtt</span></span>
|
||
<span class="key">name</span>: <span class="string"><span class="delimiter">"</span><span class="content">Lottery</span><span class="delimiter">"</span></span>
|
||
<span class="key">state_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">home-assistant/lottery/number</span><span class="delimiter">"</span></span>
|
||
<span class="key">unit_of_measurement</span>: <span class="string"><span class="delimiter">"</span><span class="content">No.</span><span class="delimiter">"</span></span>
|
||
</pre></td>
|
||
</tr></table>
|
||
</div>
|
||
|
||
<p>Don’t forget to restart Home Assistant to make the configuration active.</p>
|
||
|
||
<p>To play, we need numbers from 1 to 49 which can be marked on the ticket. Those numbers should be random and displayed in the Home Assistant frontend. The Python script below is another simple example on how to send MQTT messages from the commandline; this time in a loop. For further information and examples please check the <a href="https://eclipse.org/paho/clients/python/docs/">Paho MQTT</a> documentation.</p>
|
||
|
||
<div class="highlighter-coderay"><table class="CodeRay"><tr>
|
||
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
|
||
<a href="#n2" name="n2">2</a>
|
||
<a href="#n3" name="n3">3</a>
|
||
<a href="#n4" name="n4">4</a>
|
||
<a href="#n5" name="n5">5</a>
|
||
<a href="#n6" name="n6">6</a>
|
||
<a href="#n7" name="n7">7</a>
|
||
<a href="#n8" name="n8">8</a>
|
||
<a href="#n9" name="n9">9</a>
|
||
<strong><a href="#n10" name="n10">10</a></strong>
|
||
<a href="#n11" name="n11">11</a>
|
||
<a href="#n12" name="n12">12</a>
|
||
<a href="#n13" name="n13">13</a>
|
||
<a href="#n14" name="n14">14</a>
|
||
<a href="#n15" name="n15">15</a>
|
||
<a href="#n16" name="n16">16</a>
|
||
<a href="#n17" name="n17">17</a>
|
||
<a href="#n18" name="n18">18</a>
|
||
<a href="#n19" name="n19">19</a>
|
||
<strong><a href="#n20" name="n20">20</a></strong>
|
||
<a href="#n21" name="n21">21</a>
|
||
<a href="#n22" name="n22">22</a>
|
||
</pre></td>
|
||
<td class="code"><pre><span class="comment">#!/usr/bin/python3</span>
|
||
<span class="comment">#</span>
|
||
<span class="keyword">import</span> <span class="include">time</span>
|
||
<span class="keyword">import</span> <span class="include">random</span>
|
||
<span class="keyword">import</span> <span class="include">paho.mqtt.client</span> <span class="keyword">as</span> mqtt
|
||
<span class="keyword">import</span> <span class="include">paho.mqtt.publish</span> <span class="keyword">as</span> publish
|
||
|
||
broker = <span class="string"><span class="delimiter">'</span><span class="content">127.0.0.1</span><span class="delimiter">'</span></span>
|
||
state_topic = <span class="string"><span class="delimiter">'</span><span class="content">home-assistant/lottery/number</span><span class="delimiter">'</span></span>
|
||
delay = <span class="integer">5</span>
|
||
|
||
<span class="comment"># Send a single message to set the mood</span>
|
||
publish.single(<span class="string"><span class="delimiter">'</span><span class="content">home-assistant/fabian/mood</span><span class="delimiter">'</span></span>, <span class="string"><span class="delimiter">'</span><span class="content">good</span><span class="delimiter">'</span></span>, hostname=broker)
|
||
|
||
<span class="comment"># Send messages in a loop</span>
|
||
client = mqtt.Client(<span class="string"><span class="delimiter">"</span><span class="content">ha-client</span><span class="delimiter">"</span></span>)
|
||
client.connect(broker)
|
||
client.loop_start()
|
||
|
||
<span class="keyword">while</span> <span class="predefined-constant">True</span>:
|
||
client.publish(state_topic, random.randrange(<span class="integer">0</span>, <span class="integer">50</span>, <span class="integer">1</span>))
|
||
time.sleep(delay)
|
||
</pre></td>
|
||
</tr></table>
|
||
</div>
|
||
|
||
<p>Every 5 seconds a message with a new number is sent to the broker and picked up by Home Assistant. By the way, my mood is much better now.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-09-mqtt/lottery.png" />
|
||
The Lottery sensor
|
||
</p>
|
||
|
||
<p>With only a few lines of Python and an MQTT broker you can create your own “smartdevice” or send information to Home Assistant which you haven’t think of. Of course this is not limited to Python. If there is an MQTT library available, the device can be used with Home Assistant now.</p>
|
||
|
||
<h3><a class='title-link' name='arduino' href='#arduino'></a> Arduino </h3>
|
||
|
||
<p>To get started with real hardware that is capable to send MQTT messages, the Arduino platform is an inexpensive way to do it. In this section an Arduino UNO with an Ethernet shield and a photo resistor is used. The photo resistor is connected to analog pin 0 (A0) and has an output from 0 to 1024.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-09-mqtt/arduino-shield.png" />
|
||
The Arduino UNO with Ethernet shield and photo resistor
|
||
</p>
|
||
|
||
<p>The <a href="http://knolleary.github.io/pubsubclient/">MQTT client</a> for the Arduino needs to be available in your Arduino IDE. Below you will find a sketch which could act as a starting point. Please modify the IP addresses, the MAC address, and the pin as needed and upload the sketch to your Arduino.</p>
|
||
|
||
<div class="highlighter-coderay"><table class="CodeRay"><tr>
|
||
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
|
||
<a href="#n2" name="n2">2</a>
|
||
<a href="#n3" name="n3">3</a>
|
||
<a href="#n4" name="n4">4</a>
|
||
<a href="#n5" name="n5">5</a>
|
||
<a href="#n6" name="n6">6</a>
|
||
<a href="#n7" name="n7">7</a>
|
||
<a href="#n8" name="n8">8</a>
|
||
<a href="#n9" name="n9">9</a>
|
||
<strong><a href="#n10" name="n10">10</a></strong>
|
||
<a href="#n11" name="n11">11</a>
|
||
<a href="#n12" name="n12">12</a>
|
||
<a href="#n13" name="n13">13</a>
|
||
<a href="#n14" name="n14">14</a>
|
||
<a href="#n15" name="n15">15</a>
|
||
<a href="#n16" name="n16">16</a>
|
||
<a href="#n17" name="n17">17</a>
|
||
<a href="#n18" name="n18">18</a>
|
||
<a href="#n19" name="n19">19</a>
|
||
<strong><a href="#n20" name="n20">20</a></strong>
|
||
<a href="#n21" name="n21">21</a>
|
||
<a href="#n22" name="n22">22</a>
|
||
<a href="#n23" name="n23">23</a>
|
||
<a href="#n24" name="n24">24</a>
|
||
<a href="#n25" name="n25">25</a>
|
||
<a href="#n26" name="n26">26</a>
|
||
<a href="#n27" name="n27">27</a>
|
||
<a href="#n28" name="n28">28</a>
|
||
<a href="#n29" name="n29">29</a>
|
||
<strong><a href="#n30" name="n30">30</a></strong>
|
||
<a href="#n31" name="n31">31</a>
|
||
<a href="#n32" name="n32">32</a>
|
||
<a href="#n33" name="n33">33</a>
|
||
<a href="#n34" name="n34">34</a>
|
||
<a href="#n35" name="n35">35</a>
|
||
<a href="#n36" name="n36">36</a>
|
||
<a href="#n37" name="n37">37</a>
|
||
<a href="#n38" name="n38">38</a>
|
||
<a href="#n39" name="n39">39</a>
|
||
<strong><a href="#n40" name="n40">40</a></strong>
|
||
<a href="#n41" name="n41">41</a>
|
||
<a href="#n42" name="n42">42</a>
|
||
<a href="#n43" name="n43">43</a>
|
||
<a href="#n44" name="n44">44</a>
|
||
<a href="#n45" name="n45">45</a>
|
||
<a href="#n46" name="n46">46</a>
|
||
<a href="#n47" name="n47">47</a>
|
||
<a href="#n48" name="n48">48</a>
|
||
<a href="#n49" name="n49">49</a>
|
||
<strong><a href="#n50" name="n50">50</a></strong>
|
||
<a href="#n51" name="n51">51</a>
|
||
<a href="#n52" name="n52">52</a>
|
||
<a href="#n53" name="n53">53</a>
|
||
<a href="#n54" name="n54">54</a>
|
||
<a href="#n55" name="n55">55</a>
|
||
<a href="#n56" name="n56">56</a>
|
||
<a href="#n57" name="n57">57</a>
|
||
<a href="#n58" name="n58">58</a>
|
||
<a href="#n59" name="n59">59</a>
|
||
<strong><a href="#n60" name="n60">60</a></strong>
|
||
<a href="#n61" name="n61">61</a>
|
||
<a href="#n62" name="n62">62</a>
|
||
<a href="#n63" name="n63">63</a>
|
||
<a href="#n64" name="n64">64</a>
|
||
<a href="#n65" name="n65">65</a>
|
||
<a href="#n66" name="n66">66</a>
|
||
<a href="#n67" name="n67">67</a>
|
||
<a href="#n68" name="n68">68</a>
|
||
</pre></td>
|
||
<td class="code"><pre><span class="comment">/*
|
||
This sketch is based on the basic MQTT example by
|
||
http://knolleary.github.io/pubsubclient/
|
||
*/</span>
|
||
|
||
<span class="preprocessor">#include</span> <span class="include"><SPI.h></span>
|
||
<span class="preprocessor">#include</span> <span class="include"><Ethernet.h></span>
|
||
<span class="preprocessor">#include</span> <span class="include"><PubSubClient.h></span>
|
||
|
||
<span class="preprocessor">#define</span> DEBUG <span class="integer">1</span> <span class="comment">// Debug output to serial console</span>
|
||
|
||
<span class="comment">// Device settings</span>
|
||
IPAddress deviceIp(<span class="integer">192</span>, <span class="integer">168</span>, <span class="integer">0</span>, <span class="integer">43</span>);
|
||
byte deviceMac[] = { <span class="hex">0xAB</span>, <span class="hex">0xCD</span>, <span class="hex">0xFE</span>, <span class="hex">0xFE</span>, <span class="hex">0xFE</span>, <span class="hex">0xFE</span> };
|
||
<span class="predefined-type">char</span>* deviceId = <span class="string"><span class="delimiter">"</span><span class="content">sensor01</span><span class="delimiter">"</span></span>; <span class="comment">// Name of the sensor</span>
|
||
<span class="predefined-type">char</span>* stateTopic = <span class="string"><span class="delimiter">"</span><span class="content">home-assistant/sensor01/brightness</span><span class="delimiter">"</span></span>; <span class="comment">// MQTT topic where values are published</span>
|
||
<span class="predefined-type">int</span> sensorPin = A0; <span class="comment">// Pin to which the sensor is connected to</span>
|
||
<span class="predefined-type">char</span> buf[<span class="integer">4</span>]; <span class="comment">// Buffer to store the sensor value</span>
|
||
<span class="predefined-type">int</span> updateInterval = <span class="integer">1000</span>; <span class="comment">// Interval in miliseconds</span>
|
||
|
||
<span class="comment">// MQTT server settings</span>
|
||
IPAddress mqttServer(<span class="integer">192</span>, <span class="integer">168</span>, <span class="integer">0</span>, <span class="integer">12</span>);
|
||
<span class="predefined-type">int</span> mqttPort = <span class="integer">1883</span>;
|
||
|
||
EthernetClient ethClient;
|
||
PubSubClient client(ethClient);
|
||
|
||
<span class="directive">void</span> reconnect() {
|
||
<span class="keyword">while</span> (!client.connected()) {
|
||
<span class="preprocessor">#if</span> DEBUG
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">Attempting MQTT connection...</span><span class="delimiter">"</span></span>);
|
||
<span class="preprocessor">#endif</span>
|
||
<span class="keyword">if</span> (client.connect(deviceId)) {
|
||
<span class="preprocessor">#if</span> DEBUG
|
||
Serial.println(<span class="string"><span class="delimiter">"</span><span class="content">connected</span><span class="delimiter">"</span></span>);
|
||
<span class="preprocessor">#endif</span>
|
||
} <span class="keyword">else</span> {
|
||
<span class="preprocessor">#if</span> DEBUG
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">failed, rc=</span><span class="delimiter">"</span></span>);
|
||
Serial.print(client.state());
|
||
Serial.println(<span class="string"><span class="delimiter">"</span><span class="content"> try again in 5 seconds</span><span class="delimiter">"</span></span>);
|
||
<span class="preprocessor">#endif</span>
|
||
delay(<span class="integer">5000</span>);
|
||
}
|
||
}
|
||
}
|
||
|
||
<span class="directive">void</span> setup() {
|
||
Serial.begin(<span class="integer">57600</span>);
|
||
client.setServer(mqttServer, mqttPort);
|
||
Ethernet.begin(deviceMac, deviceIp);
|
||
delay(<span class="integer">1500</span>);
|
||
}
|
||
|
||
<span class="directive">void</span> loop() {
|
||
<span class="keyword">if</span> (!client.connected()) {
|
||
reconnect();
|
||
}
|
||
client.loop();
|
||
|
||
<span class="predefined-type">int</span> sensorValue = analogRead(sensorPin);
|
||
<span class="preprocessor">#if</span> DEBUG
|
||
Serial.print(<span class="string"><span class="delimiter">"</span><span class="content">Sensor value: </span><span class="delimiter">"</span></span>);
|
||
Serial.println(sensorValue);
|
||
<span class="preprocessor">#endif</span>
|
||
client.publish(stateTopic, itoa(sensorValue, buf, <span class="integer">10</span>));
|
||
delay(updateInterval);
|
||
}
|
||
</pre></td>
|
||
</tr></table>
|
||
</div>
|
||
|
||
<p>The Arduino will send the value of the sensor every second. To use the data in Home Assistant, add an additional MQTT sensor to the <code>configuration.yaml</code> file.</p>
|
||
|
||
<div class="highlighter-coderay"><table class="CodeRay"><tr>
|
||
<td class="line-numbers"><pre><a href="#n1" name="n1">1</a>
|
||
<a href="#n2" name="n2">2</a>
|
||
<a href="#n3" name="n3">3</a>
|
||
<a href="#n4" name="n4">4</a>
|
||
</pre></td>
|
||
<td class="code"><pre> - <span class="string"><span class="content">platform: mqtt</span></span>
|
||
<span class="key">name</span>: <span class="string"><span class="delimiter">"</span><span class="content">Brightness</span><span class="delimiter">"</span></span>
|
||
<span class="key">state_topic</span>: <span class="string"><span class="delimiter">"</span><span class="content">home-assistant/sensor01/brightness</span><span class="delimiter">"</span></span>
|
||
<span class="key">unit_of_measurement</span>: <span class="string"><span class="delimiter">"</span><span class="content">cd</span><span class="delimiter">"</span></span>
|
||
</pre></td>
|
||
</tr></table>
|
||
</div>
|
||
|
||
<p>After a restart of Home Assistant the values of your Arduino will be available.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-09-mqtt/arduino.png" />
|
||
The Brightness sensor
|
||
</p>
|
||
|
||
<p>I hope that this post could give you some ideas about the usage Home Assistant and MQTT. If you are working on a cool project that includes Home Assistant, please let us now.</p>
|
||
]]></content>
|
||
</entry>
|
||
|
||
</feed>
|