MQTT Protocol Learning and practice (Linux server, Android client example)

Source: Internet
Author: User

Objective

MQTT (Message Queuing Telemetry Transport), an IoT transport protocol, is designed for lightweight publish/subscribe messaging, designed to provide reliable network services for IoT devices in low-bandwidth and unstable network environments. MQTT is a lightweight transport protocol developed specifically for the Internet of things. The MQTT protocol is optimized for low-bandwidth networks and low-capacity devices, enabling them to adapt to a variety of IoT applications. The purpose of this paper is to study its application in message Publish/subscribe/receive scenarios.

Several important concepts in the MQTT protocol
    • Service side

      Is the intermediary between the client sending the message and the client requesting the subscription, also known as the broker. It receives network connections from the client, receives messages from the client, processes the client's subscription and cancels the subscription request, and forwards the corresponding message to the eligible subscribed clients.

    • Client

      Subscribe to related messages; publish messages to other related clients

    • Subscription

      The subscription contains a subject filter and a maximum quality of service (QoS) level. Clients receive a message for a topic only if they subscribe to a related topic

Mosquitto compiling and installing and using

The Mosquitto is an open source messaging agent software that implements the MQTT protocol version v3.1, https://mosquitto.org/download/, using the latest version of the current 1.4.10

tar xvf mosquitto-1.4.10.tar.gzcd mosquitto-1.4.10vi config.mk(将WITH_SRV:=yes中的yes改为no,否则会报ares.h文件找不到的错误)makesudo make install

If you do not do a global installation, you need to copy the Lib/libmosquitto.so.1 Dynamic Library to/usr/lib/and execute it, otherwise it will report errors that the dynamic library cannot use.

Once the installation is complete, we will simulate the entire information push process.

We use 3 terminal to represent broker, subscriber, and publisher respectively.

Terminal a launches the Mosquitto broker service, which can listen to all the interactive processes

mosquitto

Terminal b opens the subscription service, after which all messages about the subscription subject can be received

mosquitto_sub -v -t shopping

Parameter-V displays details,-t represents the topic

Terminal C Post Message

mosquitto_pub -t shopping -m "What a nice day! Go shopping with me?"

Parameter-T represents the subject,-m represents the content of the specific message


Mtqq_mosquittomqtt Androd Client

Use MQTT3 Java implementation code to do a simple Android client


Mtqq_mosquitto

4 buttons are placed on the main interface to connect, subscribe, publish and Disconnect 4 operations. Before you do this, you must start the MQTT service, the IP address of my server is 10.10.10.48, and then press the Connect button on the Android side, You can see the printing information of the successful connection of Logcat, and then press subscribe button to complete the subscription of topic shopping (as demo, I am writing dead here). To receive information from the Android side, I opened a terminal on the server side, Also subscribed to shopping as the subject of the message. When everything is ready, press the Android publish, and when you're done, you'll be able to see shopping at the end of the subscription. A nice day! Go Shopping with me topic message.


Mtqq_mosquitto
Mtqq_mosquitto

In addition to the Android side can send messages, of course, can also receive. Open another terminal and publish a message with the shopping topic

mosquitto_pub -t shopping -m "Sorry,I have no time."

You can see the Logcat in Android and show that you have received the corresponding message.


Mtqq_mosquitto
Mtqq_mosquittoandroid Project Download

Https://github.com/djstava/MqttClientAndroid

Reference documents

1 https://mosquitto.org/documentation/

2 http://tokudu.com/post/50024574938/how-to-implement-push-notifications-for-android

3 http://blog.csdn.net/xukai871105/article/details/39252653

4 http://mqtt.org/

5 https://github.com/LichFaker/MqttClientAndroid



Djstava
Links: http://www.jianshu.com/p/a7de93895416
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source. http://www.jianshu.com/p/a7de93895416

MQTT Protocol Learning and practice (Linux server, Android client example)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.