首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Contiki os MQTT

Contiki os MQTT
EN

Stack Overflow用户
提问于 2017-01-13 14:31:16
回答 1查看 1K关注 0票数 0

您好,我尝试将我的cooja模拟器中的mqtt微尘连接到我桌面上的蚊子服务器。但它总是连接失败,我可以ping通rpl路由器,但不能ping通我的mqtt客户端。我使用的是https://github.com/esar/contiki-mqtt中的示例。

有人能就我犯的错误给我提个建议吗?谢谢

这是我使用的代码

代码语言:javascript
复制
#include "stdio.h"
#include "contiki.h"
#include "contiki-net.h"
#include "mqtt-service.h"
PROCESS(mqtt_publisher_process, "MQTT Publisher");
PROCESS_NAME(mqtt_process);
AUTOSTART_PROCESSES(&mqtt_process, &mqtt_publisher_process);

PROCESS_THREAD(mqtt_publisher_process, ev, data)
{

    static uip_ip6addr_t server_address;

    // printf("address %d\n",server_address);
    // Allocate buffer space for the MQTT client
    static uint8_t in_buffer[64];
    static uint8_t out_buffer[64];

    // Setup an mqtt_connect_info_t structure to describe
    // how the connection should behave
    static mqtt_connect_info_t connect_info =
    {
        .client_id = "contiki",
        .username = "user",
        .password = "user",
        .will_topic = "dev/text",
        .will_message = NULL,
        .keepalive = 60,
        .will_qos = 0,
        .will_retain = 0,
        .clean_session = 1
    };

    // The list of topics that we want to subscribe to
    static const char* topics[] =
    {
      "0", "1", "2", "3", "4", "5", NULL
    };
     PROCESS_BEGIN();
        // Set the server address
    uip_ip6addr(&server_address,0,0,0,0,0,0,0,1);

    // Initialise the MQTT client
    mqtt_init(in_buffer, sizeof(in_buffer),
              out_buffer, sizeof(out_buffer));

    // Ask the client to connect to the server
    // and wait for it to complete.
    mqtt_connect(&server_address, UIP_HTONS(33065),1, &connect_info);
    // mqtt_connect(&server_address, 1883,1, &connect_info);
    PROCESS_WAIT_EVENT_UNTIL(ev == mqtt_event);
    if(mqtt_connected())
    {
        static int i;

        for(i = 0; topics[i] != NULL; ++i)
        {
            // Ask the client to subscribe to the topic
            // and wait for it to complete
            mqtt_subscribe(topics[i]);
            PROCESS_WAIT_EVENT_UNTIL(ev == mqtt_event);
        }

        // Loop waiting for events from the MQTT client
        while(1)
        {
            PROCESS_WAIT_EVENT_UNTIL(ev == mqtt_event);

            // If we've received a publish event then print
            // out the topic and message
            if(mqtt_event_is_publish(data))
            {
                const char* topic = mqtt_event_get_topic(data);
                const char* message = mqtt_event_get_data(data);
                int level = 0;

                printf("%s = %s\n", topic, message);

                // Relay the received message to a new topic
                mqtt_publish("new_topic", message, 0, 1);
            }else{

            }
        }
    } else
        printf("mqtt service connect failed\n");
     PROCESS_END();
}
EN

回答 1

Stack Overflow用户

发布于 2017-01-13 16:14:21

我怀疑MQTT服务器是否有IPv6地址::1,对吗?

而不是:

代码语言:javascript
复制
uip_ip6addr(&server_address,0,0,0,0,0,0,0,1);

你可能想把你的台式电脑的地址放在运行Mosquitto的电脑上?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41628649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档