在 HTTPREAD 之后将值存储在变量中
Store value in variable after HTTPREAD
我正在使用 GSM SIM900 和 Arduino Uno。我正在为 SIM900 使用 AT 命令。我成功地从 GET 请求中获取数据并显示在串行监视器上,但是在 AT+HTTPREAD
命令之后我想将数据存储到一个变量中。我怎样才能做到这一点?我正在从 Web 服务器获取 JSON 对象,我想从该对象获取 Status
属性 并将其保存到变量中。
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(2,3);
void setup() {
gprsSerial.begin(9600);
Serial.begin(9600);
Serial.println("Con");
delay(2000);
Serial.println("Done!...");
gprsSerial.flush();
Serial.flush();
// See if the SIM900 is ready
gprsSerial.println("AT");
delay(1000);
toSerial();
// SIM card inserted and unlocked?
gprsSerial.println("AT+CPIN?");
delay(1000);
toSerial();
// Is the SIM card registered?
gprsSerial.println("AT+CREG?");
delay(1000);
toSerial();
// Is GPRS attached?
gprsSerial.println("AT+CGATT?");
delay(1000);
toSerial();
// Check signal strength
gprsSerial.println("AT+CSQ ");
delay(1000);
toSerial();
// Set connection type to GPRS
gprsSerial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
delay(2000);
toSerial();
// Set the APN
gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"wap.mobilinkworld.com\"");
delay(2000);
toSerial();
// Enable GPRS
gprsSerial.println("AT+SAPBR=1,1");
delay(10000);
toSerial();
// Check to see if connection is correct and get your IP address
gprsSerial.println("AT+SAPBR=2,1");
delay(2000);
toSerial();
}
void loop() {
// initialize http service
gprsSerial.println("AT+HTTPINIT");
delay(2000);
toSerial();
// set http param value
// ToDO : send dynamic value
gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://smockfyp.azurewebsites.net/api/Device/GetStatus?did=1\"");
delay(4000);
toSerial();
// set http action type 0 = GET, 1 = POST, 2 = HEAD
gprsSerial.println("AT+HTTPACTION=0");
delay(6000);
toSerial();
// read server response
gprsSerial.println("AT+HTTPREAD");
delay(1000);
toSerial();
gprsSerial.println("AT+HTTPTERM");
toSerial();
delay(300);
gprsSerial.println("");
delay(10000);
}
void toSerial() {
while(gprsSerial.available()!=0) {
Serial.write(gprsSerial.read());
}
}
这是我要存储在变量中的输出片段:
AT+HTTPINIT
OK
AT+HTTPPARA="URL","http://smockfyp.azurewebsites.net/api/DeviceAT+HTTPACTION=0
OK
+HTTPACTION: 0,200,17
AT+HTTPREAD
+HTTPREAD: 17
[{"Status":true}]
OK
先买一张大的A3sheet纸,找一支红笔写1000遍
I will never use delay
as a substitute for reading and parsing responses from a modem.
I will never use delay
as a substitute for reading and parsing responses from a modem.
I will never use delay
as a substitute for reading and parsing responses from a modem.
I will never use delay
as a substitute for reading and parsing responses from a modem.
I will never use delay
as a substitute for reading and parsing responses from a modem.
...
然后按照有关 V.250 的说明阅读 this answer、。当你从答案中正确消化了所有信息(可能需要一些时间让所有信息都吸收),然后在它下面的评论中跟随 link 到另一个答案(其中包含捕获响应内容的信息)。
当然,第一部分是为了搞笑,但我对其余部分非常认真;您必须填补一些巨大的 AT 命令知识“漏洞”。除非你这样做,否则你将无法获得任何信息。应该不是很难,但是需要一定的努力。
首先你应该初始化一个名为 a
的 char 数组来存储值,并声明一个变量 int flag=0;
.
然后修改你的toSerial()
函数如下:
void toSerial() {
while(gprsSerial.available()!=0) {
if( gprsSerial.read() == '[' )
flag=2;
else if(flag == 2 && gprsSerial.read() == ':')
while(gprsSerial.read() != '}') {
a[i]= gprsSerial.read();
i++;
}
else if(flag == 0)
Serial.write(gprsSerial.read());
else
flag--;
}
}
我正在使用 GSM SIM900 和 Arduino Uno。我正在为 SIM900 使用 AT 命令。我成功地从 GET 请求中获取数据并显示在串行监视器上,但是在 AT+HTTPREAD
命令之后我想将数据存储到一个变量中。我怎样才能做到这一点?我正在从 Web 服务器获取 JSON 对象,我想从该对象获取 Status
属性 并将其保存到变量中。
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(2,3);
void setup() {
gprsSerial.begin(9600);
Serial.begin(9600);
Serial.println("Con");
delay(2000);
Serial.println("Done!...");
gprsSerial.flush();
Serial.flush();
// See if the SIM900 is ready
gprsSerial.println("AT");
delay(1000);
toSerial();
// SIM card inserted and unlocked?
gprsSerial.println("AT+CPIN?");
delay(1000);
toSerial();
// Is the SIM card registered?
gprsSerial.println("AT+CREG?");
delay(1000);
toSerial();
// Is GPRS attached?
gprsSerial.println("AT+CGATT?");
delay(1000);
toSerial();
// Check signal strength
gprsSerial.println("AT+CSQ ");
delay(1000);
toSerial();
// Set connection type to GPRS
gprsSerial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
delay(2000);
toSerial();
// Set the APN
gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"wap.mobilinkworld.com\"");
delay(2000);
toSerial();
// Enable GPRS
gprsSerial.println("AT+SAPBR=1,1");
delay(10000);
toSerial();
// Check to see if connection is correct and get your IP address
gprsSerial.println("AT+SAPBR=2,1");
delay(2000);
toSerial();
}
void loop() {
// initialize http service
gprsSerial.println("AT+HTTPINIT");
delay(2000);
toSerial();
// set http param value
// ToDO : send dynamic value
gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://smockfyp.azurewebsites.net/api/Device/GetStatus?did=1\"");
delay(4000);
toSerial();
// set http action type 0 = GET, 1 = POST, 2 = HEAD
gprsSerial.println("AT+HTTPACTION=0");
delay(6000);
toSerial();
// read server response
gprsSerial.println("AT+HTTPREAD");
delay(1000);
toSerial();
gprsSerial.println("AT+HTTPTERM");
toSerial();
delay(300);
gprsSerial.println("");
delay(10000);
}
void toSerial() {
while(gprsSerial.available()!=0) {
Serial.write(gprsSerial.read());
}
}
这是我要存储在变量中的输出片段:
AT+HTTPINIT
OK
AT+HTTPPARA="URL","http://smockfyp.azurewebsites.net/api/DeviceAT+HTTPACTION=0
OK
+HTTPACTION: 0,200,17
AT+HTTPREAD
+HTTPREAD: 17
[{"Status":true}]
OK
先买一张大的A3sheet纸,找一支红笔写1000遍
I will never use
delay
as a substitute for reading and parsing responses from a modem.I will never use
delay
as a substitute for reading and parsing responses from a modem.I will never use
delay
as a substitute for reading and parsing responses from a modem.I will never use
delay
as a substitute for reading and parsing responses from a modem.I will never use
delay
as a substitute for reading and parsing responses from a modem....
然后按照有关 V.250 的说明阅读 this answer、。当你从答案中正确消化了所有信息(可能需要一些时间让所有信息都吸收),然后在它下面的评论中跟随 link 到另一个答案(其中包含捕获响应内容的信息)。
当然,第一部分是为了搞笑,但我对其余部分非常认真;您必须填补一些巨大的 AT 命令知识“漏洞”。除非你这样做,否则你将无法获得任何信息。应该不是很难,但是需要一定的努力。
首先你应该初始化一个名为 a
的 char 数组来存储值,并声明一个变量 int flag=0;
.
然后修改你的toSerial()
函数如下:
void toSerial() {
while(gprsSerial.available()!=0) {
if( gprsSerial.read() == '[' )
flag=2;
else if(flag == 2 && gprsSerial.read() == ':')
while(gprsSerial.read() != '}') {
a[i]= gprsSerial.read();
i++;
}
else if(flag == 0)
Serial.write(gprsSerial.read());
else
flag--;
}
}