Thanks @johnwasser, I tried your method, but the result stays exactly the same. Before you posted that tip I tried to do the same by doing delay(100); right after inData += sim900response; .
Would this not do the same as your suggestion?
Anyhow, the result remains the same.
Now I did manage to get the whole USSD response printed out, but it's nasty and unpredictable. In my full program I have sendATcommand, a wonderful little piece of code that takes care of sending AT commands and dealing with the responses. See the code right at the bottom. When calling the template you specify the expected response, and the function repeats the command until the expected answer arrives or the timeout is reached. It reads the serial response until it finds your expected answer, and leaves the rest of the response in serial to do with as I please.
When I put in the expected answer as "777" (this is just for testing purposes, not a permanent solution) the output looks like this:
OK
AT+CMGF=0
OK
AT+CUSD=1,"*114#"
OK
+CUSD: 0,"WIN BIG with MTN CHALILA on any Bundle bought using *777
inData = #. Dial NOW!Your Account Balance is K11.3620",64
When I use an expected answer that is located earlier in the response, like "MTN", the result looks like this:
AT+CUSD=1,"*114#"
OK
+CUSD: 0,"WIN BIG with MTN
inData =
When I use an expected answer like "Balance" (which would be the right one to use) the result is this:
AT+CUSD=1,"*114#"
OK
+CUSD: 0,"WIN BIG with MTN CHALILA on any Bundle bought using *777#. Dial NOW))))))))))
with the ))) going on into eternity.
Any advice on how I could tweak sendATcommand to do the ritgh thing?
The full result:
WIN BIG with MTN CHALILA on any Bundle
bought using *777#. Dial NOW!Your Account
Balance is K85.5280
The sketch currently:
String inData;
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
CheckBalance();
}
void loop() {
}
void CheckBalance()
{
inData = "";
Serial2.println(F("AT+CMGF=0"));
delay(500);
while(Serial2.available() != 0)
Serial.write(Serial2.read());
Serial2.println(F("AT+CUSD=1,\"*114#\""));
delay(5000);
while (sendATcommand(F("AT+CUSD=1,\"*114#\""), F("777"), 8000) == 0);
while(Serial2.available() != 0)
{
char sim900response = Serial2.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[100];
unsigned long previous;
memset(response, '\0', 100); // initalize string
delay(100);
while (Serial2.available() > 0)
Serial2.read(); // clears the buffer
Serial2.println(ATcommand);
x = 0;
previous = millis();
const char* expected_answer_pointer = (const char PROGMEM *)expected_answer;
do
{
if (Serial2.available() != 0)
{
response[x] = Serial2.read();
x++;
if (strstr_P(response, expected_answer_pointer) != NULL)
{
answer = 1;
}
}
}
while ((answer == 0) && ((millis() - previous) < timeout));
Serial.println(response);
return answer;
}