How to run USSD code in Linux
Tags: linux
Published: 2021-10-15 16:51:02

Recently in one of my home PC I removed windows and installed Ubuntu 20.04.3 LTS. 

One of the very first challenges I faced was making sure I can connect to the internet using my D-Link DWM-157 modem.
I was able to connect to the internet using my modem pretty easily. Now it's time to face the next one - I was not able to find a way to check my SIM balance or internet balance by running USSD code (e.g. *123# etc).

After doing some google search I came to know about ‘Modem Manager GUI’ and I was so happy. Opened my terminal and simply typed:
`sudo apt-get update
sudo apt-get install modem-manager-gui`

Someone was smiling behind the scene and was whispering not so easy boy! Prepare for the next hurdle.

I was so happy it is a very nice GUI, everything is here for me i just a single click away to check anything. Everything was working perfectly in the GUI except the USSD code !
When i submit USSD code it refuses me with following error:
> Modem Manager >= 0.7.0: GDBus.Error:org.freedesktop.ModemManager1.Error.Core.Aborted: USSD terminated by network

I did not find any work around to solve this issue. 

Then I came to know about a command line application called "gammu".
`sudo apt-get install gammu`

After installing gammu created the configuration file for gammu using the command: `gammu-detect > .gammurc`

This command created a hidden configuration file in the home folder. `ctrl+h` to view hidden files.

Opened the configuration file created and checked for the devices detected by gammu. There were devices listed as ttyUSB0, ttyUSB1, ttyUSB2 etc. ttyUSB0 to ttyUSB3 belongs to the D-Link modem.

Disconnected from the internet to send ussd code (because it was required).

Now to send a ussd, type the following command in the terminal: `gammu getussd "USSD_CODE"`. For example `gammu getussd *125#`

But each time I ran that command after waiting for some time I was refused by this error message: 
> No response in specified timeout. Probably the phone is not connected. 

Although my modem was connected properly.

I also tried with adding the device number along with the above command ie, `gammu 1 getussd *125#` or `gammu 2 getussd *125#` etc.

But i was continuously getting follow messages:
> Warning: No configuration file found!
Error: Failed to read [gammu1] section from configuration file (gammurc)!
Warning: No configuration read, using builtin defaults!
No response in specified timeout. Probably the phone is not connected.

End of another frustrating tryout!

Then i came to know that i can send ussd code using picocom by giving AT commands. To install picocom: `sudo apt install picocom`

Now again to issue the AT commands i have to disable internet connection to free the modem.

Next i need to find out the various ports of the modem using: `ls /dev/ttyU*`

Usually 3 ports will be returned :
> dialogue port (i.e. 1st port) is /dev/ttyUSB0
audio  port (i.e. 2nd port) is /dev/ttyUSB1   (audio and microphone if supported)
sms & internet aka data  port (i.e. 3rd port) is /dev/ttyUSB3

I started picocom on the dialogue port:
`sudo picocom -c /dev/ttyUSB0`
the -c means local echo on, making AT commands visible as i type.

Now check USSD:
`AT+CUSD=1,"*333#"`

But success is yet one step away!!! I was not able to type properly in the terminal. So i changed port and and started picocom on other ports available from : `ls /dev/ttyU*`

`sudo picocom -c /dev/ttyUSB1`

And finally the success! Yesssssssss…
Now i am able to run any ussd code using following format : 
`AT+CUSD=1,"*333#"`

To quit picocom, first press `CTRL+a` followed by a regular `x`

Thank you.