[One article to learn MQTT protocol and mosquitto]

1. The basic concept of MQTT

MQTT is a protocol based on the publish/subscribe model, in which publishers publish messages, subscribers subscribe to topics of interest, and receive messages published by publishers.

  1. Client (Client)
    The client in the MQTT protocol refers to a device or program that establishes a connection with the MQTT server and transmits messages. The client can be a publisher (Publisher), a subscriber (Subscriber), or a device or program that is both a publisher and a subscriber.

  2. Server (Broker)
    The server in the MQTT protocol refers to the middleware that accepts client connections and forwards messages. The server can accept messages from different clients and distribute them to the corresponding subscribers or store them in the message queue and wait for the subscribers to connect and receive them.

  3. Message topic (Topic)
    The message topic in the MQTT protocol is a string used to identify the message type or message content. The message publisher can publish the message to one or more topics, and the subscriber can subscribe to one or more topics. Receive relevant news.

  4. QoS (Quality of Service)
    QoS in the MQTT protocol refers to the quality of service level of message transmission, which has three levels:
    QoS0: send at most once, and do not guarantee message arrival.
    QoS1: Send at least once, ensure message arrives, but may be repeated.
    QoS2: Send only once to ensure that the message arrives and is not repeated.

  5. Connection (Connection)
    The connection in the MQTT protocol refers to the TCP connection established between the client and the server, through which message transmission and state interaction can be performed.

  6. Session (Session)
    The session in the MQTT protocol refers to the communication state between the client and the server, including connection state, subscription state, QoS level, etc. Sessions can be persistent or short-lived.

  7. Retained Message (Retained Message)
    The message retention in the MQTT protocol means that the message published by the publisher can be kept on the server and not sent to the subscriber until the subscriber is connected. Retained messages can overwrite older messages, or keep multiple versions.
    insert image description here

Two, mosquitto

Basic concept of mosquitto

An open source message agent software that implements the message push protocol MQTT, provides a lightweight, supportable publish/subscribe message push mode, and makes short message communication between devices simple, such as it is widely used now Low-power sensors for mobile phones, embedded computers, microcontrollers and other mobile devices.

Common APIs

①struct mosquitto *mosquitto_new(const char *id, bool clean_session, void *userdata);
This function is used to create a new Mosquitto instance and return a pointer to the instance. You can use this instance to connect to an MQTT broker server, subscribe to topics, publish messages, etc.
②int mosquitto_connect(struct mosquitto *mosq, const char *host, int port, int keepalive);
This function is used to connect to the MQTT proxy server. Need to pass the address and port number of the MQTT proxy server, and optional username and password. It is also possible to set MQTT options such as keepalive intervals, will messages, etc.
③int mosquitto_disconnect(struct mosquitto *mosq);
This function is used to disconnect from the MQTT proxy server.
④int mosquitto_publish(struct mosquitto *mosq, int *mid, const char *topic, int payloadlen, const void *payload, int qos, bool retain); This function is used to publish a message
to the MQTT proxy server. It is necessary to pass the pointer of the Mosquitto instance, the subject of the message, the content of the message, the length of the message, and optional QoS level and reserved flag.
⑤int mosquitto_subscribe(struct mosquitto *mosq, int *mid, const char *sub, int qos);
This function is used to subscribe to one or more topics. A pointer to a Mosquitto instance, a topic string, and an optional QoS class need to be passed.
⑥int mosquitto_unsubscribe(struct mosquitto *mosq, int *mid, const char *sub);
This function is used to unsubscribe one or more topics. Need to pass the pointer and topic of Mosquitto instance.
⑦void mosquitto_connect_callback_set(struct mosquitto * mosq, void (*on_connect)(struct mosquitto *mosq, void *obj, int rc) ) function is used in the MQTT protocol to set the callback function when the client successfully connects to the MQTT broker.
⑧void mosquitto_message_callback_set( struct mosquitto * mosq, void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *) )
function is used to set the message receiving callback function in the MQTT protocol. When the client receives a message from a subscribed topic, the callback function will be called and can be used to process the received message.
⑨The mosquitto_publish_callback_set() function is used to set the message publishing callback function in the MQTT protocol. When the client successfully publishes the message to the broker, the callback function will be called and can be used to process the published confirmation message.
⑩ The mosquitto_disconnect_callback_set() function is a function used to set the disconnection callback function in the MQTT protocol. When the client disconnects from the broker, the callback function will be called and can be used to handle the disconnection situation.

3. MQTT test

EMQX

1. Need to enter the bin under the decompression folder Open the
cmd window and enter: emqx start press Enter
emqx-5.0.6-windows-amd64\bin>emqx start
to close the server Input: emqx stop
Open http:/ /127.0.0.1:18083/You can see the background management page, the default username/password is admin/public
insert image description here

2. Connect MQTTX to EMQX
to create a new connection and fill in relevant information
insert image description here

3. Connect the development board to EMQX
to modify the mqtt config information,
insert image description here

Restart configuration
This configuration file is configured for a bridge (bridge) of mosquitto. The bridge is used to connect two different MQTT Brokers so that messages can be exchanged between the two Brokers.

  • user root: Indicates that the user who starts mosquitto is root.
  • connection ODU: Indicates that the name of the bridge is ODU.
  • address 169.254.252.100:1883: Indicates that the address of the target MQTT Broker to be connected is 169.254.252.100:1883.
  • restart_timeout 5: indicates that the timeout period for restarting the bridge is 5 seconds.
  • topic: Indicates the topic of the message to be processed by the bridge.
  • out: Indicates the message to be published by the bridge, that is, which topics are allowed to publish messages to the target MQTT Broker.
  • in: Indicates the messages to be subscribed by the bridge, that is, which topics are allowed to be subscribed to the local MQTT Broker.
  • listener: Indicates the port and IP address to be monitored, here is the listening port 1883, and the IP address is 169.254.251.1.
  • allow_anonymous true: Indicates that anonymous users are allowed to connect to the MQTT Broker.
  • log_dest file /var/log/mosquitto.log: Indicates that the log is output to the file /var/log/mosquitto.log.

Guess you like

Origin blog.csdn.net/a1379292747/article/details/130293327