This is my 10th day of the August Challenge

Software and Hardware Environment

  • Ubuntu 16.04 64 – bit
  • Android Studio 2.0
  • OTT BOx with android 5.1.1
  • Mosquitto – 1.4.10

preface

Message Queuing Telemetry Transport (MQTT) is a Transport protocol for the Internet of Things (iot). MQTT is designed for lightweight publish/subscribe Message transmission and is 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. MQTT protocol is specially optimized for low-bandwidth networks and devices with low computing power, so that it can adapt to various iot application scenarios. The purpose of this paper is to study its application in the message publish/subscribe/receive scenario.

Several important concepts in the MQTT protocol

  • The service side

    A broker is an intermediary between a client sending a message and a client requesting a subscription. It receives network connections from the client; Receive messages published by the client; Handle subscription and unsubscription requests from clients; Forwards the corresponding message to the client that meets the subscription conditions.

  • The client

    Subscribe to relevant messages; Publish messages to other relevant clients

  • To subscribe to

    Subscriptions contain a topic filter and a maximum quality of Service (QoS) level. A client can receive messages from a topic only when it subscribes to the topic

Mosquitto compile, install and use

Mosquitto is an open source messaging agent that implements v3.1 of the MQTT protocol, available at mosquitto.org/download/, using the latest version 1.4.10 (Apt-get install is available on Ubuntu) Mosquitto to install)

Tar XVF mosquito-1.4.10.tar. gz CD mosquito-1.4.10 make sudo make installCopy the code

If you don’t want to do a global installation, you need to copy the lib/ libmosquit.so.1 dynamic library to /usr/lib/and then execute it. Otherwise, you will report an error saying that the dynamic library is not usable.

After the installation, let’s simulate the whole process of information push.

We use three terminals to represent broker, subscriber, and publisher.

Terminal A starts the Mosquitto Broker service, which listens to all interactions

mosquitto
Copy the code

Terminal B starts the subscription service, and then it receives all the messages about that subscription topic

mosquitto_sub -v -t shopping
Copy the code

Parameter -v displays detailed information, and -t indicates the subject

Terminal C releases the message

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

Parameter -t indicates the topic, and parameter -m indicates the content of the specific message

MQTT Androd client

Using MQTT3 Java implementation code, made a simple Android client

There are four buttons on the main interface, corresponding to connect, subscribe, publish and disconnect operations. Before the operation, the MQTT service must be started first, my server IP address is 10.10.10.48, and then on the Android side press the CONNECT button, you can see logcat connection successful print information, and then press the SUBSCRIBE button to complete the topic of shopping subscription (for) For demo, I wrote dead here). In order to receive the information published by the Android side, I opened a terminal on the server side and subscribed to the shopping themed message as well. When everything is ready, press PUBLISH on the Android side. When it is finished, you can see shopping What a nice day on the subscribed terminal! Go shopping with me? Topic message.

In addition to sending messages, Android can also receive them. Open another terminal and post a message with the subject of shopping

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

You can see logcat in Android, indicating that the corresponding message has been received.

Android Project Download

Github.com/xugaoxiang/…

Add authentication to broker

Here’s the mosquitto_passwd command line tool. If you installed it from the source code, you’d have to add TLS support to the mosquitto_passwd command line tool

Set a password for the user named longjing

sudo mosquitto_passwd -c /etc/mosquitto/passwd longjing
Copy the code

Then edit/etc/mosquitto mosquitto. Conf, increase the statement

password_file /etc/mosquitto/passwd
allow_anonymous false
Copy the code

Restart the

mosquitto -c /etc/mosquitto/mosquitto.conf
Copy the code

Once configured, both Mosquitto_SUB and Mosquitto_pub need to keep up with the username and password

mosquitto_sub -v -t longjing -u longjing -P longjing
Copy the code
mosquitto_pub -t longjing -m "Hello mosquitto" -u longjing -P longjing
Copy the code

Mosquitto compile errors and solutions

sudo apt install libssl-dev
Copy the code

sudo apt install libc-ares-dev
Copy the code

sudo apt install uuid-dev
Copy the code

reference

  • Mosquitto.org/documentati…
  • Tokudu.com/post/500245…
  • Blog.csdn.net/xukai871105…
  • mqtt.org/
  • Github.com/LichFaker/M…