10-MQTT介绍

 https://www.cnblogs.com/yangfengwu/p/9953920.html

 

 

 

看到这个项目第一想法肯定需要一个服务器,所有的wifi设备和手机都去连接这个服务器,然后服务器进行信息的中转

这个服务器呢第一种方式是自己开发

就变成了

其实MQTT的基本功能就是上面的

还要说一点MQTT实质上就是个TCP服务器

https://www.cnblogs.com/yangfengwu/p/9124299.html   

 

然后在TCP的基础上封装了一套协议

只要大家的设备支持TCP,也就可以自己写封装函数,然后实现MQTT功能

现在说一下两个设备是如何通信的

 

假设设备1   先连接MQTT服务器  然后告诉MQTT服务器它接收标志是1111的消息

假设设备2   先连接MQTT服务器  然后告诉MQTT服务器它接收标志是2222的消息

 

然后设备1 , 发送消息的时候 , 消息的开头写上标志 2222 然后后面跟上数据,  服务器收到以后就转发给接收标志是2222的设备   ,这样设备2就接收到了设备1的数据

 

然后设备2 , 发送消息的时候 , 消息的开头写上标志 1111 然后后面跟上数据,  服务器收到以后就转发给接收标志是1111的设备   ,这样设备1就接收到了设备2的数据

 

其实具体的协议呢看下面,  这是我用单片机写的MQTT封包,就是在网络芯片实现TCP的基础上让芯片连接MQTT服务器,然后实现通信

复制代码
/**
* @brief  连接服务器的打包函数
* @param
* @retval
* @example
**/
char 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  

<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span>(i = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>; i < ClientIDLen; i++<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;">)
{
    MqttSendData[Index </span>+ i] =<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;"> ClientID[i];
}
Index </span>= Index +<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;"> ClientIDLen;

</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span>(UsernameLen > <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span><span style="color: rgb(0, 0, 0); line-height: 1.5 !important;">)
{
    MqttSendData[Index</span>++] = (<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0xff00</span>&UsernameLen)>><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">8</span>;<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">username length MSB    </span>
    MqttSendData[Index++] = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0xff</span>&UsernameLen;    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">username length LSB    </span>
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span>(i = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>; i < UsernameLen ; i++<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;">)
    {
        MqttSendData[Index </span>+ i] =<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;"> Username[i];
    }
    Index </span>= Index +<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;"> UsernameLen;
}

</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span>(PasswordLen > <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span><span style="color: rgb(0, 0, 0); line-height: 1.5 !important;">)
{
    MqttSendData[Index</span>++] = (<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0xff00</span>&PasswordLen)>><span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">8</span>;<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">password length MSB    </span>
    MqttSendData[Index++] = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0xff</span>&PasswordLen;    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">password length LSB    </span>
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span>(i = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>; i < PasswordLen ; i++<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;">)
    {
        MqttSendData[Index </span>+ i] =<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;"> Password[i];
    }
    Index </span>= Index +<span style="color: rgb(0, 0, 0); line-height: 1.5 !important;"> PasswordLen;
}
</span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span><span style="color: rgb(0, 0, 0); line-height: 1.5 !important;"> Index;

}

/

  • @brief MQTT订阅/取消订阅数据打包函数
  • @param SendData
  • @param topic 主题
  • @param qos 消息等级
  • @param whether 订阅/取消订阅请求包
  • @retval
  • @example
  • */
    char MqttSubscribeTopic(char *topic,char qos,char whether)
    {
    Index
    = 0;
    i
    = 0; if(whether)
       MqttSendData[Index</span>++] = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0x82</span>;                        <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">0x82 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">消息类型和标志 SUBSCRIBE 订阅</span>
    
    else
       MqttSendData[Index</span>++] = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0xA2</span>;                        <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">0xA2 取消订阅</span>
    
    MqttSendData[Index++] = strlen(topic) + 5; //剩余长度(不包括固定头部) MqttSendData[Index++] = 0; //消息标识符,高位 MqttSendData[Index++] = 0x01; //消息标识符,低位 MqttSendData[Index++] = (0xff00&strlen(topic))>>8; //主题长度(高位在前,低位在后) MqttSendData[Index++] = 0xff&strlen(topic); //主题长度 for (i = 0;i < strlen(topic); i++) { MqttSendData[Index + i] = topic[i]; } Index = Index + strlen(topic); if(whether) { MqttSendData[Index] = qos;//QoS级别 Index++; } return Index; }

/

  • @brief MQTT发布数据打包函数

  • @param mqtt_message

  • @param topic 主题

  • @param qos 消息等级

  • @retval

  • @example

  • */
    char MqttPublishData(char * topic, char * message,char length, char qos)
    {
    char topic_length = strlen(topic);
    char message_length = length;
    char i,index=0;
    static char id=0;

    MqttSendData[index++] = 0x30; // MQTT Message Type PUBLISH

    if(qos)

       MqttSendData[index</span>++] = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span> + topic_length + <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span> + message_length;<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">数据长度</span>
    

    else

       MqttSendData[index</span>++] = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span> + topic_length + message_length;   <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> Remaining length  </span>
    
    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; }
复制代码

 

https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.8a931debd5LqD6&id=582060294154

当然啦,咱现在用的WIFI和GPRS里面已经写好了这种组合包,咱直接调用API就可以了

其实大家不需要去研究这个协议,最起码现在不需要,我只是让大家稍微了解下MQTT,后期如果真的需要研究了,再研究

也不迟,现在就是先学会用

 

资料链接

 

链接:https://pan.baidu.com/s/1-SRfsKGQ7rZVvFmp1ObHWw 密码:p9qs

 

基础教程源码链接如果失效,请在淘宝介绍中下载,由于链接还是失效,请联系卖家,谢谢

 

https://item.taobao.com/item.htm?spm=a1z10.1-c-s.w4004-18540610442.6.36a74814ZSaRsu&id=569295486025

 

 

 

 

 https://www.cnblogs.com/yangfengwu/p/9955765.html

上次更新 2021-01-28