Article From:

Now the Internet of things is popular with MQTT

In fact, MQTT set up a set of agreements on the basis of TCP.


I can watch this. Originally I wanted to monitor with Wireshark, but when Baidu searched a lot, I wouldn’t test it.


So as long as TCP can be connected, and as long as we know the protocol of MQTT, we can directly use TCP as MQTT.

However, we need to write some protocols that are compatible with MQTT communication, then send and receive data through the protocol processing, then send and receive through TCP.

There is a ready – made agreement

You can see these two of the protocol on STM32 /



I also plan to transplant (application) to STM32 first, but I do not plan to use network module W5500, though it is very familiar with it, I feel no freshness.


I feel that I should do it with ESP8266

In fact, the idea is very simple. 8266 build the TCP client (using AT command), because there is no AT instruction version MQTT now, so configure 8266 with AT instruction.

Then the address of the connected server is MQTT of my cloud. Of course, TCP is passed through and then encapsulates part of the program through MQTT protocol when sending data.

To the WIFI module, then the WIFI module will be sent to the MQTT server again, and the reception will be the same… Then…


 But it just seemed to see the same…

In fact, as long as you use the network monitoring software to see the data, and then look at the MQTT agreement below, you can write it yourself. 

I tried to write it myself, and I just looked at the protocol and the data that came back, and then I tried to write a TCP connection and send a data (the MQTT protocol) to connect MQTT.


First, the first byte is






Come up with a direct picture


After connecting with TCP, then use TCP to send the instructions above, then connect MQTT.

 Then test……



And then don’t say, and do not want to say, everyone to see the agreement,,, because I feel that the status of MQTT in my heart is greatly…… ah,,, feel that the things that I speak out really feel so simple…

As long as I get through it, I can write the agreement myself. I write the agreement myself, and estimate that the variables I write are less. 51 MCU can do it.


And finally, if it’s the 4 version of MQTT




Then today, write a single chip computer program, with the MQTT package written by himself, on the basis of the 8266 TCP client, connect my cloud MQTT server, and then test the remote communication with the debug assistant, the code is very few, striving to be directly transplanted to the 51 single chip microcomputer.




#include "include.h"

unsigned char MqttSendData[70]={0};

* @brief  A package function to connect the server* @param* @retval* @example**/
int ConnectMqtt(char *ClientID,char *Username,char *Password)
    int ClientIDLen = strlen(ClientID);
    int UsernameLen    = strlen(Username);
    int PasswordLen = strlen(Password);
    int DataLen = 0;
    int Index = 2;
    int i = 0;
    DataLen = 12 + 2+2+ClientIDLen+UsernameLen+PasswordLen;
    MqttSendData[0] = 0x10;                //MQTT Message Type CONNECT
    MqttSendData[1] = DataLen;    //Residual length (excluding fixed head)
    MqttSendData[Index++] = 0;        // Protocol Name Length MSB    
    MqttSendData[Index++] = 4;        // Protocol Name Length LSB    
    MqttSendData[Index++] = 'M';        // ASCII Code for M    
    MqttSendData[Index++] = 'Q';        // ASCII Code for Q    
    MqttSendData[Index++] = 'T';        // ASCII Code for T    
    MqttSendData[Index++] = 'T';        // ASCII Code for T    
    MqttSendData[Index++] = 4;        // MQTT Protocol version = 4    
    MqttSendData[Index++] = 0xc2;        // conn flags 
    MqttSendData[Index++] = 0;        // Keep-alive Time Length MSB    
    MqttSendData[Index++] = 60;        // Keep-alive Time Length LSB  60SHeartbeat
    MqttSendData[Index++] = (0xff00&ClientIDLen)>>8;// Client ID length MSB    
    MqttSendData[Index++] = 0xff&ClientIDLen;    // Client ID length LSB  

    for(i = 0; i < ClientIDLen; i++)
        MqttSendData[Index + i] = ClientID[i];          
    Index = Index + ClientIDLen;
    if(UsernameLen > 0)
        MqttSendData[Index++] = (0xff00&UsernameLen)>>8;//username length MSB    
        MqttSendData[Index++] = 0xff&UsernameLen;    //username length LSB    
        for(i = 0; i < UsernameLen ; i++)
            MqttSendData[Index + i] = Username[i];    
        Index = Index + UsernameLen;
    if(PasswordLen > 0)
        MqttSendData[Index++] = (0xff00&PasswordLen)>>8;//password length MSB    
        MqttSendData[Index++] = 0xff&PasswordLen;    //password length LSB    
        for(i = 0; i < PasswordLen ; i++)
            MqttSendData[Index + i] = Password[i];    
        Index = Index + PasswordLen; 
    return Index;

* @brief  MQTTSubscribe / unsubscribe data package function* @param SendData* @param topic theme* @param QoS message level*Param whether subscribe / unsubscribe request package* @retval* @example**/
int MqttSubscribeTopic(char *topic,u8 qos,u8 whether)
    int topiclen = strlen(topic);
    int i=0,index = 0;
        MqttSendData[index++] = 0x82;                        //0x82 //Message type and flag SUBSCRIBE subscription
        MqttSendData[index++] = 0xA2;                        //0xA2 unsubscribe
    MqttSendData[index++] = topiclen + 5;                //Residual length (excluding fixed head)
    MqttSendData[index++] = 0;                          //Message identifier, high level
    MqttSendData[index++] = 0x01;                    //Message identifier, low
    MqttSendData[index++] = (0xff00&topiclen)>>8;    //The length of the subject (high in the front, after the low position)
    MqttSendData[index++] = 0xff&topiclen;              //Topic length
    for (i = 0;i < topiclen; i++)
        MqttSendData[index + i] = topic[i];
    index = index + topiclen;
        MqttSendData[index] = qos;//QoSlevel
    return index;

* @brief  MQTTPublishing data packing function* @param mqtt_message* @param topic theme* @param QoS message level* @rEtval* @example**/
int MqttPublishData(char * topic, char * message, u8 qos)
    int topic_length = strlen(topic);    
    int message_length = strlen(message);  
    int i,index=0;    
    static u16 id=0;
    MqttSendData[index++] = 0x30;    // MQTT Message Type PUBLISH  

        MqttSendData[index++] = 2 + topic_length + 2 + message_length;//Data length
        MqttSendData[index++] = 2 + topic_length + message_length;   // Remaining length  

    MqttSendData[index++] = (0xff00&topic_length)>>8;//Topic length
    MqttSendData[index++] = 0xff&topic_length;
    for(i = 0; i < topic_length; i++)
        MqttSendData[index + i] = topic[i];//Copy subject
    index += topic_length;
        MqttSendData[index++] = (0xff00&id)>>8;
        MqttSendData[index++] = 0xff&id;
    for(i = 0; i < message_length; i++)
        MqttSendData[index + i] = message[i];//Copy data
    index += message_length;
    return index;


#include <stm32f10x.h>
#ifndef MQTTCONFIG_C_//If there is no definition of _TIME_C_
#define MQTTCONFIG_C_ extern

MQTTCONFIG_C_ unsigned char MqttSendData[200];

int ConnectMqtt(char *ClientID,char *Username,char *Password);
int MqttSubscribeTopic(char *topic,u8 qos,u8 whether);
int MqttPublishData(char * topic, char * message, u8 qos);



Leave a Reply

Your email address will not be published. Required fields are marked *