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!
Just as an introduction, we may say that
AT+SAPBRcommand, 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,1isFrom you code
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
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,5if you work in roaming).