Embedded IoT Software Development: Have You Used AT Commands?

Scan to FollowLearn Embedded Together, Learn and Grow Together

Embedded IoT Software Development: Have You Used AT Commands?

Overview

AT commands (Attention Command) are a widely used text command line protocol in communication devices such as modems, GSM/GPRS modules, and Bluetooth modules.

It originated from the command set designed by Hayes for its smart modem in 1981 and later became the de facto industry standard.

Development History

The AT command set was initially developed by Dennis Hayes for the Hayes Smartmodem and was quickly adopted by other manufacturers due to its simplicity and efficiency.

With the development of mobile communication technology, the GSM standard organization ETSI standardized AT commands in the 07.05 and 07.07 specifications, making them the standard for controlling the interface between Mobile Equipment (ME) and Terminal Equipment (TE).

Basic Features

  • Text Format: Based on ASCII characters, easy for humans to read and write
  • Request-Response Model: Each command has a corresponding response
  • Standardization and Extensibility: There is a standard command set, and manufacturers can also define custom extensions
  • Simplicity: Basic syntax is simple and easy to implement

Common Application Scenarios

  1. Mobile communication modules (GSM/GPRS/3G/4G modules)
  2. Bluetooth module configuration
  3. WiFi module control
  4. GPS module data acquisition
  5. Industrial serial device configuration

Detailed AT Command Syntax

2.1 Basic Syntax Structure

AT commands typically follow the following format:

AT<command>[=<parameter>][<sub-command>][<...>][<CR>]
  • <span>AT</span>: Prefix indicating Attention
  • <span><command></span>: A command composed of one or more letters
  • <span>=<parameter></span>: Optional parameter part
  • <span><CR></span>: Carriage return (ASCII 13), usually represented as<span> </span> when actually sent

2.2 Response Format

The responses to AT commands typically come in the following forms:

  1. Success Response: <span>
    OK
    </span>
  2. Error Response: <span>
    ERROR
    </span>
  3. Data Response: <span>
    <data>
    OK
    </span>
  4. Asynchronous Notification: <span>
    <prefix><data>
    </span>

2.3 Common Special Characters

  • <span> </span>: Carriage return (ASCII 13)
  • <span>
    </span>
    : Line feed (ASCII 10)
  • <span>></span>: Indicates input waiting, usually appears when sending SMS text
  • <span>Ctrl+Z</span> (ASCII 26): Indicates end of input

2.4 Parameter Types

  1. Numeric Parameter: <span>AT+VOL=5</span>
  2. String Parameter: <span>AT+CMGS="13800138000"</span>
  3. Boolean Parameter: <span>AT+CFUN=1</span> (1 means on, 0 means off)
  4. Enumeration Parameter: <span>AT+CMGF=1</span> (1 means text mode, 0 means PDU mode)

Classification of AT Commands

3.1 Basic Commands

Command Description Example
AT Test command AT
ATE Echo control ATE1 (enable echo)
AT+CGMI Query manufacturer AT+CGMI
AT+CGMM Query module model AT+CGMM
AT+CGMR Query firmware version AT+CGMR
AT+CSQ Signal quality query AT+CSQ

3.2 Call Related Commands

Command Description Example
ATD Dial ATD13800138000;
ATA Answer incoming call ATA
ATH Hang up call ATH
AT+CLIP Caller ID display AT+CLIP=1

3.3 SMS Related Commands

Command Description Example
AT+CMGF Set SMS mode AT+CMGF=1
AT+CMGS Send SMS AT+CMGS=”13800138000″
text<ctrl+z>
AT+CMGR Read SMS AT+CMGR=1
AT+CMGD Delete SMS AT+CMGD=1

3.4 Network Service Commands

Command Description Example
AT+COPS Operator selection AT+COPS?
AT+CREG Network registration status AT+CREG?
AT+CGATT GPRS attachment status AT+CGATT?
AT+CSTT Set APN AT+CSTT=”cmnet”

3.5 TCP/IP Related Commands

Command Description Example
AT+CIICR Activate mobile context AT+CIICR
AT+CIFSR Get local IP AT+CIFSR
AT+CIPSTART Establish TCP connection AT+CIPSTART=”TCP”,”example.com”,80
AT+CIPSEND Send data AT+CIPSEND=5
> GET / HTTP/1.1
SEND OK

Practical Applications of AT Commands

4.1 Hardware Connections

A typical AT command application requires the following hardware connections:

  1. Serial Connection: Connect the module and controller via UART (TX/RX)
  2. Power Supply: Ensure the module has a stable power supply
  3. SIM Card: A valid SIM card is required for GSM modules
  4. Antenna: Ensure good signal reception

4.2 Example of Sending SMS

AT+CMGF=1
                      // Set to text mode
OK

AT+CMGS="13800138000"
          // Set target number
> Hello, this is a test message!\x1A // Input message content and end with Ctrl+Z
+CMGS: 45

OK

4.3 Example of Establishing TCP Connection

AT+CIPSHUT
                      // Ensure previous connection is closed
SHUT OK

AT+CSTT="cmnet"
                 // Set APN
OK

AT+CIICR
                        // Activate mobile context
OK

AT+CIFSR
                        // Get local IP
10.120.45.67

AT+CIPSTART="TCP","example.com",80
 // Establish TCP connection
CONNECT OK

AT+CIPSEND=12
                   // Prepare to send 12 bytes of data
> GET / HTTP/1.1
                // Send HTTP request
SEND OK

4.4 Error Handling

When a command execution fails, the module will return ERROR. Common reasons for errors include:

  1. Syntax error (command spelling error)
  2. Parameter range error
  3. Module not ready (e.g., SIM card not inserted)
  4. Network unavailable
  5. Insufficient memory (e.g., SMS storage full)

Debugging Tips

5.1 Common Issue Troubleshooting

  1. No Response:
  • Check hardware connections (TX/RX cross-connected?)
  • Confirm baud rate settings are correct
  • Check if the module power is stable
  • Returns ERROR:
    • Confirm command spelling is correct
    • Check if parameters are within valid range
    • Confirm if the module is in the correct state (e.g., network registered)
  • Data Loss:
    • Increase serial receive buffer
    • Add appropriate delays
    • Implement flow control (hardware or software)

    5.2 Performance Optimization

    1. Reduce Delays:
    • Combine multiple query commands
    • Use caching to reduce duplicate queries
    • Asynchronous processing notifications
  • Memory Management:
    • Delete unnecessary SMS promptly
    • Close unnecessary connections
    • Regularly query module status

    5.3 Security Considerations

    1. Sensitive Information Protection:
    • Do not hard-code APN passwords in code
    • Handle SMS content with caution
    • Implement appropriate access control
  • Firmware Updates:
    • Regularly check and update module firmware
    • Use secure update channels
    • Backup critical configurations

    Implementation on Different Platforms

    6.1 Embedded Platforms (e.g., STM32)

    // Example: Sending AT commands via STM32
    void SendATCommand(UART_HandleTypeDef *huart, char *cmd) 
    {
        HAL_UART_Transmit(huart, (uint8_t*)cmd, strlen(cmd), HAL_MAX_DELAY);
        HAL_UART_Transmit(huart, (uint8_t*)"\r\n", 2, HAL_MAX_DELAY);
    }
    
    // Receive response
    void ReceiveResponse(UART_HandleTypeDef *huart) 
    {
        char buffer[256];
        HAL_UART_Receive(huart, (uint8_t*)buffer, sizeof(buffer), HAL_MAX_DELAY);
        printf("Response: %s\n", buffer);
    }
    

    6.2 Python Implementation

    import serial
    
    # Create serial connection
    ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
    
    def send_at_command(command, wait_time=1):
        ser.write((command + '\r\n').encode())
        time.sleep(wait_time)
        response = ser.read(ser.in_waiting).decode()
        return response
    
    # Example: Query module information
    print(send_at_command('AT+CGMI'))
    print(send_at_command('AT+CGMM'))
    

    6.3 Arduino Implementation

    #include <SoftwareSerial.h>
    
    SoftwareSerial sim800l(10, 11); // RX, TX
    
    void setup()
    {
      Serial.begin(9600);
      sim800l.begin(9600);
    
      // Test AT command
      sim800l.println("AT");
      delay(1000);
    while(sim800l.available())
      {
        Serial.write(sim800l.read());
      }
    }
    
    void loop() 
    {
      // Other operations...
    }
    

    Development Trends

    Despite the decades-long history of AT commands, they are still widely used in IoT devices.

    Future development trends include:

    1. Richer Command Sets: Support for more IoT protocols (MQTT, CoAP, etc.)
    2. Binary Protocol Supplementation: Providing more efficient binary protocols while maintaining the simplicity of AT commands
    3. Security Enhancements: Support for security protocols such as TLS/SSL
    4. Cloud Integration: Native support for interaction with cloud services
    5. AI Integration: Providing simple AI functionality control at the module level

    Conclusion

    As a fundamental protocol for controlling communication devices, the simplicity and reliability of AT commands still hold significant value in the IoT era.

    Through this detailed introduction, readers should be able to understand the basic principles of AT commands, master the usage of common commands, and apply this knowledge in practical projects.

    With the development of technology, AT commands may evolve into new forms, but their core idea—simple and effective device control—will continue to influence future communication interface design.

    Embedded IoT Software Development: Have You Used AT Commands?

    Follow 【Learn Embedded Together】 to become better together.

    If you find this article useful, click “Share”, “Like”, or “Recommend”!

    Leave a Comment