DIY Projects

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

8 test websocket mqtt domoticz cloudmqtt 1

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

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

Configure the connection to Mosquitto

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

I advise you set the QoS to 2.

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

Connect the MQTT Node-RED router to cloudMQTT

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

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

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

Here is an example configuration.

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.

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.

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:

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 + (wildboard one level *)

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

(*) the multi-level wildboard # 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":"","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":"","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 …)

Click to rate this post!
[Total: 0 Average: 0]
Exit mobile version