Hi everyone, this blog is to send a data to ThingSpeak server using the STM32 microcontroller.
Table of Contents
What are AT commands?
AT commands are commands which are used to control the modems where AT stands for Attention. These commands were derived from Hayes commands which were used by the Hayes smart modems. Every wireless, as well as the dial up modems, require an AT command to interact with a computer machine. These AT commands along with other extended commands also require Hayes command set as a subset.
Requirements:
- STM32F303 Discovery board – 1
- GSM 800/900/M66/M60
- ST-Link Debugger – 1
- FTDI – 1
- Breadboard – 1
- Jumper wires
Usage
The AT commands can be used with GSM module and GPRS MODEMs or phone to access these services and information:
- SMS
- MMS
- Fax
- Voice link and other data over mobile network
- Information and configuration concerning the mobile devices or MODEM and SIM card.
Types of AT Commands:
There are four types of AT commands:
Fig. 1: Image Showing Classification of AT Commands
1) Test commands – used to check whether a command is supported or not by the MODEM.
SYNTAX: AT<command name>=?
For example: ATD=?
2) Read command – used to get mobile phone or MODEM settings for an operation.
SYNTAX: AT<command name>?
For example: AT+CBC?
3) Set commands – used to modify mobile phone or MODEM settings for an operation.
SYNTAX: AT<command name>=value1, value2, …, valueN
Some values in set commands can be optional.
For example: AT+CSCA=”+xxxxxxxxxx”, 120
4) Execution commands – used to carry out an operation.
SYNTAX: AT<command name>=parameter1, parameter2, …, parameterN
The read commands are not available to get value of last parameter assigned in execution commands because parameters of execution commands are not stored.
For example: AT+CMSS=1,”+ 9876543210”, 120
1) AT – This command is used to check communication between the module and the computer.
For example,
AT
OK
The command returns a result code OK if the computer (serial port) and module are connected properly. If any of module or SIM is not working, it would return a result code ERROR.
2) +CMGF – This command is used to set the SMS mode. Either text or PDU mode can be selected by assigning 1 or 0 in the command.
SYNTAX: AT+CMGF=<mode>
0: for PDU mode
1: for text mode
The text mode of SMS is easier to operate but it allows limited features of SMS. The PDU (protocol data unit) allows more access to SMS services but the operator requires bit level knowledge of TPDUs. The headers and body of SMS are accessed in hex format in PDU mode so it allows availing more features.
For example,
AT+CMGF=1
OK
3) +CMGW – This command is used to store message in the SIM.
SYNTAX: AT+CMGW=” Phone number”> Message to be stored Ctrl+z
As one types AT+CMGW and phone number, ‘>’ sign appears on next line where one can type the message. Multiple line messages can be typed in this case. This is why the message is terminated by providing a ‘Ctrl+z’ combination. As Ctrl+z is pressed, the following information response is displayed on the screen.
+CMGW: Number on which message has been stored
4) +CMGS – This command is used to send a SMS message to a phone number.
SYNTAX: AT+CMGS= serial number of message to be send.
As the command AT+CMGS and serial number of message are entered, SMS is sent to the particular SIM.
For example,
AT+CMGS=1
OK
5) ATD – This command is used to dial or call a number.
SYNTAX: ATD<Phone number>;(Enter)
For example,
ATD123456789;
6) ATA – This command is used to answer a call. An incoming call is indicated by a message ‘RING’ which is repeated for every ring of the call. When the call ends ‘NO CARRIER’ is displayed on the screen.
SYNTAX: ATA(Enter)
As ATA followed by enter key is pressed, incoming call is answered.
For example,
RING
RING
ATA
The AT commands for both, GSM module and the mobile phone, are listed below. Some of these commands may not be supported by all the GSM modules available. Also there might be some commands which won’t be supported by some mobile handsets.
List of AT commands:
Testing :
Command | Description |
AT | Checking communication between the module and computer. |
Call control :
Command | Description |
ATA | Answer command |
ATD | Dial command |
ATH | Hang up call |
ATL | Monitor speaker loudness |
ATM | Monitor speaker mode |
ATO | Go on-line |
ATP | Set pulse dial as default |
ATT | Set tone dial as default |
AT+CSTA | Select type of address |
AT+CRC | Cellular result codes |
Data card Control :
Command | Description |
ATI | Identification |
ATS | Select an S-register |
ATZ | Recall stored profile |
AT&F | Restore factory settings |
AT&V | View active configuration |
AT&W | Store parameters in given profile |
AT&Y | Select Set as power up option |
AT+CLCK | Facility lock command |
AT+COLP | Connected line identification presentation |
AT+GCAP | Request complete capabilities list |
AT+GMI | Request manufacturer identification |
AT+GMM | Request model identification |
AT+GMR | Request revision identification |
AT+GSN | Request product serial number identification (IMEI) |
Phone control :
Command | Description |
AT+CBC | Battery charge |
AT+CGMI | Request manufacturer identification |
AT+CGMM | Request model identification |
AT+CGMR | Request revision identification |
AT+CGSN | Request product serial number identification |
AT+CMEE | Report mobile equipment error |
AT+CPAS | Phone activity status |
AT+CPBF | Find phone book entries |
AT+CPBR | Read phone book entry |
AT+CPBS | Select phone book memory storage |
AT+CPBW | Write phone book entry |
AT+CSCS | Select TE character set |
AT+CSQ | Signal quality |
Computer data interface :
Command | Description |
ATE | Command Echo |
ATQ | Result code suppression |
ATV | Define response format |
ATX | Response range selection |
AT&C | Define DCD usage |
AT&D | Define DTR usage |
AT&K | Select flow control |
AT&Q | Define communications mode option |
AT&S | Define DSR option |
AT+ICF | DTE-DCE character framing |
AT+IFC | DTE-DCE Local flow control |
AT+IPR | Fixed DTE rate |
Service :
Command | Description |
AT+CLIP | Calling line identification presentation |
AT+CR | Service reporting control |
AT+DR | Data compression reporting |
AT+ILRR | DTE-DCE local rate reporting |
Network Communication parameter :
Command | Description |
ATB | Communications standard option |
AT+CBST | Select bearer service type |
AT+CEER | Extended error report |
AT+CRLP | Radio link protocol |
AT+DS | Data compression |
Miscellaneous :
Command | Description |
A/ | Re-execute command line |
AT? | Command help |
AT*C | Start SMS interpreter |
AT*T | Enter SMS block mode protocol |
AT*V | Activate V.25bis mode |
AT*NOKIATEST | Test command |
AT+CESP | Enter SMS block mode protocol |
SMS Text mode :
Command | Description |
AT+CSMS | Select message service |
AT+CPMS | Preferred message storage |
AT+CMGF | Message format |
AT+CSCA | Service centre address |
AT+CSMP | Set text mode parameters |
AT+CSDH | Show text mode parameters |
AT+CSCB | Select cell broadcast message types |
AT+CSAS | Save settings |
AT+CRES | Restore settings |
AT+CNMI | New message indications to TE |
AT+CMGL | List messages |
AT+CMGR | Read message |
AT+CMGS | Send message |
AT+CMSS | Send message from storage |
AT+CMGW | Write message to memory |
AT+CMGD | Delete message |
SMS PDU mode :
Command | Description |
AT+CMGL | List Messages |
AT+CMGR | Read message |
AT+CMGS | Send message |
AT+CMGW | Write message to memory |
List of commands used in this project and their description
TO CHECK THE MODEM WORKING OR NOT :
AT
Response
OK
AT+QIFGCNT Select a Context as Foreground Context
AT+QICSGP Select CSD or GPRS as the Bearer
HAL_UART_Transmit(&huart1, (uint8_t *)”AT+QICSGP=1,\”Airteliot.com\”\r\n”, sizeof(“AT+QICSGP=1,\”Airteliot.com\”\r\n”), 300);
If you are using any other network add related network name.
AT+QIREGAPP Set APN, User Name
HAL_UART_Transmit(&huart1, (uint8_t *)”AT+QIREGAPP\r\n”, sizeof(“AT+QIREGAPP\r\n”), 300);
AT+QIACT Activate GPRS/CSD Context
HAL_UART_Transmit(&huart1, (uint8_t *)”AT+QIACT\r\n”, sizeof(“AT+QIACT\r\n”), 300);
AT+QHTTPGET //Send GET Request to HTTP server
HAL_UART_Transmit(&huart1, (uint8_t *)”AT+QHTTPGET=40\r\n”, sizeof(“AT+QHTTPGET=80\r\n”), 300);
AT+QHTTPREAD//Read the response of HTTP server
HAL_UART_Transmit(&huart1, (uint8_t *)”AT+QHTTPREAD=60\r\n”, sizeof(“AT+QHTTPREAD=60\r\n”), 300);
AT+QIDEACT //Deactivate PDP context.
HAL_UART_Transmit(&huart1, (uint8_t *)”AT+QIDEACT\r\n”, sizeof(“AT+QIDEACT\r\n”), 300);
CODE
1 st turn on the gsm module
void gsm_on()
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
HAL_Delay(1000);
}
Create GSM SEND function
void gsm_cmd(char *cmd, int gsm_delay)
{
gsm_wait_status=0;
memset(gsm_buffer,'\0',sizeof(gsm_buffer));
HAL_UART_Transmit(&huart1,(uint8_t*) cmd, strlen(cmd),1000);
HAL_Delay(300);
if(gsm_index>0)
{
HAL_Delay(10);
gsm_index=0;
gsm_wait_status=1;
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, strlen(gsm_buffer), 300);
}
}
Write code to execute the function at commands
while (1)
{
/* USER CODE END WHILE */
HAL_UART_Transmit(&huart1, (uint8_t *)"AT\r\n", sizeof("AT\r\n"), 300);
HAL_Delay(300);
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, sizeof(gsm_buffer), 300);
HAL_Delay(500);
HAL_UART_Transmit(&huart1, (uint8_t *)"AT+QIFGCNT=0\r\n", sizeof("AT+QIFGCNT=0\r\n"), 300);
HAL_Delay(500);
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, sizeof(gsm_buffer), 300);
HAL_Delay(500);
HAL_UART_Transmit(&huart1, (uint8_t *)"AT+QICSGP=1,\"Airteliot.com\"\r\n", sizeof("AT+QICSGP=1,\"Airteliot.com\"\r\n"), 300);
HAL_Delay(500);
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, sizeof(gsm_buffer), 300);
HAL_Delay(500);
HAL_UART_Transmit(&huart1, (uint8_t *)"AT+QIREGAPP\r\n", sizeof("AT+QIREGAPP\r\n"), 300);
HAL_Delay(1000);
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, sizeof(gsm_buffer), 300);
HAL_Delay(1000);
HAL_UART_Transmit(&huart1, (uint8_t *)"AT+QIACT\r\n", sizeof("AT+QIACT\r\n"), 300);
HAL_Delay(3000);
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, sizeof(gsm_buffer), 300);
HAL_Delay(1000);
sprintf(url,"AT+QHTTPURL=%d,40\r\n",strlen(http));
HAL_Delay(2000);
HAL_UART_Transmit(&huart1, (uint8_t *)url, strlen(url), 300);
HAL_Delay(1000);
HAL_UART_Transmit(&huart1, (uint8_t *)http, strlen(http), 300);
HAL_Delay(1000);
HAL_UART_Transmit(&huart1, (uint8_t *)"AT+QHTTPGET=40\r\n", sizeof("AT+QHTTPGET=80\r\n"), 300);
HAL_Delay(6000);
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, sizeof(gsm_buffer), 300);
HAL_UART_Transmit(&huart1, (uint8_t *)"AT+QHTTPREAD=60\r\n", sizeof("AT+QHTTPREAD=60\r\n"), 300);
HAL_Delay(2000);
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, sizeof(gsm_buffer), 300);
HAL_Delay(10000);
HAL_UART_Transmit(&huart1, (uint8_t *)"AT+QIDEACT\r\n", sizeof("AT+QIDEACT\r\n"), 300);
HAL_Delay(1000);
HAL_UART_Transmit(&huart3, (uint8_t *)gsm_buffer, sizeof(gsm_buffer), 300);
}