MQTT服务器搭建及测试
操作系统:Debian 8 / raspi
在线安装:
$ sudo apt-get install mosquitto
源码安装:
$ git clone https://github.com/eclipse/mosquitto.git
$ cd mosquitto
$ make && make install
修改config.mk
WITH_DOC=no
需要安装的依赖库:
$ sudo apt-get install libssl-dev libcjson-dev xsltproc
新建配置文件:
在/etc/mosquitto/conf.d目录下,建立test.conf,内容如下:
# 关闭匿名访问,客户端必须使用用户名
allow_anonymous false
# 指定用户名-密码文件(不需要创建该文件,新建用户时会自动创建)
password_file /etc/mosquitto/pwfile.txt
通过命令新建用户:
$ sudo mosquitto_passwd -c /etc/mosquitto/pwfile.txt user
Password:
Reenter password:
该命令会重新密码文件(会清除掉原来的数据)内容如下:
user:$6$U6n6MqxF2hiuugbv$Pm3t3Ak+vVEGDxozv005AK+G4WF1KO5MzF3U9L87A2C9fR0efWGu+CM789FokdOK046dp12FXdSCOMTW1ijhAg==
可以直接编辑该文件添加或者删除用户。
修改用户密码:
$ sudo mosquitto_passwd -U /etc/mosquitto/pwfile.txt user
删除用户命令:
$ sudo mosquitto_passwd -D /etc/mosquitto/pwfile.txt user
下载iphone手机客户端:
从App Store下载 MQTTool应用
基于LinuxC的应用程序
编译库:
$ git clone https://github.com/eclipse/paho.mqtt.c.git
$ cd paho.mqtt.c
$ make && make install
安装目录:
库文件/usr/local/lib/
头文件/usr/local/include/
例程源文件在src/samples中,编译后的可执行文件在build/output/samples目录中。
编译命令:
cc -o subscribe subscribe.c -lpaho-mqtt3cs
// 清除会话: 如果设置为1,将清除历史消息
conn_opts.cleansession = 0;
// 设置保持在线时长
conn_opts.keepAliveInterval = 20;
// 设置用户名密码
conn_opts.username = "user";
conn_opts.password = "123456";
https://www.cnblogs.com/lulipro/p/10914482.html