赞
踩
在前几天,我大致了解了一下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已经实现了,后续我会详细的讲解一下这个过程。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。