來自個人CSDN博客php
在前幾天,我大體瞭解了一下Paho C項目,並對其的一些內容進行了翻譯。俗話說,光說不練假把戲,今天就給你們講一下使用Paho的客戶端庫文件實現MQTT C Client的過程。html
本文是在Linux下安裝的,推薦直接進行克隆並安裝便可。git
git clone https://github.com/eclipse/paho.mqtt.c.git cd paho.mqtt.c make sudo make install
在make完以後,在paho.mqtt.c/build/output下能夠找到以下的輸出文件:github
而make install則是將生成的庫文件移動到系統路徑之下。在MQTT Client library for C這個翻譯的文章中,Paho給出的建立一個客戶端有以下相似的步驟:windows
1.建立一個客戶端對象;
2.設置鏈接MQTT服務器的選項;
3.若是多線程(異步模式)操做被使用則設置回調函數(詳見 Asynchronous >vs synchronous client applications);
4.訂閱客戶端須要接收的任意話題;
5.重複如下操做直到結束:
a.發佈客戶端須要的任意信息;
b.處理全部接收到的信息;
6.斷開客戶端鏈接;
7.釋放客戶端使用的全部內存。服務器
爲了簡單起見,咱們使用Paho自帶的示例程序。打開paho.mqtt.c/src/samples下的MQTTClient_publish .c文件。將如下的代碼更改:session
#define ADDRESS "tcp://m2m.eclipse.org:1883"
#define CLIENTID "ExampleClientPub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"多線程
若是你的MQTT服務器不容許匿名訪問,則還須要添加姓名和密碼:app
char *username= "test_user"; //添加的用戶名 char *password = "aaa777"; //添加的密碼
並將用戶名和密碼寫入鏈接選項中:eclipse
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已經實現了,後續我會詳細的講解一下這個過程。