51 single connection OneNet implement publish / subscribe (on) protocol by MQTT

material:

  1. 51 Microcontroller
  2. ESP8266-01S
  3. ESP LINK writer (USB-TTL can be)
  4. 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:

Here Insert Picture Description
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.

  1. The ESP8266 ESP LINK writer inserted and plugged into the computer programmer.

  2. The initial baud rate of 115200, 115200 open the serial port.

  3. Check to send new line, HEX send unchecked.

  4. Transmitting AT, to return the OK, show normal instruction execution.

  5. ESP8266 has three operating modes:

    1. SoftAP mode (corresponding to a hot or a router, using a mobile phone may be connected)
    2. Station mode (mode clients, the server is connected to mode)
    3. Station + SoftAP mode (coexistence of two or more)
  6. Is set to Station + SoftAP mode and saved to the Flash (no electricity in the next set), send AT+CWMODE_DEF=3, return ok.

  7. Connection WIFI, and saved to Flash, send AT+CWJAP_DEF="TP-LINK_1696","123456789", return CONNECTED WIFI
    WIFI IP GOT

    OK, this time, has been connected to the router of your home.

  8. 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.

  9. After the above steps as long as the power on ESP8266 MQTT server will connect to the router and OneNet.
    10. FIG example:
    Here Insert Picture Description

Equipment connected OneNet:

Description: Learn about the message, then jump directly to Article 21.

  1. 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.

  2. 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

  3. The first one byte 10binary form 00010000high 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,

  4. The second byte 3Brepresents remainedLength latter information is much longer, 3Bconverted to decimal 59, do not believe you can count, 3Bthen 59 bytes is indeed.

  5. Length of protocol names used in the byte represents 3-4, 00 04i.e. 4 bytes.

  6. Then the back four bytes 4D 51 54 54name 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.

  7. The first 9 bytes 04representing the protocol version number is 4.

  8. The first 10 bytes own C2behalf userFlag=1 passwordFlag=1 willFlag=0 willRetainFlag=0 willQosFlag=0 clenSessionFlag=1 clenSessionFlag=0details, please see the link above documents.

  9. 11-12 byte 00 78loaded into decimal is 120, meaning that keepAlive = 120, i.e. once every keepalive 120s. Otherwise your device will be dropped.

  10. 13-14 byte 00 09represents the device ID behind you takes 9 bytes.

  11. Then the next nine bytes is my device ID, and there is not translated out.

  12. 24-25 byte 00 06represents the back of your product ID 6 bytes.

  13. Obviously, this is the product behind the six-byte ID.

  14. In the next two bytes 00 1Crepresentative of Master-Apikey length, i.e. 28 bytes.

  15. 28 bytes is behind the Master-Apikey, I made a change (ha ha). A total of 61 bytes.

  16. Send this message, then you will see the corresponding devices on OneNet platform has been online.
    Here Insert Picture Description

  17. Subscribe a name for Topic1 of TOPIC, sending 82 0B 00 02 00 06 54 6F 70 69 63 31 00can be explained on their own.

  18. Post a name for Topic2, contents TOPIC 1, the transmission 30 09 00 06 54 6F 70 69 63 32 31can explain yourself.

  19. Keep-alive transmission C0 00, meaning that the client sends the PING (connection keepalive) command.

  20. 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.

  21. Packets so downright too strenuous, you can go here to download Mqtt-device, it can automatically generate the appropriate message. FIG example:
    Here Insert Picture Description

  22. 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.

  23. I would recommend using the SDK to develop the use of official MQTT_SDK .

  24. 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.

  25. 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,0the baud rate is changed to 9600.

Released two original articles · won praise 7 · views 2877

Guess you like

Origin blog.csdn.net/weixin_42181820/article/details/104814921