Store value in variable after HTTPREAD

放肆的年华 提交于 2019-12-17 03:44:33

问题


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

回答1:


First you should initialize a char array named a for storing the value and also declare a variable int flag=0;.

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--;
  }
}



回答2:


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.



来源:https://stackoverflow.com/questions/46062323/store-value-in-variable-after-httpread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!