paho实现MQTTClient发布消息

paho实现MQTTClient发布消息

接下来会用paho开源的一个项目,实现mqtt客户端发布消息,此文主要参考MQTT Client library for C,Paho给出的创建一个客户端有如下类似的步骤:

1、安装
//从github上下载项目
git clone https://github.com/eclipse/paho.mqtt.c.git
//进入文件夹
cd paho.mqtt.c
//编译
make
//安装
sudo make install
2、目录介绍

在这里插入图片描述

src:源文件、库文件目录
build:编译过后的动态库及执行文件
2.1、src目录

src中有许多源文件,有发布、订阅、同步、异步文件。
在这里插入图片描述

2.2、build目录

build文件是编译过后的执行文件,也是同步、异步、发布、订阅消息等功能
在这里插入图片描述

3、实现过程
  1.创建一个客户端对象;
  2.设置连接MQTT服务器的选项;
  3.如果多线程(异步模式)操作被使用则设置回调函数(详见 Asynchronous >vs synchronous client applications);
  4.订阅客户端需要接收的任意话题;
  5.重复以下操作直到结束:
    a.发布客户端需要的任意信息;
    b.处理所有接收到的信息;
  6.断开客户端连接;
  7.释放客户端使用的所有内存。
4、文件操作

我们直接使用paho自带文件,首先修改src中MQTTClient_publish.c源代码。

4.1、修改信息
//mqtt服务器地址
#define ADDRESS “tcp://m2m.eclipse.org:1883”
//客户端号
#define CLIENTID “ExampleClientPub”
//主题
#define TOPIC “MQTT Examples”
//消息
#define PAYLOAD “Hello World!”
4.2、添加信息

需要添加服务器中用户名和密码

 char *username= "test"; //添加的用户名
 char *password = "test"; //添加的密码

将其写入客户端选项

 conn_opts.username = username; //将用户名写入连接选项中
 conn_opts.password = password; //将密码写入连接选项中
5、源码

源码我做了详尽的解释,其中有许多函数需要自己去上面文件自己查看,下面这是同步发布,不是异步,异步的代码也有,大家可以去看上面的网站

/*******************************************************************************
 * Copyright (c) 2012, 2020 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * and Eclipse Distribution License v1.0 which accompany this distribution. 
 *
 * The Eclipse Public License is available at 
 *   https://www.eclipse.org/legal/epl-2.0/
 * and the Eclipse Distribution License is available at 
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Ian Craggs - initial contribution
 *******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"

#define ADDRESS     "59.110.42.24:1883"
#define CLIENTID    "0bd981c5-a055-4196-8b7f-efb9f7a4d6ac"
#define TOPIC       "test"
#define PAYLOAD     "Hello World!"
#define QOS         1
#define TIMEOUT     10000L

int main(int argc, char* argv[])
{
    //声明mqtt客户端
    MQTTClient client;
    //初始化客户端选项 conn_opts
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    //消息初始化 pubmsg
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc;
    char *username = "test";
    char *password = "test";
    if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
    {
         printf("Failed to create client, return code %d\n", rc);
         exit(EXIT_FAILURE);
    }
    //保持心跳20
    conn_opts.keepAliveInterval = 20;
    //清理会话
    conn_opts.cleansession = 1;
    //客户端的用户名和密码
    conn_opts.username = username;
    conn_opts.password = password;
    //创建连接
    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    //消息负载(内容)
    pubmsg.payload = PAYLOAD;
    //消息长度
    pubmsg.payloadlen = (int)strlen(PAYLOAD);
    //消息质量分为0:不重要的消息比如温度,可以多次上传丢失一次没事。1:可能会丢失1次 2:永远不会丢失
    pubmsg.qos = QOS;
    //有true和false 判断消息是否保留
    pubmsg.retained = 0;
    //发布消息 token是消息发布后,传递令牌将返回客户端检查令牌是否已成功传递到其目的地
    if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
    {
         printf("Failed to publish message, return code %d\n", rc);
         exit(EXIT_FAILURE);
    }

    printf("Waiting for up to %d seconds for publication of %s\n"
            "on topic %s for client with ClientID: %s\n",
            (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
    //阻塞函数,等待消息发布
    rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
    printf("Message with delivery token %d delivered\n", token);

    if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
    	printf("Failed to disconnect, return code %d\n", rc);
    //断开连接
    MQTTClient_destroy(&client);
    return rc;
}
6、实现效果

服务器中发布
在这里插入图片描述
mqtt测试软件订阅的消息就能接受到
在这里插入图片描述

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值