4 years, 11 months ago.

MQTT Hello world on NUCLEO_F429ZI (and F746ZG) freezes after a couple of minutes or so


Did anybody know why a simple usage of MQTT paho embedded C client for mbedos5 like the following hangs after 90 seconds? I referred to

Import programHelloMQTT

Sample MQTT program - simple send and receive

for usage of such lib.

Actually, using a periodic publisher publishing messages at 1 msg per second rate as well as at 1 msg every 3 seconds (periodic publisher has been implemented through a node-red flow), I always have my firmware stop receving published messages after 90 seconds (many trials, same result).

This is my code (excerpt):


void messageArrived(MQTT::MessageData& md)
    MQTT::Message &message = md.message;
    logMessage("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
    logMessage("Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
    logMessage("Received messages count: %d\r\n", ++arrivedcount);


int main(int argc, char* argv[])
    float version = 0.6;
    char* topic = "mbed-sample";

    logMessage("HelloMQTT: version is %.2f\r\n", version);

    NetworkInterface* network = easy_connect(true);
    if (!network) {
        return -1;

    MQTTNetwork mqttNetwork(network);

    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);

    const char* hostname = ""; // "m2m.eclipse.org";
    int port = 1883;
    logMessage("Connecting to %s:%d\r\n", hostname, port);
    int rc = mqttNetwork.connect(hostname, port);
    if (rc != 0)
        logMessage("rc from TCP connect is %d\r\n", rc);

    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    data.clientID.cstring = "mbed-sample";
    data.username.cstring = "sk"; //"testuser";
    data.password.cstring = "sk-1"; //"testpassword";

    if ((rc = client.connect(data)) != 0)
        logMessage("rc from MQTT connect is %d\r\n", rc);

    if ((rc = client.subscribe(topic, MQTT::QOS0, messageArrived)) != 0)
        logMessage("rc from MQTT subscribe is %d\r\n", rc);

    MQTT::Message message;

    // QoS 0
    /*char buf[100];
    sprintf(buf, "Hello World!  QoS 0 message from app version %f\r\n", version);
    message.qos = MQTT::QOS0;
    message.retained = false;
    message.dup = false;
    message.payload = (void*)buf;
    message.payloadlen = strlen(buf)+1;
    rc = client.publish(topic, message);
    while (arrivedcount < 1)

    // QoS 1
    /*sprintf(buf, "Hello World!  QoS 1 message from app version %f\r\n", version);
    message.qos = MQTT::QOS1;
    message.payloadlen = strlen(buf)+1;
    rc = client.publish(topic, message);
    while (arrivedcount < 2)

    // QoS 2
    /*sprintf(buf, "Hello World!  QoS 2 message from app version %f\r\n", version);
    message.qos = MQTT::QOS2;
    message.payloadlen = strlen(buf)+1;
    rc = client.publish(topic, message);
    while (arrivedcount < 3)

    logMessage("waiting for blue button...\r\n");

    while(!blueButton.read()) client.yield();


    if ((rc = client.unsubscribe(topic)) != 0)
        logMessage("rc from unsubscribe was %d\r\n", rc);

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


    logMessage("Version %.2f: finish %d msgs\r\n", version, arrivedcount);

    return 0;

Does the issue also occur when using HelloMQTT version 49c9daf2b0ff, and MQTT version e335fcc1a663 ? We were tracing a bug down last week with using free'd memory with the latest release, and maybe this is related?

posted by Jan Jongboom 11 Sep 2017

1 Answer

4 years, 9 months ago.

Hello. The MQTT library has now been updated to work better with mbed OS 5, so hopefully should work better for you.