-
Notifications
You must be signed in to change notification settings - Fork 34
MQTT Basics
Some thoughts for a basics description for MQTT as this comes up a lot in the forums at the moment:
Couple of links to consider including:
- https://www.hivemq.com/mqtt/ - some great learning info here
- http://mqtt.org/
- https://mosquitto.org/
- http://www.steves-internet-guide.com/mqtt/
- https://kamilfb.github.io/mqtt-spy/ - very useful MQTT utility
- http://www.mqtt-dashboard.com/index.html - a public broker for testing
- https://www.cloudmqtt.com/plans.html - a cloud (SaaS) broker with a free tier
To use MQTT, you need an MQTT Broker which is a separate service that needs to be running somewhere accessible on your local network. Many of us run it on the same Pi that Node-RED is running on. We tend to use an application called Mosquitto as it is available on all platforms and is very compact.
MQTT uses a publish and subscribe model. You publish messages to a "topic". Elsewhere, you subscribe to 1 or more topics to get the data that is published elsewhere. This is helpful, even when only using Node-RED, because it lets you disaggregate your logic between the parts that capture information from somewhere (e.g. a sensor) and the parts that do something useful with that data. So when you have to change things or add new processing, it is easier to do so without disrupting everything else.
Node-RED has both publish (mqtt out) and subscribe (mqtt in) nodes.
If you were using a sensor that didn't support MQTT but that can send data to Node-RED, you would connect the sensor to NR and use an mqtt out node to push the data to a topic that you specify - perhaps something like: "SENSORS/LIVINGROOM/TEMPERATURE". In a separate flow, you subscribe to that sensor (mqtt in) to process the data as it is updated.
Sensor Input -> Function Node (if required to sanitise and normalise the data) -> MQTT Out (topic)
MQTT In (topic) -> Nodes to do something useful like pass the information to Dashboard