material:
- 51 Microcontroller
- ESP8266-01S
- ESP LINK writer (USB-TTL can be)
- DuPont line number
MQTT Description:
MQTT is based publish / subscribe paradigm messaging protocol. You released a Topic, as long as people subscribe to this Topic, then he will be able to receive the message. Similarly, you should receive a message that someone else posted, first you have to subscribe to other people. (Like vibrato you follow someone, when the man released the video, the system will notify you). If you say that several people have subscribed to a Topic, then these people will receive the information.
14 kinds MQTT message:
Specific packet format please refer to the device terminal access protocol -MQTT , Aberdeen reading you can find out the meaning of each byte represents.
ESP8266 brush AT commands:
AT command set downloaded choose ESP8266 Non-os SDK AT command set.
-
The ESP8266 ESP LINK writer inserted and plugged into the computer programmer.
-
The initial baud rate of 115200, 115200 open the serial port.
-
Check to send new line, HEX send unchecked.
-
Transmitting
AT
, to return the OK, show normal instruction execution. -
ESP8266 has three operating modes:
- SoftAP mode (corresponding to a hot or a router, using a mobile phone may be connected)
- Station mode (mode clients, the server is connected to mode)
- Station + SoftAP mode (coexistence of two or more)
-
Is set to Station + SoftAP mode and saved to the Flash (no electricity in the next set), send
AT+CWMODE_DEF=3
, return ok. -
Connection WIFI, and saved to Flash, send
AT+CWJAP_DEF="TP-LINK_1696","123456789"
, return CONNECTED WIFI
WIFI IP GOTOK, this time, has been connected to the router of your home.
-
Connecting to the remote server and save the Flash, transmission
AT+SAVETRANSLINK=1,"183.230.40.39",6002,"TCP"
, returns ok, the case has been connected OneNet MQTT server, and opens the power transparent mode. -
After the above steps as long as the power on ESP8266 MQTT server will connect to the router and OneNet.
10. FIG example:
Equipment connected OneNet:
Description: Learn about the message, then jump directly to Article 21.
-
CONNECT message transmission connection, selection choice HEX transmission, packet device I as follows, for each product ID, Master-Apikey, different device ID so that different packets.
-
10 3B 00 04 4D 51 54 54 04 C2 00 78 00 09 35 33 36 35 32 33 34 30 37 00 06 32 36 32 36 38 35 00 1C 48 45 34 4C 43 73 49 73 59 78 46 37 57 6B 44 50 54 54 54 34 75 61 37 69 73 66 77 3D
-
The first one byte
10
binary form00010000
high four representatives MQTT Packet Type, which is the value of the packet 1, check the above message table you will find that this is the CONNECT message, -
The second byte
3B
represents remainedLength latter information is much longer,3B
converted to decimal 59, do not believe you can count,3B
then 59 bytes is indeed. -
Length of protocol names used in the byte represents 3-4,
00 04
i.e. 4 bytes. -
Then the back four bytes
4D 51 54 54
name is a protocol, check the ASCII code table, you will find 0x4D corresponding letter is M, the corresponding word is four bytes MQTT, which is the protocol we use. -
The first 9 bytes
04
representing the protocol version number is 4. -
The first 10 bytes own
C2
behalfuserFlag=1 passwordFlag=1 willFlag=0 willRetainFlag=0 willQosFlag=0 clenSessionFlag=1 clenSessionFlag=0
details, please see the link above documents. -
11-12 byte
00 78
loaded into decimal is 120, meaning that keepAlive = 120, i.e. once every keepalive 120s. Otherwise your device will be dropped. -
13-14 byte
00 09
represents the device ID behind you takes 9 bytes. -
Then the next nine bytes is my device ID, and there is not translated out.
-
24-25 byte
00 06
represents the back of your product ID 6 bytes. -
Obviously, this is the product behind the six-byte ID.
-
In the next two bytes
00 1C
representative of Master-Apikey length, i.e. 28 bytes. -
28 bytes is behind the Master-Apikey, I made a change (ha ha). A total of 61 bytes.
-
Send this message, then you will see the corresponding devices on OneNet platform has been online.
-
Subscribe a name for Topic1 of TOPIC, sending
82 0B 00 02 00 06 54 6F 70 69 63 31 00
can be explained on their own. -
Post a name for Topic2, contents TOPIC 1, the transmission
30 09 00 06 54 6F 70 69 63 32 31
can explain yourself. -
Keep-alive transmission
C0 00
, meaning that the client sends the PING (connection keepalive) command. -
At this point, you can publish or subscribe to use the simulator test, the serial port will receive the appropriate information. Send command or use the platform.
-
Packets so downright too strenuous, you can go here to download Mqtt-device, it can automatically generate the appropriate message. FIG example:
-
In this case, the logic has been completed, 51 And esp8266 using serial communications, 51 in the preparation of the program need only to write messages died on it, you need to dynamically change the message can be assembled into a packet based on the packet format Wen. This would publish news; plus interrupt program can achieve read messages from the ESP8266. This will achieve a two-way communication. In addition, there must be a timer, to a certain time it needs to keep alive (keepalive), otherwise it is down equipment.
-
I would recommend using the SDK to develop the use of official MQTT_SDK .
-
The next write a blog offers 51 programs, and they can use the Android application control 51 provided on a blog, publish and subscribe change needs to be done.
-
Note: The current baud rate of 115200, when using single-chip and single-chip baud rate to be consistent. AT commands can be used to adjust the baud rate ESP8266. For example:
AT+UART=9600,8,1,0,0
the baud rate is changed to 9600.