关于TCP和MQTT之间的转换(转载)

如今物联网流行的就是MQTThtml

其实MQTT就是在TCP的基础上创建了一套协议git

能够看这个,原本我本身想用Wireshark监听一下,不过百度一搜索一大把,我就不测试了服务器

https://blog.csdn.net/libaineu2004/article/details/78773610网络

因此说只要能够TCP链接了,而后只要知道了MQTT的协议,,,,直接就能够用TCP来当作MQTT来使用了函数

不过要写一些配合MQTT通讯的协议,而后发送和接收数据都经过协议处理以后,经过TCP发送和接收,测试

其实有现成的写好的协议spa

能够看这两篇.net

http://sun2y.me/2017/05/12/MQTT协议在STM32上的移植/调试

https://blog.csdn.net/kh766200466/article/details/79694119code

我也打算先移植(应用)到stm32上,不过我不打算用网络模块W5500,虽然用的挺熟,感受没有新鲜感

我感受应该用ESP8266实现

其实思路很简单,8266建TCP客户端(用AT指令),由于如今没有AT指令版的MQTT,因此用AT指令配置8266

而后链接的服务器的地址是个人云端的MQTT,固然TCP是透传的,而后发数据的时候都经过MQTT协议封装部分的程序,而后

发给WIFI模块,而后WIFI模块再发给MQTT服务器,,,接收也同样......而后....就没而后了,,能够用了再说

 

不过刚恰好像看透了同样.......

其实呢...只要用网络监控的软件看见了数据,而后再看下面的MQTT协议.....就能够本身写了

 

https://mcxiaoke.gitbooks.io/mqtt-cn/content/

咱试一试本身写,我呢只是看着协议和传回来的数据,,,而后咱本身试一试写个在TCP链接以后,发个数据(就是MQTT规定的协议)链接MQTT

 

首先第一个字节是

0x10

算啦仍是直接一张图搞定

用TCP链接上之后,而后用TCP发上面的指令,,,就链接上MQTT了 .....

 而后测试一下把........................................

而后就不说了,也不想说了,你们本身看协议把,,,,由于让本身感受MQTT在我心中的地位大大的受到了..........唉,,,,感受本身讲出来的东西确实感受居然的如此的简单......

只要弄透了,本身写协议就好啦,.....我本身去写协议去,估计写的变量少一点,51单片机就能够....

最后说一下若是是4版本的MQTT

而后今天写好了单片机程序,用本身写的MQTT封装的协议,在8266做为TCP客户端的基础上,链接了个人云端的MQTT服务器,而后用调试助手测试了远程通讯,代码不多,力求能够直接移植到51单片机上

#define MQTTCONFIG_C_
#include "include.h"

unsigned char MqttSendData[70]={0};

/**
* @brief  链接服务器的打包函数
* @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;    //剩余长度(不包括固定头部)
    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  60S心跳包  
    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  MQTT订阅/取消订阅数据打包函数
* @param  SendData 
* @param  topic                主题 
* @param  qos         消息等级 
* @param  whether     订阅/取消订阅请求包
* @retval 
* @example 
**/
int MqttSubscribeTopic(char *topic,u8 qos,u8 whether)
{    
    int topiclen = strlen(topic);
    int i=0,index = 0;
  
    if(whether)
        MqttSendData[index++] = 0x82;                        //0x82 //消息类型和标志 SUBSCRIBE 订阅
    else
        MqttSendData[index++] = 0xA2;                        //0xA2 取消订阅
    MqttSendData[index++] = topiclen + 5;                //剩余长度(不包括固定头部)
    MqttSendData[index++] = 0;                          //消息标识符,高位
    MqttSendData[index++] = 0x01;                    //消息标识符,低位
    MqttSendData[index++] = (0xff00&topiclen)>>8;    //主题长度(高位在前,低位在后)
    MqttSendData[index++] = 0xff&topiclen;              //主题长度 
    
    for (i = 0;i < topiclen; i++)
    {
        MqttSendData[index + i] = topic[i];
    }
    index = index + topiclen;
    
    if(whether)
    {
        MqttSendData[index] = qos;//QoS级别
        index++;
    }
    return index;
}


/**
* @brief  MQTT发布数据打包函数
* @param  mqtt_message 
* @param  topic                主题 
* @param  qos         消息等级 
* @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  

  
    if(qos)
        MqttSendData[index++] = 2 + topic_length + 2 + message_length;//数据长度
    else
        MqttSendData[index++] = 2 + topic_length + message_length;   // Remaining length  

  
    MqttSendData[index++] = (0xff00&topic_length)>>8;//主题长度
    MqttSendData[index++] = 0xff&topic_length;
         
    for(i = 0; i < topic_length; i++)
    {
        MqttSendData[index + i] = topic[i];//拷贝主题
    }
    index += topic_length;
        
    if(qos)
    {
        MqttSendData[index++] = (0xff00&id)>>8;
        MqttSendData[index++] = 0xff&id;
        id++;
    }
  
    for(i = 0; i < message_length; i++)
    {
        MqttSendData[index + i] = message[i];//拷贝数据
    }
    index += message_length;
        
    return index;
}
#ifndef MQTTCONFIG_H_
#define MQTTCONFIG_H_
#include <stm32f10x.h>
#ifndef MQTTCONFIG_C_//若是没有定义  _TIME_C_
#define MQTTCONFIG_C_ extern
#else
#define MQTTCONFIG_C_
#endif

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);

#endif

 

文章转载自https://www.cnblogs.com/yangfengwu/p/9124299.html,若有侵权请联系我删除

相关文章
相关标签/搜索