发送MQTT包

时间:2018-03-05 18:54:05

标签: c mqtt iot broker

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

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侦听器

现在我想做的是发送类似&#34; Hello World&#34;到此Broker或Putty或显示整个MQTT有效负载的东西。 C如何处理这个?文档帮助我理解了发生了什么但是没有真正帮助我编写C代码,因为我还是很新的。

1 个答案:

答案 0 :(得分:0)

这花了一段时间但我明白了。仍有很多问题需要解决,但至少它会将一个MQTT(Wireshark批准的)数据包发送到localhost。

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

    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. 设置套接字:
    2. 这段代码对我有用,但它的LINUX SPECIFIC!

      #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. 连接到套接字
      2. 使用此:

        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. 发送套接字!
        2. 这应该有效:

          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命令,因此此代码中的大多数问题都无法转移到新的代码库。