# How to send messages to a broker via mqtt

**URL:** <https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651>\
**Category:** General\
**Created:** [24 April 2021 09:55 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651 "2021-04-24T09:55:48Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 April 2021 17:57 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/21 "2021-04-24T17:57:50Z")

</div>

Add a debug node showing what you are sending to the mqtt node, and show us how you have configured that node.

---

<div class="post-metadata">

**Author:** ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)\
**Post date:** [25 April 2021 10:30 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/22 "2021-04-25T10:30:36Z")

</div>

the mqtt one or the debug node? data are correct, in the debug of node red i can see the right data

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [25 April 2021 11:18 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/23 "2021-04-25T11:18:20Z")

</div>

That's great you can see the data, now can you do as @colin asked and show it to us?  
It would be helpfup if you posted an export of your flow since no one but you can see it.

In order to make code more readable and importable it is important to surround your code with three backticks  
`````  
`like this`  
`````

You can edit and correct your post by clicking the pencil icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)\
**Post date:** [25 April 2021 11:46 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/24 "2021-04-25T11:46:06Z")

</div>

So, I have in my description of MQTT-out node:

```auto
msg.topic=msg.payload.main.temp;
return msg.topic;

```

where msg.payload.main.temp is the data I read correctly from the sensor as I see in the debug window but that I can manage to publish on my broker (although my broker says there has been the entry of the data)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 April 2021 11:51 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/25 "2021-04-25T11:51:04Z")

</div>

Hi, it looks like you dont quite get the concepts.

- `topic` is a name like `"room1/temperature"`
- `payload` should be the value e.g. `21.5`

you send these in a `msg` object to the mqtt-out node...

```auto
msg.topic = "room1/temperature";
msg.payload = msg.payload.main.temp
return msg;

```

try sending that to your mqtt out node.

Also, try subscribing to topic "room1/temperature" - you will see a value when the publish occurs.

---

<div class="post-metadata">

**Author:** ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)\
**Post date:** [25 April 2021 12:00 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/26 "2021-04-25T12:00:33Z")

</div>

Ok, I got "Invalid topic specified". I think the topic is correct because I know the structure of topic on thingspeak. I have to use " ", right?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [25 April 2021 12:43 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/27 "2021-04-25T12:43:17Z")

</div>

> [@fred1985](#):
>
> . I have to use " ", right?

No. Just put room1/temperature not "room1/temperature" in the MQTT node.

---

<div class="post-metadata">

**Author:** ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)\
**Post date:** [25 April 2021 15:08 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/28 "2021-04-25T15:08:48Z")

</div>

still doesn't work. I don't have to write the topic also in the topic section because I am setting the node via msg, correct?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [25 April 2021 16:13 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/29 "2021-04-25T16:13:07Z")

</div>

Why not make life easy for yourself, and just specify the topic in the MQTT node instead of using the input msg...  
Then all you need to do is to inject the sensor reading as a payload.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 May 2021 16:13 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/30 "2021-05-09T16:13:45Z")

</div>

Closed on 9 May 2021 16:13 UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651.md?page=1)
