Ubuntu搭建Mqtt服务器

快速上手mosquitto在ubuntu18.04下的安装及测试_小刀_的博客-CSDN博客

0.基本认识

每个机器可以订阅,发布。谁订阅了某个topic,消息就会发给谁。

比如机器0订阅了msg1, 于是机器1/机器2发布msg1,机器0都会收到。

1. 安装服务器

  • sudo apt-get install mosquitto
  • sudo vi /etc/mosquitto/conf.d/myconfig.conf

config文件如下

#添加监听端口(很重要,否则只能本机访问)
listener 1883
#-------------------------------------------
# 关闭匿名访问,客户端必须使用用户名
allow_anonymous false

#指定 用户名-密码 文件
password_file /etc/mosquitto/pwfile.txt
#--------------------------------------------
  • sudo mosquitto_passwd -c /etc/mosquitto/pwfile.txt lbw123
  • 密码123输入两次
  • sudo service mosquitto start   //启动mqtt服务
  • sudo service mosquitto status  //查看mqtt状态

2.安装客户端

  • sudo apt-get install mosquitto-clients  
  • 订阅:mosquitto_sub -h localhost -t "lai001/#" -u 用户名 -P 密码 -i “client1”
  • 发布:mosquitto_pub -h localhost -t "lai001/testTopic" -u 用户名 -P 密码 -m "Hello MQTT from mosquitto-clients"

 在这里插入图片描述

2. Mqtt的client端测试

  • 下载mqtt-c :mqtt-c源码
  • 安装 cmocka-1.1.5:https://cmocka.org/     【测试框架cmocka】                                          然后cmake  和  make 和 ldconfig
  • 编译mqtt-c源码 : make install   

版权声明:本文为m0_37844072原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。