publish binary data over MQTT

7,230 views
Skip to first unread message

Pavan Kumar

unread,
Jun 6, 2014, 8:44:08 AM6/6/14
to mq...@googlegroups.com
Hi,

I am using mosquitto broker & client in my application and as a part of it I need to transfer a binary file from publisher to subscriber. For that I have tried to open the file in binary mode, read the data and published the same.

#define LENGTH 512
char sdbuf[LENGTH];
char pubCommand[800];
fs = fopen("test", "rb");
int fs_block_sz;
                    while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs)) > 0)
                    {
                        memset(pubCommand,'\0',sizeof(pubCommand));
                        strcpy(pubCommand,"/home/administrator/mosquitto-1.0.2/client/mosquitto_pub -d -t fromserver -m ");
                        strcat(pubCommand,"\"");

                        for(i=0;i<fs_block_sz;i++)
                        {
                            sprintf(tempData,"%c",sdbuf[i]);
                            strcat(pubCommand,tempData);
                        }

                        strcat(pubCommand,"\"");
                        system(pubCommand);
                     }

Subsriber is waiting for messages on same topic (i.e. fromserver)

But data is not received properly. The file size is 8000 bytes but only few bytes are received at other end. One thing i have observed is the moment quote (") is present in the data it was taken as end of message and bytes went missing

Some body who have worked on binary data publishing or binary file transferring from publisher to subscriber must have faced this kind of issue

Please help me

Paul Fremantle

unread,
Jun 6, 2014, 9:08:55 AM6/6/14
to mq...@googlegroups.com
I think the problem maybe that you are using the command-line tools rather than a C library. There are several libraries you can use including:

Paul




--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at http://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.



--
Paul Fremantle
Part-time PhD student - School of Computing
twitter: pzfreo / skype: paulfremantle / blog: http://pzf.fremantle.org
CTO and Co-Founder, WSO2
OASIS WS-RX TC Co-chair, Apache Member
07740 199 729

Stefano Costa

unread,
Jun 6, 2014, 9:10:27 AM6/6/14
to mq...@googlegroups.com

You should go with mosquitto as a library linked, its functions called from your C code instead of building a shell command.
--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at http://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.


-- 
  Stefano Costa, Managing Director R&D

  M +39 335 6565749
  Skype stefanocosta.bluewind
  Twitter @stefanobluewind
  http://www.bluewind.it

Roger Light

unread,
Jun 6, 2014, 10:30:31 AM6/6/14
to mq...@googlegroups.com
Hi,

In addition to what the others have said, did you try this?

mosquitto_pub -t fromserver -f filename

Cheers,

Roger

Matteo Collina

unread,
Jun 7, 2014, 1:31:36 AM6/7/14
to mq...@googlegroups.com
Hi,

You can try to use MQTT.js, we have full support for binary data.

Cheers,

Matteo

Pavan Kumar

unread,
Jun 9, 2014, 6:32:56 AM6/9/14
to mq...@googlegroups.com, ro...@atchoo.org
Hi Roger,

I have tried what you have suggested (with -f filename). File is getting transferred from publisher end to subscriber end. But I am not able to run the received file at subscriber end. When I tried to run the transferred file below is the error:

administrator@ubuntu:~/Desktop$ ./test
bash: ./test: cannot execute binary file

The original file size & transferred file size are same. Below is the ouput after running "file" command on the received file

administrator@ubuntu:~/Desktop$ file test
app: ELF 64-bit LSB no machine, invalid version

Below is the ouput after running "file" command on the original file
administrator@ubuntu:~/Desktop$ file work/hello
work/hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xa497e829e5b50f567cfdd109c22d2d11ac106431, not stripped

I could n't figure out what went wrong.

Pavan Kumar

unread,
Jun 9, 2014, 6:35:07 AM6/9/14
to mq...@googlegroups.com
Hi Matteo,

I am working on for solution to this problem in C language and don't know much about java/java scripts. It will help me if you suggest some thing on C language end

Regards,
Pavan Kumar

Stefano Costa

unread,
Jun 9, 2014, 8:25:16 AM6/9/14
to mq...@googlegroups.com
Why not binary comparing the two files? Some transformation has occurred.

Stefano Costa

unread,
Jun 9, 2014, 8:39:45 AM6/9/14
to mq...@googlegroups.com
On 06/09/2014 12:32 PM, Pavan Kumar wrote:
> I have tried what you have suggested (with -f filename). File is
> getting transferred from publisher end to subscriber end. But I am not
> able to run the received file at subscriber end. When I tried to run
> the transferred file below is the error:

...was curious and tried with the two commands (two consoles):

stefanoco@stefanocopc:~/Documents/code/orig$ mosquitto_pub -h
somehost.somewhere.com -t "/dev/bin" -f main

stefanoco@stefanocopc:~/Documents/code/dest$ mosquitto_sub -N -h
somehost.somewhere.com -t "/dev/bin">main

and it works: file is correct, and it runs. Without "-N" works again
even if a newline is appended at the end. Are you using latest build of
mosquitto?

Roger Light

unread,
Jun 9, 2014, 8:59:26 AM6/9/14
to Pavan Kumar, mq...@googlegroups.com
This really ought to be on the mosquitto mailing list by the way.

I would try with a simple payload to see where the problem lies,
something like "abcdefghijklmnopqrstuvwxyz". That would make it easy
to see if bytes were being transposed or otherwise transformed.

Cheers,

Roger

Pavan Kumar

unread,
Jun 9, 2014, 9:04:11 AM6/9/14
to mq...@googlegroups.com, stefan...@bluewind.it

Hi,

I was using mosquitto-1.0.2.tar.gz. After seeing your response, I have updated to the latest one 1.3.1 & now it is working fine.

Thanks a lot Stefano & Roger for your support

Regards,
Pavan Kumar
Reply all
Reply to author
Forward
0 new messages