How to send messages to a broker via mqtt

Hi everyone, it is the first time I am using Node Red and I have a question to you.
I'm trying to send my data to a broker via mqtt and I want to specify the message in the "function" block. I've already set the broker adress on the mqtt out node. What should I write on the function node to pass a message directly to the mqtt out using msg.topic?
Thank you all

You have to specify the mqtt topic in either the mqtt-out node or pass it into the mqtt-out node in msg.topic. The data being sent should be in msg.payload.

Ok, the broker I'm using is thingspeak so in the topic of the mqtt-out I have to put the channel where i want to publish the data with the correct structure and then for example "/msg.payload.temp" if my data is a temperature?

Give us an actual example with real numbers. Tell us what is in the example incoming message and tell us what MQTT topic and value you want published.

From the Help tab of the mqtt-out node:

Details

msg.payload is used as the payload of the published message. If it contains an Object it will be converted to a JSON string before being sent. If it contains a binary Buffer the message will be published as-is.

I have some weather data in Json, and I want to pass these data to my thingspeak channel using MQTT

Ok thanks, but can you give me a guide of how write messages in the function block? I am not very practical

Why do you need to use a function node?
What does the data coming if into your flow look like and what does it need to look like going into the MQTT-out node?

Have you read the documentation about functions?

Because I am trying to follow this sentence on the mqtt out node: Leave topic blank if you want to set it via msg properties". So I do not know where to set it, i tought it was something on the function node...

You can set msg.topic in a function node or a change node.

Or in the MQTT-out node. You have a choice and can pick the one best suited to what you want to do.

ok, so in the function node i can write something like "msg.topic=msg.payload..."? And i have to return msg.topic?

In the Topic field? Because I have tried many times to set it in the MQTT node

You must return a msg object from a function.

What are you actually trying to achieve? Why would you want to set topic to value of payload?

Because I didn't manage to set it by using the mqtt-out "topic" field. I want to send 2 different data in json to a broker

Fred, a MQTT message has two parts, the topic and the payload. The topic is what another device will subscribe to.

Let's say you have device A that is reading a BME280 and publishing the temperature, humidity and pressure. It would put all three in the payload and then set a topic like node34/livingroom/data and then publish that message. The broker (a device in control of all the messages) when it receives the message will look to see who has subscribed to that topic (there are wildcard's thattt can be used in the topic but ignore them for now).

Now say device X and device M have both subscribed to that topic, then the broker will send the message to both and each one will receive the message with the topic and the payload.
(you shoud read MQTT Essentials - All Core Concepts explained to get an understanding of MQTT)

So in your case, you can set the topic in the mqtt-out node (and it will be fixed) or you can set it in the msg being sent to the mqtt-out node in msg.topic with the data in msg.payload.

Sending it via msg.topic allows you to publish to multiple topics and only need one mqtt-out node. If you hard code in in the mqtt-out node, then you will ned one node for every topic you publish to.

1 Like

Ok thank you. So I have to write two parts: one is the channel where I am publishing it (e.g. the channel of thingspeak I created), the second one is the payload. the thing I do not understeand is how i can write the payload part...

I try to be more specific: I have set my channel and all the stuff and the broker tells me that I am publishing something (entries != 0) indeed. So I think the problem is the way/format I am publishing data

Do this, add an mqtt-in node connected to a debug node (set to display the complete msg object) and set the mqtt topic in the node to the one you are using to publish the message. Then you will see that the msg is being sent.

I still can't see anything on my broker as a subscriber...