Site updated at 2015-10-11 20:16:19 UTC

This commit is contained in:
Paulus Schoutsen 2015-10-11 13:16:19 -07:00
parent 0cadd801b9
commit 0f05893e2c
57 changed files with 3700 additions and 559 deletions

View file

@ -0,0 +1,256 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Category: esp8266 | Home Assistant]]></title>
<link href="https://home-assistant.io/blog/categories/esp8266/atom.xml" rel="self"/>
<link href="https://home-assistant.io/"/>
<updated>2015-10-11T13:16:06-07: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[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-11T12:10:00-07: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&rsquo;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/ha-sensor.png' />
Home Assistant will keep track of historical values and allow you to integrate it into automation.
</p>
<h3>Components</h3>
<p>I&rsquo;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 &amp; 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.html#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&rsquo;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 &lsquo;O Leary</li>
<li>Adafruit HDC1000</li>
</ul>
<h3>Sketch</h3>
<p>If you have followed the previous steps, you&rsquo;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>
<pre><code class="cpp">
#include &lt;ESP8266WiFi.h&gt;
#include &lt;Wire.h&gt;
#include &lt;PubSubClient.h&gt;
#include &lt;Adafruit_HDC1000.h&gt;
#define wifi_ssid "YOUR WIFI SSID"
#define wifi_password "WIFI PASSWORD"
#define mqtt_server "YOUR_MQTT_SERVER_HOST"
#define mqtt_user "your_username"
#define mqtt_password "your_password"
#define humidity_topic "sensor/humidity"
#define temperature_topic "sensor/temperature"
WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_HDC1000 hdc = Adafruit_HDC1000();
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
// Set SDA and SDL ports
Wire.begin(2, 14);
// Start sensor
if (!hdc.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
bool checkBound(float newValue, float prevValue, float maxDiff) {
return newValue &lt; prevValue - maxDiff || newValue &gt; prevValue + maxDiff;
}
long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg &gt; 1000) {
lastMsg = now;
float newTemp = hdc.readTemperature();
float newHum = hdc.readHumidity();
if (checkBound(newTemp, temp, diff)) {
temp = newTemp;
Serial.print("New temperature:");
Serial.println(String(temp).c_str());
client.publish(temperature_topic, String(temp).c_str(), true);
}
if (checkBound(newHum, hum, diff)) {
hum = newHum;
Serial.print("New humidity:");
Serial.println(String(hum).c_str());
client.publish(humidity_topic, String(hum).c_str(), true);
}
}
}
</code></pre>
<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>
<pre><code class="yaml">mqtt:
broker: YOUR_MQTT_SERVER_HOST
username: your_username
password: your_password
sensor:
platform: mqtt
name: "Temperature"
state_topic: "sensor/temperature"
qos: 0
unit_of_measurement: "ºC"
sensor 2:
platform: mqtt
name: "Humidity"
state_topic: "sensor/humidity"
qos: 0
unit_of_measurement: "%"
</code></pre>
]]></content>
</entry>
</feed>

View file

@ -0,0 +1,309 @@
<!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>Category: esp8266 - Home Assistant</title>
<meta name="author" content="Paulus Schoutsen">
<meta name="description" content="Category: esp8266">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="https://home-assistant.io/blog/categories/esp8266">
<meta property="fb:app_id" content="338291289691179">
<meta property="og:title" content="Category: esp8266">
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="https://home-assistant.io/blog/categories/esp8266/">
<meta property="og:type" content="website">
<meta property="og:description" content="Category: esp8266">
<meta property="og:image" content="https://home-assistant.io/images/home-assistant-logo-2164x2164.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='/images/favicon-192x192.png'> Home Assistant
</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>
<ul>
<li><a href='/getting-started/'>Installing Home Assistant</a></li>
<li><a href='/getting-started/configuration.html'>Configuration basics</a></li>
<li><a href='/getting-started/devices.html'>Adding devices</a></li>
<li><a href='/getting-started/presence-detection.html'>Presence detection</a></li>
<li><a href='/getting-started/automation.html'>Automation</a></li>
<li><a href='/cookbook'>Configuration cookbook</a></li>
<li><a href='/components/'>Component overview</a></li>
</ul>
</li>
<li>
<a href="/developers/">Developers</a>
<ul>
<li><a href="/developers/architecture.html">Architecture</a></li>
<li><a href="/developers/frontend.html">Frontend development</a></li>
<li><a href="/developers/creating_components.html">
Creating components
</a></li>
<li><a href="/developers/add_new_platform.html">
Adding platform support
</a></li>
<li><a href="/developers/api.html">API</a></li>
<li><a href="/developers/credits.html">Credits</a></li>
</ul>
</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="page">
<header>
<h1 class="title indent">
Category: Esp8266
</h1>
</header>
<hr class="divider">
<div id="archive-list">
<h2>2015</h2>
<article>
<div class="grid">
<div class="grid__item one-fifth palm-one-whole">
<time datetime="2015-10-11T12:10:00-07:00" pubdate>
<span class='month'>Oct</span> <span class='day'>11</span>
</time>
</div>
<div class="grid__item four-fifths palm-one-whole">
<h1 class="gamma"><a href="/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/">Report the temperature with ESP8266 to MQTT</a></h1>
<footer class="meta">
<span>
<i class="icon-tags"></i>
<ul class="tags unstyled">
<li><a class='category' href='/blog/categories/esp8266/'>esp8266</a></li>
<li><a class='category' href='/blog/categories/how-to/'>how-to</a></li>
<li><a class='category' href='/blog/categories/mqtt/'>mqtt</a></li>
</ul>
</span>
</footer>
<hr class="divider">
</div>
</div>
</article>
</div>
</article>
</div>
<aside id="sidebar" class="grid__item one-third lap-one-whole palm-one-whole">
<div class="grid">
<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-url="https://home-assistant.io/blog/categories/esp8266/index.html"
data-counturl="https://home-assistant.io/blog/categories/esp8266/index.html" >Tweet</a>
<div class="g-plusone" data-size="standard"></div>
<div class="fb-share-button" style='top: -6px;'
data-href="https://home-assistant.io/blog/categories/esp8266/index.html"
data-layout="button_count">
</div>
</section>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '338291289691179',
xfbml : true,
version : 'v2.2'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<section id="recent-posts" class="aside-module grid__item one-whole lap-one-half">
<h1 class="title delta">Other Posts</h1>
<ul class="divided">
<li class="post">
<a href="/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/">Report the temperature with ESP8266 to MQTT</a>
</li>
<li class="post">
<a href="/blog/2015/10/11/rfxtrx-blinkstick-and-snmp-support/">0.7.5: Blinkstick, SNMP, Telegram</a>
</li>
<li class="post">
<a href="/blog/2015/10/05/home-assistant-goes-geo-with-owntracks/">Home Assistant goes geo with OwnTracks</a>
</li>
<li class="post">
<a href="/blog/2015/09/19/alarm-sonos-and-itunes-support/">Alarms, Sonos and iTunes now supported</a>
</li>
<li class="post">
<a href="/blog/2015/09/18/monitoring-with-glances-and-home-assistant/">Remote Monitoring with Glances</a>
</li>
</ul>
</section>
</div>
</aside>
</div>
</div>
<footer>
<div class="grid-wrapper">
<div class="grid">
<div class="grid__item">
<p class="copyright">
<span class="credit">Powered by <a href="http://octopress.org">Octopress</a>, <a href='http://jekyllrb.com/'>Jekyll</a> and the <a href='https://github.com/coogie/oscailte'>Oscalite theme</a>. Hosted by <a href='https://pages.github.com/'>GitHub</a> and served by <a href='https://cloudflare.com'>CloudFlare</a>.</span>
</p>
</div>
</div>
</div>
</footer>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
<![endif]-->
<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_script = 'count.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>