在前幾天,我大致了解了一下Paho C項目,並對其的一些內容進行了翻譯。俗話說,光說不練假把戲,今天就給大家講一下使用Paho的客戶端庫文件實現MQTT C Client的過程。
安裝
本文是在Linux下安裝的,推薦直接進行克隆並安裝即可。
git clone https://github.com/eclipse/paho.mqtt.c.git
cd paho.mqtt.c
make
sudo make install
在make完之后,在paho.mqtt.c/build/output下可以找到如下的輸出文件:
而make install則是將生成的庫文件移動到系統路徑之下。在MQTT Client library for C 這個翻譯的文章中,Paho給出的創建一個客戶端有如下類似的步驟:
1.創建一個客戶端對象;
2.設置連接MQTT服務器的選項;
3.如果多線程(異步模式)操作被使用則設置回調函數(詳見 Asynchronous >vs synchronous client applications);
4.訂閱客戶端需要接收的任意話題;
5.重復以下操作直到結束:
a.發布客戶端需要的任意信息;
b.處理所有接收到的信息;
6.斷開客戶端連接;
7.釋放客戶端使用的所有內存。
為了簡單起見,我們使用Paho自帶的示例程序。打開paho.mqtt.c/src/samples下的MQTTClient_publish .c文件。將以下的代碼更改:
#define ADDRESS "tcp://m2m.eclipse.org:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
如果你的MQTT服務器不允許匿名訪問,則還需要添加姓名和密碼:
char *username= "test_user"; //添加的用戶名
char *password = "aaa777"; //添加的密碼
並將用戶名和密碼寫入連接選項中:
conn_opts.username = username; //將用戶名寫入連接選項中
conn_opts.password = password; //將密碼寫入連接選項中
添加的代碼具體位置詳細查看代碼,更改后的代碼如下所示:
/*******************************************************************************
* Copyright (c) 2012, 2017 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* 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"
#if !defined(WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#define ADDRESS "tcp://localhost:1883" //更改此處地址
#define CLIENTID "aaabbbccc" //更改此處客戶端ID
#define TOPIC "topic01" //更改發送的話題
#define PAYLOAD "Hello Man, Can you see me ?!" //更改信息內容
#define QOS 1
#define TIMEOUT 10000L
int main(int argc, char* argv[])
{
//聲明一個MQTTClient
MQTTClient client;
char *username= "test_user"; //添加的用戶名
char *password = "aaa777"; //添加的密碼
//初始化MQTT Client選項
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
//#define MQTTClient_message_initializer { {'M', 'Q', 'T', 'M'}, 0, 0, NULL, 0, 0, 0, 0 }
MQTTClient_message pubmsg = MQTTClient_message_initializer;
//聲明消息token
MQTTClient_deliveryToken token;
int rc;
//使用參數創建一個client,並將其賦值給之前聲明的client
MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.username = username; //將用戶名寫入連接選項中
conn_opts.password = password;//將密碼寫入連接選項中
//使用MQTTClient_connect將client連接到服務器,使用指定的連接選項。成功則返回MQTTCLIENT_SUCCESS
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 = strlen(PAYLOAD);
pubmsg.qos = QOS;
pubmsg.retained = 0;
MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
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);
MQTTClient_disconnect(client, 10000);
MQTTClient_destroy(&client);
return rc;
}
更改完成之后回到paho.mqtt.c目錄,執行make,輸出一下結果:
打開paho.mqtt.c/build/output/samples目錄,剛剛我們修改的MQTTClient_publish .c文件生成了MQTTClient_publish ,執行以下命令:
./MQTTClient_publish
輸出以下結果:
為了確認發送的信息已經到達客戶端,我打開了mqtt.fx客戶端,並連接到MQTT服務器,訂閱了topic01的話題,此時可以看到發送過來的信息:
至此,Paho - MQTT C 發送Cient已經實現了,后續我會詳細的講解一下這個過程。