Node-RED: Lecture 3 – Example 3.6 Using an mqtt output node to publish MQTT messages

As an alternative to using the HiveMQ test page to publish on the MQTT topic, we can configure an mqtt output node and use it to publish MQTT messages. This is the mirror of the mqtt input node and allows you to configure an MQTT service and the topic you are publishing on.

You can then send the node messages with the exact same JSON string we’ve been sending via the HiveMQ test page.

If you’ve not been following these examples sequentially and the MQTT nodes are not familiar to you, take a look at Example 3.1 to see how to setup and provision the MQTT node.

To use the mqtt output node, drag and drop three inject nodes and an mqtt output node as shown in Fig 3.17, and configure and wire up as shown. Make sure you are connecting to the same MQTT server as you configured for the mqtt in node in the previous examples.

Figure 3.17 using an mqtt node to publish on the noderedlecture/sensor topic

You can now test the flow you created to analyze the MQTT message directly from the workspace by clicking on the three inject nodes in sequence. The first will exercise the part of the flow handling the case when analysis is turned off; the 2nd two messages will cause the path with the rbe node to be exercised.

You can view and copy the node-red description of this flow below:

Open to view/copy flow
[{"id":"50c4d9227192eb19","type":"mqtt out","z":"8be911e7df739406","name":"","topic":"noderedlecture/sensor","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"bbe24556bc1690f5","x":580,"y":500,"wires":[]},{"id":"6112bd8fc5dd08cd","type":"inject","z":"8be911e7df739406","name":"{\"analyze\":false, \"value\":10}","repeat":"","crontab":"","once":false,"topic":"","payload":"{\"analyze\":false, \"value\":6}","payloadType":"string","x":309.00001525878906,"y":447,"wires":[["50c4d9227192eb19"]]},{"id":"e54a8816bc41e44d","type":"inject","z":"8be911e7df739406","name":"{\"analyze\":true, \"value\":6}","repeat":"","crontab":"","once":false,"topic":"","payload":"{\"analyze\":true, \"value\":6}","payloadType":"string","x":321.00001525878906,"y":497,"wires":[["50c4d9227192eb19"]]},{"id":"629cbc4ae5b416f8","type":"inject","z":"8be911e7df739406","name":"{\"analyze\":true, \"value\":1}","repeat":"","crontab":"","once":false,"topic":"","payload":"{\"analyze\":true, \"value\":10}","payloadType":"string","x":318.00001525878906,"y":546,"wires":[["50c4d9227192eb19"]]},{"id":"bbe24556bc1690f5","type":"mqtt-broker","name":"test mqtt","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"noderedlecture/sensor","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

Back to Lecture 3

                                   PREVIOUS example                        NEXT example


© Lea, Blackstock, Calderon

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Author: Rodger Lea

Currently CEO of Internet of Things startup, Sense Tecnic, Dr. Lea has over 25 years experience spanning academic, large corporations and startups. For the last 10 years, he has started or helped start 4 new companies while managing an active research program (University of British Columbia, Canada and Lancaster University, UK) into distributed and ubiquitous computing, the IoT and Smart Cities.