Any developer who must work with a cellular modem will have to make use of what are called ‘AT commands’ at some point. AT commands are essentially modem instructions. Originally developed by the modem maker Hayes as means to operate their dial-up landline products, AT commands — the ‘AT’ stands for ‘come to ATtention’ — are now used by all modems, of all types.
AT commands are primarily used to configure a modem and establish its network connection. They can also be used to get modem and connection status information, and this can be very helpful in debugging applications and in confirming that a modem is operating correctly: it has connected to the right network, is using the correct cellular technology, has roaming enabled, etc. We’ll look at some useful commands for tasks like these shortly.
AT commands are sent to the modem as plain text over a serial (UART) connection comprising two wires, one for receive (RX) and one for transmit (TX), or via USB. In the field, a cellular-enabled IoT device will manage its modem by sending it AT commands, but during application development and debugging, it’s not uncommon to tap the modem’s connection. This allows you to fire up a terminal and interact with the modem directly by issuing AT commands of your own.
The commands you send must obey the following basic syntax:
This is called a ‘command line’. As you can see, it starts with
AT, followed by a command, a suffix to indicate command mode or ‘type’, and finally some data, though not all commands need this field to be included.
This structure is called a ‘command line’ and it is always terminated by a single carriage return. You can include multiple commands in the line, each separated by a semi-colon:
Every line, no matter how many commands it contains, only has one
AT, at the start. You’ll get an error if you include
AT more than once in a single line. The commands in multi-command lines are processed sequentially. The line length is usually limited to 80 characters.
<COMMAND> field indicates what you want the modem to do. Older, ‘basic’ commands have single-character names; later additions to the commands set, called ‘extended’ commands, are prefixed with a
+ sign, such as
AT+COPS (scan for networks) and
AT+CGMI (Get modem manufacturer). Some commands are prefixed with & — for example,
AT&F, which resets the modem’s settings. All cellular commands are extended commands. According to the AT command specification, commands must comprise uppercase characters, but many modems allow commands to be sent in lowercase too. We’ll stick with the standard.
The values of the
<SUFFIX> fields depend on the command type, of which there are four:
||Get a modem configuration setting|
||Set a modem configuration setting|
|Execute||なし||Trigger a modem operation|
||Check whether a modem supports the named command — but note that
We’ll see how these types affect the use of AT commands in a moment, but first let’s see how the modem responds to commands.
When an AT command is issued, depending on the command sent, the modem may or may not return data, ie. make what is called an ‘information response’. Either way, the command operation always ends with the modem returning a ‘result code’.
For example, suppose we ask for the name of the modem’s manufacturer using the following command:
This will generate the following response when called on a u-blox modem:
Each unit of the response — the information (top line) and the result code (bottom line) — are bracketed by Carriage Return and Line Feed characters (Ascii codes
0x0A, respectively), hence the empty line before the
OK is one return code; the other is
ERROR — you made a mistake. Perhaps the modem doesn’t respond to the AT command you sent, or you mistyped it. Maybe you provided too few or too many data values or command parameters. By default, modems may not provide much in the way of explanation. Fortunately, some of them can be set to return extended error information; again, you’ll need to send an AT command to apply this setting:
It’s important to understand that you may also receive
ERROR from a perfectly valid AT command. When this happens, it’s an indication that the modem is in state that is incompatible with the command. For example, if you attempt to change a modem’s Access Point Name (APN) while it is active, this will typically return
ERROR. The remedy is to deactivate the modem, make the APN change, and then reactivate it.
You can mitigate the problem of seemingly correct commands throwing errors by making use of the test command suffix (
=?), which is used to ask the modem if it supports the given command. If it doesn’t, you’ll receive an
ERROR return code; you know not to issue this command. If the modem does support the command, you will receive an information response indicating the command’s parameters, if it has any, followed by
The example above can be used to demonstrate command testing. We can ask the modem if it supports the
and it will return OK because it does support this command. However, because the modem’s manufacturer is fixed, attempting to change it will always result in
ERROR. How might we have attempted to change its value? With a set command suffix (
Note how the string data is placed within double quotes — this too is an AT command requirement.
There’s one key exception to the command test rule:
AT+COPS=?. This does not report on the availability of the
+COPS command, but instead triggers a network scan and reports the results.
AT+COPS? reports the network to which the modem is currently connected.
Test mode can also be used to determine what parameters a supported command takes. For example, if you send the command
+CBC in test mode (it asks the modem what its current battery level is) you will get a result indicating the number of parameters and the range of values for each:
AT+CBC=? +CBC: (0-2),(0-100) OK
The parameters are separated by commas; each parameter’s value range is presented in brackets in the form
<LOWEST VALUE>-<HIGHEST VALUE>.
Here’s an example of a multi-parameter command: this sets the modem’s power-on welcome text:
AT+CSGT=1,"A Super SIM-containing Modem" OK
There are no spaces in the command line; only those within strings are permitted.
We can read back the values with a read command suffix (
AT+CSGT? 1,A Super SIM-containing Modem OK
Which commands a given module supports and what values are returned are generally manufacturer specific and the list of commands is often extensive. You will need to download the command guide for your chosen modem from its manufacturer’s website.
The following table lists some of the most useful AT commands you may need while working with a cellular modem and Super SIM.
||Return the modem’s manufacturer|
||Return the modem’s model number|
||Return the modem’s serial number|
||Get the SIM’s current IMSI|
||Enabled extended error reporting: instead of the usual
||Get the current signal strength: RSSI,ERR_RATE
The value ranges are:
RSSI — an indexed dbM value
Error rate — an indexed percentage bit error rate
||The output fields are:
1. Automatic operator selection
2. Operator name format
3. Operator name
4. Network type
||Scan and report visible networks|
||Get the APN (the third response parameter)|
To use a Super SIM, its host modem’s APN must be set to
super, and roaming must be enabled. The following AT commands will help you do so.
Note Other modems will require their own vendor-specific roaming setup commands — please check it documentation. We hope to extend the following sections to include other modems over time.
||Set the modem’s APN
Note It’s possible to set multiple APNs, for different IP types (the second parameter), but we recommend setting just one
||Quectel BG96 only|
||u-blox Lara-R2xx only|
- You can download the comprehensive AT Command Set for User Equipment from the ETSI standards agency. It lists all AT commands that are not vendor specific.
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。