MQTT on embedded system

113 views
Skip to first unread message

yell...@gmail.com

unread,
Mar 6, 2017, 9:44:56 PM3/6/17
to MQTT
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 192.168.20.100 for PC and 192.168.20.1 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 = "192.168.20.100";
    int port = 1883;

    mysock = transport_open(host,port);
    if(mysock < 0){
        perror("error");
        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");
    else
        printf("Publish failed\n");

exit:
    transport_close(mysock);
}

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

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

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

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

unread,
Mar 7, 2017, 12:07:04 AM3/7/17
to mq...@googlegroups.com
Your broker side log for your C code shows IP address as 192.168.20.1; whereas, this is the IP address in the code - 192.168.20.100. Typo?

On Tue, Mar 7, 2017 at 8:22 AM, <yell...@gmail.com> 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 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+unsubscribe@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at https://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

yell...@gmail.com

unread,
Mar 7, 2017, 12:52:55 AM3/7/17
to MQTT
The IP of broker is 192.168.20.100. And client is running on 192.168.20.1

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 mqtt+uns...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages