MQTT String Json Format
Hi Community,
I am using the MQTT client function block of the IIoT Library from PLCnext Store and I want to publish topics to ASW cloud.
By using the example, I was able to sent the messages. The Json string was generated by the IIOT_JSON_Coder block and then pusblished by the IIOT_MqttClient_Publish block.
Is it possbile to generate the message string directly and pass it to the block? I tired it out and on the AWS side its not possible to read the message correctly. The JSON format is not send correctly.
The message string looks like:
{ "ts": 1710320564503, "dc_supply": { "avg_voltage": 0, "total_current": 0, "units_count": 8, "apfc_unit_0": { "voltage": 0, "current": 0 }, "apfc_unit_1": { "voltage": 0, "current": 0 }, "apfc_unit_2": { "voltage": 0, "current": 0 }, "targets": { "voltage": 500, "current": 300 } } }
Thanks
Shane
Comments
Hi Shane, you can ask the developers of that library for their advice (using the link on the app page in the PLCnext Store), but I suspect what you're seeing is a result of this feature of that function block:
Hi Martin,
thanks for your quick response.
Yes I saw this in the documentation. But for me it was not clear, how to use the STRING_TO_BUF function.
Here is the help for that function block:
What did you try, and what was the result?
Hi implemented like this, with a simple message: STRING#'{"ts":17}'
And this is, what AWS sees:
What data type is mqtt_message1?
What are the "live" contents of mqtt_message1, as seen in the Watch window?
Also:
Be aware that *_TO_BUF is not guaranteed to complete instantly, so you should check the DONE output on the FB before using the buffer, to be sure that the conversion has been completed.
The data typ is String and in live content I see also the same value as mqtt_message
OK, that explains the error.
A STRING variable has four header bytes, as mentioned in the documentation for the IIOT library.
If you change the type of mqtt_message1 to an array of bytes, then the four header bytes will not appear in the buffer, and AWS should accept the data.
Now I made the data typ to Array of Byte and the Header are gone.
But still AWS doesnt now recognize the String as JSON format
What's the value of the udiLength parameter on the Publish function block?
Is AWS expecting a null-terminator at the end of the string?
If so, is there a null terminator in the buffer, after the
}
character?Yes, that was the error the lengtth was wrong.
Thanks a lot Martin!!!
Great to hear that it's working.
Let us know if there are any other questions.