如今物聯網流行的就是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,若有侵權請聯繫我刪除