home-assistant.github.io/blog/categories/mqtt/atom.xml
2018-02-16 22:00:49 +00:00

748 lines
31 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Category: MQTT | Home Assistant]]></title>
<link href="https://home-assistant.io/blog/categories/mqtt/atom.xml" rel="self"/>
<link href="https://home-assistant.io/"/>
<updated>2018-02-16T21:42:46+00:00</updated>
<id>https://home-assistant.io/</id>
<author>
<name><![CDATA[Home Assistant]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[ESP8266 and MicroPython - Part 2]]></title>
<link href="https://home-assistant.io/blog/2016/08/31/esp8266-and-micropython-part2/"/>
<updated>2016-08-31T04:17:25+00:00</updated>
<id>https://home-assistant.io/blog/2016/08/31/esp8266-and-micropython-part2</id>
<content type="html"><![CDATA[<img src='/images/blog/2016-07-micropython/micropython.png' style='clear: right; border:none; box-shadow: none; float: right; margin-bottom: 12px;' width='200' />
So, part 1 of [ESP8266 and MicroPython](/blog/2016/07/28/esp8266-and-micropython-part1/) was pretty lame, right? Instead of getting information out of Home Assistant we are going a step forward and create our own sensor which is sending details about its state to a Home Assistant instance.
<!--more-->
Beside [HTTP POST](https://en.wikipedia.org/wiki/POST_(HTTP)) requests, MQTT is the quickest way (from the author's point of view) to publish information with DIY devices.
You have to make a decision: Do you want to pull or to [poll](https://en.wikipedia.org/wiki/Polling_(computer_science)) the information for the sensor? For slowly changing values like temperature it's perfectly fine to wait a couple of seconds to retrieve the value. If it's a motion detector the state change should be available instantly in Home Assistant or it could be missed. This means the sensor must take initiative and send the data to Home Assistant.
An example for pulling is [aREST](/components/sensor.arest/). This is a great way to work with the ESP8266 based units and the Ardunio IDE.
### <a class='title-link' name='mqtt' href='#mqtt'></a> MQTT
You can find a simple examples for publishing and subscribing with MQTT in the [MicroPython](https://github.com/micropython/micropython-lib) library overview in the section for [umqtt](https://github.com/micropython/micropython-lib/tree/master/umqtt.simple).
The example below is adopted from the work of [@davea](https://github.com/davea) as we don't want to re-invent the wheel. The configuration feature is crafty and simplyfies the code with the usage of a file called `/config.json` which stores the configuration details. The ESP8266 device will send the value of a pin every 5 seconds.
```python
import machine
import time
import ubinascii
import webrepl
from umqtt.simple import MQTTClient
# These defaults are overwritten with the contents of /config.json by load_config()
CONFIG = {
"broker": "192.168.1.19",
"sensor_pin": 0,
"client_id": b"esp8266_" + ubinascii.hexlify(machine.unique_id()),
"topic": b"home",
}
client = None
sensor_pin = None
def setup_pins():
global sensor_pin
sensor_pin = machine.ADC(CONFIG['sensor_pin'])
def load_config():
import ujson as json
try:
with open("/config.json") as f:
config = json.loads(f.read())
except (OSError, ValueError):
print("Couldn't load /config.json")
save_config()
else:
CONFIG.update(config)
print("Loaded config from /config.json")
def save_config():
import ujson as json
try:
with open("/config.json", "w") as f:
f.write(json.dumps(CONFIG))
except OSError:
print("Couldn't save /config.json")
def main():
client = MQTTClient(CONFIG['client_id'], CONFIG['broker'])
client.connect()
print("Connected to {}".format(CONFIG['broker']))
while True:
data = sensor_pin.read()
client.publish('{}/{}'.format(CONFIG['topic'],
CONFIG['client_id']),
bytes(str(data), 'utf-8'))
print('Sensor state: {}'.format(data))
time.sleep(5)
if __name__ == '__main__':
load_config()
setup_pins()
main()
```
Subscribe to the topic `home/#` or create a [MQTT sensor](/components/sensor.mqtt/) to check if the sensor values are published.
```bash
$ mosquitto_sub -h 192.168.1.19 -v -t "home/#"
```
```yaml
sensor:
- platform: mqtt
state_topic: "home/esp8266_[last part of the MAC address]"
name: "MicroPython"
```
[@davea](https://github.com/davea) created [sonoff-mqtt](https://github.com/davea/sonoff-mqtt). This code will work on ESP8622 based devices too and shows how to use a button to control a relay.
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Smarter SmartThings with MQTT and Home Assistant]]></title>
<link href="https://home-assistant.io/blog/2016/02/09/Smarter-Smart-Things-with-MQTT-and-Home-Assistant/"/>
<updated>2016-02-09T07:44:00+00:00</updated>
<id>https://home-assistant.io/blog/2016/02/09/Smarter-Smart-Things-with-MQTT-and-Home-Assistant</id>
<content type="html"><![CDATA[_This is a guest post by Home Assistant users [Jeremiah Wuenschel](https://github.com/jer) and [St. John Johnson](https://github.com/stjohnjohnson)._
So you own a [SmartThings][smartthings] Hub. You probably bought it when you were looking to get into the whole Home Automation hobby because it worked with pretty much everything and offered you the ability to automate __anything.__ After a week of ownership, you realized that building dashboards and automating required writing way more Groovy then you expected. Then one day you were browsing [reddit][r/homeautomation] and discovered the amazingness that is Home Assistant! A solution that offered dashboards, graphs, working support for Nest, and REAL EASY automation!
You spent your weekend getting everything set up, showing it off to your significant other, but in the end you got stumped when it came to integrating with all your existing SmartThings toys. What do I do now? Should I buy another hub? Should I just buy a Z-Wave stick?
That's where we came in. We wanted a solution that can bridge the awesomeness of Home Assistant with the SmartThings hub that works with almost everything.
<p class='img'>
<img src='/images/blog/2016-02-smartthings/splash.png'>
</p>
<!--more-->
## Glossary
This is going to be a pretty detailed tutorial on setting up our SmartThings bridge. However, there are a couple key terms that _might_ be new to you:
- [MQTT][mqtt]: A lightweight message protocol for listening and publishing events that happen. Many home automation platforms have built in support for this [(especially Home Assistant)][mqtt-ha].
- [Docker][docker]: A tool for running applications that are self-contained. No need for installing any dependencies or worrying about conflicts. Installs easily on Linux and OSX.
## Setting up the Bridge
### MQTT
Assuming that you already have Home Assistant and Smart Things running, you will first want to get an MQTT broker running. There are a handful of [MQTT][mosquitto] [brokers][emqttd] available in Open Source land. We chose [Mosca][mosca] for its simplicity.
There is very little you need to do to get Mosca running. The easiest approach is to use [Docker][docker], and run a command like the following:
```bash
$ docker run \
-d \
--name="mqtt" \
-v /opt/mosca:/db \
-p 1883:1883 \
matteocollina/mosca
```
This will start Mosca up inside of a docker container, while keeping persistent storage for Mosca in `/opt/mosca`. The default configuration is the only thing we need to get things up and running.
If you don't want to mess with Docker and can get node.js installed without trouble, the [standalone][mosca-standalone] instructions are all you need.
### MQTT Bridge
This is the small piece of magic that bridges the gap between MQTT and SmartThings. It is a node.js app, and like Mosca it is probably easiest to install with Docker:
```bash
$ docker run \
-d \
--name="mqtt-bridge" \
-v /opt/mqtt-bridge:/config \
-p 8080:8080 \
stjohnjohnson/smartthings-mqtt-bridge
```
The code for this bridge is [on Github][mqtt-bridge] if you want to start it up independently.
The MQTT Bridge only needs to know where your MQTT broker lives. If you are using these docker commands as-is, edit `/opt/mqtt-bridge/config.yml` to look like this:
```yaml
---
mqtt:
host: <IP of the host>
```
Restart the bridge, and you are ready to go:
```bash
$ docker restart mqtt-bridge
```
### SmartThings Device
The next step (and possibly the most confusing) is the device type. Go to the [Smart Things Device IDE][ide-dt] and `Create New Device Handler`. Choose `From Code` and paste in the [MQTT Bridge Device Code][devicetype]. Click `Save`, `Publish`, and then `For Me`.
Now to install your new Device Handler. Go back to `My Devices` in the IDE, and click `New Device`. Enter a name, and pick any random set of characters for the Device Network Id (this will automatically update later). For Type, scroll to the bottom of the list and find your newly created `MQTT Bridge`. Fill in the other boxes however you like.
Go back to `My Devices`, and click on your new device in the list. This will bring up a page that allows you to edit your device's Preferences. Click `edit` and fill in the 3 pieces of information it asks for.
- MQTT Bridge IP Address: \<IP address of the MQTT Bridge from the previous step>
- MQTT Bridge Port: \<8080 if you have changed nothing in the previous commands>
- MQTT Bridge MAC Address: \<Mac address of machine running the Bridge code>
This will create the link between SmartThings and the MQTT Bridge.
### SmartThings App
The last step is to setup the SmartApp. After this, any registered devices will start sending their events to MQTT.
Go to the [Smart App IDE][ide-app]. Click `New SmartApp`, followed by `From Code`. Paste in the [MQTT Bridge SmartApp code][smartapp] and click `Save`. Click `Publish` and then `For Me`. In the SmartThings mobile app, add the new SmartApp and configure it with your devices and MQTT Bridge device. Clicking `done` will subscribe SmartThings to your MQTT broker and begin 2-way propagation of events.
### Configure Home Assistant
To add SmartThings devices to Home Assistant over MQTT, first enable MQTT in Home Assistant:
```yaml
mqtt:
broker: localhost
```
Replace `localhost` with the location of the running MQTT Broker. Devices from the MQTT Bridge are published to the path `smartthings/<Device Name>/<Attribute>`
For example, my Dimmer Z-Wave Lamp is called "Fireplace Lights" in SmartThings. The following topics are published:
| Topic | Description
| ----- | -----------
| smartthings/Fireplace Lights/level | Brightness (0-99)
| smartthings/Fireplace Lights/switch | Switch State (on/off)
Here is an example Home Assistant config:
```yaml
switch:
platform: mqtt
name: "Fireplace Lights"
state_topic: "smartthings/Fireplace Lights/switch"
command_topic: "smartthings/Fireplace Lights/switch"
brightness_state_topic: "smartthings/Fireplace Lights/level"
brightness_command_topic: "smartthings/Fireplace Lights/level"
payload_on: "on"
payload_off: "off"
retain: true
```
We recommend `retain: true` for every MQTT device in order to keep states in sync when things become disconnected.
Start digging through the [MQTT Components][mqtt-ha] in Home Assistant to find which components map to the new events being published to MQTT.
### Configuring with Docker-Compose
Our personal preference for starting the whole suite of software is to use a single Docker-Compose file. Just create a file called `docker-compose.yml` like this:
```yaml
mqtt:
image: matteocollina/mosca
ports:
- 1883:1883
mqttbridge:
image: stjohnjohnson/smartthings-mqtt-bridge
volumes:
- ./mqtt-bridge:/config
ports:
- 8080:8080
links:
- mqtt
homeassistant:
image: homeassistant/home-assistant:latest
ports:
- 80:80
volumes:
- ./home-assistant:/config
- /etc/localtime:/etc/localtime:ro
links:
- mqtt
```
This will start home-assistant, MQTT, and the Bridge, in dependency order. All config can reference the name of the docker container instead of using IP addresses (e.g. mqtt for the broker host in Home Assistant).
### How it works
**HTTP Endpoint**: There are really only 2 ways to communicate with the SmartThings hub that we could find. The easiest approach is to create a RESTful SmartApp authenticated with OAuth that provides state changes via HTTP directly. This approach is pretty straightforward to implement, but it requires communication with the SmartThings cloud service, and can't be done entirely on your LAN. We hoped to keep all communication internal, and came up with a second approach.
**Custom Device Type:** SmartThings custom device types allow developers to define handlers for HTTP events received directly over the local network by the SmartThings hub. Messages received are authenticated by MAC address, and can contain arbitrary strings in their payload. Since a Device Type is only ever tied to a single device, we need to add a SmartApp to the mix in order to translate events between individual devices and our special Home Assistant Bridge device. Here is what we have so far:
```text
Z-Wave Switch |
Zigbee motion sensor |<---> Bridge App <---> Bridge Device Type <---> <Local network>
Z-Wave light bulb |
```
On the Home Assistant side, there is a powerful platform available based on the MQTT lightweight message bus protocol. Everything from lights to switches to temperature sensors can be defined in Home Assistant as an MQTT component, so it makes for a convenient integration point. This requires an MQTT broker for handling the message bus, and one last piece to translate between the HTTP that SmartThings supports and MQTT.
Here is the final sequence of events:
<p class='img'>
<a href='/images/blog/2016-02-smartthings/SmartThings-HomeAssistant.png'>
<img src='/images/blog/2016-02-smartthings/SmartThings-HomeAssistant.png' alt='SmartThings Bridge Sequence'>
</a>
SmartThings Bridge Sequence
</p>
There are a lot of stops along the way for these events, but each piece is a simple translation layer to shuttle the events between systems.
### Future Improvements
- **Raspberry Pi**: There is a lot of interest in getting this running on the Raspberry Pi. It only requires binaries compiled for ARM, so we plan to get ARM-compatible versions of the containers going at some point.
- **Authentication for MQTT**: At the moment, the MQTT bridge doesn't understand how to authenticate to MQTT, so only unauthenticated MQTT is supported. This is mitigated to some degree if you use our Docker Compose config, because MQTT's port is not actually shared publicly.
- **Authentication for MQTT Bridge**: Right now the bridge expects that anyone subscribing is the SmartThings hub. This could use proper authentication.
[mosquitto]: http://mosquitto.org/
[emqttd]: https://github.com/emqtt/emqttd
[mosca]: http://www.mosca.io/
[docker]: https://www.docker.com/
[mosca-standalone]: https://github.com/mcollina/mosca#standalone
[mqtt-bridge]: https://github.com/stjohnjohnson/smartthings-mqtt-bridge
[ide-dt]: https://graph.api.smartthings.com/ide/devices
[devicetype]: https://github.com/stjohnjohnson/smartthings-mqtt-bridge/blob/master/devicetypes/stj/mqtt-bridge.src/mqtt-bridge.groovy
[ide-app]: https://graph.api.smartthings.com/ide/apps
[smartapp]: https://github.com/stjohnjohnson/smartthings-mqtt-bridge/blob/master/smartapps/stj/mqtt-bridge.src/mqtt-bridge.groovy
[mqtt-ha]: /components/mqtt/
[smartthings]: http://smartthings.com
[r/homeautomation]: https://www.reddit.com/r/homeautomation
[mqtt]: https://en.wikipedia.org/wiki/MQTT
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Report the temperature with ESP8266 to MQTT]]></title>
<link href="https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt/"/>
<updated>2015-10-11T19:10:00+00:00</updated>
<id>https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt</id>
<content type="html"><![CDATA[{::options coderay_line_numbers="table" /}
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 Assistant can pick it up.
<p class='img'>
<img src='/images/blog/2015-10-esp8266-temp/setup.png' />
Picture of the final setup (+ 2 LED for decoration)
</p>
<p class='img'>
<img src='/images/blog/2015-10-esp8266-temp/ha-sensor.png' />
Home Assistant will keep track of historical values and allow you to integrate it into automation.
</p>
<!--more-->
### <a class='title-link' name='components' href='#components'></a> Components
I've been using Adafruit for my shopping:
- [Adafruit HUZZAH ESP8266 Breakout](http://www.adafruit.com/product/2471) ([assembly instructions](https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout/assembly))
- [Adafruit HDC1008 Temperature & Humidity Sensor Breakout Board](http://www.adafruit.com/product/2635) ([assembly instructions](https://learn.adafruit.com/adafruit-hdc1008-temperature-and-humidity-sensor-breakout/assembly))
- [MQTT server](/components/mqtt/#picking-a-broker)
_Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, soldering iron + wire, Serial USB cable._
### <a class='title-link' name='alternatives' href='#alternatives'></a> Alternatives
Since this article has been published the HDC1008 has been discontinued. Updated sketches are available for the following alternatives:
- [DHT22 sensor](https://www.adafruit.com/product/385) and [updated sketch](https://gist.github.com/balloob/1176b6d87c2816bd07919ce6e29a19e9).
- [BME280 sensor](https://www.adafruit.com/product/2652) and [updated sketch](https://gist.github.com/mtl010957/9ee85fb404f65e15c440b08c659c0419).
### <a class='title-link' name='connections' href='#connections'></a> 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._
### <a class='title-link' name='preparing-your-ide' href='#preparing-your-ide'></a> Preparing your IDE
Follow [these instructions](https://github.com/esp8266/Arduino#installing-with-boards-manager) 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
### <a class='title-link' name='sketch' href='#sketch'></a> 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 62.
- 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.
```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 !isnan(newValue) &&
(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);
}
}
}
```
### <a class='title-link' name='configuring-home-assistant' href='#configuring-home-assistant'></a> 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.
```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: "%"
```
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Using MQTT with Home Assistant]]></title>
<link href="https://home-assistant.io/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant/"/>
<updated>2015-09-11T09:19:38+00:00</updated>
<id>https://home-assistant.io/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant</id>
<content type="html"><![CDATA[{::options coderay_line_numbers="table" /}
<img src='/images/supported_brands/mqtt.png' style='border:none; box-shadow: none; float: right;' height='80' /> [MQTT](https://en.wikipedia.org/wiki/MQTT) support was added to Home Assistant recently. The [MQTT component](https://home-assistant.io/components/mqtt/) 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](https://home-assistant.io/blog/2015/08/26/laundry-automation-with-moteino-mqtt-and-home-assistant/) post in this blog.
This post will give you a small overview of some other possibilities on how to use MQTT with Home Assistant.
<!--more-->
### <a class='title-link' name='manual-usage' href='#manual-usage'></a> 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.
```yaml
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](https://home-assistant.io/components/mqtt/) or the [MQTT sensor](https://home-assistant.io/components/sensor.mqtt/) documentation.
Now we can set the mood. The commandline tool (`mosquitto_pub`) which is shipped with `mosquitto` is used to send an MQTT message.
```bash
$ mosquitto_pub -h 127.0.0.1 -t "home-assistant/fabian/mood" -m "bad"
```
<p class='img'>
<img src='/images/blog/2015-09-mqtt/mood.png' />
The Mood sensor
</p>
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.
### <a class='title-link' name='python-mqtt-bindings' href='#python-mqtt-bindings'></a> 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](https://eclipse.org/paho/clients/python/) 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.".
```yaml
- 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](https://eclipse.org/paho/clients/python/docs/) documentation.
```python
#!/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.
<p class='img'>
<img src='/images/blog/2015-09-mqtt/lottery.png' />
The Lottery sensor
</p>
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.
### <a class='title-link' name='arduino' href='#arduino'></a> 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.
<p class='img'>
<img src='/images/blog/2015-09-mqtt/arduino-shield.png' />
The Arduino UNO with Ethernet shield and photo resistor
</p>
The [MQTT client](http://knolleary.github.io/pubsubclient/) 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.
```c
/*
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 milliseconds
// 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.
```yaml
- 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.
<p class='img'>
<img src='/images/blog/2015-09-mqtt/arduino.png' />
The Brightness sensor
</p>
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.
]]></content>
</entry>
</feed>