MQTT profile

MQ telemetry Transport (MQTT) is a lightweight proxy-based publish/subscribe messaging protocol designed to be open, simple, lightweight, and easy to implement. These characteristics make it suitable for constrained environments. The characteristics of the agreement are:

  • Use the publish/subscribe message pattern to provide one-to-many message publishing and decouple the application.
  • Message transmission masked with payload content.
  • Provide network connections using TCP/IP.
  • Small transmission, minimal overhead (fixed length header is 2 bytes), minimal protocol switching to reduce network traffic.
  • The Last Will and Testament features are used to notify parties of client outages.
  • There are three message publishing qualities of service:
  • “At most once,” message publishing is entirely dependent on the underlying TCP/IP network. Message loss or duplication may occur. This level can be used for environmental sensor data where it does not matter if the first read is lost because a second read will be sent shortly after.
  • “At least once” ensures the message arrives, but message duplication can occur.
  • “Once only,” to ensure that the message arrives once. This level can be used in cases where duplicate or lost messages can result in incorrect results in a billing system.

preface

Recently, I wanted to use MQTT protocol in my project, so I need to build an MQTT server for debugging. After a day of searching on the Internet, most of the clients I found were MQTT clients. Finally, I found the tutorial written by this blog can be used, for the record.

Looking for process

In Mosquitto’s search for the MQTT server, I found out that they were “An Open Source MQTT V3.1 / V3.1.1 Broker” — An Open Source MQTT proxy server with a Windows installation package.

But I use Win10 64-bit system, these two are downloaded after installation is not an error or lack of DLL file, can not use.

Finally, according to the previous blog, WE built the Apollo server, using Apollo 1.7.1 here.

  • Liverpoolfc.tv: activemq.apache.org/index.html
  • Download address: activemq.apache.org/apollo/down…
  • Quick start tutorial: activemq.apache.org/apollo/docu…

Set up the MQTT server

Steps to build an MQTT server using Apollo:

  1. Download the Apollo server, unzip it, and run it in the CMD environment in the working directory. \bin\apollo.cmd, followed by the parameter”create mybroker“To create a server instance. You need the Java environment here, and you need it under the system environment variablesJAVA_HOME.
  2. After the instance is created, the myBroker folder is generated in the bin directory, where. \etc\apollo.xmlUnder the file is the file that configures the server information,. \etc\users.propertiesThe file contains the username and password used to connect to the MQTT server. The initial default account isadminThe passwordpassword;
  3. Enter the. \mybroker\bin\Directory, enter the “CMD” commandapollo-broker.cmd run“, you can use TAB key to auto-complete, after running the command, the output information is as follows:

Among them we should pay attention to:

The MQTT server TCP connection port is TCP ://0.0.0.0:61613

Background Web management page: https://127.0.0.1:61681/ or http://127.0.0.1:61680/

After logging in to the server, if the MQTT server has a client connection, the background will display as follows

MQTT client for Python

There is an MQTT client package in Python — Paho-MQTT.

Install command

pip install paho-mqtt
Copy the code

Client code listing

The following is a listing of MQTT client code

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc) :
    print("Connected with result code "+str(rc))

    client.subscribe("lettuce")

def on_message(client, userdata, msg) :
    print(msg.topic+""+str(msg.payload))

client = mqtt.Client()
client.username_pw_set("admin"."password") Connected with result code 4
client.on_connect = on_connect
client.on_message = on_message

HOST = "127.0.0.1"

client.connect(HOST, 61613.60)
client.loop_forever()
Copy the code

Publish message code

Here is the code to publish the message to the MQTT server

import paho.mqtt.publish as publish

HOST = "127.0.0.1"

publish.single("lettuce"."payload", hostname=HOST, port=61613,
               auth={'username': "admin".'password':"password"})
Copy the code