Go Down

Topic: Reading response from SIM900 USSD code with balance check (Read 1 time) previous topic - next topic

StephanJacobs

Hi
I am trying to read the response from the AT Command to check my airtime balance. I am getting the correct message from the AT command but I cant seem to get it to print over serial. The serial print command is just printing the AT command that was sent.

Here is my results that I get. It should print Balance is = +CUSD: 0,"Balance: R 0.00 .SMS: 12.",64
Instead its printing
Balancec is = AT+CUSD=1,"*101#"

I have tried different methods but all with the same results. This is my first time in 6 years trying to program again so please be gentle :D

+CMT: "+27XXXXXXXXX","","19/02/24,14:41:00+08"
Balance
Balancec is = AT+CUSD=1,"*101#"


OK

+CUSD: 0,"Balance: R 0.00 .SMS: 12.",64    <--- I want to read this part
TEMPRATURE = 32*C
Light Must Switch OFF
TEMPRATURE = 32*C

here is my code: (I had to remove a lot to get it to post)

Code: [Select]
#include "DHT.h"
#include <Adafruit_Sensor.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>
//*************************************************************
#define DHTPIN 2//DHT sensor attached to digital pin 2 of Arduino Uno
// Uncomment whatever type of sensor you're using
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
// Initialize DHT sensor for normal 16mhz Arduino

//***************************LM35****************************
int val;
int tempPin = 1;
//**********************************************************


DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial SIM900module(7, 8);//software serial port

// Create global varibales to store temperature and humidity
int t; // temperature in celcius
int f; // temperature in fahrenheit
int h; // humidity


char incomingChar;//variable to store incoming SMS characters

String Num; //Variable to store incoming phone number
String txtmsg; //Store entire string message
String t2;
String St = "+27846035086";
String Na = "+2772595723";
String W; // Week number
String TT; // AT command error test variable
String balance; // return ussd message

int Weeknum = 0;
int Status = 0;
int Week = 0;
int Balance = 0;
int Lights = 5;
int Fan = 4;
int Tmin; // Minimum temp
int Tmax; // Maximum temp
int Hmin; // Humidity min var
int Hmax; // Humidity max var


//**************************************************************

void setup() {
  dht.begin();
  Serial.begin(19200);
  Serial.read();
  delay(1000);
 
  SIM900module.begin(19200);
  delay(100);
  powerUpOrDown();
  // Give time to your GSM shield log on to network
  delay(10000);
  Serial.println("SIM900 module ready...");
  // AT command to set SIM900 to SMS mode
 
 
  SIM900module.print("AT+CMGF=1\r");
  delay(100);
  // Set module to send SMS data to serial out upon receipt
  SIM900module.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  pinMode(Lights, OUTPUT);
  pinMode (Fan , OUTPUT);
  W = EEPROM.read(0);
  Week = EEPROM.read(0);
  Tmin = EEPROM.read(1);
  Tmax = EEPROM.read(2);
  Hmin = EEPROM.read(2);
  Hmax = EEPROM.read(4);
}

//*****************************************************Power ON/OFF**********************************
void powerUpOrDown()
{
   
  SIM900module.print("AT\r");
  delay(1000);
  String serIn = SIM900module.readString();
  Serial.print(serIn);
  if(serIn.indexOf("OK") >=0) //test if OK was found in AT command response (is modem on or off)
  {
    return;
  }
   
 
    delay(100);
    pinMode(9, OUTPUT);
    digitalWrite(9,LOW);
    delay(1000);
    digitalWrite(9,HIGH);
    delay(2000);
    digitalWrite(9,LOW);
    delay(3000);

 
}

//**********************************************************************Main Loop**********************************************************************

void loop() {
 
  if (SMSRequest()) {
    if (readData()) {
     if(findNum())  {
      delay(100);

      if (Weeknum > 0) // Sends message for week selection
      {
        String dataMessage = ("Week: " + String (Weeknum) + " Selected");
        SIM900module.print(dataMessage);
        delay(100);
       
      }

      if (Status > 0) // Reports on sensor data
      {
      // REPLACE WITH YOUR OWN SMS MESSAGE CONTENT
      String dataMessage = ("Week: "+ String(W) + " Temperature:  " + String(t2) + "*C ");// + " Humidity: " + String(h) + "%");
      // Send the SMS text message
      SIM900module.print(dataMessage);
      delay(100);
      }

      if (Balance > 0) // Reports on sensor data
      {
         
      // Send the SMS text message
      SIM900module.print(String (balance));
      delay(100);
      }



     
      // End AT command with a ^Z, ASCII code 26
      SIM900module.println((char)26);
      delay(100);
      SIM900module.println();
      // Give module time to send SMS
      delay(5000);
       delSMS();
   
      }
    }
  }
  readData();
  temp_reg();
 // hum_reg ();
// disp_Time();
 
}

//*****************************************************************Read data from DHT11 Sensor*****************************************************

         



    if(txtmsg.indexOf("Balance") >=0)
    {
      Balance=1;   // Just a market to see if sms should be sent
      SIM900module.println("AT+CUSD=1,\"*101#\"\r"); // AT command for Balance request
      delay(5000);
     
     String balance = SIM900module.readStringUntil("64");  // Read string until 64 is found (end of message
      Serial.print("Balancec is = ");
      Serial.print(balance);
     
   
            //balance = balance.substring(11,37);  this part is for when I can actually get the message
          //  Serial.print("Balance received is: ");
           // Serial.println(balance);
           // return true;
     
       
           
    }


       
 
    return false;
  }
}
//******************************************************************Find Senders Number***********************************************************

/*  void findNum()
{
    Num = txtmsg.substring(9,21); // Set global var for cell number to received SMS number
    delay(100);
 Serial.println(String ("number is")+Num);
  if (Num == St)  // test if it is first Number
  {
    Serial.println("Message received from Stephan");
    SIM900module.println("AT + CMGS = \"+27846035086\"");
  delay(100);
   
   
  }
  else if (Num == Na) // test if it is second number
  {
    Serial.println("Message received from Nadia");
    SIM900module.println("AT + CMGS = \"+27846035086\"");
    delay(100); 
     
  }
  else if (Num !=St && Num !=Na)
  {
  Serial.println("Number Not recognized");
 
  }
}*/

    boolean findNum()
    {
        if(txtmsg.indexOf("+27846035086") >=0)
         {
            Serial.println("Received Message: from Stephan");
            SIM900module.println("AT + CMGS = \"+27846035086\"");
            delay(100);
            return true;           
           
         }
        if(txtmsg.indexOf("+27725957223") >=0)
           {
            Serial.println("Received Message: from Nadia");
            SIM900module.println("AT + CMGS = \"+27725957223\"");
            return true;
           }

        else
        {
            Serial.println("Number not recognized");
             return false;
        }
     
    }
//***************************************Delete SMS When done************************************

void delSMS() {  // Delete All messages
  //mySerial.println( "AT+CMGF=1" ); // Did not help
 //delay(1000);                                        //Did not help
  SIM900module.print("AT+CMGDA=\"");
  SIM900module.println("DEL ALL\"");
  delay(500);
 Serial.println( "All Messages Deleted" );
}



The part I am struggeling with

Code: [Select]

   

    if(txtmsg.indexOf("Balance") >=0)
    {
      Balance=1;   // Just a market to see if sms should be sent
      SIM900module.println("AT+CUSD=1,\"*101#\"\r"); // AT command for Balance request
      delay(5000);
     
     String balance = SIM900module.readStringUntil("64");  // Read string until 64 is found (end of message
      Serial.print("Balancec is = ");
      Serial.print(balance);
     
   
            //balance = balance.substring(11,37);  this part is for when I can actually get the message
          //  Serial.print("Balance received is: ");
           // Serial.println(balance);
           // return true;
     
       
           
    }



Please if anyone can tell me what I am doing wrong

Thanks in advance

Go Up