一种简单的基于MQTT的路由网关到云端的数据通路搭建

需求情况:路由器作为网关和云端服务器建立连接,实现物联网数据上传云服务器以及APP控制命令传送到路由器网关

该方案用MQTT服务器mosquitto软件来搭建

第一步,在路由器中把MQTT服务器mosquitto软件跑起来

/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

第二步,在云服务器中把MQTT服务器mosquitto软件跑起来

/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

第三步,配置bridge功能实现两个MQTT服务器之间的数据交互

配置路由器网关的/etc/mosquitto/mosquitto.conf配置文件

# =================================================================
# Bridges
# =================================================================

# A bridge is a way of connecting multiple MQTT brokers together.

#connection <name>
connection openwrt-cloud
#address <host>[:<port>] [<host>[:<port>]]
address 39.1XX.XXX.2XX:1883    //云端服务器公网IP及开放的端口
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
topic # both 2 gw/ gw/    //消息订阅、发布的格式

# Set the version of the MQTT protocol to use with for this bridge. Can be one
# of mqttv31 or mqttv311. Defaults to mqttv31.
bridge_protocol_version mqttv31


cleansession false

start_type automatic

第四步,用MQTT客户端 mosquitto_sub、 mosquitto_pub做收发测试

路由器订阅:

阿里云CES服务器发布:

 ==================================================================================

阿里云CES服务器订阅:

路由器发布:

 可以看到云服务器发送hello,路由器能收到hello

 可以看到路由器发送world,云服务器也能收到world

原文地址:https://www.cnblogs.com/caoyongan/p/7251471.html