Weekly Summary

The new project needs to use the hardware connection, the use of technology to mqtt, query relevant information, summed up

A, MQTT Profile

  MQTT (Message Queuing Telemetry Transport, Message Queuing Telemetry Transport Protocol), based on a publish / subscribe (publish / subscribe) mode lightweight protocol built on TCP / IP protocol, the biggest advantage is that the MQTT be in minimal code and limited bandwidth for real-time remote devices to provide reliable messaging service. As a low-cost, low bandwidth usage of instant messaging protocol, so there is more widely used in things, small equipment, mobile applications.

  MQTT is based on a client - server message publish / subscribe transport protocol. MQTT agreement is lightweight, simple, open and easy to implement, these features make it suitable for a very wide range. In many cases, including constrained environments, such as: machine to machine (M2M) communication and Internet of Things (IoT). Communication by satellite link sensors, occasional dial-up medical equipment, intelligent home, and some of the smaller devices have been widely used.  

Second, the characteristics

  MQTT communication protocol operating at the remote sensors and control devices low bandwidth, unreliable network protocol design, which has the following several major characteristics:

  (1) using the publish / subscribe messaging model, providing many message distribution decouples applications.

  (2) load the contents of message transmission mask.

  (3) using the TCP / IP provides a network connection.

  Mainstream MQTT is based on TCP connection for data push, but also has a UDP-based version, called MQTT-SN. Because the two versions based on different connections, each with different strengths and weaknesses naturally.

  (4) There are three kinds of messages publishing quality of service (qss):

  "At most once", the news release completely dependent on the underlying TCP / IP network. Occur missing or duplicate messages. This level can be used in the following cases, the environmental sensor data, loss of a read record does not matter, because there will be sent a second time soon after. This is a way to push major general of APP, if your smart device is offline when the message push, push past did not receive, will not receive the network again.

  "At least once" to ensure that the message reaches, but the message is repeated may occur.

  "Only once" to ensure that the message reaches once. In some of the more stringent requirements of the billing system, you can use this level. In the billing system, the message is repeated or missing will lead to incorrect results. This news release highest quality service can also be used to push APP instant communications, ensuring that users receive and will receive only once. 

  

 

 

 Of course, the smaller the number, the lower-band wideband station

Third, the principle

 

 

MQTT achieve agreement requires the client and server communication is completed, in the communication process, there are three protocols MQTT identity: the publisher (Publish), agent (Broker) (server), subscribers (Subscribe). Among them, news publishers and subscribers are clients, messages proxy server, news release may be subscribers simultaneously.

MQTT transmitted into message: topic (Topic) and a load (payload) in two parts:

  • (1) Topic, can be understood as the type of message, after subscriber subscription (Subscribe), you will receive the message content that topic (payload);
  • (2) payload, can be understood as the content of the message, the content refers to specific subscribers want to use.

Fourth, to achieve

1. Download emq

 

 

 2. After start Access

localhost: 18083 is the default admin account password to get the figure below

 

 

 3. Establish connection node red graphical tools

 

Note that this theme is the theme topic subscription must be consistent

 

 

 

 

 

 After the completion of the normal flow of operations can then emq graphics pages to see the client connection

 

 After sending the message, the thief can indeed receive data connection is successful

 

 Here substantially to achieve even a simple connection mqtt

Follow-up you do not want to use emq, you can use Ali's principle is similar, but there are a few id key purchase, it depends on the individual circumstances

        public class MqttPublishSample {

        public static void main(String[] args) {

            String topic        = "MQTT Examples";
            String content      = "Message from MqttPublishSample";
            int qos             = 2;
            String broker       = "tcp://mqtt.eclipse.org:1883";
            String clientId     = "JavaSample";
            MemoryPersistence persistence = new MemoryPersistence();

            try {
                MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
                MqttConnectOptions connOpts = new MqttConnectOptions();
                connOpts.setCleanSession(true);
                System.out.println("Connecting to broker: "+broker);
                sampleClient.connect(connOpts);
                System.out.println("Connected");
                System.out.println("Publishing message: "+content);
                MqttMessage message = new MqttMessage(content.getBytes());
                message.setQos(qos);
                sampleClient.publish(topic, message);
                System.out.println("Message published");
                sampleClient.disconnect();
                System.out.println("Disconnected");
                System.exit(0);
            } catch(MqttException me) {
                System.out.println("reason "+me.getReasonCode());
                System.out.println ( "MSG" + me.getMessage ());
                System.out.println("loc "+me.getLocalizedMessage());
                System.out.println ( "the cause is" + me.getCause ()); 
                System.out.println ( "excep" + Me); 
                me.printStackTrace (); 
            } 
        } 
    } 
recording a careless mistake week -> redis Read take server address is not turned on, and replaced with a local on ok

 

 

 

 

Guess you like

Origin www.cnblogs.com/xiufengchen/p/11827844.html