Need help to extract data from String

Hi,

I need some help to extract the specific text from string. Below is is part of my sketch which is AT+command to check the balance and return is:

1AT+CUSD=1,"*102#"

OK

+CUSD: 0,"Your balance is 17.55 SR.",64         

inData =

I want to print only 17.55 in serial monitor or LCD

// the exact string is : Your balance is 17.55 SR

void CheckBalance()
    {
       inData = "";
       SIM900.println(F("AT+CMGF=0")); 
       delay(500);
       if(SIM900.available()> 0)
         Serial.write(SIM900.read());
      // SIM900.println(F("AT+CUSD=1,\"*142#\"")); 
       SIM900.println(F("AT+CUSD=1,\"*102#\""));
       delay(3000);
      // if (sendATcommand(F("AT+CUSD=1,\"*142#\""), F("777"), 8000) == 0);
         if (sendATcommand(F("AT+CUSD=1,\"*102#\""), F("777"), 8000) == 0);
       if(SIM900.available() > 0)
       {
          char sim900response = SIM900.read();
          inData += sim900response;
          delay(200);       
       }                    
       Serial.print(F("inData = "));
       Serial.println(inData);
    }

     template <typename ATcommandType>int8_t sendATcommand(const ATcommandType ATcommand, const __FlashStringHelper* expected_answer, unsigned int timeout)
   {
     uint8_t x = 0, answer = 0;
     char response[200];
     unsigned long previous;
     memset(response, '\0', 200);            // initalize string
     delay(100);
     while (SIM900.available() > 0)
       SIM900.read();                        // clears the buffer
     SIM900.println(ATcommand);
     x = 0;
     previous = millis();
     const char* expected_answer_pointer = (const char PROGMEM *)expected_answer;
     do
     {
       if (SIM900.available() != 0)
       {
         response[x] = SIM900.read();
         x++;
         if (strstr_P(response, expected_answer_pointer) != NULL)
         {
           answer = 1;
         }
       }
     } 
     while ((answer == 0) && ((millis() - previous) < timeout));
     Serial.println(response);
     return answer;
   }

Serial output:

What is the actual string that you want to parse.
This (1):

+CUSD: 0,"Your balance is 17.55 SR.",64

or this (2):

Your balance is 17.55 SR

If 1, Can you change the format that the sender sends? Does the app that sends the data have to include the quotes around Your balance is 17.55 SR.? Quotes inside a string need to be escaped (").

This sketch illustrates how to parse the number from the string using strtok(). There may be easier or better ways, but this is the one that I would use. The quotes, inside the string, complicate things and should be removed or escaped if possible or replaced during reception with, for instance, spaces.

char responce[] = "+CUSD: 0,\"Your balance is 17.55 SR.\",64 ";
char *responce_strings[4];
char *numSearch_strings[6];
char *respPtr = NULL;
char *numPtr = NULL;

void setup()
{
   Serial.begin(115200);

   // this sepatates the srting of interest form the rest
   // remove this part if the string is as #2 from the above post
   byte respIndex = 0;
   respPtr = strtok(responce, ",");
   while (respPtr != NULL)
   {
      responce_strings[respIndex] = respPtr;
      respIndex++;
      respPtr = strtok(NULL, ",");
   }
   Serial.println("Parsed responce strings");
   for (int n = 0; n < respIndex; n++)
   {
      Serial.println(responce_strings[n]);
   }
   Serial.println();

   // this part parses the number from the rest
   byte numIndex = 0;
   numPtr = strtok(responce_strings[1], " ");
   while (numPtr != NULL)
   {
      numSearch_strings[numIndex] = numPtr;
      numIndex++;
      numPtr = strtok(NULL, " ");
   }
   Serial.println("Parsed number search strings");
   for (int n = 0; n < numIndex; n++)
   {
      Serial.println(numSearch_strings[n]);
   }
   Serial.println();

   // print the number as a float
   Serial.println("The number searched for is ");
   Serial.println(atof((numSearch_strings[3])));
}

void loop()
{

}

groundFungus:
What is the actual string that you want to parse.
This (1):or this (2):
If 1, Can you change the format that the sender sends? Does the app that sends the data have to include the quotes around Your balance is 17.55 SR.? Quotes inside a string need to be escaped (").

The actual string is return from UCSD is Your balance is 17.55 SR and all others are return from sketch.

Also inData = in my first sketch is not printed in serial monitor only AT command return the result.

Thanks for your time.

You are welcome, but I still don't know what is the exact string that you want to parse nor if the code that I posted does what you want or can be modified to do so.

groundFungus:
You are welcome, but I still don’t know what is the exact string that you want to parse nor if the code that I posted does what you want or can be modified to do so.

Below is my complete sketch and serial output also. If I dial *102# from my mobile It return
Your balance is 16.80 SR.

Currently I am getting the balance information in serial monitor which is return of AT command but cannot hold the data on “inData” once it hold the message than we can subtract.

I believe there is some mistake in my sketch which I cannot see.

#include <LiquidCrystal_I2C.h>               //addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);//Configuring the serial pins by software

String inData;  

void setup() {
     Serial.begin(9600);
  lcd.begin(16, 2);
  Serial.println(F("Control System Starting"));
     SIM900.begin(9600);
      CheckBalance();
}

void loop() {
 Serial.println(F("Control System Starting"));
 delay(1000);
}

       
 void CheckBalance()
    {
       inData = "";
       SIM900.println(F("AT+CMGF=0")); 
       delay(500);
       if(SIM900.available()> 0)
         Serial.write(SIM900.read());
      // SIM900.println(F("AT+CUSD=1,\"*142#\"")); 
       SIM900.println(F("AT+CUSD=1,\"*102#\""));
       delay(5000);
      // if (sendATcommand(F("AT+CUSD=1,\"*142#\""), F("777"), 8000) == 0);
         if (sendATcommand(F("AT+CUSD=1,\"*102#\""), F("777"), 8000) == 0);
       if(SIM900.available() > 0)
       {
          char sim900response = SIM900.read();
          inData += sim900response;
          delay(200);       
       }                    
       Serial.print(F("inData = "));
       Serial.println(inData);
          lcd.setCursor(0, 0);
    lcd.print(inData);
    }

     template <typename ATcommandType>int8_t sendATcommand(const ATcommandType ATcommand, const __FlashStringHelper* expected_answer, unsigned int timeout)
   {
     uint8_t x = 0, answer = 0;
     char response[200];
     unsigned long previous;
     memset(response, '\0', 200);            // initalize string
     delay(100);
     while (SIM900.available() > 0)
       SIM900.read();                        // clears the buffer
     SIM900.println(ATcommand);
     x = 0;
     previous = millis();
     const char* expected_answer_pointer = (const char PROGMEM *)expected_answer;
     do
     {
       if (SIM900.available() != 0)
       {
         response[x] = SIM900.read();
         x++;
         if (strstr_P(response, expected_answer_pointer) != NULL)
         {
           answer = 1;
         }
       }
     } 
     while ((answer == 0) && ((millis() - previous) < timeout));
     Serial.println(response);
     return answer;
   }

And serial output is like:

Control System Starting
AAT+CUSD=1,"*102#"

OK

+CUSD: 0,“Your balance is 16.80 SR.”,64

inData =
Control System Starting