概述:工作之余学习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
(3)mosquitto的安装
//解压
$ tar zxvf mosquitto-1.4.9.tar.gz
$ cd mosquitto-1.4.9
$ make
$ sudo make install
(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
3,mosquitto使用
进入命令行界面
(1)启动mosquitto服务器
$ mosquitto -v
(2)订阅消息(如topic)
$ mosquitto_sub -v -t topic
(3)发布消息
$ mosquitto_pub -t topic -m good
效果如下图所示:
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
客户端(发布消息)
#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;
}
注意
在编译程序时需加上-lmosquitto链接。如:
$ gcc -o mosquitto_client_sub mosquitto_client_sub.c -lmosquitto
5,客户端使用
(1)开启mosquitto服务
$ mosquitto -v
(2)启动订阅消息客户端及发布消息客户端
$ ./mosquitto_client_sub
$ ./mosquitto_client_pub
(3)效果
参考资料:
如果想详细了解mqtt中libmosquitto的使用可以看一下这篇文章。
转载自:https://blog.csdn.net/Dancer__Sky/article/details/77855249