MQTT router with Node-RED, connect all your Sonoff Tasmota modules to cloudMQTT

In the previous tutorial, we tested the free online broker cloudMQTT. The free offer is limited to 5 simultaneous connections. It is more than enough to drive a home automation server even when you are not at home, but it is too little if you have developed many connected objects controlled MQTT.

I propose here to develop a router MQTT using Node-RED. All exchanges with your DIY home automation accessories will be secure for an SSL connection between Node-RED and cloudMQTT.Configuration required: local MQTT broker, Node-RED on Raspberry PiYou will need a local broker and Node-RED. The Raspberry Pi (or any other mini-PC) is suitable for this type of application. The performance / energy consumption ratio is unbeatable. I still offer the Mosquitto broker in my projects but there are many others. No matter the broker employed, the principle will be the same.

Follow this two tutorials to prepare your environment:

If the subject interests you, here are more articles to go further with Node-RED

All Node-RED tutorials

Create the connection to the local broker (here Mosquitto)

The first thing to do and add a MQTT Node to the current flow

node-red node mqtt input

Make a double click to edit the parameters of the Node. Click on the pencil beside the Add new mqtt-broker selector.

node-red mqtt router cloudmqtt add broker

Configure the connection to Mosquitto

  • Server: enter the IP address or localhost if Mosquitto is installed on the same Raspberry Pi as NodeRED
  • Port: 1883 by default
  • User and password on the Security tab ( recommended)

mosquito node-red mqtt connexion

It is possible to publish messages automatically to know the status of the router by going to the Messages tab

  • The start of the router (birth message)
  • The shutdown of the router (close message)
  • An error (will message)

I advise you set the QoS to 2.

Fill the configuration by clicking Done in the upper right corner.

node-red mqtt connection configre messages

Connect the MQTT Node-RED router to cloudMQTT

Add a new Output MQTT Node to the flow. Double-click to configure it.

node-red mqtt output node

Configure the connection with the parameters retrieved on the cloudMQTT instance:

  • Server: the cloudMQTT
  • Port instance: specify the SSL port for the connection to be secure
  • Check Enable secure SSL / TLS connection
  • Enter the user and password for the connection instance in the Security tab

cloudmqttt node-red connection

You can also configure messages that will tell you the status of the router.

Here is an example configuration.

cloudmqtt messages node-red configuration router

Deploying the MQTT router on Node-RED

All that remains is to connect the two Nodes and deploy the flow by clicking on Deploy in the upper right corner.

node-red cloudmqtt router mosquitto raspberry pi

Open on the UI websocket on cloudMQTT to check that the router is started. Here, I also turned on a smart socket Sonoff S26 hacked with the firmware Tasmota. The message with the stopped status was sent when the flow was deployed, which is perfectly normal.

cloudmqtt websocket ui node-red router status

Bidirectional router for Tasmota accessories

This configuration unfortunately only works in one direction. With the previous flow, we will be able to recover all states and values (on Homy for example), but we will not be able to send commands to turn on / off a Sonoff module. To do this, we will have to send incoming messages back to cloudMQTT and send them back to the local broker.

So that it works, we will filter the following messages the type of topic. Tasmota publishes messages on 3 different topics:

  • cmnd: command to execute on the module (on, off, toggle)
  • stat: status feedback of a command on the module
  • tele: information on the state of the module.

The first block transfers all the messages published on the topic stat /# to cloudMQTT. The # returns all of the Tasmota accessories. The second flow transfers the commands sent to cloudMQTT on the local broker (on which the Sonoff – Tasmota accessories are connected). This time we filter only the posts published on topic cmnd and ends with POWER. For this we use the + (wildcard one level *)

sonoff tasmota mqtt router cloudmqtt

Do not forget to adapt the topic if you have configured a prefix on Tasmota

(*) the multi-level wildcard # does not work on Node-RED.

Source code of the router MQTT Node-RED

Past this code of this flow on your Node-RED then modify the connection parameters before deploying it.

[{"id":"4de21b42.3cb0d4","type":"mqtt in","z":"1c02b62a.c1ff4a","name":"","topic":"stat/#","qos":"0","datatype":"auto","broker":"80ccacd4.35a24","x":90,"y":100,"wires":[["ddff4f0.0c79fb"]]},{"id":"ddff4f0.0c79fb","type":"mqtt out","z":"1c02b62a.c1ff4a","name":"cloudMQTT","topic":"","qos":"","retain":"","broker":"6c628311.0486ac","x":330,"y":100,"wires":[]},{"id":"e6695679.c6a678","type":"mqtt in","z":"1c02b62a.c1ff4a","name":"","topic":"cmnd/+/POWER","qos":"0","datatype":"auto","broker":"6c628311.0486ac","x":120,"y":180,"wires":[["d756894a.745608","80b760e.ba703a"]]},{"id":"d756894a.745608","type":"mqtt out","z":"1c02b62a.c1ff4a","name":"Mosquitto local","topic":"","qos":"","retain":"","broker":"80ccacd4.35a24","x":340,"y":180,"wires":[]},{"id":"80b760e.ba703a","type":"debug","z":"1c02b62a.c1ff4a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":330,"y":240,"wires":[]},{"id":"80ccacd4.35a24","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.1.90","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"node-red-router","birthQos":"2","birthRetain":"false","birthPayload":"started","closeTopic":"node-red-router","closeQos":"2","closePayload":"stopped","willTopic":"node-red-router","willQos":"2","willPayload":"stopped"},{"id":"6c628311.0486ac","type":"mqtt-broker","z":"","name":"cloudMQTT","broker":"m24.cloudmqtt.com","port":"26307","tls":"","clientid":"","usetls":true,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"node-red-router/status","birthQos":"2","birthRetain":"false","birthPayload":"online","closeTopic":"node-red-router/status","closeQos":"2","closePayload":"stopped","willTopic":"node-red-router/status","willQos":"2","willPayload":"stopped"}]

That’s it, you can now connect so many home accessories and MQTT connected objects and drive them out of your home from Homy or an app. Nothing prevents you to adapt the principle to adapt to other firmware (Espurna, ESPEasy, Arduino code or Python …)

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

We will be happy to hear your thoughts

Leave a Reply

DIY Projects
%d bloggers like this: