如何初始化LwIP以使用MQTT库

如何初始化LwIP以使用MQTT库,mqtt,atmel,lwip,Mqtt,Atmel,Lwip,我已经查看并遵循了LWIP2.0.2中的文档。我的项目包含LWIP2.0.2中提供的MQTT库。我还找到了一些帮助。我无法获得连接,我使用了免费的公共MQTT代理,但连接没有希望。我可以ping我的设备。我做错什么了吗 static void mqtt_test(mqtt_client_t *mqtt_client) if (mqtt_client_is_connected(&mqtt_client) == 1) { example_subscribe(&

我已经查看并遵循了LWIP2.0.2中的文档。我的项目包含LWIP2.0.2中提供的MQTT库。我还找到了一些帮助。我无法获得连接,我使用了免费的公共MQTT代理,但连接没有希望。我可以ping我的设备。我做错什么了吗

static void mqtt_test(mqtt_client_t *mqtt_client)
    if (mqtt_client_is_connected(&mqtt_client) == 1)
    {
        example_subscribe(&mqtt_client, NULL);
    } else {
        mqtt_do_connect(&mqtt_client);
    }
}
当我调用此方法时,它总是输入mqtt_do_connect(),从不连接。这里是mqtt_do_connect

static void mqtt_do_connect(mqtt_client_t *mqtt_client)
{
    ip4_addr_t broker_ipaddr;
    struct mqtt_connect_client_info_t ci;
    err_t err;
    if (ipaddr_aton("52.58.177.181", &broker_ipaddr))
    {
        err = ERR_OK;
    }
    /* Setup an empty client info structure */
    memset( & ci, 0, sizeof(ci));
    /* Minimal amount of information required is client identifier, so set it here */
    ci.client_id = "test";
    /* Initiate client and connect to server, if this fails immediately an error code is returned
      otherwise mqtt_connection_cb will be called with connection result after attempting
      to establish a connection with the server.
      For now MQTT version 3.1.1 is always used */
    err = mqtt_client_connect(mqtt_client, & broker_ipaddr, 1883, mqtt_connection_cb, 0, & ci);
    /* For now just print the result code if something goes wrong */
    if (err != ERR_OK) {
        printf("mqtt_connect return %d\n", err);
    }
}
我还注意到,在mqtt.c中的方法mqtt_client_connect中存在以下情况:

/* Any local address, pick random local port number */
    err = tcp_bind(client->conn, IP_ADDR_ANY, 0); 
为什么需要这样做?如果我将IP_ADDR_ANY替换为它所运行的设备的静态IP的本地地址,而不抛出错误,那么就不会调用回调mqtt_connection_cb

我还使用静态IP初始化了TCP/IP堆栈。我正在使用NO_SYS as 1,但会将其移到FreeRTOS,但会先使用baby steps

在LwIP实现中,我没有发现对MQTT的太多支持,如果我遗漏了一些明显的东西,任何帮助都将不胜感激


我使用MQTTfx在代理上运行了一两个测试,响应良好,但我的嵌入式设备(Atmel SAME54)没有任何响应。

我找到了解决方案。我在FreeRTOS线程中运行了TCP设置,并打开了一个套接字

static void mqtt_start(void *p)
{
    struct sockaddr_in address;
    int                s_create, new_socket;
    int                addrlen = sizeof(address);
    int                opt     = 1;
    int                socket_check;

    sys_sem_t sem;
    err_t     err_sem;
    err_sem = sys_sem_new(&sem, 0); /* Create a new semaphore. */
    tcpip_init(tcpip_init_done, &sem);
    sys_sem_wait(&sem); /* Block until the lwIP stack is initialized. */
    sys_sem_free(&sem); /* Free the semaphore. */

    /*Create a socket*/
    s_create = socket(AF_INET, 1, 0);

    setsockopt(s_create, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));

    address.sin_family      = AF_INET;
    address.sin_addr.s_addr = htonl(IPADDR_ANY);
    address.sin_port        = htons(HTTP_PORT);
    /* bind the connection to port */
    socket_check = bind(s_create, (struct sockaddr *)&address, sizeof(address));

    if (socket_check < 0) {
        LWIP_DEBUGF(LWIP_DBG_ON, ("Bind error=%d\n", socket_check));
        goto socket_close;
    }
    /* tell the connection to listen for incoming connection requests */
    listen(s_create, 3);

    mqtt_connect(&mqtt_client);

    for (;;) {
        new_socket = accept(s_create, (struct sockaddr *)&address, (socklen_t *)&addrlen);
        socket_close:
            close(new_socket);
    }
}
然后稍后在mqtt_start中使用它

mqtt_client_t mqtt_client;