MQTT teaching (a): Recognizing MQTT

http://swf.com.tw/?p=1002

This series of articles is intended to complement " super graphic Things IoT implementation Getting Started ", using Arduino, ESP8266 and Node.js implementation MQTT Things communication experiment.

MQTT by IBM Dr Andy Stanford-Clark and Arcom (has been renamed Eurotech) Dr. Arlen Nipper in communications in 1999, the invention of the agreement. They were to demand under the premise of narrow network bandwidth and small power loss, providing oil pipeline between the sensors and satellites a lightweight, reliable binary protocol. November 2011, IBM and Eurotech will donate MQTT agreement to manage the Eclipse Foundation open source project, and join Eclipse M2M Industry work organization. October 2014, MQTT officially become an open international standard OASIS (Organization Advancement Structured Information Standards, Information Standard Architecture Association, a non-profit organization to develop e-commerce, internet services and electronic publishing).

MQTT original meaning is represented Message Queueing Telemetry Transport (Message Queue Telemetry Transport), now without this argument, MQTT is MQTT, not the abbreviation of another word. Since the message content MQTT agreement is very concise, very suitable for use in limited processor resources and network bandwidth Things devices, plus there have been many MQTT libraries continue to be developed for Arduino control board (C / C ++ ), JavaScript (Node.js, Espruino control panel), Python, ... etc, as well as open-source MQTT server, making the development MQTT things, communication between the machine (machine-to-machine, M2M) becomes very simple. Facebook Messenger instant messaging protocol is used MQTT.

Before using Arduino and ESP8266 implementation MQTT, this article first provides background on some of the terms MQTT and instructions.

Compare HTTP protocol and MQTT

MQTT and HTTP are the underlying TCP / IP, that is, things device can follow the existing network infrastructure and equipment, but different processing mechanisms in circulation on the Internet "Message Format" and app.

Compare HTTP protocol and MQTT

Suppose a device through a Web browser, the HTTP protocol to transmit temperature to the web server, the HTTP POST message content that went something like:

HTTP POST message

In addition to the message on behalf of the HTTP request command, and the body 21 degrees, this intermediate post pile described entrained UE header (header) information corresponding to introduce the server: I from Google Chrome, operating system Android 7, I read Chinese and English ... and so on. These additional message headers communications applications in many things not only unnecessary, but also take up network bandwidth, memory and processing waste of time.

MQTT Message Format

Similar released this temperature MQTT message format:

Message transmitted temperature value MQTT

Unlike HTTP header using the written description, the MQTT digital coding header, the entire length of only 2 bytes, equivalent to two characters, followed by a message relating to (Topic) and content (payload), the actual format :

MQTT Message Format

MQTT header of the message type, quality ... and so on, leave it explained below. In addition to streamlining the header, readers can find, MQTT header area and is not marked transfer destination IP address.

MQTT of Publisher, Broker and Subscriber

According MQTT 3.1.1 version of the specifications described, MQTT is a kind of "publish / subscribe" mechanism of message transfer protocol (MQTT is a Client Server publish / subscribe messaging transport protocol) based, we can think of it as magazine distribution and subscription mechanism. MQTT message sending end, the equivalent of magazine publishing, magazine publishing after not sent directly to consumers, but to the distributor or bookstore general agent (broker), to co-ordinate the management issue and subscription issues. Each message sources (publications) has a unique theme name (publication name).

代理人是個伺服器軟體,向伺服器發送主題的一方是發布者(publisher),從伺服器獲取主題的一方則是訂閱者(subscriber)。以下圖為例,傳送感測器資料的一邊是發布者,接收感測器資料的一邊則是訂閱者。每個感測器∕微控器的訊息都需要有個主題名稱以利識別,像下圖的主題A、B和C。

MQTT of Publisher, Broker and Subscriber

代理人(broker)可儲存發布者的訊息,在發布者中斷連線的情況下,提供訂閱者最近更新的訊息。「訂閱者」需要告知代理人想要訂閱的主題,每當「發布者」傳入新訊息時,代理人就會依照主題,傳送給所有訂閱者。「發布者」和「訂閱者」都是用戶端,代理人是伺服器。由於兩個用戶端之間有伺服器當作中繼站,所以兩邊並不需要知道彼此的IP位址。

MQTT的主題(Topic)名稱

MQTT主題名稱是UTF-8(萬國碼)編碼的字串,我們可以自行決定主題名稱,例如,傳送溫度的訊息主題可命名成「溫度」、傳送亮度的訊息主題叫做「照度」…等等。主題名稱也支援類似檔案路徑的階層式命名方式,假設住家裡面有許多感測器,我們可依照測器所在位置,規劃如下的命名階層結構:

Theme named hierarchical structure

每個階層之間用斜線分隔,例如,位於庭院的人體感測器#1,其主題名稱可命名為:

Examples of the theme name

命名主題的注意事項:

  • 由於某些微控器或程式語言不支援UTF-8編碼或中文,主題名稱請使用英文,並且取個有意義的名字。
  • 名稱長度不可超過216位元組(65536個字元)。
  • 自訂的主題名稱請勿用$開頭(“$SYS”是MQTT伺服器的控制介面主題的保留字),也不可包含#和+字元;減號和乘號(*)在程式語言中有特殊意義,為了避免誤會,也不建議使用。
  • 名稱的英文大小寫有區別,home和Home是兩個不同的名稱。
  • 雖然名稱可以包含空格,但是英文的「半形」空格和中文的「全形」空格的內碼不一樣,若輸入名稱時沒有統一,會導致程式讀取不到,因此名稱最好不要加入空格。
  • 階層名稱可以空白,像這樣的命名(連續的斜線)是合法的:“home//yard”,代表有三個階層,中間階層沒有名字,在語意上怪怪的。
  • Some programmers accustomed to in front of the subject name with a slash (in Linux systems, the file path beginning with a slash for the root directory), but this is not necessary. Please note, "/ home" and "home" are two different names, the former representative of "blank the name of the root class" under the "home", a single "/" is also legal name.

Examples of the theme name

In addition to the device according to the installation site to name the theme, when the sensor contains many of the same place, with a unique identification number or code name theme is a more reasonable choice. For example, suppose a device located in the kitchen of a MAC address is DEADBEEFFEED, it may be named as:

Home/kitchen/DEADBEEFFEED

Guess you like

Origin www.cnblogs.com/zkwarrior/p/10948303.html