Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3029576
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-06-08 13:35:27

在前几天,我大致了解了一下Paho C项目,并对其的一些内容进行了翻译。俗话说,光说不练假把戏,今天就给大家讲一下使用Paho的客户端库文件实现MQTT C Client的过程。

安装

??本文是在Linux下安装的,推荐直接进行克隆并安装即可。

git clone .com/eclipse/paho.mqtt.c.git cd paho.mqtt.c make
sudo make install
  • 1
  • 2
  • 3
  • 4

??在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"; //添加的密码
  • 1
  • 2

??并将用户名和密码写入连接选项中:

 conn_opts.username = username; //将用户名写入连接选项中 conn_opts.password = password; //将密码写入连接选项中
  • 1
  • 2

??添加的代码具体位置详细查看代码,更改后的代码如下所示:

/*******************************************************************************
 * 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 
 *   
 * and the Eclipse Distribution License is available at 
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Ian Craggs - initial contribution
 *******************************************************************************/ #include  #include  #include  #include "MQTTClient.h" #if !defined(WIN32) #include  #else #include  #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;
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

??更改完成之后回到paho.mqtt.c目录,执行make,输出一下结果:
输出结果

??打开paho.mqtt.c/build/output/samples目录,刚刚我们修改的MQTTClient_publish .c文件生成了MQTTClient_publish ,执行以下命令:

 ./MQTTClient_publish
  • 1

??输出以下结果:

make输出结果

??为了确认发送的信息已经到达客户端,我打开了mqtt.fx客户端,并连接到MQTT服务器,订阅了topic01的话题,此时可以看到发送过来的信息:
mqtt.fx收到的信息
??至此,Paho - MQTT C 发送Cient已经实现了,后续我会详细的讲解一下这个过程。

阅读(6234) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~