在 HTTPREAD 之后将值存储在变量中 [英] Store value in variable after HTTPREAD
问题描述
我正在使用 GSM SIM900 和 Arduino Uno.我正在为 SIM900 使用 AT 命令.我成功地从 GET 请求中获取数据并显示在串行监视器上,但是在 AT+HTTPREAD
命令之后,我想将数据存储到一个变量中.我怎样才能做到这一点?我从 Web 服务器获取 JSON 对象,我想从该对象获取 Status
属性并将其保存到变量中.
I am working with a GSM SIM900 and an Arduino Uno. I am using AT commands for the SIM900. I am successfully getting data from GET requests and showing on the serial monitor, but after the AT+HTTPREAD
command I want to store data into a variable. How can I do this? I am getting a JSON Object from the web server and I want to get the Status
property from that object and save it into a variable.
#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());
}
}
这是我想存储在变量中的输出:
This is piece of output I want to store in a variable:
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
推荐答案
首先你应该初始化一个名为 a
的字符数组来存储值,同时声明一个变量 int flag=0;
.
First you should initialize a char array named a
for storing the value and also declare a variable int flag=0;
.
然后修改你的 toSerial()
函数如下:
Then modify your toSerial()
function as follows:
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--;
}
}
这篇关于在 HTTPREAD 之后将值存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!