AT Commands
20.10.2022

Why you shouldn’t tell IoT modems to detach/attach

Using the at commands AT+COPS=0 and AT+COPS=2 to attach and detach modems from networks are bad ideas in most cases. Enabling features that are already enabled can backfire.
Peter Bornerup
Peter Bornerup

Developer

Modems come in many sizes and shapes. Common to all is that they are generally quite powerful devices – often with processing power far greater than the MCU managing the connection – and running some sort of embedded linux distribution has been a typical way to implement the necessary stacks.

They can have some quirks that make them difficult to operate efficiently, however. A lot of these quirks need to be learned from first hand experience, aka trial-and-error.

One quirk is that, most of the time, you shouldn’t manually tell the modem to attach or detach using AT+COPS=0 and AT+COPS=2.

Let’s explore why.

Do not tell the modem to detach.

AT+COPS=2 is the standard AT command to de-register a modem from a registered network. If you’re not familiar with AT+COPS=0 or AT+COPS=2, you can read a full overview of AT+COPS commands here: AT+COPS: Modes, parameters, and examples.

Counterintuitively, using this command to detach from networks is in 99.99% of the cases a really bad idea. Unless you have oddly specific needs you really – really – shouldn’t tell the modem to detach.

Telling the modem to detach also implies that you do not want to use that network next time you need a connection – or at least that it should pick a new network next time it establishes a connection. Next time the modem is powered on, it’ll start scanning for networks as described in the standards.

3GPP TS 23.122 and automatic network selection.

This is a slow and power hungry procedure and not something you’ll want to occur, in most cases (refer to the 99.99%).

Instead of using AT+COPS=2, you should just gently power the modem down – it knows what to do and will detach for you.

While doing that, the modem will involve the SIM and write a bit of data to the EF_LOCI file. That file more or less works as a local persistent cache and when it is powered on again it’ll use this cache to significantly speed up the attachment.

The result of explicitly detaching: As a side effect, the current (known) network is cleared from all caches. On the next boot, this triggers a complete network scan.
By powering the modem off, we avoid unwanted side-effects. Instead the current network is written to the EF_LOCI and is stored in non-volatile memory.

The effects of explicit detach are demonstrated in the illustrations above.

The lengthy network scan problem is more prominent on non-steered SIMs and might not be an issue if you are testing with steered SIMs (where PLMN lists are set).

This is because the modem will initially look for networks predefined on the SIM. When no networks are defined in the PLMN, the modem picks the network based on signal strength instead.

Learn how leaving PLMN lists blank reduces SIM registration times: Automatic network selection.

Don’t tell the modem to automatically attach.

First and foremost AT+COPS=0 is a big no-no, especially if you just powered on the modem.

Alright, maybe this doesn’t apply to all modems out there, but all modems I’ve had my hands on did some very unexpected things.

There’s no way to be 100% sure what happens after issuing an AT+COPS=0 command, but I imagine something like this:

Something is restarted or the modem enters a different mode when AT+COPS=0 is set.

I’m maybe exaggerating a tiny bit. What actually happens during automatic attach is almost documented in some of ublox at-command manuals:

Auto-registration (also sometimes called “auto-COPS”, not to be confused with automatic =0)

Yes, this is an actual quote from a manual.

The modem will start in a mode called “auto-COPS” (that is more or less the only mention of that mode) which is different from COPS in automatic mode.

Slightly confusing, but at least we’re told not to confuse those two modes.

I can imagine that all modems have this kind of mode, but many have no documentation about it.

When first powered on, the modem enters an automatic selection mode where it will use the previously cached network and this is much more efficient than scanning for networks on all boot-ups.

If you issue the AT+COPS=0 command, the modem will enter a slightly different mode where a full scan is required.

This is not intuitive at all, but you’ve gotta trust me on this (well, or test and look at the results).

This funky behavior applies to other commands as well, but as I don’t have an exhaustive list of all commands you shouldn’t issue, I can only give the general advice of not issuing commands unless absolutely needed.

This is easy to implement in practice as all SET commands will have a corresponding READ command. In short, read the current setting and set it if it is different from the expected setting.

Good practice. The READ command is issued before any SET command is issued.

Final thoughts on AT+COPS=0 and AT+COPS=2

In general, issuing unnecessary AT commands can have drastic side-effects.

If you follow this advice, you should be able to achieve rapid attachment and lower your power consumption compared to issuing additional AT+COPS=… commands.

I hope these insights on powering modems off and on help you in your IoT development journey. Look out for more development insights on our blog.

Build your
own network

Enhance signal quality and network control with non-steered SIM cards. Connect all your devices securely to a global network with Onomondo IoT SIMs.

Start testing Onomondo for free

Ready to experience next-generation IoT connectivity? Create an account, explore the platform, and start testing Onomondo’s IoT SIM cards for free.

Related articles

Read more about the topic

LTE-M vs 2G
Articles
LTE-M vs 2G: A critical comparison for IoT deployments
Cellular networks IoT Strategy
Explore the critical LTE-M vs 2G transition for IoT: better security, power efficiency, and future-proof connectivity as 2G networks sunset globally.
A new chapter at Onomondo
Articles
Onomondo reshapes telecom and now accelerates growth with strengthened leadership
Press Release News
Onomondo is entering an exciting new phase with a leadership transition that strengthens our ability to support customers and scale our platform.
Onomondo Syniverse collaboration
Articles
Onomondo and Syniverse break legacy barriers to simplify IoT connectivity
Press Release News
Onomondo and Syniverse are joining forces to enhance global IoT connectivity with robust, secure, and efficient solutions for global IoT data transmission.