Search code examples
arduinohttp-getgsmat-commandsim900

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

Solution

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