4-Openwrt MQTT broker使用

mosquitto算是MQTT在linux平台应用比较广泛的开源软件,包含了服务端broker,也提供了lib库给client使用。

1.mosquitto下载编译

到官网下载需要的版本:http://mosquitto.org/files/source/

在openwrt下面添加mosquitto package

files下面放启动脚本和conf配置文件,Makefile里面编译信息,src下面就是官网下载的mosquitto源码

mosquitto/
├── files
│   ├── mosquittoConf
│   │   └── mosquitto.conf
│   └── mosquitto.init
├── Makefile
└── src
    ├── about.html
    ├── aclfile.example
    ├── ChangeLog.txt
    ├── client

mosquitto/Makefile

include $(TOPDIR)/rules.mk

PKG_NAME:=mosquitto
PKG_VERSION:=1.5
PKG_RELEASE:=20191126

PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)_$(PKG_VERSION)
include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
  SECTION:=net
  CATEGORY:=Network
  TITLE:=mosuqitto
  PKGARCH:=all
  SUBMENU:=net
  URL:= http://mosquitto.org/files/source/mosquitto-1.5.tar.gz
  DEPENDS:= +libopenssl +librt +libuuid
endef

define Package/$(PKG_NAME)/description
 mosquitto
endef

define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Build/InstallDev
    $(INSTALL_DIR) $(1)/usr/include
    $(CP) $(PKG_BUILD_DIR)/lib/mosquitto.h $(1)/usr/include
    $(INSTALL_DIR) $(1)/usr/lib
    $(CP) $(PKG_BUILD_DIR)/lib/libmosquitto.so.1 $(1)/usr/lib/
    $(LN) libmosquitto.so.1 $(1)/usr/lib/libmosquitto.so
endef

define Package/$(PKG_NAME)/install
    $(INSTALL_DIR) $(1)/usr/lib
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/lib/libmosquitto.so.1 $(1)/usr/lib/
    $(LN) libmosquitto.so.1 $(1)/usr/lib/libmosquitto.so
    $(INSTALL_DIR) $(1)/usr/sbin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/src/mosquitto $(1)/usr/sbin/mosquitto
    $(INSTALL_DIR) $(1)/etc/mosquittoConf
    $(CP) ./files/mosquittoConf/* $(1)/etc/mosquittoConf/
    $(INSTALL_DIR) $(1)/etc/init.d
    $(INSTALL_BIN) ./files/mosquitto.init $(1)/etc/init.d/mosquitto
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

mosquitto/files/mosquitto.init

!/bin/sh /etc/rc.common
# Basic init script for mosquitto
# April 2012, OpenWrt.org
# Provides support for the luci-app-mosquitto package, if installed

START=80
USE_PROCD=1

CONF=/etc/mosquittoConf/mosquitto.conf

start_service() {
    procd_open_instance
    procd_set_param command mosquitto
    procd_append_param command -c $CONF
    procd_set_param respawn
    [ -e /proc/sys/kernel/core_pattern ] && {
        procd_set_param limits core="unlimited"
    }   
    procd_close_instance
}

mosquitto/files/mosquittoConf/mosquitto.conf

user root

listener 1883

log_type error
log_type warning
log_type notice
log_type information
log_type debug

allow_anonymous true

上面文件都添加好之后,在.config里面选中mosquitto,编译

CONFIG_PACKAGE_mosquitto=y
2.mosquitto配置启动

编译正常后,在openwrt上面使用,上面在init.d里面加了自启动,先/etc/init.d/mosquitto stop停止到手动启动

mosquitto -c /etc/mosquittoConf/mosquitto.conf

mosquitto启动的时候根据mosquitto.conf里面的信息进行启动,默认监听端口是1883,打开debug信息,允许匿名登录allow_anonymous true

正常启动如下:

root@openwrt:/# mosquitto -c /etc/mosquittoConf/mosquitto.conf 
1593326317: mosquitto version 1.5 starting
1593326317: Config loaded from /etc/mosquittoConf/mosquitto.conf.
1593326317: Opening ipv6 listen socket on port 1883.
1593326317: Warning: Address family not supported by protocol
1593326317: Opening ipv4 listen socket on port 1883.
1593326317: Warning: Mosquitto should not be run as root/administrator.
3.命令行发布订阅测试

上面mosquitto Broker启动之后,就可以用命令mosquitto_submosquitto_pub进行测试是否正常

跟在ubuntu上面的测试一样,先订阅

root@zihome:/# mosquitto_sub -t "local/test" -h 127.0.0.1 -p 1883 -d -i ubuntu_client1
Client ubuntu_client1 sending CONNECT
Client ubuntu_client1 received CONNACK (0)
Client ubuntu_client1 sending SUBSCRIBE (Mid: 1, Topic: local/test, QoS: 0)
Client ubuntu_client1 received SUBACK
Subscribed (mid: 1): 0

此时也可以看到Broker上面的log,添加了一个设备

1593326374: New connection from 127.0.0.1 on port 1883.
1593326374: New client connected from 127.0.0.1 as ubuntu_client1 (c1, k60).
1593326374: No will message specified.
1593326374: Sending CONNACK to ubuntu_client1 (0, 0)
1593326374: Received SUBSCRIBE from ubuntu_client1
1593326374:     local/test (QoS 0)
1593326374: Sending SUBACK to ubuntu_client1

再开启一个终端,发布主题

root@zihome:/# mosquitto_pub -t "local/test" -m "{test:111}" -h 127.0.0.1 -p 1883 -d -i ubuntu_client2
Client ubuntu_client2 sending CONNECT
Client ubuntu_client2 received CONNACK (0)
Client ubuntu_client2 sending PUBLISH (d0, q0, r0, m1, 'local/test', ... (10 bytes))
Client ubuntu_client2 sending DISCONNECT

可以看到Broker收到client2的消息后,转发给client1

1593326497: New client connected from 127.0.0.1 as ubuntu_client2 (c1, k60).
1593326497: No will message specified.
1593326497: Sending CONNACK to ubuntu_client2 (0, 0)
1593326497: Received PUBLISH from ubuntu_client2 (d0, q0, r0, m0, 'local/test', ... (10 bytes))
1593326497: Sending PUBLISH to ubuntu_client1 (d0, q0, r0, m0, 'local/test', ... (10 bytes))
1593326497: Received DISCONNECT from ubuntu_client2
1593326497: Client ubuntu_client2 disconnected.

client1收到Broker转发的数据

Client ubuntu_client1 received PINGRESP
Client ubuntu_client1 received PUBLISH (d0, q0, r0, m0, 'local/test', ... (10 bytes))
{test:111}

测试一切正常。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值