From 0f05893e2c916d4dc56d9e4925386d9fcf53e3d8 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 11 Oct 2015 13:16:19 -0700 Subject: [PATCH] Site updated at 2015-10-11 20:16:19 UTC --- atom.xml | 399 ++++++++++- blog/2014/12/18/website-launched/index.html | 12 +- .../index.html | 12 +- .../index.html | 12 +- .../index.html | 12 +- blog/2015/01/13/nest-in-da-house/index.html | 12 +- blog/2015/01/24/release-notes/index.html | 12 +- .../2015/02/08/looking-at-the-past/index.html | 12 +- blog/2015/02/24/streaming-updates/index.html | 12 +- .../index.html | 12 +- blog/2015/03/08/new-logo/index.html | 12 +- blog/2015/03/11/release-notes/index.html | 12 +- blog/2015/03/22/release-notes/index.html | 12 +- blog/2015/04/25/release-notes/index.html | 12 +- .../05/09/utc-time-zone-awareness/index.html | 12 +- blog/2015/05/14/release-notes/index.html | 12 +- blog/2015/06/10/release-notes/index.html | 12 +- .../index.html | 12 +- .../index.html | 12 +- .../index.html | 12 +- .../index.html | 12 +- .../index.html | 12 +- .../index.html | 14 +- .../13/home-assistant-meets-ifttt/index.html | 8 +- .../index.html | 12 +- .../alarm-sonos-and-itunes-support/index.html | 12 +- .../index.html | 12 +- .../index.html | 673 ++++++++++++++++++ .../index.html | 12 +- blog/archives/index.html | 57 +- blog/categories/architecture/atom.xml | 2 +- blog/categories/architecture/index.html | 12 +- blog/categories/branding/atom.xml | 2 +- blog/categories/branding/index.html | 12 +- blog/categories/component/atom.xml | 2 +- blog/categories/component/index.html | 12 +- blog/categories/core/atom.xml | 2 +- blog/categories/core/index.html | 12 +- blog/categories/esp8266/atom.xml | 256 +++++++ blog/categories/esp8266/index.html | 309 ++++++++ blog/categories/frontend/atom.xml | 2 +- blog/categories/frontend/index.html | 12 +- blog/categories/how-to/atom.xml | 242 ++++++- blog/categories/how-to/index.html | 55 +- blog/categories/mqtt/atom.xml | 453 ++++++++++++ blog/categories/mqtt/index.html | 348 +++++++++ blog/categories/release-notes/atom.xml | 2 +- blog/categories/release-notes/index.html | 12 +- blog/categories/user-stories/atom.xml | 2 +- blog/categories/user-stories/index.html | 12 +- blog/categories/website/atom.xml | 2 +- blog/categories/website/index.html | 12 +- blog/index.html | 520 +++++++++++--- blog/posts/2/index.html | 166 +++-- blog/posts/3/index.html | 74 +- .../blog/2015-10-esp8266-temp/ha-sensor.png | Bin 0 -> 36197 bytes sitemap.xml | 273 +++---- 57 files changed, 3700 insertions(+), 559 deletions(-) create mode 100644 blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/index.html create mode 100644 blog/categories/esp8266/atom.xml create mode 100644 blog/categories/esp8266/index.html create mode 100644 blog/categories/mqtt/atom.xml create mode 100644 blog/categories/mqtt/index.html create mode 100644 images/blog/2015-10-esp8266-temp/ha-sensor.png diff --git a/atom.xml b/atom.xml index b2b166b1f3..c931d31bc4 100644 --- a/atom.xml +++ b/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ @@ -13,6 +13,374 @@ Octopress + + <![CDATA[Report the temperature with ESP8266 to MQTT]]> + + 2015-10-11T12:10:00-07:00 + https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt + 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.

+ +

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.

+ +

+ +Home Assistant will keep track of historical values and allow you to integrate it into automation. +

+ + +

Components

+ +

I’ve been using Adafruit for my shopping:

+ + + + +

Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, +soldering iron + wire, Serial USB cable.

+ +

Connections

+ +

On your breadboard, make the following connections from your ESP8266 to the HDC1008:

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
ESP8266 HDC1008
GND GND
3V Vin
14 SCL
#2 SDA
+ + +

I picked #2 and 14 myself, you can configure them in the sketch.

+ +

Preparing your IDE

+ +

Follow these instructions on how +to install and prepare the Arduino IDE for ESP8266 development.

+ +

After you’re done installing, open the Arduino IDE, in the menu click on sketch -> include library -> +manage libraries and install the following libraries:

+ +
    +
  • PubSubClient by Nick ‘O Leary
  • +
  • Adafruit HDC1000
  • +
+ + +

Sketch

+ +

If you have followed the previous steps, you’re all set.

+ +
    +
  • Open Arduino IDE and create a new sketch (File -> New)
  • +
  • Copy and paste the below sketch to the Arduino IDE
  • +
  • Adjust the values line 6 - 14 to match your setup
  • +
  • Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.
  • +
  • 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.
  • +
  • Press the upload button in Arduino IDE
  • +
  • Open the serial monitor (Tools -> Serial Monitor) to see the output from your device
  • +
+ + +

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 True. This means that anyone +connecting to the MQTT topic will automatically be notified of the last reported value.

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+75
+76
+77
+78
+79
+80
+81
+82
+83
+84
+85
+86
+87
+88
+89
+90
+91
+92
+93
+94
+95
+96
+97
+98
+99
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+
#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);
+    }
+  }
+}
+
+ + +

Configuring Home Assistant

+ +

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.

+ +
1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+
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: "%"
+
+ +]]>
+
+ <![CDATA[0.7.5: Blinkstick, SNMP, Telegram]]> @@ -2463,35 +2831,6 @@ YAML allows the use of lists, which should make the configuration file a bit mor

The home-assistant.conf.example has been replaced with an updated configuration.yaml.example.

Users of Home Assistant should migrate as the old configuration format is deprecated.

-]]> -
- - - <![CDATA[Streaming updates]]> - - 2015-02-24T22:41:27-08:00 - https://home-assistant.io/blog/2015/02/24/streaming-updates - Home Assistant has learned a new trick to get the latest information from the server: streaming updates. No longer will the frontend poll every 30 seconds for updates but instead it will keep a connection open and get the latest changes pushed as soon as they happen.

- -

A new toggle has been added ot the sidebar to turn streaming updates on and off. This preference will be saved on a per-browser basis using local storage. The toggle will also indicate when there is an error setting up a stream after which it will fall back to use polling.

- -

- - - - - - - -

Streaming updates has been implemented using the HTML5 EventSource tag. Implementation is pretty straight forward as all the reconnection logic will be handled by the event source tag. The server-side code is 50 lines and the client-side code is 80 lines of code.

- -

All events that happen on the server will now also be sent to the browser. This turns any browser running the UI into a fully functioning slave instance of Home Assistant. This opens up new possibilities for Home Assistant components that live completely client-side.

- -

Implementing EventSource was not without challenges. Here are some of the issues that had to be solved:

- -

A connection can go stale in Chrome without any event handler being called. This happens when a device goes into standby. For computers this is rare but for phones this occurs quite often. This has been solved by sending a regular ping from the server. The frontend will assume the connection has gone stale when it hasn’t heard any communication for a while. Sending a ping will also help the server detect broken connections and clean them up.

- -

Another issue that I encountered is that Safari and Firefox would not fire the open event when the connection has been opened but when the first message has been received. To work around this the server will now fire a ping when the connection gets opened.

]]>
diff --git a/blog/2014/12/18/website-launched/index.html b/blog/2014/12/18/website-launched/index.html index c1cdc71ef4..6972712b7e 100644 --- a/blog/2014/12/18/website-launched/index.html +++ b/blog/2014/12/18/website-launched/index.html @@ -215,6 +215,12 @@ diff --git a/blog/2014/12/26/home-control-home-automation-and-the-smart-home/index.html b/blog/2014/12/26/home-control-home-automation-and-the-smart-home/index.html index 56566d9973..1117057c54 100644 --- a/blog/2014/12/26/home-control-home-automation-and-the-smart-home/index.html +++ b/blog/2014/12/26/home-control-home-automation-and-the-smart-home/index.html @@ -277,6 +277,12 @@ This article will try to explain how they all relate.

diff --git a/blog/2015/01/04/hey-pushbullet-nice-talking-to-you/index.html b/blog/2015/01/04/hey-pushbullet-nice-talking-to-you/index.html index 8195f80600..bb9ee38da6 100644 --- a/blog/2015/01/04/hey-pushbullet-nice-talking-to-you/index.html +++ b/blog/2015/01/04/hey-pushbullet-nice-talking-to-you/index.html @@ -259,6 +259,12 @@ diff --git a/blog/2015/01/11/bootstrapping-your-setup-with-discovery/index.html b/blog/2015/01/11/bootstrapping-your-setup-with-discovery/index.html index fa78f0b9d6..844631ba0d 100644 --- a/blog/2015/01/11/bootstrapping-your-setup-with-discovery/index.html +++ b/blog/2015/01/11/bootstrapping-your-setup-with-discovery/index.html @@ -227,6 +227,12 @@ diff --git a/blog/2015/01/13/nest-in-da-house/index.html b/blog/2015/01/13/nest-in-da-house/index.html index 6237022123..83e1c5e962 100644 --- a/blog/2015/01/13/nest-in-da-house/index.html +++ b/blog/2015/01/13/nest-in-da-house/index.html @@ -234,6 +234,12 @@ diff --git a/blog/2015/01/24/release-notes/index.html b/blog/2015/01/24/release-notes/index.html index f5c7b98a13..e25c0512db 100644 --- a/blog/2015/01/24/release-notes/index.html +++ b/blog/2015/01/24/release-notes/index.html @@ -239,6 +239,12 @@ Home Assistant now supports --open-ui and --demo-mode diff --git a/blog/2015/02/08/looking-at-the-past/index.html b/blog/2015/02/08/looking-at-the-past/index.html index 4466449031..624aace01a 100644 --- a/blog/2015/02/08/looking-at-the-past/index.html +++ b/blog/2015/02/08/looking-at-the-past/index.html @@ -257,6 +257,12 @@ Events are saved in a local database. Google Graphs is used to draw the graph. D diff --git a/blog/2015/02/24/streaming-updates/index.html b/blog/2015/02/24/streaming-updates/index.html index e54670d42b..18e5df5690 100644 --- a/blog/2015/02/24/streaming-updates/index.html +++ b/blog/2015/02/24/streaming-updates/index.html @@ -235,6 +235,12 @@ diff --git a/blog/2015/03/01/home-assistant-migrating-to-yaml/index.html b/blog/2015/03/01/home-assistant-migrating-to-yaml/index.html index 051e976a6a..c03d2cca0a 100644 --- a/blog/2015/03/01/home-assistant-migrating-to-yaml/index.html +++ b/blog/2015/03/01/home-assistant-migrating-to-yaml/index.html @@ -222,6 +222,12 @@ YAML allows the use of lists, which should make the configuration file a bit mor diff --git a/blog/2015/03/08/new-logo/index.html b/blog/2015/03/08/new-logo/index.html index c3f1ea1af4..2810f4d343 100644 --- a/blog/2015/03/08/new-logo/index.html +++ b/blog/2015/03/08/new-logo/index.html @@ -223,6 +223,12 @@ The old logo, the new detailed logo and the new simple logo. diff --git a/blog/2015/03/11/release-notes/index.html b/blog/2015/03/11/release-notes/index.html index e5c3a01e64..43740e2d52 100644 --- a/blog/2015/03/11/release-notes/index.html +++ b/blog/2015/03/11/release-notes/index.html @@ -269,6 +269,12 @@ An initial version of voice control for Home Assistant has landed. The current i diff --git a/blog/2015/03/22/release-notes/index.html b/blog/2015/03/22/release-notes/index.html index 7e7aa64f7f..6cee1aa4e2 100644 --- a/blog/2015/03/22/release-notes/index.html +++ b/blog/2015/03/22/release-notes/index.html @@ -342,6 +342,12 @@ James Cole has also contributed support for the diff --git a/blog/2015/04/25/release-notes/index.html b/blog/2015/04/25/release-notes/index.html index 0de280bc56..6ebeb2e375 100644 --- a/blog/2015/04/25/release-notes/index.html +++ b/blog/2015/04/25/release-notes/index.html @@ -358,6 +358,12 @@ James has also contributed support for integrating Transmission into Home Assist diff --git a/blog/2015/05/09/utc-time-zone-awareness/index.html b/blog/2015/05/09/utc-time-zone-awareness/index.html index ae7c3d6fa5..464c429c7c 100644 --- a/blog/2015/05/09/utc-time-zone-awareness/index.html +++ b/blog/2015/05/09/utc-time-zone-awareness/index.html @@ -247,6 +247,12 @@ diff --git a/blog/2015/05/14/release-notes/index.html b/blog/2015/05/14/release-notes/index.html index 7b056876ac..51e762ea4b 100644 --- a/blog/2015/05/14/release-notes/index.html +++ b/blog/2015/05/14/release-notes/index.html @@ -384,6 +384,12 @@ Before diving into the newly supported devices and services, I want to highlight diff --git a/blog/2015/06/10/release-notes/index.html b/blog/2015/06/10/release-notes/index.html index 62159502e1..495cca1f76 100644 --- a/blog/2015/06/10/release-notes/index.html +++ b/blog/2015/06/10/release-notes/index.html @@ -463,6 +463,12 @@ This switch platform allows you to control your motion detection setting on your diff --git a/blog/2015/07/11/ip-cameras-arduino-kodi-efergy-support/index.html b/blog/2015/07/11/ip-cameras-arduino-kodi-efergy-support/index.html index 97f4accfab..7756f0f20b 100644 --- a/blog/2015/07/11/ip-cameras-arduino-kodi-efergy-support/index.html +++ b/blog/2015/07/11/ip-cameras-arduino-kodi-efergy-support/index.html @@ -407,6 +407,12 @@ Fabian has added support for Forecast.io to g diff --git a/blog/2015/08/09/mqtt-raspberry-pi-squeezebox-asuswrt-support/index.html b/blog/2015/08/09/mqtt-raspberry-pi-squeezebox-asuswrt-support/index.html index 6f5095a348..7b3a54ed4f 100644 --- a/blog/2015/08/09/mqtt-raspberry-pi-squeezebox-asuswrt-support/index.html +++ b/blog/2015/08/09/mqtt-raspberry-pi-squeezebox-asuswrt-support/index.html @@ -370,6 +370,12 @@ Support for Temper temperature sensors has been contributed by +
  • + Report the temperature with ESP8266 to MQTT +
  • + + +
  • 0.7.5: Blinkstick, SNMP, Telegram
  • @@ -393,12 +399,6 @@ Support for Temper temperature sensors has been contributed by - Home Assistant meets IFTTT - - - diff --git a/blog/2015/08/17/verisure-and-modern-tp-link-router-support/index.html b/blog/2015/08/17/verisure-and-modern-tp-link-router-support/index.html index 52a50d9bc1..7117125d7a 100644 --- a/blog/2015/08/17/verisure-and-modern-tp-link-router-support/index.html +++ b/blog/2015/08/17/verisure-and-modern-tp-link-router-support/index.html @@ -248,6 +248,12 @@ Home Assistant support to integrate your Ver diff --git a/blog/2015/08/26/laundry-automation-with-moteino-mqtt-and-home-assistant/index.html b/blog/2015/08/26/laundry-automation-with-moteino-mqtt-and-home-assistant/index.html index 4b9639cf42..02def3be43 100644 --- a/blog/2015/08/26/laundry-automation-with-moteino-mqtt-and-home-assistant/index.html +++ b/blog/2015/08/26/laundry-automation-with-moteino-mqtt-and-home-assistant/index.html @@ -432,6 +432,12 @@ diff --git a/blog/2015/08/31/version-7-revamped-ui-and-improved-distribution/index.html b/blog/2015/08/31/version-7-revamped-ui-and-improved-distribution/index.html index 2d02165a8d..af42a7d859 100644 --- a/blog/2015/08/31/version-7-revamped-ui-and-improved-distribution/index.html +++ b/blog/2015/08/31/version-7-revamped-ui-and-improved-distribution/index.html @@ -378,6 +378,12 @@ or AM2302 device.

    diff --git a/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant/index.html b/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant/index.html index f8e9ca6499..7275238176 100644 --- a/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant/index.html +++ b/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant/index.html @@ -132,6 +132,8 @@
  • how-to
  • +
  • mqtt
  • + @@ -517,6 +519,12 @@ The MQTT support was added to H diff --git a/blog/2015/09/13/home-assistant-meets-ifttt/index.html b/blog/2015/09/13/home-assistant-meets-ifttt/index.html index ad3ec5691b..83b75f8b37 100644 --- a/blog/2015/09/13/home-assistant-meets-ifttt/index.html +++ b/blog/2015/09/13/home-assistant-meets-ifttt/index.html @@ -417,6 +417,12 @@ Setup Maker channel with event name HA_FIRE_ALARM and Twitter chann diff --git a/blog/2015/09/18/monitoring-with-glances-and-home-assistant/index.html b/blog/2015/09/18/monitoring-with-glances-and-home-assistant/index.html index bc022fcd76..30ab397985 100644 --- a/blog/2015/09/18/monitoring-with-glances-and-home-assistant/index.html +++ b/blog/2015/09/18/monitoring-with-glances-and-home-assistant/index.html @@ -297,6 +297,12 @@ Inspried by a fea diff --git a/blog/2015/09/19/alarm-sonos-and-itunes-support/index.html b/blog/2015/09/19/alarm-sonos-and-itunes-support/index.html index 353439818b..e71a8f5057 100644 --- a/blog/2015/09/19/alarm-sonos-and-itunes-support/index.html +++ b/blog/2015/09/19/alarm-sonos-and-itunes-support/index.html @@ -305,6 +305,12 @@ support by @persandstrom.

    diff --git a/blog/2015/10/05/home-assistant-goes-geo-with-owntracks/index.html b/blog/2015/10/05/home-assistant-goes-geo-with-owntracks/index.html index 31c811f1aa..0d46552893 100644 --- a/blog/2015/10/05/home-assistant-goes-geo-with-owntracks/index.html +++ b/blog/2015/10/05/home-assistant-goes-geo-with-owntracks/index.html @@ -251,6 +251,12 @@ short summary of highlights:

    diff --git a/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/index.html b/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/index.html new file mode 100644 index 0000000000..17f995782b --- /dev/null +++ b/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/index.html @@ -0,0 +1,673 @@ + + + + + + + + + + + + Report the temperature with ESP8266 to MQTT - Home Assistant + + + + + + + + + + + + + + + + + + + + + + + + +
    + +
    + + + +
    +
    + +
    + + +
    +
    + +

    Report the temperature with ESP8266 to MQTT

    + + + +
    + + + + + + + + + + + + + + + + + + + nine minutes reading time + + + + + + + + + Comments + +
    + +
    + + + + +

    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.

    + +

    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.

    + +

    + +Home Assistant will keep track of historical values and allow you to integrate it into automation. +

    + + +

    Components

    + +

    I’ve been using Adafruit for my shopping:

    + + + + +

    Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, +soldering iron + wire, Serial USB cable.

    + +

    Connections

    + +

    On your breadboard, make the following connections from your ESP8266 to the HDC1008:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ESP8266 HDC1008
    GND GND
    3V Vin
    14 SCL
    #2 SDA
    + + +

    I picked #2 and 14 myself, you can configure them in the sketch.

    + +

    Preparing your IDE

    + +

    Follow these instructions on how +to install and prepare the Arduino IDE for ESP8266 development.

    + +

    After you’re done installing, open the Arduino IDE, in the menu click on sketch -> include library -> +manage libraries and install the following libraries:

    + +
      +
    • PubSubClient by Nick ‘O Leary
    • +
    • Adafruit HDC1000
    • +
    + + +

    Sketch

    + +

    If you have followed the previous steps, you’re all set.

    + +
      +
    • Open Arduino IDE and create a new sketch (File -> New)
    • +
    • Copy and paste the below sketch to the Arduino IDE
    • +
    • Adjust the values line 6 - 14 to match your setup
    • +
    • Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.
    • +
    • 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.
    • +
    • Press the upload button in Arduino IDE
    • +
    • Open the serial monitor (Tools -> Serial Monitor) to see the output from your device
    • +
    + + +

    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 True. This means that anyone +connecting to the MQTT topic will automatically be notified of the last reported value.

    + +
    1
    +2
    +3
    +4
    +5
    +6
    +7
    +8
    +9
    +10
    +11
    +12
    +13
    +14
    +15
    +16
    +17
    +18
    +19
    +20
    +21
    +22
    +23
    +24
    +25
    +26
    +27
    +28
    +29
    +30
    +31
    +32
    +33
    +34
    +35
    +36
    +37
    +38
    +39
    +40
    +41
    +42
    +43
    +44
    +45
    +46
    +47
    +48
    +49
    +50
    +51
    +52
    +53
    +54
    +55
    +56
    +57
    +58
    +59
    +60
    +61
    +62
    +63
    +64
    +65
    +66
    +67
    +68
    +69
    +70
    +71
    +72
    +73
    +74
    +75
    +76
    +77
    +78
    +79
    +80
    +81
    +82
    +83
    +84
    +85
    +86
    +87
    +88
    +89
    +90
    +91
    +92
    +93
    +94
    +95
    +96
    +97
    +98
    +99
    +100
    +101
    +102
    +103
    +104
    +105
    +106
    +107
    +108
    +109
    +
    #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);
    +    }
    +  }
    +}
    +
    + + +

    Configuring Home Assistant

    + +

    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.

    + +
    1
    +2
    +3
    +4
    +5
    +6
    +7
    +8
    +9
    +10
    +11
    +12
    +13
    +14
    +15
    +16
    +17
    +18
    +
    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: "%"
    +
    + + + +
    + + +
    +

    Comments

    +
    +
    +
    + + +
    + + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/blog/2015/10/11/rfxtrx-blinkstick-and-snmp-support/index.html b/blog/2015/10/11/rfxtrx-blinkstick-and-snmp-support/index.html index f213674195..d085cd63eb 100644 --- a/blog/2015/10/11/rfxtrx-blinkstick-and-snmp-support/index.html +++ b/blog/2015/10/11/rfxtrx-blinkstick-and-snmp-support/index.html @@ -239,6 +239,12 @@ This has been implemented for the +
  • + Report the temperature with ESP8266 to MQTT +
  • + + +
  • @@ -258,12 +264,6 @@ This has been implemented for the - -
  • - Home Assistant meets IFTTT -
  • - - diff --git a/blog/archives/index.html b/blog/archives/index.html index ead93962fb..9153897065 100644 --- a/blog/archives/index.html +++ b/blog/archives/index.html @@ -22,7 +22,7 @@ - + @@ -117,6 +117,47 @@ + + + +
    + +
    @@ -324,6 +365,8 @@
  • how-to
  • +
  • mqtt
  • + @@ -1178,6 +1221,12 @@ diff --git a/blog/categories/architecture/atom.xml b/blog/categories/architecture/atom.xml index 566a648fbb..27bdf37572 100644 --- a/blog/categories/architecture/atom.xml +++ b/blog/categories/architecture/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: architecture | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ diff --git a/blog/categories/architecture/index.html b/blog/categories/architecture/index.html index a26eab5254..d59d4bdc60 100644 --- a/blog/categories/architecture/index.html +++ b/blog/categories/architecture/index.html @@ -209,6 +209,12 @@ diff --git a/blog/categories/branding/atom.xml b/blog/categories/branding/atom.xml index 18d50148bd..fe4758dfbe 100644 --- a/blog/categories/branding/atom.xml +++ b/blog/categories/branding/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: branding | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ diff --git a/blog/categories/branding/index.html b/blog/categories/branding/index.html index e7a8772d78..8eac7fa0f6 100644 --- a/blog/categories/branding/index.html +++ b/blog/categories/branding/index.html @@ -209,6 +209,12 @@ diff --git a/blog/categories/component/atom.xml b/blog/categories/component/atom.xml index 5c10b80fdf..dcdde9bf94 100644 --- a/blog/categories/component/atom.xml +++ b/blog/categories/component/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: component | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ diff --git a/blog/categories/component/index.html b/blog/categories/component/index.html index 9a143bddf9..54819aa8d0 100644 --- a/blog/categories/component/index.html +++ b/blog/categories/component/index.html @@ -322,6 +322,12 @@ diff --git a/blog/categories/core/atom.xml b/blog/categories/core/atom.xml index 9637e43268..92fae15bcf 100644 --- a/blog/categories/core/atom.xml +++ b/blog/categories/core/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: core | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ diff --git a/blog/categories/core/index.html b/blog/categories/core/index.html index 9b12bacce1..6230e922fe 100644 --- a/blog/categories/core/index.html +++ b/blog/categories/core/index.html @@ -246,6 +246,12 @@ diff --git a/blog/categories/esp8266/atom.xml b/blog/categories/esp8266/atom.xml new file mode 100644 index 0000000000..540c45f531 --- /dev/null +++ b/blog/categories/esp8266/atom.xml @@ -0,0 +1,256 @@ + + + + <![CDATA[Category: esp8266 | Home Assistant]]> + + + 2015-10-11T13:16:06-07:00 + https://home-assistant.io/ + + + + + Octopress + + + + <![CDATA[Report the temperature with ESP8266 to MQTT]]> + + 2015-10-11T12:10:00-07:00 + https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt + 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.

    + +

    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.

    + +

    + +Home Assistant will keep track of historical values and allow you to integrate it into automation. +

    + + +

    Components

    + +

    I’ve been using Adafruit for my shopping:

    + + + + +

    Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, +soldering iron + wire, Serial USB cable.

    + +

    Connections

    + +

    On your breadboard, make the following connections from your ESP8266 to the HDC1008:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ESP8266 HDC1008
    GND GND
    3V Vin
    14 SCL
    #2 SDA
    + + +

    I picked #2 and 14 myself, you can configure them in the sketch.

    + +

    Preparing your IDE

    + +

    Follow these instructions on how +to install and prepare the Arduino IDE for ESP8266 development.

    + +

    After you’re done installing, open the Arduino IDE, in the menu click on sketch -> include library -> +manage libraries and install the following libraries:

    + +
      +
    • PubSubClient by Nick ‘O Leary
    • +
    • Adafruit HDC1000
    • +
    + + +

    Sketch

    + +

    If you have followed the previous steps, you’re all set.

    + +
      +
    • Open Arduino IDE and create a new sketch (File -> New)
    • +
    • Copy and paste the below sketch to the Arduino IDE
    • +
    • Adjust the values line 6 - 14 to match your setup
    • +
    • Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.
    • +
    • 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.
    • +
    • Press the upload button in Arduino IDE
    • +
    • Open the serial monitor (Tools -> Serial Monitor) to see the output from your device
    • +
    + + +

    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 True. This means that anyone +connecting to the MQTT topic will automatically be notified of the last reported value.

    + +
    
    +#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);
    +    }
    +  }
    +}
    +
    + +

    Configuring Home Assistant

    + +

    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.

    + +
    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: "%"
    +
    +]]>
    +
    + +
    diff --git a/blog/categories/esp8266/index.html b/blog/categories/esp8266/index.html new file mode 100644 index 0000000000..20c1835347 --- /dev/null +++ b/blog/categories/esp8266/index.html @@ -0,0 +1,309 @@ + + + + + + + + + + + + Category: esp8266 - Home Assistant + + + + + + + + + + + + + + + + + + + + + + + + +
    + +
    + + + +
    +
    + +
    + + + + + +
    + + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/blog/categories/frontend/atom.xml b/blog/categories/frontend/atom.xml index 5da3cb6f94..433a92176f 100644 --- a/blog/categories/frontend/atom.xml +++ b/blog/categories/frontend/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: frontend | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ diff --git a/blog/categories/frontend/index.html b/blog/categories/frontend/index.html index 9cc168c0ed..7590e15e7a 100644 --- a/blog/categories/frontend/index.html +++ b/blog/categories/frontend/index.html @@ -248,6 +248,12 @@ diff --git a/blog/categories/how-to/atom.xml b/blog/categories/how-to/atom.xml index e05e08e807..dd1ae288ef 100644 --- a/blog/categories/how-to/atom.xml +++ b/blog/categories/how-to/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: how-to | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ @@ -13,6 +13,246 @@ Octopress + + <![CDATA[Report the temperature with ESP8266 to MQTT]]> + + 2015-10-11T12:10:00-07:00 + https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt + 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.

    + +

    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.

    + +

    + +Home Assistant will keep track of historical values and allow you to integrate it into automation. +

    + + +

    Components

    + +

    I’ve been using Adafruit for my shopping:

    + + + + +

    Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, +soldering iron + wire, Serial USB cable.

    + +

    Connections

    + +

    On your breadboard, make the following connections from your ESP8266 to the HDC1008:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ESP8266 HDC1008
    GND GND
    3V Vin
    14 SCL
    #2 SDA
    + + +

    I picked #2 and 14 myself, you can configure them in the sketch.

    + +

    Preparing your IDE

    + +

    Follow these instructions on how +to install and prepare the Arduino IDE for ESP8266 development.

    + +

    After you’re done installing, open the Arduino IDE, in the menu click on sketch -> include library -> +manage libraries and install the following libraries:

    + +
      +
    • PubSubClient by Nick ‘O Leary
    • +
    • Adafruit HDC1000
    • +
    + + +

    Sketch

    + +

    If you have followed the previous steps, you’re all set.

    + +
      +
    • Open Arduino IDE and create a new sketch (File -> New)
    • +
    • Copy and paste the below sketch to the Arduino IDE
    • +
    • Adjust the values line 6 - 14 to match your setup
    • +
    • Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.
    • +
    • 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.
    • +
    • Press the upload button in Arduino IDE
    • +
    • Open the serial monitor (Tools -> Serial Monitor) to see the output from your device
    • +
    + + +

    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 True. This means that anyone +connecting to the MQTT topic will automatically be notified of the last reported value.

    + +
    
    +#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);
    +    }
    +  }
    +}
    +
    + +

    Configuring Home Assistant

    + +

    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.

    + +
    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: "%"
    +
    +]]>
    +
    + <![CDATA[Remote Monitoring with Glances]]> diff --git a/blog/categories/how-to/index.html b/blog/categories/how-to/index.html index d764a7ab4a..c0f51951e0 100644 --- a/blog/categories/how-to/index.html +++ b/blog/categories/how-to/index.html @@ -117,6 +117,47 @@ + + + +
    + +
    @@ -213,6 +254,8 @@
  • how-to
  • +
  • mqtt
  • + @@ -285,6 +328,12 @@ diff --git a/blog/categories/mqtt/atom.xml b/blog/categories/mqtt/atom.xml new file mode 100644 index 0000000000..4bc79c9431 --- /dev/null +++ b/blog/categories/mqtt/atom.xml @@ -0,0 +1,453 @@ + + + + <![CDATA[Category: mqtt | Home Assistant]]> + + + 2015-10-11T13:16:06-07:00 + https://home-assistant.io/ + + + + + Octopress + + + + <![CDATA[Report the temperature with ESP8266 to MQTT]]> + + 2015-10-11T12:10:00-07:00 + https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt + 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.

    + +

    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.

    + +

    + +Home Assistant will keep track of historical values and allow you to integrate it into automation. +

    + + +

    Components

    + +

    I’ve been using Adafruit for my shopping:

    + + + + +

    Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, +soldering iron + wire, Serial USB cable.

    + +

    Connections

    + +

    On your breadboard, make the following connections from your ESP8266 to the HDC1008:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ESP8266 HDC1008
    GND GND
    3V Vin
    14 SCL
    #2 SDA
    + + +

    I picked #2 and 14 myself, you can configure them in the sketch.

    + +

    Preparing your IDE

    + +

    Follow these instructions on how +to install and prepare the Arduino IDE for ESP8266 development.

    + +

    After you’re done installing, open the Arduino IDE, in the menu click on sketch -> include library -> +manage libraries and install the following libraries:

    + +
      +
    • PubSubClient by Nick ‘O Leary
    • +
    • Adafruit HDC1000
    • +
    + + +

    Sketch

    + +

    If you have followed the previous steps, you’re all set.

    + +
      +
    • Open Arduino IDE and create a new sketch (File -> New)
    • +
    • Copy and paste the below sketch to the Arduino IDE
    • +
    • Adjust the values line 6 - 14 to match your setup
    • +
    • Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.
    • +
    • 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.
    • +
    • Press the upload button in Arduino IDE
    • +
    • Open the serial monitor (Tools -> Serial Monitor) to see the output from your device
    • +
    + + +

    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 True. This means that anyone +connecting to the MQTT topic will automatically be notified of the last reported value.

    + +
    
    +#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);
    +    }
    +  }
    +}
    +
    + +

    Configuring Home Assistant

    + +

    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.

    + +
    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: "%"
    +
    +]]>
    +
    + + + <![CDATA[Using MQTT with Home Assistant]]> + + 2015-09-11T02:19:38-07:00 + https://home-assistant.io/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant + +The MQTT support was added to Home Assistant recently. The MQTT component 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.

    + +

    A great example is shown in a Laundry Automation post in this blog.

    + +

    This post will give you a small overview of some other possibilities on how to use MQTT with Home Assistant.

    + + + + +

    Manual usage

    + +

    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 configuration.yaml file consists of two parts: one for the broker and one for the sensor.

    + +
    mqtt:
    +  broker: 127.0.0.1
    +
    +sensor:
    +  - platform: mqtt
    +    name: "Fabian's Mood"
    +    state_topic: "home-assistant/fabian/mood"
    +
    + +

    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 MQTT component or the MQTT sensor documentation.

    + +

    Now we can set the mood. The commandline tool (mosquitto_pub) which is shipped with mosquitto is used to send an MQTT message.

    + +
    mosquitto_pub  -h 127.0.0.1 -t "home-assistant/fabian/mood" -m "bad"
    +
    + +

    + + The Mood sensor +

    + + +

    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.

    + +

    Python MQTT bindings

    + +

    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 :-).

    + +

    This example is using the Paho MQTT Python binding 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 (pip3 install paho-mqtt).

    + +

    The first step is to add an additional MQTT sensor to the configuration.yaml file. The sensor will be called “Lottery” and the unit of measurement will be “No.”.

    + +
      - platform: mqtt
    +    name: "Lottery"
    +    state_topic: "home-assistant/lottery/number"
    +    unit_of_measurement: "No."
    +
    + +

    Don’t forget to restart Home Assistant to make the configuration active.

    + +

    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 Paho MQTT documentation.

    + +
    #!/usr/bin/python3
    +#
    +import time
    +import random
    +import paho.mqtt.client as mqtt
    +import paho.mqtt.publish as publish
    +
    +broker = '127.0.0.1'
    +state_topic = 'home-assistant/lottery/number'
    +delay = 5
    +
    +# Send a single message to set the mood
    +publish.single('home-assistant/fabian/mood', 'good', hostname=broker)
    +
    +# Send messages in a loop
    +client = mqtt.Client("ha-client")
    +client.connect(broker)
    +client.loop_start()
    +
    +while True:
    +    client.publish(state_topic, random.randrange(0, 50, 1))
    +    time.sleep(delay)
    +
    + +

    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.

    + +

    + + The Lottery sensor +

    + + +

    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.

    + +

    Arduino

    + +

    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.

    + +

    + + The Arduino UNO with Ethernet shield and photo resistor +

    + + +

    The MQTT client 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.

    + +
    /*
    +  This sketch is based on the basic MQTT example by
    +  http://knolleary.github.io/pubsubclient/
    +*/
    +
    +#include <SPI.h>
    +#include <Ethernet.h>
    +#include <PubSubClient.h>
    +
    +#define DEBUG 1 // Debug output to serial console
    +
    +// Device settings
    +IPAddress deviceIp(192, 168, 0, 43);
    +byte deviceMac[] = { 0xAB, 0xCD, 0xFE, 0xFE, 0xFE, 0xFE };
    +char* deviceId  = "sensor01"; // Name of the sensor
    +char* stateTopic = "home-assistant/sensor01/brightness"; // MQTT topic where values are published
    +int sensorPin = A0; // Pin to which the sensor is connected to
    +char buf[4]; // Buffer to store the sensor value
    +int updateInterval = 1000; // Interval in miliseconds
    +
    +// MQTT server settings
    +IPAddress mqttServer(192, 168, 0, 12);
    +int mqttPort = 1883;
    +
    +EthernetClient ethClient;
    +PubSubClient client(ethClient);
    +
    +void reconnect() {
    +  while (!client.connected()) {
    +#if DEBUG
    +    Serial.print("Attempting MQTT connection...");
    +#endif
    +    if (client.connect(deviceId)) {
    +#if DEBUG
    +      Serial.println("connected");
    +#endif
    +    } else {
    +#if DEBUG
    +      Serial.print("failed, rc=");
    +      Serial.print(client.state());
    +      Serial.println(" try again in 5 seconds");
    +#endif
    +      delay(5000);
    +    }
    +  }
    +}
    +
    +void setup() {
    +  Serial.begin(57600);
    +  client.setServer(mqttServer, mqttPort);
    +  Ethernet.begin(deviceMac, deviceIp);
    +  delay(1500);
    +}
    +
    +void loop() {
    +  if (!client.connected()) {
    +    reconnect();
    +  }
    +  client.loop();
    +
    +  int sensorValue = analogRead(sensorPin);
    +#if DEBUG
    +  Serial.print("Sensor value: ");
    +  Serial.println(sensorValue);
    +#endif
    +  client.publish(stateTopic, itoa(sensorValue, buf, 10));
    +  delay(updateInterval);
    +}
    +
    + +

    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 configuration.yaml file.

    + +
      - platform: mqtt
    +    name: "Brightness"
    +    state_topic: "home-assistant/sensor01/brightness"
    +    unit_of_measurement: "cd"
    +
    + +

    After a restart of Home Assistant the values of your Arduino will be available.

    + +

    + + The Brightness sensor +

    + + +

    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.

    +]]>
    +
    + +
    diff --git a/blog/categories/mqtt/index.html b/blog/categories/mqtt/index.html new file mode 100644 index 0000000000..284283184c --- /dev/null +++ b/blog/categories/mqtt/index.html @@ -0,0 +1,348 @@ + + + + + + + + + + + + Category: mqtt - Home Assistant + + + + + + + + + + + + + + + + + + + + + + + + +
    + +
    + + + +
    +
    + +
    + + + + + +
    + + + + +
    +
    + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/blog/categories/release-notes/atom.xml b/blog/categories/release-notes/atom.xml index 9085f2ceda..e031544ca1 100644 --- a/blog/categories/release-notes/atom.xml +++ b/blog/categories/release-notes/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: release-notes | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ diff --git a/blog/categories/release-notes/index.html b/blog/categories/release-notes/index.html index e61ee9a907..696dcd2fdc 100644 --- a/blog/categories/release-notes/index.html +++ b/blog/categories/release-notes/index.html @@ -655,6 +655,12 @@ diff --git a/blog/categories/user-stories/atom.xml b/blog/categories/user-stories/atom.xml index 591fc12330..ff4983abbf 100644 --- a/blog/categories/user-stories/atom.xml +++ b/blog/categories/user-stories/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: user-stories | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ diff --git a/blog/categories/user-stories/index.html b/blog/categories/user-stories/index.html index 3738d8f2a9..23395553a7 100644 --- a/blog/categories/user-stories/index.html +++ b/blog/categories/user-stories/index.html @@ -246,6 +246,12 @@ diff --git a/blog/categories/website/atom.xml b/blog/categories/website/atom.xml index 58c71f84ba..940a2161a6 100644 --- a/blog/categories/website/atom.xml +++ b/blog/categories/website/atom.xml @@ -4,7 +4,7 @@ <![CDATA[Category: website | Home Assistant]]> - 2015-10-11T11:39:41-07:00 + 2015-10-11T13:16:06-07:00 https://home-assistant.io/ diff --git a/blog/categories/website/index.html b/blog/categories/website/index.html index 41622a167a..b959eb4db6 100644 --- a/blog/categories/website/index.html +++ b/blog/categories/website/index.html @@ -209,6 +209,12 @@ diff --git a/blog/index.html b/blog/index.html index d06f72cb8b..ae8e591a90 100644 --- a/blog/index.html +++ b/blog/index.html @@ -22,7 +22,7 @@ - + @@ -99,6 +99,430 @@ +
    +
    + +

    + Report the temperature with ESP8266 to MQTT +

    + + + +
    + + + + + + + + + + + + + + + + + + + nine minutes reading time + + + + + + + + + Comments + +
    + +
    + + + + +

    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.

    + +

    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.

    + +

    + +Home Assistant will keep track of historical values and allow you to integrate it into automation. +

    + + +

    Components

    + +

    I’ve been using Adafruit for my shopping:

    + + + + +

    Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, +soldering iron + wire, Serial USB cable.

    + +

    Connections

    + +

    On your breadboard, make the following connections from your ESP8266 to the HDC1008:

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    ESP8266 HDC1008
    GND GND
    3V Vin
    14 SCL
    #2 SDA
    + + +

    I picked #2 and 14 myself, you can configure them in the sketch.

    + +

    Preparing your IDE

    + +

    Follow these instructions on how +to install and prepare the Arduino IDE for ESP8266 development.

    + +

    After you’re done installing, open the Arduino IDE, in the menu click on sketch -> include library -> +manage libraries and install the following libraries:

    + +
      +
    • PubSubClient by Nick ‘O Leary
    • +
    • Adafruit HDC1000
    • +
    + + +

    Sketch

    + +

    If you have followed the previous steps, you’re all set.

    + +
      +
    • Open Arduino IDE and create a new sketch (File -> New)
    • +
    • Copy and paste the below sketch to the Arduino IDE
    • +
    • Adjust the values line 6 - 14 to match your setup
    • +
    • Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.
    • +
    • 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.
    • +
    • Press the upload button in Arduino IDE
    • +
    • Open the serial monitor (Tools -> Serial Monitor) to see the output from your device
    • +
    + + +

    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 True. This means that anyone +connecting to the MQTT topic will automatically be notified of the last reported value.

    + +
    1
    +2
    +3
    +4
    +5
    +6
    +7
    +8
    +9
    +10
    +11
    +12
    +13
    +14
    +15
    +16
    +17
    +18
    +19
    +20
    +21
    +22
    +23
    +24
    +25
    +26
    +27
    +28
    +29
    +30
    +31
    +32
    +33
    +34
    +35
    +36
    +37
    +38
    +39
    +40
    +41
    +42
    +43
    +44
    +45
    +46
    +47
    +48
    +49
    +50
    +51
    +52
    +53
    +54
    +55
    +56
    +57
    +58
    +59
    +60
    +61
    +62
    +63
    +64
    +65
    +66
    +67
    +68
    +69
    +70
    +71
    +72
    +73
    +74
    +75
    +76
    +77
    +78
    +79
    +80
    +81
    +82
    +83
    +84
    +85
    +86
    +87
    +88
    +89
    +90
    +91
    +92
    +93
    +94
    +95
    +96
    +97
    +98
    +99
    +100
    +101
    +102
    +103
    +104
    +105
    +106
    +107
    +108
    +109
    +
    #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);
    +    }
    +  }
    +}
    +
    + + +

    Configuring Home Assistant

    + +

    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.

    + +
    1
    +2
    +3
    +4
    +5
    +6
    +7
    +8
    +9
    +10
    +11
    +12
    +13
    +14
    +15
    +16
    +17
    +18
    +
    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: "%"
    +
    + + + +
    +
    + +
    @@ -565,6 +989,8 @@ read more button for some example recipes.

  • how-to
  • +
  • mqtt
  • + @@ -871,98 +1297,6 @@ Home Assistant support to integrate your Ver

    - -
    -
    - -

    - MQTT, Rasperry PI, Logitech Squeezebox and ASUSWRT routers now supported -

    - - - -
    - - - - - - - - - - - - - - - - - - - four minutes reading time - - - - - - - - - Comments - -
    - -
    - - - - -
    -

    It’s time for the August release and there is some serious good stuff this time. The core of Home Assistant has gone some serious clean up and a bump in test coverage thanks to @balloob. If you’re a developer, make sure you read up on the deprecation notices. @fabaff did another great round of documentating all the various components.

    - -

    MQTT Support
    - -The big new addition in this release is the support for the MQTT protocol by @fabaff with some help from @balloob. It will now be possible to integrate any IoT device that talks via MQTT. For the initial release we support connecting Home Assistant to a broker (no TLS yet). Components can now subscribe and publish to MQTT topics (see the example) and also support for the automation component has been added. For more information, see the MQTT component page.

    - -
    1
    -2
    -3
    -4
    -5
    -6
    -7
    -8
    -9
    -
    # Example configuration.yaml entry
    -mqtt:
    -  broker: IP_ADDRESS_BROKER
    -  # All the other options are optional:
    -  port: 1883
    -  keepalive: 60
    -  qos: 0
    -  username: your_username
    -  password: your_secret_password
    -
    - - - - - - Read on → -
    - -
    -
    -