分类 MQTT 下的文章

“MQTT消息队列遥测传输(英语:Message Queuing Telemetry Transport)是ISO 标准(ISO/IEC PRF 20922)下基于发布 (Publish)/订阅 (Subscribe)范式的消息协议,可视为“资料传递的桥梁”它工作在 TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟糕的情况下而设计的发布/订阅型消息协议。”

参照RabbitMq官网的安装教程(Installing on Debian and Ubuntu),来进行安装。

sudo apt install erlang-nox

安装完成后执行

erl

Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1]

Eshell V10.6.4 (abort with ^G)

wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -

sudo apt update
sudo apt install rabbitmq-server

安装完成后,查看服务状态

systemctl status rabbitmq-server

● rabbitmq-server.service - RabbitMQ Messaging Server

 Loaded: loaded (/lib/systemd/system/rabbitmq-server.service; enabled; vendor preset: enabled)
 Active: active (running) since Tue 2021-09-07 09:53:48 CST; 2 days ago

Main PID: 1581436 (beam.smp)

 Status: "Initialized"
  Tasks: 235 (limit: 18732)
 Memory: 95.7M
 CGroup: /system.slice/rabbitmq-server.service
         ├─1581432 /bin/sh /usr/sbin/rabbitmq-server
         ├─1581436 /usr/lib/erlang/erts-10.6.4/bin/beam.smp -W w -A 192 -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 1048576 -t 5000000 -stbt db>
         ├─1581884 erl_child_setup 65536
         ├─1581943 inet_gethost 4
         └─1581944 inet_gethost 4

启动、停止、重启rabbitmq

sudo service rabbitmq-server start    # 启动
sudo service rabbitmq-server stop     # 停止
sudo service rabbitmq-server restart  # 重启 

执行了上面的步骤,rabbitMq已经安装成功。

启用 web端可视化操作界面,我们还需要配置Management Plugin插件

sudo rabbitmq-plugins enable rabbitmq_management   # 启用插件
sudo service rabbitmq-server restart    # 重启

此时,应该可以通过 http://localhost:15672/ 查看,使用默认账户guest/guest 登录。

安裝 Mosquitto MQTT Broke

sudo apt install -y mosquitto mosquitto-clients

TEST Mosquitto MQTT Broker
建立 Subscriber

mosquitto_sub -h test.mosquitto.org -t Sensor/Temperature/Room1

建立 Publisher

mosquitto_pub -h 54.159.81.55 -t Sensor/Temperature/Room1 -m "hello world"

安裝paho-mqtt套件

pip install paho-mqtt

mosquitto_mqtt.py

import paho.mqtt.client as mqtt  
import time  
import json  
import random

# *********************************************************************
# MQTT Config

dataChnId1 = "Temperature"  
MQTT_SERVER = "test.mosquitto.org"  
MQTT_PORT = 1883  
MQTT_ALIVE = 60  
MQTT_TOPIC1 = "Sensor/" + dataChnId1 + "/Room1" 

# *********************************************************************

mqtt_client = mqtt.Client()  
mqtt_client.connect(MQTT_SERVER, MQTT_PORT, MQTT_ALIVE)    

while True:  
  t0 = random.randint(0,30)
  payload = {"dataChnId":dataChnId1,"value":t0}
  print(dataChnId1 + " : " + str(t0))
  mqtt_client.publish(MQTT_TOPIC1, json.dumps(payload), qos=1)
  time.sleep(10)

错误代码:disconnect response code 7

错误信息:The connection was lost

可能的原因是,使用了相同的clientid

在mosquitto_new 的第一个参数中,使用NULL就好。

还有另一个原因,也会出现这个错误.就是当mqtt broker重启会导致disconnect 7.

安装mosquitto borker

sudo apt-get update
sudo apt-get install mosquitto

安装mosquitto clients

sudo apt-get install mosquitto-clients

查看运行状态

systemctl status mosquitto

状态信息:

 mosquitto.service - Mosquitto MQTT v3.1/v3.1.1 Broker
 Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor pre>
 Active: active (running) since Mon 2022-05-09 16:59:13 CST; 16h ago
   Docs: man:mosquitto.conf(5)
         man:mosquitto(8)
  Main PID: 524488 (mosquitto)
  Tasks: 3 (limit: 18708)
 Memory: 1.2M
 CGroup: /system.slice/mosquitto.service
         └─524488 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

重启mqtt服务

systemctl restart mosquitto

停止mqtt服务

systemctl stop mosquitto

测试使用mqtt

mosquitto -v

1652145455: mosquitto version 1.6.9 starting
1652145455: Using default config.
1652145455: Opening ipv4 listen socket on port 1883.
1652145455: Opening ipv6 listen socket on port 1883.

启动mosquitto监听1883端口

mosquitto -p 1883

启动mosquitto后台运行监听1884端口

mosquitto -p 1884 -d

**启动mosquitto后台运行使用mosquitti-2.conf配置文件

mosquitto -c /etc/mosquitto/mosquitto-2.conf -d

1. Create CA Key Pair
Run as administrator the following command:

openssl genrsa -out m2mqtt_ca.key 2048

2. Create CA Certificate
Next I’m creating a certificate for the CA, using the key pair I have created in step 1:

openssl req -new -x509 -days 3650 -key m2mqtt_ca.key -out m2mqtt_ca.crt

3. Create Mosquitto Broker Key Pair
Next, I’m creating a private key for the server (m2mqtt_srv.key) with:

openssl genrsa -out m2mqtt_srv.key 2048

4. Create Certificate Request from CA
That key we need to be certified, so we create a certificate request for it, and the certificate needs to be signed by the CA:

openssl req -new -out m2mqtt_srv.csr -key m2mqtt_srv.key

5. Verify and Sign the Certificate Request
The last step is to sign the server request through the CA to get the broker certificate:

openssl x509 -req -in m2mqtt_srv.csr -CA m2mqtt_ca.crt -CAkey m2mqtt_ca.key -CAcreateserial -out m2mqtt_srv.crt -days 3650

文件清单

m2mqtt_ca.crt : CA Certificate
m2mqtt_ca.key : CA key pair (private, public)
m2mqtt_ca.srl : CA serial number file
m2mqtt_srv.crt : server certificate
m2mqtt_srv.csr : certificate sign request, not needed any more
m2mqtt_srv.key : server key pair

将证书和密钥文件复制到下面目录
/etc/mosquitto/certs
最后目录内容应该类似这样
/etc/mosquitto/certs/m2mqtt_ca.crt
/etc/mosquitto/certs/m2mqtt_srv.crt
/etc/mosquitto/certs/m2mqtt_srv.key

修改/etc/mosquitto/mosquitto.conf配置文件
默认端口号

port 8883

指定证书文件与路径信息

#capath
cafile /etc/mosquitto/certs/m2mqtt_ca.crt

# Path to the PEM encoded server certificate.
certfile /etc/mosquitto/certs/m2mqtt_srv.crt

# Path to the PEM encoded keyfile.
keyfile /etc/mosquitto/certs/m2mqtt_srv.key

# This option defines the version of the TLS protocol to use for this listener.
# The default value allows v1.2, v1.1 and v1.0, if they are all supported by
# the version of openssl that the broker was compiled against. For openssl >=
# 1.0.1 the valid values are tlsv1.2 tlsv1.1 and tlsv1. For openssl < 1.0.1 the
# valid values are tlsv1.
tls_version tlsv1.2

配置文件最终结果

 cat /etc/mosquitto/conf.d/tls.conf 

port 8883
cafile /etc/mosquitto/certs/m2mqtt_ca.crt
certfile /etc/mosquitto/certs/m2mqtt_srv.crt
keyfile /etc/mosquitto/certs/m2mqtt_srv.key
tls_version tlsv1.2

运行测试

mosquitto -c mosquitto.conf -v

systemd服务查看

sudo systemctl status mosquitto

● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
Loaded: loaded (/etc/init.d/mosquitto; generated)
Active: active (running) since Wed 2022-10-19 09:53:21 CST; 2min 32s ago