概述:工作之余学习mqtt,使用开源的Mosquitto实现mqtt的使用。如:broker(服务器/代理),消息订阅及发布。
系统环境:ubuntu14.04
1,MQTT简介
MQTT 是一个轻量级协议,使用基于 TCP/IP 协议的发布/订阅消息转发模式,在物联网应用中大规模使用。 MQTT 协议的中心是 broker( 服务器/代理) ,客户端通过订阅消息和发布消息进行数据交互 ,如下图所示:
2,Mosquitto安装
(1)mosquitto下载
下载地址,进入下载页面,选择source中第一个下载mosquitto-1.4.9.tar.gz
(2)mosquitto依赖库安装
$ sudo apt-get install libssl-dev
$ sudo apt-get install libc-ares-dev
$ sudo apt-get install uuid-dev
1
2
3
(3)mosquitto的安装
//解压
$ tar zxvf mosquitto-1.4.9.tar.gz
$ cd mosquitto-1.4.9
$ make
$ sudo make install
1
2
3
4
5
(4)编译问题解决
如果出现error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
//创建链接
$ sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
//更新链接
$ sudo ldconfig
1
2
3
4
3,mosquitto使用
进入命令行界面
(1)启动mosquitto服务器
$ mosquitto -v
1
(2)订阅消息(如topic)
$ mosquitto_sub -v -t topic
1
(3)发布消息
$ mosquitto_pub -t topic -m good
1
效果如下图所示:
4,libmosquitto客户端编程(发布消息,订阅消息)
在安装mosquitto成功后,可以在/usr/local/lib文件夹中有libmosquitto.so.1等文件,如下图所示:
这里贴一个demo
客户端(订阅消息)
#include
#include
#include
#include
#define HOST "localhost"
#define PORT 1883
#define KEEP_ALIVE 60
bool session = true;
void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
if(message->payloadlen){
printf("%s %s", message->topic, message->payload);
}else{
printf("%s (null)\n", message->topic);
}
fflush(stdout);
}
void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
{
int i;
if(!result){
/* Subscribe to broker information topics on successful connect. */
mosquitto_subscribe(mosq, NULL, "Gai爷:", 2);
}else{
fprintf(stderr, "Connect failed\n");
}
}
void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
int i;
printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
for(i=1; i
printf(", %d", granted_qos[i]);
}
printf("\n");
}
void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
/* Pring all log messages regardless of level. */
printf("%s\n", str);
}
int main()
{
struct mosquitto *mosq = NULL;
//libmosquitto 库初始化
mosquitto_lib_init();
//创建mosquitto客户端
mosq = mosquitto_new(NULL,session,NULL);
if(!mosq){
printf("create client failed..\n");
mosquitto_lib_cleanup();
return 1;
}
//设置回调函数,需要时可使用
//mosquitto_log_callback_set(mosq, my_log_callback);
mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_message_callback_set(mosq, my_message_callback);
//mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
//客户端连接服务器
if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){
fprintf(stderr, "Unable to connect.\n");
return 1;
}
//循环处理网络消息
mosquitto_loop_forever(mosq, -1, 1);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
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
75
76
77
78
79
客户端(发布消息)
#include
#include
#include
#include
#define HOST "localhost"
#define PORT 1883
#define KEEP_ALIVE 60
#define MSG_MAX_SIZE 512
bool session = true;
int main()
{
char buff[MSG_MAX_SIZE];
struct mosquitto *mosq = NULL;
//libmosquitto 库初始化
mosquitto_lib_init();
//创建mosquitto客户端
mosq = mosquitto_new(NULL,session,NULL);
if(!mosq){
printf("create client failed..\n");
mosquitto_lib_cleanup();
return 1;
}
//连接服务器
if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){
fprintf(stderr, "Unable to connect.\n");
return 1;
}
//开启一个线程,在线程里不停的调用 mosquitto_loop() 来处理网络信息
int loop = mosquitto_loop_start(mosq);
if(loop != MOSQ_ERR_SUCCESS)
{
printf("mosquitto loop error\n");
return 1;
}
while(fgets(buff, MSG_MAX_SIZE, stdin) != NULL)
{
/*发布消息*/
mosquitto_publish(mosq,NULL,"Gai爷:",strlen(buff)+1,buff,0,0);
memset(buff,0,sizeof(buff));
}
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 0;
}
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
注意
在编译程序时需加上-lmosquitto链接。如:
$ gcc -o mosquitto_client_sub mosquitto_client_sub.c -lmosquitto
1
5,客户端使用
(1)开启mosquitto服务
$ mosquitto -v
1
(2)启动订阅消息客户端及发布消息客户端
$ ./mosquitto_client_sub
$ ./mosquitto_client_pub
阅读(4100) | 评论(0) | 转发(0) |