Search code examples
arduinoat-commandsim900

Arduino Sim900 AT+SAPBR=1,1 --> Operation not allowed


I am using an Arduino Mega and a Sim900 GSM/GPRS shield to make a request against an API.

During my initialization of my request the command AT+SAPBR=1,1 is executed. Sometimes, when I execute the shield returns OK, sometimes the shield returns "Operation not allowed", but I changed nothing compared to the working code.

#include <SoftwareSerial.h>
SoftwareSerial(18, 19); 

void setup() {
 
  Serial1.begin(19200);
  delay(10000);  
  Serial1.print("AT+CPIN=1111\r");
  Serial1.flush(); 
  Serial1.print("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r");
  Serial1.flush(); 
  Serial1.print("AT+SAPBR=3,1,\"APN\",\"my.apn.com\"\r");
  Serial1.flush(); 
  Serial1.print("AT+SAPBR=1,1\r");
  Serial1.flush(); 
// Here comes the error sometimes!

  Serial1.print("AT+SAPBR=2,1\r");
  Serial1.flush();
  Serial1.print("AT+HTTPINIT\r");
  Serial1.flush();
  Serial1.print("AT+HTTPPARA=\"CID\",1\r");
  Serial1.flush();
  Serial1.print("AT+HTTPPARA=\"URL\",\"my-api.com/foo\"\r");
  Serial1.flush();
  Serial1.print("AT+HTTPPARA=\"CONTENT\",\"application/json\"\r");
  Serial1.flush();
  Serial1.print("AT+HTTPACTION=0\r");
  Serial1.flush();
  Serial1.print("AT+HTTPREAD\r");
  Serial1.flush();

// READ the Response

}

void loop() { 
  
}

Thank you!


Solution

  • Just as an introduction, we may say that AT+SAPBR command, as described in SIM900 AT Commands guide, is used to configure and to activate the PDP context (data traffic).

    In particular, the meaning of AT+SAPBR=1,1 is

    <cmd_type> = 1 - Open bearer
    <cid> = 1 - Bearer profile identifier

    From you code

    delay(10000);  
    Serial1.print("AT+CPIN=1111\r");
    Serial1.flush(); 
    Serial1.print("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r");
    Serial1.flush(); 
    Serial1.print("AT+SAPBR=3,1,\"APN\",\"my.apn.com\"\r");
    Serial1.flush(); 
    Serial1.print("AT+SAPBR=1,1\r");
    

    I see that you only wait for 10 seconds (the other commands usually return immediately.


    For this reason, the first solution is increasing the delay (15 seconds should suffice).

    The second solution involves querying the registration status. This can be done separately for GSM network (AT+CREG?) and for 2G data network (AT+CGREG?).

    In both cases querying the command will result in an answer like

    +CGREG: <n>,<stat> (or +CREG: <n>,<stat>), where

    <n> is the setting performed through set command. It's used to enable unsolicited result messages. So, its value is usually 0

    <stat> is the current registration status. It can have the following values
    0 - Not registered. The GPRS service is disabled [...]
    1 - Registered, home network [...]
    2 - Not registered, but ME is currently trying to attach or searching an operator to register to. The GPRS service is enabled, but an allowable [...]
    3 - Registration denied. The GPRS service is disabled [...]
    4 - Unknown
    5 - Registered, roaming

    So, if you sure that the device has enough signal coverage, all you need is to provide AT+CGREG? command every second and wait for +CGREG=0,1 (or +CGREG=0,5 if you work in roaming).