Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

arduino - Store value in variable after HTTPREAD

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
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Start by acquiring a large A3 sheet of paper, find a red pen and write 1000 times

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.

...

Then read this answer, following the instructions regarding V.250. And when you have properly digested all information from the answer (it probably takes some time to let all sink in), then follow the link to another answer in the comment below it (which contains information to capture response content).


Of course the first part was meant to be funny, but I am dead serious about the rest; you have some huge AT command knowledge "holes" you must fill. You will not be able to get any information out until you do. It should not be very difficult, but it will require some effort.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...