SIM800L + Arduino TCP断开连接问题

时间:2019-07-02 17:26:17

标签: c++ sockets tcp arduino at-command

好的,所以我目前正在使用arduino-uno将AT命令发送到SIM800l模块。我在计算机上运行基本的TCP套接字服务器,SIM800L最初可以正常连接到该服务器并发送消息。在我的串行监视器上看似随机的一段时间之后,随后显示“ NUL”,此时我无法发送消息。

(因此它最初正在工作,但是似乎突然停止了中间过程)

bool connectionProcedure()
{
  bool result = false;
  if(sendATcommand("AT+CIPSHUT", "SHUT OK", 200) == 1)
  {
    Serial.println("Proceed: AT+CIPMUX=0");
    if(sendATcommand("AT+CIPMUX=0", "OK", 1000)  == 1 )
    {
      Serial.println("Proceed: AT+CGATT=1");
      if(sendATcommand("AT+CGATT=1", "OK", 1000)  == 1 )
      {
        Serial.println("Proceed: AT+CSTT=\"gifgaff.com\",\"gifgaff\"");
        if(sendATcommand("AT+CSTT=\"gifgaff.com\",\"gifgaff\"", "OK", 1000)  == 1 )
        {
          Serial.println("Proceed: AT+CIICR");
          if(sendATcommand("AT+CIICR", "OK", 60000)  == 1 )
          {
            Serial.println("Proceed: AT+CIFSR");
            if(sendATcommand("AT+CIFSR", ".", 5000)  == 1 )
            {
              Serial.println("Process Success!");
              result = true;
            }
            else
            {
              Serial.println("Error: AT+CIFSR");
            }
          }
          else
          {
            Serial.println("Error: AT+CIICR");
          }
        }
        else
        {
          Serial.println("Error: AT+CSTT=\"gifgaff.com\",\"gifgaff\"");
        }
      }
      else
      {
        Serial.println("Error: AT+CGATT=1");
      }
    }
    else
    {
      Serial.println("Error: AT+CIPMUX=0");
    }
  }
  else
  {
   Serial.println("Error: CIPSHUT");
  }
  return result;
}
void awaitGSMConnection()
{
  while( sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000) == 0 );
}
void initTCPConnection()
{
    unsigned long previous;
    unsigned int timeout = 10000;

    Serial.println("Starting TCP Connection!");
    if(sendATcommand("AT+CIPSTART=\"TCP\"," + tcpIp + "," + tcpPort, "OK", 10000) == 1)
    {
        Serial.println("Connection Success: Checking for stable connection before handshake!");

        String expected = "CLOSED";
        uint8_t x=0,  answer=0;
        char response[100];

        memset(response, '\0', 100);
        while( mySerial.available() > 0) mySerial.read();
        x = 0;
        previous = millis();

         // Loop waits for the response from SIM800L
        do {
            if(mySerial.available() != 0) {
                response[x] = mySerial.read();
                x++;
                // check if the desired answer 1  is in the response of the module
                if (strstr(response, expected.c_str()) != NULL)
                {
                    answer = 1;
                }
            }
        }
        while((answer == 0) && ((millis() - previous) < timeout));

        if(answer == 0)
        {
            Serial.println("No Apparent ISSUE sending handshake!");
            if(sendATcommand("AT+CIPSEND", ">", 4000) == 1)
            {
                Serial.println("SEND-HANDSHAKE");
                sendMessage("Handshake");
                //sendATcommand("AT+CIPCLOSE","OK",2000);
            }
            else
            {
                Serial.println("Error: AT+CIPSEND");
            }
        }
        else
        {
            Serial.println("Connection test failed: Attempting to RECONNECT!");
        }
    }
    else
    {
        Serial.println("Connection Error!");
    }  
}

串行监视器-输出

这是我正在谈论的“ NUL”,这似乎在连接期间的某个时间完全随机出现。我从服务器发送并接收到随机消息。 Serial Monitor

No Apparent ISSUE sending handshake!
AT+CIPSEND
>
SEND-HANDSHAKE
 Handshake␚
SEND OK
HELLO
AWESOME
COOL
INSANE
STILL WORKING!!!!
␀

提前谢谢!

0 个答案:

没有答案