admin管理员组文章数量:1122832
I am working with mqtt mosquitto (windows) and my situation is as follows:
I can subscribe to the mosquitto broker and listen to mqtt messages after executing a java code.
Inside the java code, I get the payload of a message using message.getPayload()
, which is a byte
array. (The java code works fine)
And here is the problem: When I publish a message via command prompt, my java code seems to receive a completely different payload from what I published.
More precisely: At first, I would like to publish a message which consists of one byte, lets say 11001111. I would like to point out: the message payload is the above one byte, not eight characters!
The text-file data2.txt contains the numbers: 11001111
When I type
mosquitto_pub -h localhost -t sample_data -f "C:\Users\....\data2.txt"
in the command prompt to publish a message, my java code receives a strange message payload:
When I display the components of the byte array message.getPayload()
, the result is
49 49 48 48 49 49 49 49
I wondered about it, but it turned out, that the above numbers are the corresponding decimals of 1 and 0 in the ASCII-table.
The questions: (1) How can I publish the above byte using the command
mosquitto_pub -h localhost -t sample_data ...
in the command prompt?
(2) If I want to publish several bytes (for example 11001111 11111111 11111111; these are three bytes, not 24 characters!), how can I do that?
At the beginning I worked with
mosquitto_pub -h localhost -t sample_data -m "11001111"
but the result is still the same.
Any advice is welcome. Thanks in advance!
I am working with mqtt mosquitto (windows) and my situation is as follows:
I can subscribe to the mosquitto broker and listen to mqtt messages after executing a java code.
Inside the java code, I get the payload of a message using message.getPayload()
, which is a byte
array. (The java code works fine)
And here is the problem: When I publish a message via command prompt, my java code seems to receive a completely different payload from what I published.
More precisely: At first, I would like to publish a message which consists of one byte, lets say 11001111. I would like to point out: the message payload is the above one byte, not eight characters!
The text-file data2.txt contains the numbers: 11001111
When I type
mosquitto_pub -h localhost -t sample_data -f "C:\Users\....\data2.txt"
in the command prompt to publish a message, my java code receives a strange message payload:
When I display the components of the byte array message.getPayload()
, the result is
49 49 48 48 49 49 49 49
I wondered about it, but it turned out, that the above numbers are the corresponding decimals of 1 and 0 in the ASCII-table.
The questions: (1) How can I publish the above byte using the command
mosquitto_pub -h localhost -t sample_data ...
in the command prompt?
(2) If I want to publish several bytes (for example 11001111 11111111 11111111; these are three bytes, not 24 characters!), how can I do that?
At the beginning I worked with
mosquitto_pub -h localhost -t sample_data -m "11001111"
but the result is still the same.
Any advice is welcome. Thanks in advance!
Share Improve this question asked Nov 23, 2024 at 4:56 user1user1 11 silver badge1 bronze badge 4 |1 Answer
Reset to default 0using the -f option mqtt_pub send the file as "binary"..
As in Does mosquitto_pub convert a binary file to ASCII?
To do what you would like to do there is probably a TextEncoding problem, probably not handled in the library with a default
For question number 2 The problem is the same.. you are sending ascii string that are number just for you, is interpreted as a text.
I suggest you to do a base64 code/encode.
本文标签: How to modify mqtt payload in mosquittoStack Overflow
版权声明:本文标题:How to modify mqtt payload in mosquitto - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299674a1930541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
mosquitto_pub
also works (see the answer) but MQTTX can be a bit easier to use because it offers a graphical interface. – Brits Commented Nov 25, 2024 at 7:25