• GPRS模块用TCP实现MQTT协议(基于SIM900A)


    mqtt部分:

    int strlen(char *str)  
    {  
        int len = 0;  
        while (*str != '') {  
            len++;  
            str++;  
        }  
        return len;  
    }  
      
    // **********************************  
    // *                                *  
    // * 根据主机地址和端口号连接服务器 *  
    // *                                *  
    // **********************************  
      
      
    void tcp_connect(char *host,int port)  
    {  
       uart_send_str("AT
    ");  
       uart_send_str("AT+CPIN? 
    ");  
       seelp(500);  
       uart_send_str("AT+CREG? 
    ");  
       seelp(500);  
       uart_send_str("AT+CGATT? 
    ");  
       seelp(500);  
       uart_send_str("AT+CSTT="CMNET" 
    ");  
       seelp(500);  
       uart_send_str("AT+CIICR 
    ");  
       seelp(1500);  
       uart_send_str("AT+CIFSR 
    ");  
       seelp(1000);  
       uart_send_str("AT+CIPSTART="TCP"\,"23.106.139.167"\,"1883" 
    "); //连接TCP Server  8081 1883  
    }  
      
      
    // *************************  
    // *                       *  
    // *  构建MQTT连接包       *  
    // *                       *  
    // *************************  
      
    int baseIndex;  
    int mqtt_connect_message(unsigned char *mqtt_message,char *client_id,char *username,char *password)  
    {   
        char i = 0;    
        char client_id_length = strlen(client_id);    
        int username_length = strlen(username);    
        int password_length = strlen(password);    
        int packetLen = 12 + 2 + client_id_length + 2 + username_length + 2 + password_length;    
            
        mqtt_message[0] = 16;                      // MQTT Message Type CONNECT    
        mqtt_message[1] = packetLen%256;    
        baseIndex = 2;    
        if( packetLen >127 ){//    
            mqtt_message[2] = 1;//packetLen/127;    
            baseIndex = 3;    
        }    
            
            
        mqtt_message[baseIndex] = 0;                       // Protocol Name Length MSB    
        mqtt_message[baseIndex+1] = 6;                       // Protocol Name Length LSB    
        mqtt_message[baseIndex+2] = 77;                      // ASCII Code for M    
        mqtt_message[baseIndex+3] = 81;                      // ASCII Code for Q    
        mqtt_message[baseIndex+4] = 73;                      // ASCII Code for I    
        mqtt_message[baseIndex+5] = 115;                     // ASCII Code for s    
        mqtt_message[baseIndex+6] = 100;                     // ASCII Code for d    
        mqtt_message[baseIndex+7] = 112;                     // ASCII Code for p    
        mqtt_message[baseIndex+8] = 3;                      // MQTT Protocol version = 3    
        mqtt_message[baseIndex+9] = 194;                   // conn flags    
        mqtt_message[baseIndex+10] = 0;                      // Keep-alive Time Length MSB    
        mqtt_message[baseIndex+11] = 60;                     // Keep-alive Time Length LSB    
            
        mqtt_message[baseIndex+12] = 0;                      // Client ID length MSB    
        mqtt_message[baseIndex+13] = client_id_length;       // Client ID length LSB    
            
        baseIndex += 14;    
        // Client ID    
        for(i = 0; i < client_id_length; i++){    
            mqtt_message[baseIndex + i] = client_id[i];    
        }    
            
        baseIndex = baseIndex+client_id_length;    
            
        //username    
        mqtt_message[baseIndex] = 0;                      //username length MSB    
        mqtt_message[baseIndex+1] = username_length;       //username length LSB    
        baseIndex = baseIndex+2;    
        for(i = 0; i < username_length ; i++){    
            mqtt_message[baseIndex + i] = username[i];    
        }    
            
        baseIndex = baseIndex + username_length;    
            
        //password    
        mqtt_message[baseIndex] = 0;                      //password length MSB    
        mqtt_message[baseIndex+1] = password_length;       //password length LSB    
        baseIndex = baseIndex + 2;    
        for(i = 0; i < password_length ; i++){    
            mqtt_message[baseIndex + i] = password[i];    
        }    
            
        baseIndex += password_length;    
        
        return baseIndex;    
    }  
      
      
    int mqtt_publish_message(unsigned char *mqtt_message, char * topic, char * message) {    
            
        //mqtt_message = 0;    
            
        unsigned char i = 0;    
        unsigned char topic_length = strlen(topic);    
        unsigned char message_length = strlen(message);    
            
        mqtt_message[0] = 48;                                  // MQTT Message Type CONNECT    
        mqtt_message[1] = 2 + topic_length + message_length;   // Remaining length    
        mqtt_message[2] = 0;                                   // MQTT Message Type CONNECT    
        mqtt_message[3] = topic_length;                        // MQTT Message Type CONNECT    
            
        // Topic    
        for(i = 0; i < topic_length; i++){    
            mqtt_message[4 + i] = topic[i];    
        }    
            
        // Message    
        for(i = 0; i < message_length; i++){    
            mqtt_message[4 + topic_length + i] = message[i];    
        }    
            
        return 4 + topic_length + message_length;    
    }    
      
      
    int subindex = 0;  
    int mqtt_subscribe_message(unsigned char *mqtt_message,unsigned char *topic)    
    {    
        unsigned char topic_len = strlen(topic);    
        mqtt_message[0] = 130;    
        mqtt_message[1] = topic_len+5;    
        mqtt_message[2] = 0;    
        mqtt_message[3] = 1;    
        mqtt_message[4] = 0;    
        mqtt_message[5] = topic_len;    
      
        for (subindex=0; subindex<topic_len; subindex++) {    
            mqtt_message[6+subindex] = topic[subindex];    
        }    
        mqtt_message[topic_len+6+1] = 0;    
        return topic_len+7;    
    }    
    

      

    mqtt部分:

     

     

    [cpp] view plain copy
     
    1. int strlen(char *str)  
    2. {  
    3.     int len = 0;  
    4.     while (*str != '') {  
    5.         len++;  
    6.         str++;  
    7.     }  
    8.     return len;  
    9. }  
    10.   
    11. // **********************************  
    12. // *                                *  
    13. // * 根据主机地址和端口号连接服务器 *  
    14. // *                                *  
    15. // **********************************  
    16.   
    17.   
    18. void tcp_connect(char *host,int port)  
    19. {  
    20.    uart_send_str("AT ");  
    21.    uart_send_str("AT+CPIN?  ");  
    22.    seelp(500);  
    23.    uart_send_str("AT+CREG?  ");  
    24.    seelp(500);  
    25.    uart_send_str("AT+CGATT?  ");  
    26.    seelp(500);  
    27.    uart_send_str("AT+CSTT="CMNET"  ");  
    28.    seelp(500);  
    29.    uart_send_str("AT+CIICR  ");  
    30.    seelp(1500);  
    31.    uart_send_str("AT+CIFSR  ");  
    32.    seelp(1000);  
    33.    uart_send_str("AT+CIPSTART="TCP"\,"23.106.139.167"\,"1883"  "); //连接TCP Server  8081 1883  
    34. }  
    35.   
    36.   
    37. // *************************  
    38. // *                       *  
    39. // *  构建MQTT连接包       *  
    40. // *                       *  
    41. // *************************  
    42.   
    43. int baseIndex;  
    44. int mqtt_connect_message(unsigned char *mqtt_message,char *client_id,char *username,char *password)  
    45. {   
    46.     char i = 0;    
    47.     char client_id_length = strlen(client_id);    
    48.     int username_length = strlen(username);    
    49.     int password_length = strlen(password);    
    50.     int packetLen = 12 + 2 + client_id_length + 2 + username_length + 2 + password_length;    
    51.         
    52.     mqtt_message[0] = 16;                      // MQTT Message Type CONNECT    
    53.     mqtt_message[1] = packetLen%256;    
    54.     baseIndex = 2;    
    55.     if( packetLen >127 ){//    
    56.         mqtt_message[2] = 1;//packetLen/127;    
    57.         baseIndex = 3;    
    58.     }    
    59.         
    60.         
    61.     mqtt_message[baseIndex] = 0;                       // Protocol Name Length MSB    
    62.     mqtt_message[baseIndex+1] = 6;                       // Protocol Name Length LSB    
    63.     mqtt_message[baseIndex+2] = 77;                      // ASCII Code for M    
    64.     mqtt_message[baseIndex+3] = 81;                      // ASCII Code for Q    
    65.     mqtt_message[baseIndex+4] = 73;                      // ASCII Code for I    
    66.     mqtt_message[baseIndex+5] = 115;                     // ASCII Code for s    
    67.     mqtt_message[baseIndex+6] = 100;                     // ASCII Code for d    
    68.     mqtt_message[baseIndex+7] = 112;                     // ASCII Code for p    
    69.     mqtt_message[baseIndex+8] = 3;                      // MQTT Protocol version = 3    
    70.     mqtt_message[baseIndex+9] = 194;                   // conn flags    
    71.     mqtt_message[baseIndex+10] = 0;                      // Keep-alive Time Length MSB    
    72.     mqtt_message[baseIndex+11] = 60;                     // Keep-alive Time Length LSB    
    73.         
    74.     mqtt_message[baseIndex+12] = 0;                      // Client ID length MSB    
    75.     mqtt_message[baseIndex+13] = client_id_length;       // Client ID length LSB    
    76.         
    77.     baseIndex += 14;    
    78.     // Client ID    
    79.     for(i = 0; i < client_id_length; i++){    
    80.         mqtt_message[baseIndex + i] = client_id[i];    
    81.     }    
    82.         
    83.     baseIndex = baseIndex+client_id_length;    
    84.         
    85.     //username    
    86.     mqtt_message[baseIndex] = 0;                      //username length MSB    
    87.     mqtt_message[baseIndex+1] = username_length;       //username length LSB    
    88.     baseIndex = baseIndex+2;    
    89.     for(i = 0; i < username_length ; i++){    
    90.         mqtt_message[baseIndex + i] = username[i];    
    91.     }    
    92.         
    93.     baseIndex = baseIndex + username_length;    
    94.         
    95.     //password    
    96.     mqtt_message[baseIndex] = 0;                      //password length MSB    
    97.     mqtt_message[baseIndex+1] = password_length;       //password length LSB    
    98.     baseIndex = baseIndex + 2;    
    99.     for(i = 0; i < password_length ; i++){    
    100.         mqtt_message[baseIndex + i] = password[i];    
    101.     }    
    102.         
    103.     baseIndex += password_length;    
    104.     
    105.     return baseIndex;    
    106. }  
    107.   
    108.   
    109. int mqtt_publish_message(unsigned char *mqtt_message, char * topic, char * message) {    
    110.         
    111.     //mqtt_message = 0;    
    112.         
    113.     unsigned char i = 0;    
    114.     unsigned char topic_length = strlen(topic);    
    115.     unsigned char message_length = strlen(message);    
    116.         
    117.     mqtt_message[0] = 48;                                  // MQTT Message Type CONNECT    
    118.     mqtt_message[1] = 2 + topic_length + message_length;   // Remaining length    
    119.     mqtt_message[2] = 0;                                   // MQTT Message Type CONNECT    
    120.     mqtt_message[3] = topic_length;                        // MQTT Message Type CONNECT    
    121.         
    122.     // Topic    
    123.     for(i = 0; i < topic_length; i++){    
    124.         mqtt_message[4 + i] = topic[i];    
    125.     }    
    126.         
    127.     // Message    
    128.     for(i = 0; i < message_length; i++){    
    129.         mqtt_message[4 + topic_length + i] = message[i];    
    130.     }    
    131.         
    132.     return 4 + topic_length + message_length;    
    133. }    
    134.   
    135.   
    136. int subindex = 0;  
    137. int mqtt_subscribe_message(unsigned char *mqtt_message,unsigned char *topic)    
    138. {    
    139.     unsigned char topic_len = strlen(topic);    
    140.     mqtt_message[0] = 130;    
    141.     mqtt_message[1] = topic_len+5;    
    142.     mqtt_message[2] = 0;    
    143.     mqtt_message[3] = 1;    
    144.     mqtt_message[4] = 0;    
    145.     mqtt_message[5] = topic_len;    
    146.   
    147.     for (subindex=0; subindex<topic_len; subindex++) {    
    148.         mqtt_message[6+subindex] = topic[subindex];    
    149.     }    
    150.     mqtt_message[topic_len+6+1] = 0;    
    151.     return topic_len+7;    
    152. }    

     

  • 相关阅读:
    table表框去掉相邻的间隔
    各种日期格式化返回
    校验金额、大小写字母、大写字母、合法uri、email
    vue js校验金钱、数字
    vue-router 动态添加 路由
    可视化-echarts流向图制作
    HTTP状态码
    二分查找
    编程语言的变量为啥不能是数字开头
    python位运算
  • 原文地址:https://www.cnblogs.com/jiangzhaowei/p/8777265.html
Copyright © 2020-2023  润新知