MQTT on embedded system

Skip to first unread message

Mar 6, 2017, 9:44:56 PM3/6/17
I have been struggling with MQTT on embedded device for few days.
Briefly, I am able to connect to MQTT broker but when I try to send a packet, it will be disconnected.

I includes all the files in MQTTPackettransport.c, and transport.h.

MQTT broker is running on my PC and the device is connected via LAN cable.
IP addresses are for PC and for the device.

I verified that MQTT broker is running correctly by using MQTTLens and mosquitto_pub.exe.

I copied code from qos0pub.c and run it.

Since I am not using username and password, I just change that part and ip address only from the copied code like below.

    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    int rc = 0;
    char buf[200];
    int buflen = sizeof(buf);
    int mysock = 0;
    MQTTString topicString = MQTTString_initializer;
    char* payload = "mypayload";
    int payloadlen = strlen(payload);
    int len = 0;
    char *host = "";
    int port = 1883;

    mysock = transport_open(host,port);
    if(mysock < 0){
        return mysock;

    printf("Sending to hostname %s port %d\n", host, port);

    data.clientID.cstring = "me";
    data.keepAliveInterval = 20;
    data.cleansession = 1;
    // data.username.cstring = "";
    // data.password.cstring = "";
    data.MQTTVersion = 4;

    len = MQTTSerialize_connect((unsigned char *)buf, buflen, &data);

    topicString.cstring = "mytopic";
    len += MQTTSerialize_publish((unsigned char *)(buf + len), buflen - len, 0000, topicString, (unsigned char *)payload, payloadlen);

    len += MQTTSerialize_disconnect((unsigned char *)(buf + len), buflen - len);

    rc = transport_sendPacketBuffer(mysock, buf, len);
    if (rc == len)
        printf("Successfully published\n");
        printf("Publish failed\n");


Once I run the client I got a log like below on broker side:
1488851598: New connection from on port 1883.
1488851598: Socket error on client <unknown>, disconnecting.

In normal case, the log should look like:
1488852381: New connection from on port 1883.
1488852381: New client connected from as lens_ilqbXsAoQVl7iwP042TpHrAkKOC (c1, k120).

Does anyone know this case?
Thank you.
Message has been deleted

ನಾಗೇಶ್ ಸುಬ್ರಹ್ಮಣ್ಯ (Nagesh S)

Mar 7, 2017, 12:07:04 AM3/7/17
Your broker side log for your C code shows IP address as; whereas, this is the IP address in the code - Typo?

On Tue, Mar 7, 2017 at 8:22 AM, <> wrote:
I just want to let you know that with MQTTlens, I was able to publish a message without username and password.
It seems like I am not able to set cstring value of clientid correctly?

To learn more about MQTT please visit
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
To post to this group, send email to
Visit this group at
For more options, visit

Message has been deleted

Mar 7, 2017, 12:52:55 AM3/7/17
The IP of broker is And client is running on

The code snippet is client publication.

I got the second log while I am running both broker and client on the same machine.

2017년 3월 7일 화요일 오후 2시 7분 4초 UTC+9, cogitoergosum 님의 말:
To unsubscribe from this group and stop receiving emails from it, send an email to
Reply all
Reply to author
0 new messages