sim800L USSD respond forward to blynk terminal

I am sending USSD to *100# to get prepaid simcard balance, TinyGsmclient library has function .sendUSSD(), and its return empty string after use, any ideas how to use it?


#include <Arduino.h>
#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>

char auth[] = "XXX";

char apn[] = "internet";
char user[] = "";
char pass[] = "";

#include <SoftwareSerial.h>
SoftwareSerial SerialAT(2, 3); // RX, TX

TinyGsm modem(SerialAT);
WidgetTerminal terminal(V1);
BlynkTimer timer;

String cache = "";

BLYNK_WRITE(V1)
{
  if (String("tili") == param.asString())
  {
    terminal.println(cache);
    terminal.flush();
  }
}

void setup()
{

  Serial.begin(9600);

  delay(10);

  SerialAT.begin(9600);
  delay(3000);

  Serial.println("Initializing modem...");
  modem.restart();

  Blynk.config(modem, auth, "XXX", 80);
  modem.simUnlock("1234");
  Blynk.connectNetwork(apn, user, pass);

  cache = modem.sendUSSD("*100#");

  Serial.println(cache);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Perhaps you could try with a basic example from TinyGSM library ( there is AllFunctions.ino example in the package where modem.sendUSSD() has been used) and this way verify if it works with your modem/operator.

I spend some time to figure out,
in library function

   String sendUSSD(const String& code) {
       sendAT(GF("+CMGF=1"));
       waitResponse();
       sendAT(GF("+CSCS=\"HEX\""));
       waitResponse();
       sendAT(GF("+CUSD=1,\""), code, GF("\""));
       if (waitResponse() != 1) {
         return "";
       }
       if (waitResponse(10000L, GF(GSM_NL "+CUSD:")) != 1) {
         return "";
       }
       stream.readStringUntil('"');
   String hex = stream.readStringUntil('"');

   stream.readStringUntil(',');
   int dcs = stream.readStringUntil('\n').toInt();

   if (dcs == 15) {
     return TinyGsmDecodeHex8bit(hex);
   } else if (dcs == 72) {
     return TinyGsmDecodeHex16bit(hex);
   } else {
     return hex;
   }
 }

So my operator use GSM, not HEX, and at the end of message digit 15, HEX decoder must be disable.

   String sendUSSD(const String& code, const String& format){//format: GSM,UCS2,HEX,IRA,PCCP,PCDN,8859-1
       sendAT(GF("+CMGF=1"));
       waitResponse();
       sendAT(GF("+CSCS=\""), format, GF("\""));
       waitResponse();
       sendAT(GF("+CUSD=1,\""), code, GF("\""));
       if (waitResponse() != 1) {
         return "";
       }
       if (waitResponse(10000L, GF(GSM_NL "+CUSD:")) != 1) {
         return "";
       }
       stream.readStringUntil('"');
   String hex = stream.readStringUntil('"');

   stream.readStringUntil(',');
   int dcs = stream.readStringUntil('\n').toInt();

   if (dcs == 15 && format == "HEX") {
     return TinyGsmDecodeHex8bit(hex);
   } else if (dcs == 72 && format == "HEX") {
     return TinyGsmDecodeHex16bit(hex);
   } else {
     return hex;
   }
 }

I am done @vshymanskyy

2 Likes