设备通过mqtt协议使用mqtt.embedded-c库连接IBM iot quickstart

发布于 2022-09-02 19:58:28 字数 5499 浏览 32 评论 0

去paho下载了一个mqtt.embedded.c的库文件,然后稍作移植,主要就是修改了对socket的实现,使它可以在我的设备中运行。
设备连接bluemix的iot只要有两种方式,一种是连接它的quickstart,另外一种需要注册设备,由于第二种条件比较多,我就先实现第一种,从他的transport_open()函数可以看出他是利用域名解析来得到相应地址,由于我的设备中并没有相应的域名解析函数,所以我在命令行输入ping uickstart.messaging.internetofthings.ibmcloud.com
然后得到ip:184.172.124.189
图片描述

然后我就直接connect了这个地址,但是并没有连接上,我现在不知道到底是程序出了问题还是不能已这种方式去连接connect.
以下是transport_open的代码:


    int transport_open(char* addr, int port)
    {
    int* sock = &mysock;
        int type = SOCK_STREAM;
        struct sockaddr_in address;
    #if defined(AF_INET6)
        struct sockaddr_in6 address6;
    #endif
        int rc = -1;
    #if defined(WIN32)
        short family;
    #else
        sa_family_t family = AF_INET;
    #endif
        struct addrinfo *result = NULL;
        struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL};
        static struct timeval tv;
    
        *sock = -1;
        if (addr[0] == '[')
          ++addr;
    
        if ((rc = getaddrinfo(addr, NULL, &hints, &result)) == 0)
        {
            struct addrinfo* res = result;
    
            /* prefer ip4 addresses */
            while (res)
            {
                if (res->ai_family == AF_INET)
                {
                    result = res;
                    break;
                }
                res = res->ai_next;
            }
    
    #if defined(AF_INET6)
            if (result->ai_family == AF_INET6)
            {
                address6.sin6_port = htons(port);
                address6.sin6_family = family = AF_INET6;
                address6.sin6_addr = ((struct sockaddr_in6*)(result->ai_addr))->sin6_addr;
            }
            else
    #endif
            if (result->ai_family == AF_INET)
            {
                address.sin_port = htons(port);
                address.sin_family = family = AF_INET;
                address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr;
            }
            else
                rc = -1;
    
            freeaddrinfo(result);
        }
    
        if (rc == 0)
        {
            *sock =    socket(family, type, 0);
            if (*sock != -1)
            {
    #if defined(NOSIGPIPE)
                int opt = 1;
    
                if (setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0)
                    Log(TRACE_MIN, -1, "Could not set SO_NOSIGPIPE for socket %d", *sock);
    #endif
    
                if (family == AF_INET)
                    rc = connect(*sock, (struct sockaddr*)&address, sizeof(address));
        #if defined(AF_INET6)
                else
                    rc = connect(*sock, (struct sockaddr*)&address6, sizeof(address6));
        #endif
            }
        }
        if (mysock == INVALID_SOCKET)
            return rc;
    
        tv.tv_sec = 1;  /* 1 second Timeout */
        tv.tv_usec = 0;  
        setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
        return mysock;
    }

修改后的transport_open

int transport_open(void)
{  struct socket_config conf;
   memcpy(conf.eth0.mac_address, "\x00\x0b\xb9\xf8\x21\x0b", 6);
            conf.eth0.ip_address = inet_addr("192.168.10.173");
            conf.eth0.netmask = inet_addr("255.255.255.0");
            conf.eth0.gateway = inet_addr("192.168.10.1");
            conf.sec_dns = inet_addr("192.168.10.1");
            conf.dns_timeout = 10;
    socket_init(&conf);//以上是我设备的一些信息    
    int mysock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    struct sockaddr_in sockaddr;
    sockaddr.sin_family=AF_INET;
    sockaddr.sin_port=1883;
    sockaddr.sin_addr.s_addr=inet_addr("184.172.124.189");
    connect(mysock,(struct sockaddr*)(&sockaddr),sizeof(struct sockaddr));
    return mysock;
}



qos0pub主要代码如下:
 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    int rc = 0;
    unsigned char buf[200];
    int buflen = sizeof(buf);
    int mysock = 0;
    MQTTString topicString = MQTTString_initializer;
    char* payload ="{\"d\" : {\"temp\" : 34 }}";
    int payloadlen = strlen(payload);
    int len = 0;
    char *host = "y71cjd.messaging.internetofthing.ibmcloud.com";
    int port = 1883;
    mysock = transport_open();
    if(mysock < 0)
        printf("error mysock is %d\n",mysock);
   else
    {printf("Sending to hostname %s port %d\n", host, port);}
    data.clientID.cstring = "d:y71cjd:FC3180:velox";
    data.keepAliveInterval = 20;
    data.cleansession = 1;
    data.username.cstring = "use-token-auth";
    data.password.cstring = "xdbluemix";
    data.MQTTVersion = 4;
    len = MQTTSerialize_connect(buf, buflen, &data);        
    topicString.cstring = "iot-2/evt/temp/fmt/json";
    len += MQTTSerialize_publish((unsigned char *)(buf + len), buflen - len, 0, 0, 0, 0, topicString, (unsigned char *)payload, payloadlen);
      printf("len:%d\n",len);
    len += MQTTSerialize_disconnect((unsigned char *)(buf + len), buflen - len);
      printf("len:%d\n",len);
    rc=transport_sendPacketBuffer(mysock,buf,len);
          
    if (rc == len)
        printf("Successfully published\n");
    else
        printf("Publish failed\n");
    exit:
        transport_close(mysock);

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2022-09-09 19:58:28

已经解决了,通过网络抓包发现是头部字节传反了,需要在MQTTPacket.h里加上一行#define REVERSED就能连上了。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文