需求情況:路由器作為網關和雲端服務器建立連接,實現物聯網數據上傳雲服務器以及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