构建MQTT客户端以在SIM800L调制解调器上工作时遇到的问题

时间:2018-10-04 10:32:29

标签: embedded mqtt iot

我正在尝试构建一个可以在GPRS调制解调器SIM800L上运行的MQTT客户端库。

下面是我从SIM800L打开TCP连接的代码:

void initModem(){
    int startTime=0;
    switch(modemStatus){
        case 0:
            Serial1.print("AT\r\n");
            delay(2000);
            modemStatus = 1;
            break;
        case 1:
            Serial1.print("AT+CFUN?\r\n");
            delay(2000);
            modemStatus = 2;
            break;
        case 2:
            Serial1.print("AT+CSTT=\"portalnmms\",\"\",\"\"\r\n");
            delay(2000);
            modemStatus = 3;
            break;
        case 3:
            Serial1.print("AT+CIICR\r\n");
            delay(2000);
            modemStatus = 4;
            break;
        case 4:            
            Serial1.print("AT+CIFSR\r\n");
            delay(2000);
            modemStatus = 5;
            break;
        case 5:
            //Serial1.print("AT+CIPSTART=\"TCP\",\"broker.mqttdashboard.com\",\"1883\"\r\n");                                    
            Serial1.print("AT+CIPSTART=\"TCP\",\"broker.hivemq.com\",\"1883\"\r\n");                                    

            startTime = millis();
            while(1){
                if((millis()-startTime)>8000) break;

                if (Serial1.available()){
                    Serial.print((char)Serial1.read());      
                }
            }            
            modemStatus = 6;  
            break;
        case 6:

            Serial.println("");
            Serial.println("Sending Connect Packet");

            static const char connectPacket[14] = {
                                                    0x10,   //Control Byte - 4Bits(MSB) for Control Type & 4Bits(LSB) Control Flag  
                                                    12,     //Length of Packet
                                                    0,      //Protocol Name Length (MSB)  
                                                    6,      //Protocol Name Length (LSB)
                                                    'M',    // Protocol Name 1st Byte
                                                    'Q',    // Protocol Name 2nd Byte
                                                    'I',    // Protocol Name 3rd Byte                                                    
                                                    's',    // Protocol Name 4th Byte
                                                    'd',    // Protocol Name 5th Byte
                                                    'p',    // Protocol Name 6th Byte
                                                    3,      // Protocol Level/Version (3.1.1 in this case)
                                                    0x02,   // Connect Flags 
                                                    0x00,   // Keep Alive in Seconds (MSB) 
                                                    60,     // Keep Alive in Seconds (LSB)
                                                    //0,      // Client Identifier Length (MSB) 
                                                    //1,      // Client Identifier Length (LSB)
                                                    //'M', 


                                                   };                         

            Serial1.write(connectPacket);   
            Serial1.flush();                     
            startTime = millis();
            while(1){
                if((millis()-startTime)>5000) break;

                if (Serial1.available()){
                    Serial.println(Serial1.read(),BIN);      
                }
            }

            modemStatus=7;
            break;
        case 7: 
            //PUBLISH A MESSAGE
            static const char publishPacket = 

            break;      
        default:
            Serial.println("Invalid Modem Status");
            break;                     
    }
}

上面的代码通过TCP发送一个数据包,下面是数据包结构:

static const char connectPacket[14] = {
                                                    0x10,   //Control Byte - 4Bits(MSB) for Control Type & 4Bits(LSB) Control Flag  
                                                    12,     //Length of Packet
                                                    0,      //Protocol Name Length (MSB)  
                                                    6,      //Protocol Name Length (LSB)
                                                    'M',    // Protocol Name 1st Byte
                                                    'Q',    // Protocol Name 2nd Byte
                                                    'I',    // Protocol Name 3rd Byte                                                    
                                                    's',    // Protocol Name 4th Byte
                                                    'd',    // Protocol Name 5th Byte
                                                    'p',    // Protocol Name 6th Byte
                                                    3,      // Protocol Level/Version (3.1.1 in this case)
                                                    0x02,   // Connect Flags 
                                                    0x00,   // Keep Alive in Seconds (MSB) 
                                                    60,     // Keep Alive in Seconds (LSB)
                                                    //0,      // Client Identifier Length (MSB) 
                                                    //1,      // Client Identifier Length (LSB)
                                                    //'M', 


                                                   };  

通过TCP发送此数据包后: 我得到以下二进制响应:

10000
1100
10000
1100

此处的MSB没有显示10,这是CONNECTACK数据包的控制类型。

此外,第二个(1100)和第四个字节(1100)取决于长度,并且当我更改CONNECT数据包的长度时也会发生变化。

我想知道我在这里做错了吗?

请帮助。

0 个答案:

没有答案