Losant MQTT Broker and nodered

Hi There,
I am trying to publish an MQTT containing a simple string and subscribe the same from a nodered instance on my network.
On the Losant side I have created the credentials keys which I am using to subscribe the topic on the nodered side and in the MQTT Node I have only written the topic.
On the nodered side I am using the above credentials and I see that the MQTT node is connected. However I am not receiving anything on the nodered side. I am not sure where the issue is.

Any help will be appreciated. Thanks

@Felice_Massaro,

I’d like to get a better idea of your use case. Could you tell me what your overall goal is with this project? Having this context will help me give you better answers to your questions.

First, I’d like to point out that QoS 2 is not supported for publishing or subscribing on the Losant Broker per our documentation.

Would you be able to share a screenshot of the MQTT output node you have in the workflow you shared?

Can you confirm on the Node-Red side that the subscription is successful?

As I understand it, you are attempting to trigger a workflow with a webhook, then publish to a custom MQTT topic, is that correct?

Access Keys & Secrets by default are only scoped to only losant topics. You may need to ensure that your access key & secret pair are set to “Only the following topics…” where you list the specific topic, or “All Topics”.

Please let me know if any of this helps, or if you need more clarification. Any additional information that you can provide will also be a big help.

Thank you,
Heath

Hi @Heath,
My overall goal:
I have a RaspberryPi in the field to measure a dam level. The data is encoded and transmitted with Sigfox (it is in a rural area in Africa with no other infrastructures). Thanks to your help, yesterday I finally got the Sigfox data of my interest out of the flow. Now I am attempting to publish the string received from Sigfox to an MQTT Broker which I will subscribe from a node in nodered which is running on my network 1800 km away. From there I will use the data.
This is the MQTT I have at the end of the flow now:


As mentioned in the previous post, I have generated the access keys for this device and used the same as user/password in the MQTT Node subscribing the topic “Sig1” on nodered.
On nodered I see the green light on the MQTT Node so I assume is connecting to the broker on Losant. But I do not receive any message. I am not sure how and if I should set the message template in the node. May be that’s the problem. But probably it is not the only one?

Hey @Felice_Massaro,

Thanks for providing that additional context! It’s a huge help, and I’m really interested in your use case.

I gave you some information and things to try out in my previous message. Would you be able to address those items and get back to me?

On nodered I see the green light on the MQTT Node so I assume is connecting to the broker on Losant.

Node-Red also has a Debug Node. You can use this node in a similar way to the Losant Debug node. This may give you some information on what is happening in your Node-Red workflow.

Additionally, Losant offers a Gateway Edge Agent. Have you looked into this to see if it fits your use case?

Thank you,
Heath

Hi Heath,
Yes. I am trying to publish an MQTT with the payload from a function node

payload = payload.working.decoded.data;
return payload

the output value I am trying to publish is something like this: “5af3107a1d47”

I have a debug node on the nodered MQTT subscribing the Losant broker but unless I receive a message there is nothing showing me any detail of the connection rather than the green light “connected”.
I think my problem is on the Losant side where I have used as topic “Sig1” and I have not defined a message template. How do I format a message template to publish the above payload?
Is the topic in th correct format or should I format it differently?
Thanks for helping

@Felice_Massaro,

I want to take a step back and confirm a few things.

Have you changed the QoS level of your topic subscription from 2 to 1 or 0?

Other than seeing the Green Dot in Node-Red, can you confirm on the Losant side (or some other way) that the connection is successful? How is Node-Red connecting to Losant? As a Device?

Can you also confirm that the Access Key & Secret are set to All Topics?

The reason I ask these questions is that if the connection from Node-Red to Losant were successful, and the subscription is successful, you should (with your current node configuration) would receive a message with an empty body.

Could you send me the full code from your function node? Could you also include a debug message from your workflow as well as a screenshot of your workflow?

Thank you,
Heath

@Heath
Hi. This is the status

Losant side:
I have added an MQTT message node just after the function which output the string to be published
Broker: Losant broker
Topic: Sig1
Message template: none
I generated the keys and used the “access key” in th field user on the nodered “MQTT in” node and the “access key” as password.

On the node red side:
I have deployed an “MQTT in” node
Action: Subscribe to single topic (Sig1)
QoS: 0 (I did also try 2)
Server: mqtt://broker.losant.com:1883 connect automatically
Protocol: MQTT V3.1.1
Clinet ID: autogenerated
User: the key generated by Losant
Password: the secret generated by Losant

Stus: connecting… but doesn’t connect.
Do I need to give a path to the node in the topic field on the nodered? What else?
I am not sure what you mean with this question " How is Node-Red connecting to Losant? As a Device?"


Application

Nodered

[{"id":"7a44c1be03c0f9b8","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"d863e311fe900eb3","type":"debug","z":"7a44c1be03c0f9b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":390,"y":320,"wires":[]},{"id":"f8641ad69a52f4a2","type":"mqtt in","z":"7a44c1be03c0f9b8","name":"","topic":"Sig1","qos":"0","datatype":"auto","broker":"96e3ac46313ba4cc","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":380,"wires":[["d863e311fe900eb3"]]},{"id":"96e3ac46313ba4cc","type":"mqtt-broker","name":"","broker":"mqtt://broker.losant.com:1883","port":"1883","clientid":"","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":{},"sessionExpiry":""}]

@Felice_Massaro,

I took a look at your application. And have a few things:

  • Your single access key is set to “No Additional Topics”. When your access key is set to this, your device will only have access to device-specific topics, not custom topics like you have. As I mentioned, if you are using the same access key for multiple devices (not a best practice), you will need to set this access key/secret to “All topics”, “Only the following…” and add Sig1 to the list.
  • I’m still unsure how Node-Red is connecting to Losant. You have a single device, which I assume is the device you have in the field. No other device for the node-red connection, and no MQTT Integrations. I would suggest that you create a new device with a new access key/secret for the Node-Red connection. If you use the same deviceId for multiple MQTT connections, the devices that connects last will disconnect the other device, and if your devices are set to reconnect, then you will end up in a loop of connects and disconnects.

Here is an older blog post on how to connect Node-Red to Losant as a Gateway that should help. As you can see, we create a new device just for the Node-Red connection.

So, my suggestions would be the following:

  1. Create a new device in your application for Node-Red
  2. Generate a new Access Key/Secret for this device with “All Topics” access selected.
  3. Once this new device has connected, then attempt to run your workflow again.

Let me know if this works for you or if you need any clarification.

Thanks,
Heath

1 Like

Thank you Heath
I am progressing. Finally after correcting the access key to all topics", I am receiving an empty string on the nodered side. I assume this is because I have not set a message template in the MQTT node.
Last question (sorry for that), how should I set the message template in the MQTT Node to receive the output of this function ?

payload = payload.working.decoded.data;
return payload

It is a simple string, something like this "“5af3107a1d47"”

Thank you

@Felice_Massaro,

The function node you have is not required. Since the JSON: Decode node is already doing the decoding, you don’t need the function node to be able to reference the value on the payload.

So, I would suggest that you remove the function node, and in your message body for the MQTT output node, your message body is just:

{
  "message": {{working.decoded.data}}
}

What you are doing with that function node is replacing the entire payload with just the value of working.decoded.data.

Thanks,
Heath

Finally I got it working. Thanks a million.

1 Like