树莓派服务器-搭建MQTT服务器
树莓派服务器-搭建MQTT服务器
说明:
- 介绍如何在树莓派上安装Mosquitto MQTT服务器
- 环境: ubuntu 20.04
步骤:
- 默认系统已经支持,直接安装
sudo apt update -y && sudo apt install mosquitto mosquitto-clients -y
- 检查状态
sudo systemctl status mosquitto.service
- 效果如下:
$ sudo systemctl status mosquitto.service
● mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor pre>
Active: active (running) since Sat 2022-08-20 10:21:20 CST; 4min 57s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Main PID: 5715 (mosquitto)
Tasks: 3 (limit: 1810)
CGroup: /system.slice/mosquitto.service
└─5715 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Aug 20 10:21:20 AiWater-V101 systemd[1]: Starting Mosquitto MQTT v3.1/v3.1.1 Br>
Aug 20 10:21:20 AiWater-V101 mosquitto[5715]: [ 3635.472971]~DLT~ 5715~INFO >
Aug 20 10:21:20 AiWater-V101 systemd[1]: Started Mosquitto MQTT v3.1/v3.1.1 Bro>
- 服务没起来,可以运行
sudo systemctl start mosquitto.service
- 停止服务,可以运行
sudo systemctl stop mosquitto.service
- 激活服务
sudo systemctl enable mosquitto
- 发布话题
mosquitto_pub -h localhost -t /home/sensors/temp/kitchen -m "Kitchen Temperature: 26°C"
- h 为主机IP, t为话题名, m为内容
- 订阅话题
mosquitto_sub -h localhost -t /home/sensors/temp/kitchen
- h 为主机, t为话题名
- 效果如下:
$ mosquitto_sub -h localhost -t /home/sensors/temp/kitchen
Kitchen Temperature: 26°C
Kitchen Temperature: 26°C
Kitchen Temperature: 26°C
Kitchen Temperature: 26°C
Kitchen Temperature: 26°C
Kitchen Temperature: 26°C
- 当发布端发布话题内容,订阅端既可显示发布的内容
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号