Hello,

I am trying to connect the Mosquitto broker running on my local system (linux).
The client is my mqtt program which uses lwip and is also running on the local 
system.

The problem is that the client is getting created, connection is also 
established and the function mqtt_client _connect returns ERR_OK as the status.
The callback is also hit, but here the connection status returned is not MQTT_ 
CONNECT_ ACCEPTED.

So, why is the status failing ? Is there any other different status that is 
returned on a successful connection ?

I want to also confirm is it is possible in the first place, to connect from 
the lwip client program running on local system to the mqtt broker which is 
also running on the same system?

If not, I want to know if there's any updates to be made for my code to work.

Broker runs on port 1883
lwip uses the wifi interface
I have not used the loopback IP, instead I am using the dynamic IP(wifi) and 
specifying that itself as broker IP.

Attaching the code for your reference along with the logs.

Code ********************************************************************


#include <stdio.h>
#include <string.h>
#include "lwip/apps/mqtt.h"
#include "lwip/err.h"
#include "lwip/ip_addr.h"

// Define the MQTT broker address and port
#define BROKER_IP "192.168.0.100" // Replace with your MQTT broker IP
#define BROKER_PORT 1883

// Define MQTT client ID
#define CLIENT_ID "my_client_id" // Replace with your MQTT client ID

// MQTT connection callback function
void mqtt_connection_cb(mqtt_client_t *client, void *arg, 
mqtt_connection_status_t status) {

    if (status == MQTT_CONNECT_ACCEPTED) {
        printf("Connected to MQTT broker successfully.\n");
    } else {
        printf("Failed to connect to MQTT broker: %d\n", status);
    }
}

// Main function
int mqtt_create_connection() {

    printf("Into MQTT create connection\n");
    mqtt_client_t *client;
    ip_addr_t broker_addr;
    err_t err;

    // Create a new MQTT client
    client = mqtt_client_new();
    if (client == NULL) {
        printf("Failed to create MQTT client.\n");
        return 1;
    }
    else{
      printf("MQTT client created successfully.\n");
    }

    // Convert the broker IP address string to an ip_addr_t structure
    ipaddr_aton(BROKER_IP, &broker_addr);

    // Define the MQTT client information structure
    struct mqtt_connect_client_info_t client_info = {
        .client_id = CLIENT_ID,
        .client_user = NULL,         // No username
        .client_pass = NULL,         // No password
        .keep_alive = 60,            // Keep alive time in seconds
        .will_topic = NULL,          // No last will topic
        .will_msg = NULL,            // No last will message
        .will_qos = 0,               // QoS level 0 for the last will
        .will_retain = 0             // Do not retain the last will message
    };

    // Connect to the MQTT broker
    err = mqtt_client_connect(client, &broker_addr, BROKER_PORT, 
mqtt_connection_cb, NULL, &client_info);
    if (err != ERR_OK) {
        printf("Failed to start connection to MQTT broker: %d\n", err);
        return 1;
    }
    else if (err == ERR_OK){
      printf("Established Connection to MQTT broker\n");
    }

    // Loop to keep the program running and maintain the connection
    while (1) {
        // The loop could handle other tasks or just keep the client running
    }

    // Free the MQTT client when done
    mqtt_client_free(client);
    return 0;
}


logs *********************************************************************

[cid:defa8f78-45c5-4b82-b3ef-5263e232ddfc]


Regards,
Chandana


_______________________________________________
lwip-users mailing list
lwip-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/lwip-users

Reply via email to