Solved
Sending AT commands to GSM via STM32, Answer with characters omitted
I'm developing a system with a GMS module, where AT commands are sent to the module via an STM32L0.
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[500];
uint8_t buffer[500] = { 0 };
uint8_t ATisOK = 0;
sprintf(ATcommand, "%s\r\n", command);
DEBUG_PRINT("Sending AT Command: ");
DEBUG_PRINT(ATcommand);
while (!ATisOK) {
HAL_UART_Transmit(&huart2, (uint8_t*) ATcommand, strlen(ATcommand),
2000);
HAL_UART_Receive(&huart2, buffer, sizeof(buffer) - 1, 300);
buffer[sizeof(buffer) - 1] = '\0';
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[500];
uint8_t buffer[300] = { 0 };
char *returnBuffer = malloc(300 * sizeof(char));
if (returnBuffer == NULL) {
return NULL;
}
sprintf(ATcommand, "%s\r\n", 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);
char command[500];
snprintf(command, sizeof(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);
snprintf(command, sizeof(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.
*/
}SERIAL LOG:
19:37:54.671 -> Serial2: Send AT: 19:37:54.671 -> Sending AT Command: AT 19:37:54.671 -> GSM: AT 19:37:54.671 -> OK 19:37:54.671 -> 19:37:56.282 -> Serial2: GSM: AT 19:37:56.282 -> OK 19:37:56.282 -> 19:37:57.878 -> Serial2: GSM: AT+CPIN? 19:37:57.878 -> +CPIN: READY 19:37:57.878 -> 19:37:57.878 -> OK 19:37:57.878 -> 19:37:59.455 -> Serial2: GSM: AT+CREG? 19:37:59.455 -> +CREG: 0,1 19:37:59.455 -> 19:37:59.455 -> OK 19:37:59.455 -> 19:38:01.060 -> Serial2: GSM: AT+CGATT? 19:38:01.060 -> +CGATT: 1 19:38:01.060 -> 19:38:01.060 -> OK 19:38:01.060 -> 19:38:04.242 -> Serial2: GSM: AT+CSQ 19:38:04.242 -> +CSQ: 18,0 19:38:04.242 -> 19:38:04.242 -> OK 19:38:04.242 -> Sending AT Command: AT+CGDCONT=1,"IP","zap.vivo.com.br" 19:38:04.287 -> GSM: AT+CGDCONT=1,"IP","zap.vivo.com.br" 19:38:04.287 -> OK 19:38:04.287 -> 19:38:06.839 -> Serial2: Sending AT Command: AT+CGATT=1 19:38:06.839 -> GSM: AT+CGATT=1 19:38:06.885 -> OK 19:38:06.885 -> 19:38:11.069 -> Serial2: GSM: AT+CSTT? 19:38:11.069 -> ERROR 19:38:11.069 -> Sending AT Command: AT+CSTT="zap.vivo.com.br","vivo","vivo" 19:38:11.069 -> GSM: A+CSTT="zap.vivo.com.br","vivo","vivo" 19:38:11.069 -> ERROR 19:38:11.069 -> 19:38:12.670 -> Serial2: GSM: A+CSTT="zap.vivo.com.br","vivo","vivo" 19:38:12.670 -> 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.
Apparently with two characters in the password the command is sent correctly, but with more than two the AT loses its T, I tested it this way:
19:28:09.621 -> Serial2: GSM: AT+CSTT=“zap.vivo.com.br”,“vivo”,"dd”
19:28:09.621 -> ERROR
Note that AT+CSTT is correct.
Doing some more tests, I realized that the error is in the part of sending and receiving the data by Serial, since even the DEBUG_PRINT that occurs before the while click to send the command is correct.
