762 lines
42 KiB
XML
762 lines
42 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||
|
||
<title><![CDATA[Category: How-To | Home Assistant]]></title>
|
||
<link href="https://home-assistant.io/blog/categories/how-to/atom.xml" rel="self"/>
|
||
<link href="https://home-assistant.io/"/>
|
||
<updated>2016-01-18T01:40:32+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[Set up encryption using Let's Encrypt]]></title>
|
||
<link href="https://home-assistant.io/blog/2015/12/13/setup-encryption-using-lets-encrypt/"/>
|
||
<updated>2015-12-13T18:05:00+00:00</updated>
|
||
<id>https://home-assistant.io/blog/2015/12/13/setup-encryption-using-lets-encrypt</id>
|
||
<content type="html"><![CDATA[<p>Exposing your Home Assistant instance outside of your network always has been tricky. You have to set up port forwarding on your router and most likely add a dynamic DNS service to work around your ISP changing your IP. After this you would be able to use Home Assistant from anywhere but there is one big red flag: no encryption.</p>
|
||
|
||
<p>This tutorial will take you through the steps to setup a dynamic DNS for your IP and allow trusted encrypted connection to it - for free using <a href="https://duckdns.org">DuckDNS</a> and <a href="https://letsencrypt.org">Let’s Encrypt</a>.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-12-lets-encrypt/letsencrypt-secured.png" />
|
||
</p>
|
||
|
||
<!--more-->
|
||
|
||
<h3><a class='title-link' name='requirements' href='#requirements'></a> Requirements </h3>
|
||
|
||
<p>The DuckDNS part of this tutorial has no requirements but there are a few requirements as of now to run the Let’s Encrypt client.</p>
|
||
|
||
<ul>
|
||
<li>Direct connection to the internet or admin access to your router to set up port forwarding</li>
|
||
<li>A machine running a Unix-ish OS that include Python 2.6 or 2.7 (Docker can be used)</li>
|
||
</ul>
|
||
|
||
<p><img src="https://home-assistant.io/images/supported_brands/duckdns.png" style="clear: right; border:none; box-shadow: none; float: right; margin-left: 8px; margin-bottom: 8px;" width="60" /></p>
|
||
|
||
<h3><a class='title-link' name='duckdns' href='#duckdns'></a> DuckDNS </h3>
|
||
|
||
<p>The first step is to set up <a href="https://duckdns.org">DuckDNS</a>. This is a free dynamic DNS service that you can use to get a DuckDNS.org subdomain to point at your house. A dynamic DNS service works by having your home computer tell DuckDNS.org every 5 minutes what its IP is so that DuckDNS can make sure your domain name is set up correctly.</p>
|
||
|
||
<p>For this example we will assume our domain is hass-example.duckdns.org.</p>
|
||
|
||
<p>First step is to acquire and set up our domain name. For this, go to <a href="https://duckdns.org">DuckDNS</a>, log in with any of the supported login providers and add a domain. After this check out their <a href="https://www.duckdns.org/install.jsp">installation instructions</a> to finish your installation of DuckDNS. If you’re on a Raspberry Pi, see ‘Pi’ in the category ‘Operating Systems’.</p>
|
||
|
||
<p><img src="https://home-assistant.io/images/supported_brands/letsencrypt.png" style="clear: right; border:none; box-shadow: none; float: right; margin-left: 8px; margin-bottom: 8px;" width="60" /></p>
|
||
|
||
<h3><a class='title-link' name='lets-encrypt' href='#lets-encrypt'></a> Let’s Encrypt </h3>
|
||
|
||
<p><a href="https://letsencrypt.org">Let’s Encrypt</a> is a free, automated, and open certificate authority (CA). We will use this to acquire a certificate that can be used to encrypted our connection with Home Assistant.</p>
|
||
|
||
<p>Let’s Encrypt will give you a free 90-day certificate if you pass their domain validation challenge. Domains are validated by having certain data be accessible on your domain for Let’s Encrypt (<a href="https://letsencrypt.org/howitworks/technology/">they describe it better themselves</a>).</p>
|
||
|
||
<p>Assuming that your home is behind a router, the first thing to do is to set up port forwarding from your router to your computer that will run Let’s Encrypt. For the Let’s Encrypt set up we need to temporary forward ports <code>80</code> (http connections) and <code>443</code> (https connections). This can be set up by accessing your router admin interface (<a href="http://portforward.com">Site with port forwarding instructions per router</a>).</p>
|
||
|
||
<p>Now you’re ready to run Let’s Encrypt:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ git clone https://github.com/letsencrypt/letsencrypt
|
||
[…]
|
||
$ cd letsencrypt
|
||
$ ./letsencrypt-auto certonly --email $ ./letsencrypt-auto certonly --email your@email.address -d hass-example.duckdns.org
|
||
|
||
Updating letsencrypt and virtual environment dependencies.......
|
||
Running with virtualenv: sudo /path/letsencrypt/bin/letsencrypt certonly --email your@e-mail.address -d hass-example.duckdns.org
|
||
|
||
IMPORTANT NOTES:
|
||
- Congratulations! Your certificate and chain have been saved at
|
||
/etc/letsencrypt/live/hass-example.duckdns.org/fullchain.pem. Your cert
|
||
will expire on 2016-03-12. To obtain a new version of the
|
||
certificate in the future, simply run Let's Encrypt again.
|
||
- If like Let's Encrypt, please consider supporting our work by:
|
||
|
||
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
|
||
Donating to EFF: https://eff.org/donate-le
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>If you’re using Docker, run the following command to generate the required keys:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>sudo mkdir /etc/letsencrypt /var/lib/letsencrypt
|
||
sudo docker run -it --rm -p 443:443 -p 80:80 --name letsencrypt \
|
||
-v "/etc/letsencrypt:/etc/letsencrypt" \
|
||
-v "/var/lib/letsencrypt:/var/lib/letsencrypt" \
|
||
quay.io/letsencrypt/letsencrypt:latest certonly \
|
||
--email your@e-mail.address -d hass-example.duckdns.org
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>With either method your certificate will be generated and put in the directory <code>/etc/letsencrypt/live/hass-example.duckdns.org</code>. As the lifetime is only 90 days, you will have to repeat this every 90 days.</p>
|
||
|
||
<p><img width="60" src="https://home-assistant.io/images/favicon-192x192.png" style="float: right; border:none; box-shadow: none;" /></p>
|
||
|
||
<h3><a class='title-link' name='home-assistant' href='#home-assistant'></a> Home Assistant </h3>
|
||
|
||
<p>Before updating the Home Assistant configuration, we have to update the port forwarding at your router config. We can drop the port forwarding for port <code>80</code> as we no longer care about unecrypted messages. Update port <code>443</code> to forward to port <code>8123</code> on the computer that will run Home Assistant.</p>
|
||
|
||
<p>The final step is to point Home Assistant at the generated certificates. Before you do this, make sure that the user running Home Assistant has read access to the folder that holds the certificates.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="key">http</span>:
|
||
<span class="key">api_password</span>: <span class="string"><span class="content">YOUR_SECRET_PASSWORD</span></span>
|
||
<span class="key">ssl_certificate</span>: <span class="string"><span class="content">/etc/letsencrypt/live/hass.example.com/fullchain.pem</span></span>
|
||
<span class="key">ssl_key</span>: <span class="string"><span class="content">/etc/letsencrypt/live/hass.example.com/privkey.pem</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>You can now navigate to https://hass-example.duckdns.org and enjoy encryption!</p>
|
||
|
||
<p><em>Big thanks to Fabian Affolter for his help and feedback on this article.</em></p>
|
||
|
||
]]></content>
|
||
</entry>
|
||
|
||
<entry>
|
||
<title type="html"><![CDATA[Activating Tasker tasks from Home Assistant using command line switches]]></title>
|
||
<link href="https://home-assistant.io/blog/2015/12/10/activating-tasker-tasks-from-home-assistant-using-command-line-switches/"/>
|
||
<updated>2015-12-10T10:39:41+00:00</updated>
|
||
<id>https://home-assistant.io/blog/2015/12/10/activating-tasker-tasks-from-home-assistant-using-command-line-switches</id>
|
||
<content type="html"><![CDATA[<p><img src="https://home-assistant.io/images/blog/2015-12-tasker/tasker-logo.png" style="clear: right; border:none; box-shadow: none; float: right; margin-bottom: 12px;" width="200" /><br />
|
||
In this tutorial I will explain how you can activate Tasker tasks from Home Assistant command line switches. We are going to set up a switch that when toggled will make your Android device say either “On” or “Off”.</p>
|
||
|
||
<p>You could also do this with the automation component instead so whenever you put your house to sleep mode for example your Android device will open up Google Play Books or the Kindle app ready for you to read as well as dimming your lights, but this tutorial is all about the switches.</p>
|
||
|
||
<!--more-->
|
||
|
||
<h3>AutoRemote URL</h3>
|
||
|
||
<p>First things first you should install <a href="https://play.google.com/store/apps/details?id=net.dinglisch.android.taskerm">Tasker</a> and <a href="https://play.google.com/store/apps/details?id=com.joaomgcd.autoremote">AutoRemote</a> onto your Android device and launch AutoRemote. You should see a URL above the QR code, visit it in your browser and it should bring up a page a bit like this.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-12-tasker/screenshot-1.png" />
|
||
</p>
|
||
|
||
<p>Now type in <code>SayOn</code> in the <code>Message</code> box and you should see a box appear on the right with a URL in it, this is what we will be using in the Python script later on so save that for later. Do the same thing again but this time replace <code>SayOn</code> with <code>SayOff</code>. Now just click the <code>Send message now!</code> button to test that your commands will get sent to your Android device, if they do you will see a toast message at the bottom of your screen like this one.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-12-tasker/screenshot-2.png" height="450" />
|
||
</p>
|
||
|
||
<h3>Tasker Setup</h3>
|
||
|
||
<p>Open up Tasker and make sure you’re in the <code>PROFILES</code> tab, then select the plus icon to create a new profile. Select <code>Event</code> -> <code>Plugin</code> -> <code>AutoRemote</code> -> <code>AutoRemote</code> and then the pencil icon to configure the AutoRemote event. Select <code>Message Filter</code> and enter in <code>SayOn</code> then go back until it asks you for a task. Select <code>New task</code> then just leave the next field blank and select the tick icon. <br />
|
||
This is where we’ll configure our task, so select the plus icon to select an action. Select <code>Alert</code> -> <code>Say</code> to add a Say action. Enter <code>On</code> in the text field and go back to test your task, make sure your media volume is up then select the play icon, you should hear your device say “On”.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-12-tasker/screenshot-3.png" height="450" />
|
||
</p>
|
||
|
||
<p>Now you can go back to the main Tasker screen and create another profile but this time replace <code>SayOn</code> with <code>SayOff</code> and <code>On</code> with <code>Off</code>. After you’ve done that go to the main screen again and select the menu button at the top then <code>Exit</code> and <code>Save first</code> to make sure everything is saved properly.</p>
|
||
|
||
<h3>Python Script</h3>
|
||
|
||
<p>Now it’s time to set it up the script, so create a new Python script and name it <code>On.py</code> then enter this code:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="keyword">import</span> <span class="include">requests</span>
|
||
requests.get(<span class="string"><span class="delimiter">'</span><span class="content">[URL]</span><span class="delimiter">'</span></span>)
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Enter in your “On” URL then save it. Create another script but this time call it <code>Off.py</code> and enter your “off” URL instead.</p>
|
||
|
||
<h3>Home Assistant Configuration</h3>
|
||
|
||
<p>Add a command line switch to your Home Assistant configuration:</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">command_switch</span></span>
|
||
<span class="key">switches</span>:
|
||
<span class="key">tasker_say</span>:
|
||
<span class="key">oncmd</span>: <span class="string"><span class="content">python "[LocationOfOnScript]"</span></span>
|
||
<span class="key">offcmd</span>: <span class="string"><span class="content">python "[LocationOfOffScript]"</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Now load up Home Assistant and whenever you toggle the switch you created your Android device will respond with either “On” or “Off”. :-)</p>
|
||
]]></content>
|
||
</entry>
|
||
|
||
<entry>
|
||
<title type="html"><![CDATA[InfluxDB and Grafana]]></title>
|
||
<link href="https://home-assistant.io/blog/2015/12/07/influxdb-and-grafana/"/>
|
||
<updated>2015-12-07T14:15:13+00:00</updated>
|
||
<id>https://home-assistant.io/blog/2015/12/07/influxdb-and-grafana</id>
|
||
<content type="html"><![CDATA[<p><img src="https://home-assistant.io/images/supported_brands/influxdb.png" style="clear: right; border:none; box-shadow: none; float: right; margin-bottom: 12px;" width="200" /><img src="https://home-assistant.io/images/supported_brands/grafana.png" style="clear: right; border:none; box-shadow: none; float: right; margin-bottom: 12px;" width="200" /><br />
|
||
The <a href="https://influxdb.com/">InfluxDB</a> database is a so-called time series database primarly designed to store sensor data and real-time analytics.</p>
|
||
|
||
<p>The <code>influxdb</code> component makes it possible to transfer all state changes from Home Assistant to an external <a href="https://influxdb.com/">InfluxDB</a> database.</p>
|
||
|
||
<!--more-->
|
||
|
||
<p>The first step is to install the InfluxDB packages. If you are not running Fedora, check the <a href="https://influxdb.com/docs/v0.9/introduction/installation.html">installation</a> section for further details.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo dnf -y install http://influxdb.s3.amazonaws.com/influxdb-0.9.5.1-1.x86_64.rpm
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Launch the InfluxDB service.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo systemctl start influxdb
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>If everything went well, then the web interface of the database should be accessible at <a href="http://localhost:8083/">http://localhost:8083/</a>. Create a database <code>home_assistant</code> to use with Home Assistant either with the web interface or the commandline tool <code>influx</code>.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-12-influxdb/influxdb-frontend.png" />
|
||
InfluxDB web frontend
|
||
</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ influx
|
||
Visit https://enterprise.influxdata.com to register for updates, InfluxDB server management, and monitoring.
|
||
Connected to http://localhost:8086 version 0.9.5.1
|
||
InfluxDB shell 0.9.5.1
|
||
> CREATE DATABASE home_assistant
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>An optional step is to create a user. Keep in mind to adjust the configuration (add <code>username</code> and <code>password</code>) in the next step if you prefer to go this way.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>> CREATE USER "home-assistant" WITH PASSWORD 'password'
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>To use the <code>influxdb</code> component in your installation, add the following to your <code>configuration.yaml</code> file:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="key">influxdb</span>:
|
||
<span class="key">host</span>: <span class="string"><span class="content">127.0.0.1</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>After you restart Home Assistant you should see that the InfluxDB database gets filled. The <a href="https://influxdb.com/docs/v0.9/query_language/index.html">language</a> to query the database is similar to SQL.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ influx
|
||
[...]
|
||
> USE home_assistant
|
||
Using database home_assistant
|
||
> SELECT * FROM binary_sensor
|
||
name: binary_sensor
|
||
-------------------
|
||
time domain entity_id value
|
||
1449496577000000000 binary_sensor bathroom_door 0
|
||
1449496577000000000 binary_sensor bathroom_window 0
|
||
1449496577000000000 binary_sensor basement_door 0
|
||
1449496577000000000 binary_sensor basement_window 0
|
||
1449496684000000000 binary_sensor bathroom_window 1
|
||
[...]
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p><a href="http://grafana.org/">Grafana</a> is a dashboard that can create graphs from different sources including InfluxDB. The installation is simple, and there are detailed steps for many different configurations on the <a href="http://docs.grafana.org/installation/">Grafana installation</a> page. For a recent system that is running Fedora:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo dnf -y install https://grafanarel.s3.amazonaws.com/builds/grafana-2.5.0-1.x86_64.rpm
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Start the grafana server.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo systemctl daemon-reload
|
||
$ sudo systemctl start grafana-server
|
||
$ sudo systemctl status grafana-server
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Login with the username <code>admin</code> and the password <code>admin</code> at <a href="http://localhost:3000/login">http://localhost:3000/login</a>. Now follow the <a href="http://docs.grafana.org/datasources/influxdb/">InfluxDB setup instructions</a>.</p>
|
||
|
||
<p>Now you can start to create dashboards and graphs. You have various options to get the data from the graph. The next image just shows a screenshot of the setting for a temperature sensor.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-12-influxdb/grafana-settings.png" />
|
||
Grafana settings
|
||
</p>
|
||
|
||
<p>If the graph is not showing up in the dashboard you need to adjust the time range in the right upper corner. The graph is created for all state changes recorded by Home Assistant.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-12-influxdb/grafana-graph.png" />
|
||
Grafana Temperature graph
|
||
</p>
|
||
|
||
]]></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[Remote Monitoring with Glances]]></title>
|
||
<link href="https://home-assistant.io/blog/2015/09/18/monitoring-with-glances-and-home-assistant/"/>
|
||
<updated>2015-09-18T09:00:00+00:00</updated>
|
||
<id>https://home-assistant.io/blog/2015/09/18/monitoring-with-glances-and-home-assistant</id>
|
||
<content type="html"><![CDATA[<p><img src="https://home-assistant.io/images/supported_brands/glances.png" style="border:none; box-shadow: none; float: right;" height="80" /><br />
|
||
Inspried by a <a href="https://github.com/balloob/home-assistant/issues/310">feature requests</a> I started looking into the available options to do monitoring of remote hosts. The feature request is about displaying system information in a similar way than the <a href="/components/sensor.systemmonitor/">systemmonitor</a> sensor does it for the local system. After a while I started to think that it would be a nice addition for a small home network where no full-blown system monitoring setup is present.</p>
|
||
|
||
<!--more-->
|
||
|
||
<p>The basic problem is to get the data from the remote host. Starting with <a href="https://pypi.python.org/pypi/psutil">psutil</a> that is used by the systemmonitor sensor, a possible solution is only a click away and named <a href="https://github.com/nicolargo/glances">Glances</a>. Glances has a nice curses-based interface and a <a href="https://github.com/nicolargo/glances/wiki/The-Glances-RESTFULL-JSON-API">RESTful API</a>.</p>
|
||
|
||
<p>The <a href="/components/sensor.glances/">Glances sensor</a> sensor uses that API to get all needed data.</p>
|
||
|
||
<p>In this post a default Fedora 22 Workstation installation is used on the host that should be monitored. In fact, it doesn’t matter if the system is the local one or a remote one as long as Glances is available. With some adjustments it should work on your own systems too. The difference will be the package and the firewall management tools.</p>
|
||
|
||
<p>First some extra packages are needed beside Glances, especially the <a href="http://bottlepy.org/docs/dev/index.html">bottle</a> webserver. I guess that Glances is available for your distribution as well. Otherwise follow those <a href="https://github.com/nicolargo/glances#installation">instructions</a>.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo dnf -y install glances python-bottle
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>On Fedora the Firewall settings are strict. Let’s open port 61208 to allow other hosts to connect to that port. This is not needed if you just want to observe your local machine.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ sudo firewall-cmd --permanent --add-port=61208/tcp
|
||
$ sudo firewall-cmd --reload
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Launch <code>glances</code> and keep an eye on the output.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ glances -w
|
||
Glances web server started on http://0.0.0.0:61208/
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Now browse to http://IP_ADRRESS:61208/. You should see the webified view of Glances.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-09-glances/web-glances.png" />
|
||
Glances web interface
|
||
</p>
|
||
|
||
<p>Another check is to access the API located at http://IP_ADRRESS:61208/api/2/mem/used and to confirm that a detail about your memory usage is provided as a JSON response. If so, you are good to proceed.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ curl -X GET http://IP_ADDRESS:61208/api/2/mem/used
|
||
{"used": 203943936}
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Add the <a href="/components/sensor.glances/">glances sensor</a> entry to your <code>configuration.yaml</code> file and restart Home Assistant then.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="comment"># Example configuration.yaml entry</span>
|
||
- <span class="string"><span class="content">platform: glances</span></span>
|
||
<span class="key">name</span>: <span class="string"><span class="content">NAS</span></span>
|
||
<span class="key">host</span>: <span class="string"><span class="content">IP_ADDRESS</span></span>
|
||
<span class="key">resources</span>:
|
||
- <span class="string"><span class="content">'disk_use_percent'</span></span>
|
||
- <span class="string"><span class="content">'disk_use'</span></span>
|
||
- <span class="string"><span class="content">'disk_free'</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>If there are no error in the log file then you should see your new sensors.</p>
|
||
|
||
<p class="img">
|
||
<img src="https://home-assistant.io/images/blog/2015-09-glances/sensors.png" />
|
||
The Glances sensors
|
||
</p>
|
||
|
||
<p><a href="https://github.com/nicolargo/glances">Glances</a> has a couple of optional dependencies which are extenting the range of provided information. This means that it would be possible to get details about the RAID system, HDD temperature, IP addresses, sensors, etc., please create a <a href="https://github.com/balloob/home-assistant/pulls">Pull request</a> with your additions or a <a href="https://github.com/balloob/home-assistant/issues/new">Feature request</a> if you want see more details in your Home Assistant frontend.</p>
|
||
]]></content>
|
||
</entry>
|
||
|
||
</feed>
|