I’m developing a system with a GMS module, where AT commands are sent to the module via an STM32.
In this case, I’m having problems with the AT+CSTT command, which was giving me an ERROR. After a lot of thought, I realized that the error was due to the fact that the command was going to the module with the T for AT missing (A+CSTT).
I’m working with the commands like this:
void sendAT(char command[], char answer[], int waitForAnswer) {
char ATcommand[400];
uint8_t buffer[300] = { 0 };
uint8_t ATisOK = 0;
while (!ATisOK) {
sprintf(ATcommand, "%srn", command);
HAL_UART_Transmit(&huart2, (uint8_t*) ATcommand, strlen(ATcommand),
1000);
HAL_UART_Receive(&huart2, buffer, sizeof(buffer) - 1, 100);
buffer[sizeof(buffer) - 1] = '';
HAL_Delay(500);
ATisOK = !waitForAnswer;
if (strstr((char*) buffer, "OK")) {
ATisOK = 1;
}
DEBUG_PRINT("GSM: ");
DEBUG_PRINT((char* ) buffer);
HAL_Delay(1000);
memset(buffer, 0, sizeof(buffer));
}
}
char* sendATWithReturn(char command[]) {
char ATcommand[400];
uint8_t buffer[300] = { 0 };
char *returnBuffer = malloc(300 * sizeof(char));
if (returnBuffer == NULL) {
return NULL;
}
sprintf(ATcommand, "%srn", command);
HAL_UART_Transmit(&huart2, (uint8_t*) ATcommand, strlen(ATcommand), 1000);
HAL_UART_Receive(&huart2, buffer, 300, 100);
HAL_Delay(500);
DEBUG_PRINT("GSM: ");
DEBUG_PRINT((char* ) buffer);
strncpy(returnBuffer, (char*) buffer, 300);
return returnBuffer;
}
And my command sequence goes like this:
void modemStart(char *apn, char *user, char *pwd) {
sendATWithReturn("AT");
HAL_Delay(1000);
sendATWithReturn("AT+CPIN?");
HAL_Delay(1000);
sendATWithReturn("AT+CREG?");
HAL_Delay(1000);
sendATWithReturn("AT+CGATT?");
HAL_Delay(1000);
sendATWithReturn("AT+CSQ");
HAL_Delay(1000);
sendATWithReturn("AT+CIPSHUT");
HAL_Delay(1000);
sendATWithReturn("AT+CIPSTATUS");
HAL_Delay(1000);
char command[500];
sprintf(command, "AT+CGDCONT=1,"IP","%s"", apn);
sendAT(command, "OK", 1);
memset(command, 0, sizeof(command));
HAL_Delay(1000);
sendAT("AT+CGATT=1", "OK", 0);
HAL_Delay(1000);
sendATWithReturn("AT+CSTT?");
HAL_Delay(1000);
sprintf(command, "AT+CSTT="%s","%s","%s"", apn, user, pwd);
sendAT(command, "OK", 1);
memset(command, 0, sizeof(command));
HAL_Delay(1000);
sendATWithReturn("AT+CIICR");
HAL_Delay(1000);
sendATWithReturn("AT+CIFSR");
HAL_Delay(1000);
sendATWithReturn("AT+CIPMUX=1");
/*
AT+CPIN?: Verifica o status do PIN do SIM.
AT+CREG?: Verifica o status de registro na rede.
Sequência de Inicialização:
AT+NETCLOSE: Fecha qualquer conexão de rede existente.
AT+CGATT=0: Desativa o GPRS.
AT+CGATT=1: Ativa o GPRS.
AT+CGDCONT: Define o contexto PDP.
AT+CSTT: Define o APN, usuário e senha.
AT+CIICR: Inicia a conexão sem fio.
AT+CIFSR: Obtém o endereço IP.
AT+CIPMUX=1: Habilita o modo de múltiplas conexões.
*/
}
LOG:
15:24:07.273 -> Serial2: GSM: AT+CREG?
15:24:07.273 -> +CREG: 0,1
15:24:07.273 ->
15:24:07.273 -> OK
15:24:07.273 ->
15:24:08.860 -> Serial2: GSM: AT+CGATT?
15:24:08.860 -> +CGATT: 1
15:24:08.860 ->
15:24:08.860 -> OK
15:24:08.860 ->
15:24:10.493 -> Serial2: GSM: AT+CSQ
15:24:10.493 -> +CSQ: 18,99
15:24:10.493 ->
15:24:10.493 -> OK
15:24:10.493 ->
15:24:12.081 -> Serial2: GSM: AT+CIPSHUT
15:24:12.081 -> ERROR
15:24:12.081 ->
15:24:13.648 -> Serial2: GSM: AT+CIPSTATUS
15:24:13.648 -> ERROR
15:24:13.648 ->
15:24:15.279 -> Serial2: GSM: AT+CGDCONT=1,"IP","zap.vivo.com.br"
15:24:15.279 -> OK
15:24:15.279 ->
15:24:17.877 -> Serial2: GSM: AT+CGATT=1
15:24:17.877 -> OK
15:24:17.877 ->
15:24:20.433 -> Serial2: GSM: AT+CSTT?
15:24:20.433 -> ERROR
15:24:20.433 ->
15:24:22.067 -> Serial2: GSM: A+CSTT="zap.vivo.com.br","vivo","vivo"
15:24:22.067 -> ERROR
In other words, the error is when the command is sent to the module, more precisely in the byte buffer conversion line, but I’m not sure how to fix it.