Smart Homes and MQTT

Because the HTTP protocol is most common protocol on the Internet it has become the main protocol for controlling first generation smart home devices.

However HTTP is not the main protocol for IOT (Internet of things) devices.

The choice here is MQTT (MQ Telemetry Transport ) and this will also most likely become the main control protocol for home automation.



Many second generation smart home devices will be bi-lingual in that they will support both protocols.

Using MQTT on a Home Network

MQTT is a very simple and fast messaging protocol that uses the publish and subscribe model.

This model relies on a central broker that is used to exchange messages as shown in the diagram below.

MQTT- Publish-Subscribe-Model
This model is very similar to the radio/TV broadcasting model.

In this model a publisher publishes on a topic (radio =channel) and a subscriber simply subscribes to that topic to receive the messages.

You can learn more about MQTT on my other site See- How MQTT Works.

MQTT Brokers or Servers

Although there are cloud based test and commercial brokers available See MQTT Brokers/Servers and Cloud Hosting Guide the best choice for a home network is a locally installed broker.

The most popular one is mosquitto and is the one I use and will use in the tutorials on this site.

You can install Mosquitto on Linux and Windows and also on the Raspberry Pi. See How to Install Mosquitto on The Raspberry Pi.

However if you are using node-red then Aedes is a MQTT broker that can be installed as a node. It is a replacement for the Mosca broker which is no longer maintained.

If you are using homeassistant then you will need to use mosquitto as Aedes does not provide a persistent store.

Changing Brokers

In the event of a broker failure having access to a replacement broker is important and in order to make the change over easy then it is important that you run a local DNS server on your home network.

See Using DNSMasq

MQTT Clients

There are clients for almost all programming languages.

Node-red has an MQTT publish and subscribe node built in and on this site I will concentrate on using this client.

Here is a detailed tutorial on Using the Publish and Subscribe nodes on node-red.

I also did a video here

I cover the Python client in detail on my other site here.

Important MQTT Properties

  • There is no direct connection between subscriber and publisher.
  • MQTT is a 1 to many protocol by default
  • MQTT supports QOS levels to guarantee message delivery.
  • MQTT can use SSL for security
  • MQTT can be used in a web browser Using MQTT over Websockets.
  • Any message restrictions are set on the MQTT broker.
  • MQTT brokers don’t normally store Messages.

Home Assistant and MQTT

Home assistant has a mosquitto MQTT integration but it is installed as an add-on.

This means that you will need full installation to use it (not a docker Container).

However you can just install mosquito on the home assistant system or another system and use it with home assistant. See Mosquitto install.

MQTT Topics

It is important to create a topic structure for your services that doesn’t conflict with common topic structures found on home networks

Using a topic base isn’t strictly speaking necessary but it makes topic filtering much easier and lets you support multiple networks on a single broker.

Therefore my preferred approach is to use a base topic for all MQTT devices on the network.

I use a topic base of home on my home network.

Reserved Topics

There are several common reserved topics used by other services that are common on home networks which you should avoid. The ones I am familiar with are:

  • zigbee2MQTT/#
  • tasmota2mqtt/#
  • homeassistant/#
  • shellies/#

Summary

MQTT is the effectively the de facto protocol for IOT (internet of things) and by extension Smart Homes.

It is a messaging protocol that is much faster than HTTP and easier to use.

Although many existing smart home devices currently use HTTP the future almost certainly belongs to MQTT.

Related Tutorials and Resources:

 

Please Let me Know if you found it Useful
[Total: 1 Average: 5]

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *