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
  • You might find it easier to test with MQTTX (has a GUI and enables you to enter/view payloads in hex) – Brits Commented Nov 24, 2024 at 18:43
  • Thank you for suggesting this. I decided to work with mosquitto because it can be easily downloaded without providing any e-mail address or other data. Moreover, mosquitto seems to be very easy to handle. – user1 Commented Nov 25, 2024 at 6:33
  • I'm not suggesting you change server (I use Mosquitto myself). Whilst MQTTX is developed by EMQ, the authors of EMQX, it is an Apache licensed open source tool that can be used with any MQTT server (including Mosquitto) and you don't need to provide any info to download it . 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
  • I see. I will take a look at it. Thank you for your suggestion. – user1 Commented Nov 25, 2024 at 9:37
Add a comment  | 

1 Answer 1

Reset to default 0

using 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