eclipse / paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
315 stars 178 forks source link

MQTT-SN Client packet truncated at 255 bytes #141

Closed mahsan321 closed 5 years ago

mahsan321 commented 5 years ago

Hello there!

I am trying to send a packet from my mbed device to the Gateway. The client library i am using is one that is implemented by Ian Craggs. The link of the Library is https://os.mbed.com/users/icraggs/code/HelloMQTTSN/

The client is behaving perfectly but when i try to Publish a payload of size greater than 255 bytes the gateway receives it but the forwarded packet to the broker is truncated. Suppose i send a payload of 300 bytes the Gateway receives complete packet but the broker receives only first 39 bytes. (with 400 it is 139 and so on) I dont know but the equation seems to be Payload Size % 261 = payload received by broker If the payload is less than 255 bytes it is received by the broker perfectly.

I am using Mosquitto Broker and there seems to be no issues with the broker. I do have used the Gateway tester example to validate and has sent a payload of larger size and it works perfectly with Gateway. But i am not able to identify why it is behaving as such with my mbed device.

Your help is much appreciated.

Thanks.

ty4tw commented 5 years ago

Hi,

Thank you for your info.

I checked the message length related functions.

in  https://os.mbed.com/users/icraggs/code/MQTTSNPacket/file/7fa362fa563f/MQTTSNPacket.c/

/**
 * Encodes the MQTT-SN message length
 * @param buf the buffer into which the encoded data is written
 * @param length the length to be encoded
 * @return the number of bytes written to the buffer
 */
int MQTTSNPacket_encode(unsigned char* buf, int length)
{
    int rc = 0;

    FUNC_ENTRY;
    if (length > 255)
    {
        buf[rc++] = 0x01;
        MQTTSNPacket_writeInt(&buf, length);
        rc += 2;   
    }
    else
        buf[rc++] = length;

    FUNC_EXIT_RC(rc);
    return rc;
}

void MQTTSNPacket_writeInt(unsigned char** pptr, int anInt)
{
    **pptr = (unsigned char)(anInt / 256);
    (*pptr)++;
    **pptr = (unsigned char)(anInt % 256);
    (*pptr)++;
}

in paho librally 

/**
 * Encodes the MQTT-SN message length
 * @param buf the buffer into which the encoded data is written
 * @param length the length to be encoded
 * @return the number of bytes written to the buffer
 */
int MQTTSNPacket_encode(unsigned char* buf, int length)
{
    int rc = 0;

    FUNC_ENTRY;
    if (length > 255)
    {
        writeChar(&buf, 0x01);
        writeInt(&buf, length);
        rc += 3;
    }
    else
        buf[rc++] = length;

    FUNC_EXIT_RC(rc);
    return rc;
}

void writeChar(unsigned char** pptr, char c)
{
    **pptr = (unsigned char)c;
    (*pptr)++;
}

void writeInt(unsigned char** pptr, int anInt)
{
    **pptr = (unsigned char)(anInt / 256);
    (*pptr)++;
    **pptr = (unsigned char)(anInt % 256);
    (*pptr)++;
}

There's no big difference. It might be a application's problem. Could you share your main.cpp ?

mahsan321 commented 5 years ago

Hi,

Thanks for your response and looking into the problem.

I did try changing the above functions but the problem seems to persist.

`int MQTTSNPacket_encode(unsigned char* buf, int length) { int rc = 0;

FUNC_ENTRY;
**if (length > 255)   // I changed this line from 255 to 1023**
{
    buf[rc++] = 0x01;
    MQTTSNPacket_writeInt(&buf, length);
    rc += 2;   
}
else
    buf[rc++] = length;

FUNC_EXIT_RC(rc);
return rc;

}` After changing that function i was able to publish complete data but as i already mentioned it get truncated.

My main is using some other API's and header files so i am also giving you the link to my complete program: https://os.mbed.com/teams/OneM2M/code/MQTTSN_M2M_Client/

In main i have a JSON payload which i am Publishing.

`

define MQTTSNCLIENT_QOS2 1

include "mbed.h"

include "stdio.h"

include "MQTT_server_setting.h"

include "MQTTSNUDP.h"

include "MQTTSNClient.h"

include "MbedJSONValue.h"

include "Enumeration.h"

include "sensor.h"

static volatile bool isMessageArrived = false;

int arrivedcount = 0;

void messageArrived(MQTTSN::MessageData& md) { MQTTSN::Message &message = md.message; printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n, Number: %d", message.qos, message.retained, message.dup, message.id, arrivedcount); memcpy(messageBuffer, (char)message.payload, message.payloadlen); messageBuffer[message.payloadlen] = '\0'; printf("Payload %.s\n", message.payloadlen, (char*)message.payload); printf("Payload length %d\n",message.payloadlen); ++arrivedcount; process_msg(); isMessageArrived = true; }

int main() {

float version = 1.0;

MQTTSNUDP ipstack = MQTTSNUDP();

printf("\r\nX-NUCLEO-IDW01M1 mbed Application\r\n");     
printf("\r\nconnecting to AP\r\n");

if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) {      
    printf("\r\nConnected\r\n");
} else {
    printf("\r\nerror connecting to AP.\r\n");
    return -1;
}
const char* Local_IP = spwf.get_ip_address();
printf("IP Address of Device is: %s\n", Local_IP);

const char* Local_MAC = spwf.get_mac_address();
printf("MAC Address of Device is: %s\n", Local_MAC);

MQTTSN::Client<MQTTSNUDP, Countdown> client = MQTTSN::Client<MQTTSNUDP, Countdown>(ipstack);

printf("Connecting to %s:%d\n", MQTT_SERVER_HOST_NAME, PORT);
int rc = ipstack.connect(MQTT_SERVER_HOST_NAME, PORT);
if (rc != 0)
     printf("rc from UDP connect is %d\n", rc);
else
    printf("UDP connected\n"); 

MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;       
data.clientID.cstring = MQTT_CLIENT_ID;
data.duration = 60;
if ((rc = client.connect(data)) != 0)
     printf("rc from MQTT connect is %d\n", rc);
else
     printf("MQTT Connected\n");

//Subscribe to Topic TEST1

MQTTSN_topicid topicid;
topicid.type = MQTTSN_TOPIC_TYPE_NORMAL;
topicid.data.long_.name = MQTT_TOPIC_PUB;
topicid.data.long_.len = strlen(MQTT_TOPIC_PUB);
MQTTSN::QoS grantedQoS;
if ((rc = client.subscribe(topicid, MQTTSN::QOS1, grantedQoS, messageArrived)) != 0)
     printf("rc from MQTT subscribe is %d\n", rc);
else
     printf("Subscribed to Topic %s\n", MQTT_TOPIC_PUB); 

//Create AE
//Mendatory resources: Operation, To, From, Request Identifier ,Resource Type, Content 
//Resource Specific Attributes [M]: App-ID, requestReachability, supportedReleaseVersions
//                              [0]: PointofAccess, ResourceName

AE_ID = "S";                    //Initial registration
//struct Mendatory m1;
m1.To = CSE_ID; 
m1.From = AE_ID;
m1.Request_Identifier = "createAE1";
m1.Resource_Type = 2;
m1.Operation = 1;

//struct CreateAE CAE;
CAE.resourceName = MQTT_CLIENT_ID;
CAE.requestReachability = true;
CAE.App_ID = "A01.com.sensor01";
static unsigned int count = 0;
buffer = Create_AE();  //Buffer contains the Payload to be published in JSON Format

strcpy(messageBuffer,buffer);
strncpy(buf, buffer, strlen(buffer));

delete buffer;

 while(1) {
    // Check connection //
    if(!client.isConnected()){
        break;
    }
    // Received a control message. //
    if(isMessageArrived) {
        isMessageArrived = false;
        // Just print it out here.
        printf("\r\nMessage arrived:\r\n%s\r\n", messageBuffer);
    }
    // Publish data //

    {
        count++;

        MQTTSN::Message message;
        message.qos = MQTTSN::QOS2;
        message.retained = false;
        message.dup = false;
        message.payload = (void*)buf;
        message.payloadlen = strlen(buf)+1;

        if ((rc = client.publish(topicid, message)) != 0)
        {
             printf("rc from MQTT Publish is %d\n", rc);
        }
        else
             printf("Published Buffer: %s to Topic %s\n",buf, MQTT_TOPIC_PUB);

        printf("\r\n Message Number: %d\n", count);
        client.yield(4000);
        wait(4);
    }
} 

if ((rc = client.unsubscribe(topicid)) != 0)
    printf("rc from unsubscribe was %d\n", rc);

if ((rc = client.disconnect()) != 0)
    printf("rc from disconnect was %d\n", rc);

ipstack.disconnect();

printf("Version %.2f: finish %d msgs\n", version, arrivedcount);
printf("Finishing with %d messages received\n", arrivedcount);

return 0;

} `

mahsan321 commented 5 years ago

These are the responses. Gateway Log

20190114 070132.092 PUBLISH 0015 <--- Sensor01 0C 20 00 01 00 15 7B 22 66 72 22 3A 22 53 22 2C 22 6F 70 22 3A 31 2C 22 70 63 22 3A 7B 22 6D 32 6D 3A 61 65 22 3A 7B 22 61 70 69 22 3A 22 41 30 31 2E 63 6F 6D 2E 73 65 6E 73 6F 72 30 31 22 2C 22 72 6E 22 3A 22 53 65 6E 73 6F 72 30 31 22 2C 22 72 72 22 3A 74 72 75 65 7D 7D 2C 22 72 71 69 22 3A 22 63 72 65 61 74 65 41 45 31 22 2C 22 74 6F 22 3A 22 63 73 65 31 22 2C 22 74 79 22 3A 32 2C 22 74 65 73 74 22 3A 22 54 45 53 54 22 2C 22 74 65 73 74 5F 31 22 3A 22 54 45 53 54 5F 31 22 2C 22 74 65 73 74 5F 32 22 3A 22 54 45 53 54 5F 32 22 2C 22 74 65 73 74 5F 33 22 3A 22 54 45 53 54 5F 33 22 2C 22 74 65 73 74 5F 34 22 3A 22 54 45 53 54 5F 34 22 2C 22 74 65 73 74 5F 35 22 3A 22 54 45 53 54 5F 35 22 2C 22 74 65 73 74 5F 36 22 3A 22 54 45 53 54 5F 36 22 2C 22 74 65 73 74 5F 37 22 3A 22 54 45 53 54 5F 37 22 2C 22 74 65 73 74 5F 38 22 3A 22 54 45 53 54 5F 38 22 2C 22 74 65 73 74 5F 39 22 3A 22 54 45 53 54 5F 39 22 2C 22 74 65 73 74 5F 31 30 22 3A 22 54 45 53 54 5F 31 30 22 2C 22 74 65 73 74 5F 31 31 22 3A 22 54 45 53 54 5F 31 31 22 2C 22 74 65 73 74 5F 31 32 22 3A 22 54 45 53 54 5F 31 32 22 7D 00

20190114 070132.093 PUBLISH 0015 ===> Sensor01 32 73 00 05 54 45 53 54 31 00 15 7B 22 66 72 22 3A 22 53 22 2C 22 6F 70 22 3A 31 2C 22 70 63 22 3A 7B 22 6D 32 6D 3A 61 65 22 3A 7B 22 61 70 69 22 3A 22 41 30 31 2E 63 6F 6D 2E 73 65 6E 73 6F 72 30 31 22 2C 22 72 6E 22 3A 22 53 65 6E 73 6F 72 30 31 22 2C 22 72 72 22 3A 74 72 75 65 7D 7D 2C 22 72 71 69 22 3A 22 63 72 65 61 74 65 41 45 31 22 2C 22 74

Broker Log

1547449265: Received PUBLISH from Sensor01 (d0, q1, r0, m18, 'TEST1', ... (106 bytes)) 1547449265: Sending PUBACK to Sensor01 (Mid: 18) 1547449265: Sending PUBLISH to Sensor01 (d0, q1, r0, m17, 'TEST1', ... (106 bytes)) 1547449266: Received PUBACK from Sensor01 (Mid: 17)

mbed client Log

payloadlen 366 sendPacket, rc 367 from write of 367 bytes Published Buffer: {"fr":"S","op":1,"pc":{"m2m:ae":{"api":"A01.com.sensor01","rn":"Sensor01","rr":true}},"rqi":"createAE1","to":"cse1","ty":2,"test":"TEST","test_1":"TEST_1","test_2":"TEST_2","test_3":"TEST_3","test_4":"TEST_4","test_5":"TEST_5","test_6":"TEST_6","test_7":"TEST_7","test_8":"TEST_8","test_9":"TEST_9","test_10":"TEST_10","test_11":"TEST_11","test_12":"TEST_12"} to Topic TEST1

deliverMessage topic id is 1 messageHandler 0 topic id is 1 Message arrived: qos 1, retained 0, dup 0, packetid 19 , Number: 18 Payload {"fr":"S","op":1,"pc":{"m2m:ae":{"api":"A01.com.sensor01","rn":"Sensor01","rr":true}},"rqi":"createAE1","t Payload length 106 sending puback len 7

ty4tw commented 5 years ago

Sensor01 32 73 00 05 54 45 53 54 31

73 is incorrect. 73 means RemaingLength is 115 bytes. I will check it.

ty4tw commented 5 years ago

Could you add some code to MQTTSNGWPacket.cpp. Then try it and send me a log ? I want check the message length which you send to the gateway.


422 char* MQTTSNPacket::print(char* pbuf)
{
    char* ptr = pbuf;
    char** pptr = &pbuf;
    int value = 0;

    int i = MQTTSNPacket_decode(_buf, _bufLen, &value);
    int size = _bufLen > SIZE_OF_LOG_PACKET ? SIZE_OF_LOG_PACKET : _bufLen;

431 for ( i = 0; i < size; i++)
    {
        sprintf(*pptr, " %02X", *(_buf + i));
        *pptr += 3;
    }
    **pptr = 0;
    return ptr;
}

line 431
for ( ; i < size; i++) ----> for ( i = 0; i < size; i++)

My test log is


20190114 185133.740 REGISTER 0001 <--- GatewayTestClient 12 0A 00 00 00 01 74 79 34 74 77 2F 74 6F 70 69 63 31 20190114 185133.740 REGACK 0001 ---> GatewayTestClient 07 0B 00 01 00 01 00

20190114 185133.740 PUBLISH <--- GatewayTestClient 1F 0C 00 00 01 00 00 70 75 62 6C 69 73 68 20 22 74 79 34 74 77 2F 54 6F 70 69 63 31 22 20 0A 20190114 185133.741 PUBLISH ===> GatewayTestClient 30 26 00 0C 74 79 34 74 77 2F 74 6F 70 69 63 31 70 75 62 6C 69 73 68 20 22 74 79 34 74 77 2F 54 6F 70 69 63 31 22 20 0A

20190114 185136.098 REGISTER 0003 <--- GatewayTestClient 12 0A 00 00 00 03 74 79 34 74 77 2F 74 6F 70 69 63 32 20190114 185136.098 REGACK 0003 ---> GatewayTestClient 07 0B 00 02 00 03 00

20190114 185136.099 PUBLISH 0002 <--- GatewayTestClient 01 01 54 0C 20 00 02 00 02 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 0A

20190114 185136.099 PUBLISH 0002 ===> GatewayTestClient 32 DB 02 00 0C 74 79 34 74 77 2F 74 6F 70 69 63 32 00 02 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 20 30 31 32 33 34 35 36 37 38 39 0A

20190114 185136.672 PUBACK 0002 <=== GatewayTestClient 40 02 00 02 20190114 185136.673 PUBACK 0002 ---> GatewayTestClient 07 0D 00 02 00 02 00


Nothing wrong.

MQTT-SN Message length is 01 54 = 340
MQTT RemainingLength is DB 02 = DB + 80 = 347

mahsan321 commented 5 years ago

I have used the Gateway Tester Application it is working perfectly. The problem is while publishing from mbed client.

I have changed the MQTTSNGWPacket.cpp as requested.

The log is as follows.

20190114 105137.323 CONNECT <--- Sensor01 0E 04 04 01 00 3C 53 65 6E 73 6F 72 30 31 20190114 105137.326 CONNECT ===> Sensor01 10 14 00 04 4D 51 54 54 04 02 00 3C 00 08 53 65 6E 73 6F 72 30 31 20190114 105137.569 CONNACK <=== Sensor01 20 02 00 00 20190114 105137.570 CONNACK ---> Sensor01 03 05 00

20190114 105137.617 SUBSCRIBE 0001 <--- Sensor01 0A 12 20 00 01 54 45 53 54 31 20190114 105137.618 SUBSCRIBE 0001 ===> Sensor01 82 0A 00 01 00 05 54 45 53 54 31 01 20190114 105137.664 SUBACK 0001 <=== Sensor01 90 03 00 01 01 20190114 105137.665 SUBACK 0001 ---> Sensor01 08 13 20 00 01 00 01 00

20190114 105137.768 PUBLISH 0002 <--- Sensor01 71 0C 20 00 01 00 02 7B 22 66 72 22 3A 22 53 22 2C 22 6F 70 22 3A 31 2C 22 70 63 22 3A 7B 22 6D 32 6D 3A 61 65 22 3A 7B 22 61 70 69 22 3A 22 41 30 31 2E 63 6F 6D 2E 73 65 6E 73 6F 72 30 31 22 2C 22 72 6E 22 3A 22 53 65 6E 73 6F 72 30 31 22 2C 22 72 72 22 3A 74 72 75 65 7D 7D 2C 22 72 71 69 22 3A 22 63 72 65 61 74 65 41 45 31 22 2C 22 74 6F 22 3A 22 63 73 65 31 22 2C 22 74 79 22 3A 32 2C 22 74 65 73 74 22 3A 22 54 45 53 54 22 2C 22 74 65 73 74 5F 31 22 3A 22 54 45 53 54 5F 31 22 2C 22 74 65 73 74 5F 32 22 3A 22 54 45 53 54 5F 32 22 2C 22 74 65 73 74 5F 33 22 3A 22 54 45 53 54 5F 33 22 2C 22 74 65 73 74 5F 34 22 3A 22 54 45 53 54 5F 34 22 2C 22 74 65 73 74 5F 35 22 3A 22 54 45 53 54 5F 35 22 2C 22 74 65 73 74 5F 36 22 3A 22 54 45 53 54 5F 36 22 2C 22 74 65 73 74 5F 37 22 3A 22 54 45 53 54 5F 37 22 2C 22 74 65 73 74 5F 38 22 3A 22 54 45 53 54 5F 38 22 2C 22 74 65 73 74 5F 39 22 3A 22 54 45 53 54 5F 39 22 2C 22 74 65 73 74 5F 31 30 22 3A 22 54 45 53 54 5F 31 30 22 2C 22 74 65 73 74 5F 31 31 22 3A 22 54 45 53 54 5F 31 31 22 2C 22 74 65 73 74 5F 31 32 22 3A 22 54 45 53 54 5F 31 32 22 7D 00 20190114 105137.769 PUBLISH 0002 ===> Sensor01 32 73 00 05 54 45 53 54 31 00 02 7B 22 66 72 22 3A 22 53 22 2C 22 6F 70 22 3A 31 2C 22 70 63 22 3A 7B 22 6D 32 6D 3A 61 65 22 3A 7B 22 61 70 69 22 3A 22 41 30 31 2E 63 6F 6D 2E 73 65 6E 73 6F 72 30 31 22 2C 22 72 6E 22 3A 22 53 65 6E 73 6F 72 30 31 22 2C 22 72 72 22 3A 74 72 75 65 7D 7D 2C 22 72 71 69 22 3A 22 63 72 65 61 74 65 41 45 31 22 2C 22 74 20190114 105137.825 PUBACK 0002 <=== Sensor01 40 02 00 02 20190114 105137.825 PUBACK 0002 ---> Sensor01 07 0D 00 01 00 02 00

mahsan321 commented 5 years ago

This is the Log with Payload Size of 246 and packet length of 253. With this length the published and received messages are sent and received correctly.

GW LOG

20190114 115359.110 PUBLISH 0004 <--- Sensor01 FD 0C 40 00 02 00 04 7B 22 66 72 22 3A 22 53 22 2C 22 6F 70 22 3A 31 2C 22 70 63 22 3A 7B 22 6D 32 6D 3A 61 65 22 3A 7B 22 61 70 69 22 3A 22 41 30 31 2E 63 6F 6D 2E 73 65 6E 73 6F 72 30 31 22 2C 22 72 6E 22 3A 22 53 65 6E 73 6F 72 30 31 22 2C 22 72 72 22 3A 74 72 75 65 7D 7D 2C 22 72 71 69 22 3A 22 63 72 65 61 74 65 41 45 31 22 2C 22 74 6F 22 3A 22 63 73 65 31 22 2C 22 74 79 22 3A 32 2C 22 74 65 73 74 22 3A 22 54 45 53 54 22 2C 22 74 65 73 74 5F 31 22 3A 22 54 45 53 54 5F 31 22 2C 22 74 65 73 74 5F 32 22 3A 22 54 45 53 54 5F 32 22 2C 22 74 65 73 74 5F 33 22 3A 22 54 45 53 54 5F 33 22 2C 22 74 65 73 74 5F 34 22 3A 22 54 45 53 54 5F 34 22 2C 22 74 65 73 74 5F 35 22 3A 22 54 45 53 54 5F 35 22 2C 22 74 65 73 74 5F 36 22 3A 22 54 45 53 54 5F 36 22 7D 00 20190114 115359.110 PUBLISH 0004 ===> Sensor01 34 99 02 00 1F 2F 6F 6E 65 4D 32 4D 2F 72 65 67 5F 72 65 71 2F 53 65 6E 73 6F 72 30 31 2F 43 53 45 5F 30 31 00 04 7B 22 66 72 22 3A 22 53 22 2C 22 6F 70 22 3A 31 2C 22 70 63 22 3A 7B 22 6D 32 6D 3A 61 65 22 3A 7B 22 61 70 69 22 3A 22 41 30 31 2E 63 6F 6D 2E 73 65 6E 73 6F 72 30 31 22 2C 22 72 6E 22 3A 22 53 65 6E 73 6F 72 30 31 22 2C 22 72 72 22 3A 74 72 75 65 7D 7D 2C 22 72 71 69 22 3A 22 63 72 65 61 74 65 41 45 31 22 2C 22 74 6F 22 3A 22 63 73 65 31 22 2C 22 74 79 22 3A 32 2C 22 74 65 73 74 22 3A 22 54 45 53 54 22 2C 22 74 65 73 74 5F 31 22 3A 22 54 45 53 54 5F 31 22 2C 22 74 65 73 74 5F 32 22 3A 22 54 45 53 54 5F 32 22 2C 22 74 65 73 74 5F 33 22 3A 22 54 45 53 54 5F 33 22 2C 22 74 65 73 74 5F 34 22 3A 22 54 45 53 54 5F 34 22 2C 22 74 65 73 74 5F 35 22 3A 22 54 45 53 54 5F 35 22 2C 22 74 65 73 74 5F 36 22 3A 22 54 45 53 54 5F 36 22 7D 00

ty4tw commented 5 years ago

I'd like to know the message length whose payload size exceeds 256 bytes in order to check that your client program is sending the correct message

20190114 105137.768 PUBLISH 0002 <--- Sensor01 71 0C 20190114 105137.769 PUBLISH 0002 ===> Sensor01 32 73

20190114 115359.110 PUBLISH 0004 <--- Sensor01 FD 0C 20190114 115359.110 PUBLISH 0004 ===> Sensor01 34 99

Those are too short.

mahsan321 commented 5 years ago

Ok i found the problem. It was with this function in my mbed library.

int MQTTSNPacket_encode(unsigned char* buf, int length)
{
    int rc = 0;

    FUNC_ENTRY;
    if (length > 255)
    {
        //buf[rc] = 0x01;
        **MQTTSNPacket_writeChar(&buf, 0x01);**
        MQTTSNPacket_writeInt(&buf, length);
        rc += 3;
    }
    else
        buf[rc++] = length;

    FUNC_EXIT_RC(rc);
    return rc;
}

buf[rc] = 0x01; was not writing the first byte of packet to 0x01. I changed it to MQTTSNPacket_writeChar(&buf, 0x01); Thank you so much for your help and kind support. Looking forward to discuss further issues while we progress in our project.