C语言 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,这是一个代理,它正在运行:

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

现在我想做的是向这个经纪人或 Putty 发送类似"Hello World"的东西,或者显示整个 MQTT 有效负载的东西。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 %dn", 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 命令,因此此代码中的大多数问题不会转移到新的代码库。

相关内容

  • 没有找到相关文章

最新更新