home-assistant.github.io/blog/2017/03/28/http-to-mqtt-bridge/index.html
2017-05-01 13:58:02 +00:00

234 lines
16 KiB
HTML

<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>HTTP to MQTT bridge - Home Assistant</title>
<meta name="author" content="petkov">
<meta name="description" content="How to integrate IFTTT with HA using MQTT">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="https://home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/">
<meta property="fb:app_id" content="338291289691179">
<meta property="og:title" content="HTTP to MQTT bridge">
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="https://home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/">
<meta property="og:type" content="article">
<meta property="og:description" content="How to integrate IFTTT with HA using MQTT">
<meta property="og:image" content="https://home-assistant.io/images/blog/2017-03-bridge/social.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@home_assistant">
<meta name="twitter:title" content="HTTP to MQTT bridge">
<meta name="twitter:description" content="How to integrate IFTTT with HA using MQTT">
<meta name="twitter:image" content="https://home-assistant.io/images/blog/2017-03-bridge/social.png">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet">
<link href="/atom.xml" rel="alternate" title="Home Assistant" type="application/atom+xml">
<link rel='shortcut icon' href='/images/favicon.ico' />
<link rel='icon' type='image/png' href='/images/favicon-192x192.png' sizes='192x192' />
</head>
<body >
<header>
<div class="grid-wrapper">
<div class="grid">
<div class="grid__item three-tenths lap-two-sixths palm-one-whole ha-title">
<a href="/" class="site-title">
<img width='40' src='/demo/favicon-192x192.png'>
<span>Home Assistant</span>
</a>
</div>
<div class="grid__item seven-tenths lap-four-sixths palm-one-whole">
<nav>
<input type="checkbox" id="toggle">
<label for="toggle" class="toggle" data-open="Main Menu" data-close="Close Menu"></label>
<ul class="menu pull-right">
<li><a href="/getting-started/">Getting started</a></li>
<li><a href="/components/">Components</a></li>
<li><a href="/docs/">Docs</a></li>
<li><a href="/cookbook/">Examples</a></li>
<li><a href="/developers/">Developers</a></li>
<li><a href="/blog/">Blog</a></li>
<li><a href="/help/">Need help?</a></li>
</ul>
</nav>
</div>
</div>
</div>
</header>
<div class="grid-wrapper">
<div class="grid grid-center">
<div class="grid__item two-thirds lap-one-whole palm-one-whole">
<article class="post">
<header>
<h1 class="title indent">HTTP to MQTT bridge</h1>
<div class="meta clearfix">
<time datetime="2017-03-28T06:00:00+00:00" pubdate data-updated="true"><i class="icon-calendar"></i> March 28, 2017</time>
<span class="byline author vcard"><i class='icon-user'></i> petkov</span>
<span><i class='icon-time'></i> three minutes reading time</span>
<span>
<i class="icon-tags"></i>
<ul class="tags unstyled">
<li>How-To</li>
</ul>
</span>
<a class='comments'
href="#disqus_thread"
>Comments</a>
</div>
</header>
<p>The idea of creating <a href="https://github.com/petkov/http_to_mqtt">HTTP to MQTT bridge</a> appeared when I was trying to integrate Google Assistant with my Home Assistant after watching <a href="https://youtu.be/087tQ7Ly7f4?t=265">BRUH Automation</a> video. Right now there is no MQTT service available in <a href="https://ifttt.com/about">IFTTT</a>. Existing integration solution uses <a href="https://ifttt.com/maker_webhooks">Maker Webhooks</a> which requires that your Home Assistant instance is publically accessible, which I think brings some security concerns or simply not always possible to set up.</p>
<p>The HTTP to MQTT bridge should fill that gap. The idea is to receive messages using HTTP requests and transfer them to your MQTT broker, which can be contacted by Home Assistant. The HTTP to MQTT bridge is written using Node.js with <a href="https://expressjs.com/">Express</a> for the server part and <a href="https://www.npmjs.com/package/mqtt">MQTT.js</a> for the client.</p>
<a name="read-more"></a>
<p>The app could be hosted on any Node.js hosting. I prefer <a href="https://www.heroku.com/home">Heroku: Cloud Application Platform</a> for its simplicity.</p>
<h3><a class="title-link" name="bringing-pieces-together" href="#bringing-pieces-together"></a> Bringing pieces together</h3>
<ol>
<li>Configure the Home Assistant <a href="https://home-assistant.io/docs/automation/trigger/#mqtt-trigger">MQTT trigger</a>.</li>
<li>Configure <a href="https://www.cloudmqtt.com/">CloudMQTT</a>. Check this <a href="https://www.youtube.com/watch?v=VaWdvVVYU3A">video tutorial</a> for details.</li>
<li><a href="https://heroku.com/deploy?template=https://github.com/petkov/http_to_mqtt"><img src="https://www.herokucdn.com/deploy/button.svg" alt="Deploy" /></a> HTTP to MQTT bridge app.</li>
<li>Add the <a href="https://devcenter.heroku.com/articles/config-vars#setting-up-config-vars-for-a-deployed-application">Configuration Variables</a> to your Heroku app mentioned here.
<ul>
<li>AUTH_KEY: Can be any string, eg. <code class="highlighter-rouge">912ec803b2ce49e4a541068d495ab570</code>.</li>
<li>MQTT_HOST: The host of your MQTT broker, eg. mqtts://k99.cloudmqtt.com:21234.</li>
<li>MQTT_USER: MQTT username</li>
<li>MQTT_PASS: MQTT password</li>
</ul>
</li>
<li>Create an IFTTT applet the same way as described in <a href="https://youtu.be/087tQ7Ly7f4?t=265">BRUH Automation</a> video.</li>
<li>Configure <a href="https://ifttt.com/maker_webhooks">Maker Webhooks</a> service with below parameters.
<ul>
<li>URL: <code class="highlighter-rouge">https://&lt;app_name&gt;.herokuapp.com/post/</code></li>
<li>Method: <code class="highlighter-rouge">POST</code></li>
<li>Content Type: <code class="highlighter-rouge">application/json</code></li>
<li>Body: <code class="highlighter-rouge"><span class="p">{</span><span class="nt">"topic"</span><span class="p">:</span><span class="s2">"&lt;mqtt_topic&gt;"</span><span class="p">,</span><span class="nt">"message"</span><span class="p">:</span><span class="s2">"&lt;mqtt_message&gt;"</span><span class="p">,</span><span class="nt">"key"</span><span class="p">:</span><span class="s2">"&lt;AUTH_KEY&gt;"</span><span class="p">}</span></code></li>
</ul>
</li>
</ol>
<h3><a class="title-link" name="subscribe-to-latest-version" href="#subscribe-to-latest-version"></a> Subscribe to latest version</h3>
<p>Additionally you can make Heroku to update the HTTP to MQTT bridge app to the latest available version from the GitHub repository automatically. To do this follow the instruction on the <a href="https://devcenter.heroku.com/articles/github-integration#automatic-deploys">Heroku help page</a>.</p>
<h3><a class="title-link" name="improve-response-time" href="#improve-response-time"></a> Improve response time</h3>
<p>After 30 minutes of inactivity Heroku will put your app into sleep mode. This will result in ~10 seconds response time. To prevent Heroku from putting your app into sleep mode, ping it every 10 minutes. You can do that by sending regular HTTP GET request to http://your_app/keep_alive/. But be careful. Heroku free quota is 550 hours per month. Without sleeping your app will be allowed to run only 22 days a month. Additionally the <code class="highlighter-rouge">keep_alive</code> method will send a simple MQTT message to prevent the broker from sleeping as well. The topic and message can be configured using Heroku environment variables <code class="highlighter-rouge">KEEP_ALIVE_TOPIC</code> and <code class="highlighter-rouge">KEEP_ALIVE_MESSAGE</code> and both are set to “keep_alive” by default.</p>
<p>You can even configure Home Assistant to ping HTTP to MQTT bridge every 10 minutes during daytime. Below is an example of how to do that:</p>
<div class="language-yaml highlighter-rouge"><pre class="highlight"><code><span class="s">rest_command</span><span class="pi">:</span>
<span class="s">http_to_mqtt_keep_alive</span><span class="pi">:</span>
<span class="s">url</span><span class="pi">:</span> <span class="s">https://&lt;your_app_address&gt;/keep_alive/</span>
<span class="s">method</span><span class="pi">:</span> <span class="s">get</span>
<span class="s">automation</span><span class="pi">:</span>
<span class="s">alias</span><span class="pi">:</span> <span class="s">HTTP to MQTT keep alive</span>
<span class="s">trigger</span><span class="pi">:</span>
<span class="s">platform</span><span class="pi">:</span> <span class="s">time</span>
<span class="s">minutes</span><span class="pi">:</span> <span class="s1">'</span><span class="s">/10'</span>
<span class="s">seconds</span><span class="pi">:</span> <span class="s">00</span>
<span class="s">condition</span><span class="pi">:</span>
<span class="s">condition</span><span class="pi">:</span> <span class="s">time</span>
<span class="s">after</span><span class="pi">:</span> <span class="s1">'</span><span class="s">7:30:00'</span>
<span class="s">before</span><span class="pi">:</span> <span class="s1">'</span><span class="s">23:59:59'</span>
<span class="s">action</span><span class="pi">:</span>
<span class="s">service</span><span class="pi">:</span> <span class="s">rest_command.http_to_mqtt_keep_alive</span>
</code></pre>
</div>
<h3><a class="title-link" name="thanks" href="#thanks"></a> Thanks</h3>
<p>Special thanks to Ben from <a href="https://www.youtube.com/channel/UCLecVrux63S6aYiErxdiy4w/featured">BRUH Automation</a> for awesome tutorials which inspired me to do this project.</p>
</article>
<section id="disqus">
<h3 class="indent title">Comments</h3>
<div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript></div>
</section>
</div>
<aside id="sidebar" class="grid__item one-third lap-one-whole palm-one-whole">
<div class="grid">
<section class="aside-module grid__item one-whole lap-one-half">
<h1 class="title delta">About Home Assistant</h1>
<ul class="divided">
<li>
Home Assistant is an open-source home automation platform running on Python 3. Track and control all devices at home and automate control.
</li>
<li><a href='/getting-started/'>Get started with Home Assistant</a></li>
<li><a href='/demo/'>Try the online demo</a></li>
<li><a class="twitter-follow-button" href="https://twitter.com/Home_Assistant">Follow Home Assistant on Twitter</a></li>
<li><div class="fb-like" data-href="https://www.facebook.com/homeassistantio/" data-layout="standard" data-action="like" data-size="small" data-show-faces="true" data-share="false"></div></li>
</ul>
</section>
<div id="fb-root"></div>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.async=true;js.src='//platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<script>(function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(d.getElementById(id)){return;}js=d.createElement(s);js.id=id;js.async=true;js.src="//connect.facebook.net/en_US/all.js#appId=338291289691179&xfbml=1";fjs.parentNode.insertBefore(js,fjs);}(document,'script','facebook-jssdk'));</script>
<section class="sharing aside-module grid__item one-whole lap-one-half">
<h1 class="title delta">Share this post</h1>
<a href="//twitter.com/share"
class="twitter-share-button"
data-via="home_assistant"
data-related="home_assistant"
data-url="https://home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/"
data-counturl="https://home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/" >Tweet</a>
<div class="fb-share-button" style='top: -6px;'
data-href="https://home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/"
data-layout="button_count">
</div>
<div class="g-plusone" data-size="standard"></div>
</section>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<section id="recent-posts" class="aside-module grid__item one-whole lap-one-half">
<h1 class="title delta">Recent Posts</h1>
<ul class="divided">
<li class="post">
<a href="/blog/2017/05/01/home-assistant-on-raspberry-pi-zero-in-30-minutes/">Home Assistant on a Pi Zero W in 30 minutes</a>
</li>
<li class="post">
<a href="/blog/2017/04/30/hassbian-1.21-its-about-time/">HASSbian 1.21 - It's about time isn't it</a>
</li>
<li class="post">
<a href="/blog/2017/04/25/influxdb-grafana-docker/">Setting up InfluxDB and Grafana using Docker</a>
</li>
<li class="post">
<a href="/blog/2017/04/24/hardware-contest-2017/">Hardware Contest 2017</a>
</li>
<li class="post">
<a href="/blog/2017/04/22/ikea-tradfri-spotify/">Home Assistant 0.43: IKEA Trådfri, Spotify and our iOS app is live</a>
</li>
</ul>
</section>
</div>
</aside>
</div>
</div>
<footer>
<div class="grid-wrapper">
<div class="grid">
<div class="grid__item">
<div class="copyright">
<a rel="me" href='https://twitter.com/home_assistant'><i class="icon-twitter"></i></a>
<a rel="me" href='https://facebook.com/homeassistantio'><i class="icon-facebook"></i></a>
<a rel="me" href='https://plus.google.com/110560654828510104551'><i class="icon-google-plus"></i></a>
<a rel="me" href='https://github.com/home-assistant/home-assistant'><i class="icon-github"></i></a>
<div class="credit">
Contact us at <a href='mailto:hello@home-assistant.io'>hello@home-assistant.io</a>.<br>
Website powered by <a href='http://jekyllrb.com/'>Jekyll</a> and the <a href='https://github.com/coogie/oscailte'>Oscalite theme</a>.<br />
Hosted by <a href='https://pages.github.com/'>GitHub</a> and served by <a href='https://cloudflare.com'>CloudFlare</a>.
</div>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">home-assistant.io</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
</div>
</div>
</div>
</div>
</footer>
<script>
var _gaq=[['_setAccount','UA-57927901-1'],['_trackPageview']];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
s.parentNode.insertBefore(g,s)}(document,'script'));
</script>
<script>
var disqus_shortname = 'home-assistant';
// var disqus_developer = 1;
var disqus_identifier = 'https://home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/';
var disqus_url = 'https://home-assistant.io/blog/2017/03/28/http-to-mqtt-bridge/';
var disqus_script = 'embed.js';
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}());
</script>
</body>
</html>