1、简介
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,目前广泛应用于物联网概念中。该协议支持所有平台,几乎可以把所有联网物品和外部连接起来,被用来当做传感器和制动器。
2、支持mqtt的开发包目前非常多,但是常用的依旧是mosquitto。mosquitto下载地址:http://mosquitto.org/download/
mosquitto支持跨平台开发,强烈建议:不要在windows下源码编译,会出现很多依赖问题。建议直接安装,然后将头文件,库文件拿出来开发即可。
linux下推荐源码编译,因为毕竟很有可能是需要再编译到arm架构上。
操作步骤:
在官网现在最新的source,目前最新为mosquitto-1.5.tar.gz
tar -jxvf mosquitto-1.5.tar.gz -C ~/Document
cd mosquitto-1.5
#安装需要的依赖项
sudo apt install cmake g++ libssl-dev libc-ares-dev uuid-dev xsltproc docbook-xsl
sudo make
sudo make install
3、使用
启动mqtt服务:
ycd@ycd-machine:~/Documents/mosquitto-1.5$ mosquitto
1533814446: mosquitto version 1.5 starting
1533814446: Using default config.
1533814446: Opening ipv4 listen socket on port 1883.
1533814446: Opening ipv6 listen socket on port 1883.
启动成功,接下来启动侦听通道程序
ycd@ycd-machine:~/Documents/mosquitto-1.5$ mosquitto_sub -t test
mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
出现这个错误,我们知道,这是因为动态库没有被找到,需要手动添加
所以,作如下操作:
cp /usr/local/lib/libmosquitto.so.1 /usr/lib
然后在新的终端上启动侦听:
#侦听通道test
ycd@ycd-machine:~/Documents/mosquitto-1.5$ mosquitto_sub -t test
再新开一个终端,向test通道发送消息:
ycd@ycd-machine:~/Documents/mosquitto-1.5$ mosquitto_pub -t test -m 111
这时候,sub端会收到111的消息。
大功告成?
当然没有,这不是程序员做的事情,
4、基于mosquitto的侦听端与发布端开发
其实也没有什么需要介绍的打开如下文档:
/home/ycd/Documents/mosquitto-1.5/test/msgsps_pub.c
示例程序如下,其中包含发布侦听功能
/* This provides a crude manner of testing the performance of a broker in messages/s. */
#include
#include
#include
#include
#include
// mosquitto头文件,可以进去看看里面的函数方法
#include
#include
static bool run = true;
static int message_count = 0;
static struct timeval start, stop;
// 连接回调函数,当连接成功时会进入这里,可以在这里进行连接成功之后的操作,比如连接之后的信息同步
void my_connect_callback(struct mosquitto *mosq, void *obj, int rc)
{
printf("rc: %d\n", rc);
gettimeofday(&start, NULL);
}
// 断开连接回调函数,在断开连接之后进入
void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result)
{
run = false;
}
// 发布消息回调函数
void my_publish_callback(struct mosquitto *mosq, void *obj, int mid)
{
message_count++;
if(message_count == MESSAGE_COUNT){
gettimeofday(&stop, NULL);
mosquitto_disconnect((struct mosquitto *)obj);
}
}
// 订阅消息回调
void my_subscribe_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
{
int i;
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
for (i = 1; i < qos_count; i++){
printf(", %d", granted_qos[i]);
}
printf("\n");
}
int main(int argc, char *argv[])
{
struct mosquitto *mosq;
int i;
double dstart, dstop, diff;
uint8_t *buf;
buf = malloc(MESSAGE_SIZE*MESSAGE_COUNT);
if(!buf){
printf("Error: Out of memory.\n");
return 1;
}
start.tv_sec = 0;
start.tv_usec = 0;
stop.tv_sec = 0;
stop.tv_usec = 0;
mosquitto_lib_init();
// 定义一个客户端名为perftest的发布端。客户端表示发布端的唯一性
mosq = mosquitto_new("perftest", true, NULL);
mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_disconnect_callback_set(mosq, my_disconnect_callback);
// 发布回调
mosquitto_publish_callback_set(mosq, my_publish_callback);
//订阅回调
mosquitto_message_callback_set(m_hMqtt, my_subscribe_callback);
// 连接服务端
mosquitto_connect(mosq, HOST, PORT, 600);
mosquitto_loop_start(mosq);
i=0;
for(i=0; i"perf/test", MESSAGE_SIZE, &buf[i*MESSAGE_SIZE], PUB_QOS, false);
}
mosquitto_loop_stop(mosq, false);
dstart = (double)start.tv_sec*1.0e6 + (double)start.tv_usec;
dstop = (double)stop.tv_sec*1.0e6 + (double)stop.tv_usec;
diff = (dstop-dstart)/1.0e6;
printf("Start: %g\nStop: %g\nDiff: %g\nMessages/s: %g\n", dstart, dstop, diff, (double)MESSAGE_COUNT/diff);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
主要函数就是这一些,多测试几次就熟练了,而且在windows下也可以测试调试。