【问题标题】:Send MQTT package发送 MQTT 包
【发布时间】:2018-08-13 12:38:12
【问题描述】:

我目前正在使用这个作为学习示例:

https://os.mbed.com/teams/mqtt/code/MQTTPacket/file/aedcaf7984d5/samples/simple-publish.txt/

但是,某些代码特定于示例使用的任何嵌入式系统。

到目前为止我得到的是:

#include "hw_util.h"
#include "MQTTPacket.h"
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
int main (void) {


    int i;
    float temperatura;
    unsigned char buffer[50];





    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    int rc = 0;
    char buf[200];
    int buflen = sizeof(buf);
    MQTTString topicString = MQTTString_initializer;
    char* payload = "I'm Alive";
    int payloadlen = strlen(payload);
    int len = 0;
    data.clientID.cstring = "Testing";
    data.keepAliveInterval = 20;
    data.cleansession=1;
    data.MQTTVersion=3;
    len = MQTTSerialize_connect(buf,buflen,&data);
    topicString.cstring="SampleTopic";
    len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen);

    printf("Hello world");
    rc = 0;
    while(1)
    {

        ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);







    }
}

我下载了 HiveMQ,它是一个代理并且它正在运行:

2018-03-05 19:28:08,195 INFO - 在地址 0.0.0.0 和端口 1883 上启动 TCP 侦听器

现在我想做的是向此代理或 Putty 发送类似“Hello World”的内容,或者发送显示整个 MQTT 有效负载的内容。 C如何处理这个?该文档帮助我了解发生了什么,但并没有真正帮助我编写 C 代码,因为我对它还是很陌生。

【问题讨论】:

    标签: c mqtt iot broker


    【解决方案1】:

    这需要一段时间,但我想通了。还有很多问题需要解决,但至少它会向 localhost 发送一个 MQTT(Wireshark 批准)数据包。

    1. 使用此库创建 MQTT 数据包:https://os.mbed.com/teams/mqtt/code/MQTTPacket/

    使用此代码:

    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    char buf[200];
    
    MQTTString topicString = MQTTString_initializer;
    data.clientID.cstring = "TESTIRAM";
    data.keepAliveInterval = 20;
    data.cleansession=1;
    data.MQTTVersion=3;
    len = MQTTSerialize_connect(buf,buflen,&data);
    topicString.cstring="ka";
    len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, 
    topicString, payload, payloadlen);
    
    1. 设置套接字:

    这段代码对我有用,但它是特定于 LINUX 的!

    #include "hw_util.h"
    #include "MQTTPacket.h"
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    char buf[200];
    int mysock=0;
    char *host = "127.0.0.1";
    int port = 1883;
    mysock = socket(AF_INET, SOCK_STREAM, 0);
    

    AF_INET 指定 IPv4,SOCK_STREAM 指定 TCP。

    1. 连接到套接字

    使用这个:

    struct sockaddr_in cliaddr;
    
    int rc = 0;
        //initialize the host address
    
        memset(&cliaddr, 0, sizeof(cliaddr));
        //specify IPv4 protocol
        /*following includes are necessary for this:#include <sys/types.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        #include <arpa/inet.h>*/
        cliaddr.sin_family = AF_INET;
        //intialize the IP address of the host as "127.0.0.1"
        cliaddr.sin_addr.s_addr = inet_addr(host);
        //initialize the port, 1883
        cliaddr.sin_port = htons(port);
        //client has no bind
        //socket()>connect()->send()<->recv()->close
        int statusConn = connect(mysock,(const struct sockaddr*)&cliaddr,sizeof(cliaddr));
        if(statusConn=0){
            printf("Success!");
        }
        if(statusConn=-1){
            printf("Connect unsuccessful!\n");
    
    1. 通过套接字发送!

    这应该可行:

    while(1)
    {
            printf("Sending to hostname %s port %d\n", host, port);
            //infinite loop, sending packets to the specified ASOCKET
            //after sending sleep for 10 seconds
            //sleep uses unistd.h
    
            printf("Sent packets: ");
    
            int countSend = send(mysock ,buf,buflen,0);
    
            printf("%d",countSend);
    
            printf("\n");
            sleep(10);
    }
    

    有些导入可能没有用,有些可能是特定于 Linux 的。我正在转向 AT 命令,因此此代码中的大多数问题不会转移到新的代码库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2016-11-28
      • 2018-07-16
      相关资源
      最近更新 更多