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測驗軟體訂閱的訊息就能接受到

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/220955.html
標籤:其他
