Docker MQTT Installation tutorial
- 2020-12-10 01:03:48
- OfStack
MQTT profile
MQTT (Message Queuing Telemetry Transport, message queue telemetry transmission) is an instant messaging protocol developed by IBM, which is likely to become an important part of the Internet of Things. The protocol supports all platforms, connects virtually everything connected to the outside, and is used as a communication protocol for sensors and brakes, such as the Twitter to connect a home to the Internet.
Docker Installs RabbitMQ to configure MQTT
Use RabbitMQ as the MQTT server and Eclipse Paho as the client. The host system is ubuntu16.04
Docker downloads the image
docker pull daocloud.io/library/rabbitmq:3.7.4
Start the RabbitMQ
docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 -p 1883:1883 -p 15675:15675 daocloud.io/library/rabbitmq:3.7.4
Note the mapping of the container port
15672 is the default access port for the rabbitmq management management interface 5672 is the default port for amqp 1883 is the default port of mqtt tcp protocol 15675 is the default port of web_mqtt websocket protocolTo enable the plugin
After the default installation, the rabbitmq_management plug-in, rabbitmq_mqtt plug-in, and rabbitmq_web_mqtt plug-in need to be turned on manually.
Execute the following three commands
docker exec < The container ID> rabbitmq-plugins enable rabbitmq_management
docker exec < The container ID> rabbitmq-plugins enable rabbitmq_mqtt
docker exec < The container ID> rabbitmq-plugins enable rabbitmq_web_mqtt
Of course, you can also write a script start.sh and copy it into the container
/usr/sbin/rabbitmq-plugins enable rabbitmq_management
/usr/sbin/rabbitmq-plugins enable rabbitmq_mqtt
/usr/sbin/rabbitmq-plugins enable rabbitmq_web_mqtt
Enter the container to execute the script.
sh start.sh
Open the host port
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --zone=public --add-port=1883/tcp --permanent
firewall-cmd --zone=public --add-port=15675/tcp --permanent
firewall-cmd --reload
Python MQTT client implementation
Install python package
pip install paho-mqtt
Send data demo (Consumer)
# It needs to be started before use hbase and thrift The server
# Start the hbase in cd /usr/local/hbase Under the bin/start-hbase.sh The default port is 60000
# Start the thrift The server cd /usr/local/hbase/bin perform ./hbase-daemon.sh start thrift The default port is 9090
import sys
import os
dir_common = os.path.split(os.path.realpath(__file__))[0] + '/../'
sys.path.append(dir_common) # Add the root directory to the system directory , In order to quote properly common folder
import argparse #
import logging
import time,datetime
from common.py_log import init_logger,init_console_logger
from common.config import *
from common.py_hbase import PyHbase
import time,json
from common.py_rabbit import Rabbit_Consumer
import paho.mqtt.client as mqtt
import time
HOST = "192.168.2.46"
PORT = 1883
def client_loop():
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId Cannot repeat, so use the current time
client.username_pw_set("guest", "guest") # Must be set, otherwise it will return" Connected with result code 4 "
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
client.loop_forever()
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test")
def on_message(client, userdata, msg):
print(msg.topic+" "+msg.payload.decode("utf-8"))
if __name__ == '__main__':
client_loop()
Received data demo (Producer)
import sys
import os
dir_common = os.path.split(os.path.realpath(__file__))[0] + '/../'
sys.path.append(dir_common) # Add the root directory to the system directory , In order to quote properly common folder
import paho.mqtt.client as mqtt
import time
HOST = "192.168.2.46"
PORT = 1883
def client_loop():
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id) # ClientId Cannot repeat, so use the current time
client.username_pw_set("guest", "guest") # Must be set, otherwise it will return" Connected with result code 4 "
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
client.loop_forever()
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test")
def on_message(client, userdata, msg):
print(msg.topic+" "+msg.payload.decode("utf-8"))
if __name__ == '__main__':
client_loop()
Producers demo
# import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
HOST = "192.168.2.46"
PORT = 1883
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test")
def on_message(client, userdata, msg):
print(msg.topic+" "+msg.payload.decode("utf-8"))
if __name__ == '__main__':
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
# client = mqtt.Client(client_id) # ClientId Cannot repeat, so use the current time
# client.username_pw_set("guest", "guest") # Must be set, otherwise it will return" Connected with result code 4 "
# client.on_connect = on_connect
# client.on_message = on_message
# client.connect(HOST, PORT, 60)
# client.publish("test", " hello MQTT", qos=0, retain=False) # news
publish.single("test", " hello MQTT", qos = 1,hostname=HOST,port=PORT, client_id=client
Official documents:
mqtt http://www.rabbitmq.com/mqtt.html
conclusion