发送 MQTT 包

Send MQTT package

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

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,它是一个 Broker,它是 运行:

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

现在我想做的是向此 Broker 或 Putty 或可以显示整个 MQTT 负载的东西发送 "Hello World" 之类的东西。 C如何处理这个?该文档帮助我了解了正在发生的事情,但并没有真正帮助我编写 C 代码,因为我对它还是很陌生。

这花了一段时间,但我想通了。还有很多问题需要解决,但至少它会向本地主机发送一个 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 命令,因此这段代码中的大部分问题都不会转移到新的代码库中。