Arduino Sim900 AT+SAPBR=1,1 --> 不允许操作

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

我正在使用 Arduino Mega 和 Sim900 GSM/GPRS shield 来针对 API.

发出请求

在我的请求初始化期间,命令 AT+SAPBR=1,1 被执行。有时,当我执行屏蔽 returns OK 时,有时会执行屏蔽 returns "Operation not allowed",但与工作代码相比我没有任何改变。

#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() { 
  
}

谢谢!

作为介绍,我们可以说AT+SAPBR命令,如SIM900AT Commands guide中所述,用于配置和激活PDP上下文(数据流量)。

具体来说,AT+SAPBR=1,1的意思是

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

来自你的代码

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");

我看到你只等了10秒(其他命令通常return立即。


出于这个原因,第一个解决方案增加了延迟(15 秒就足够了)。

第二种解决方案涉及查询注册状态。这可以分别为 GSM 网络 (AT+CREG?) 和 2G 数据网络 (AT+CGREG?) 完成。

在这两种情况下,查询命令都会得到类似

的答案

+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

所以,如果你确定设备有足够的信号覆盖,你只需要每秒提供AT+CGREG?命令并等待+CGREG=0,1(或者+CGREG=0,5,如果你工作在漫游中)。