Site updated at 2015-10-11 20:16:19 UTC
This commit is contained in:
parent
0cadd801b9
commit
0f05893e2c
57 changed files with 3700 additions and 559 deletions
|
@ -4,7 +4,7 @@
|
|||
<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>2015-10-11T11:39:41-07:00</updated>
|
||||
<updated>2015-10-11T13:16:06-07:00</updated>
|
||||
<id>https://home-assistant.io/</id>
|
||||
<author>
|
||||
<name><![CDATA[Paulus Schoutsen]]></name>
|
||||
|
@ -13,6 +13,246 @@
|
|||
<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’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’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.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’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>
|
||||
|
||||
<pre><code class="cpp">
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <Wire.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <Adafruit_HDC1000.h>
|
||||
|
||||
#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 < prevValue - maxDiff || newValue > 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 > 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>
|
||||
|
||||
<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/"/>
|
||||
|
|
|
@ -117,6 +117,47 @@
|
|||
<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>
|
||||
|
||||
|
||||
|
||||
<article>
|
||||
|
||||
|
||||
<div class="grid">
|
||||
|
||||
<div class="grid__item one-fifth palm-one-whole">
|
||||
|
@ -213,6 +254,8 @@
|
|||
|
||||
<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>
|
||||
|
@ -285,6 +328,12 @@
|
|||
<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>
|
||||
|
@ -308,12 +357,6 @@
|
|||
</li>
|
||||
|
||||
|
||||
|
||||
<li class="post">
|
||||
<a href="/blog/2015/09/13/home-assistant-meets-ifttt/">Home Assistant meets IFTTT</a>
|
||||
</li>
|
||||
|
||||
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue