Mortadha Dahmani
Published © LGPL

Py-SIM800L-USSD

Unstructured Supplementary Service Data (USSD) network result parser with Python language

BeginnerFull instructions provided1 hour1,495
Py-SIM800L-USSD

Things used in this project

Hardware components

SIMCom Wireless Solutions SIM800L
×1
Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
SparkFun FTDI Basic Breakout - 3.3V
SparkFun FTDI Basic Breakout - 3.3V
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Rosin Mildly Activated RMA
Solder Flux, Rosin Mildly Activated RMA
Premium Female/Female Jumper Wires, 40 x 3" (75mm)
Premium Female/Female Jumper Wires, 40 x 3" (75mm)

Story

Read more

Schematics

UART Connexion

Code

USSDDecode.py

Python
#!/usr/bin/python3
# -*- coding: utf8 -*-

import time
import serial

def USSDDecode():
    rep = phone.read_until('\n').decode('latin1', "replace")
    #print(rep)

    if rep.find("+CUSD: 0") >=0:
        #print("0")
        rep0 = rep[rep.find("+CUSD: ")+len("+CUSD: 0, \""):len(rep)-7]
        try:
            int(rep0, 16)
            #print ('it is hex')
            rep0 = bytes.fromhex(rep0).decode('latin1', "replace")
            rep0 = rep0.replace(chr(00), "")
            #print(rep0)
            rep=rep0
        except ValueError:
            #print ('it is not hex')
            #print(rep0)
            rep=rep0
    elif rep.find("+CUSD: 1") >=0:
        #print("1")
        rep1 = rep[rep.find("+CUSD: ")+len("+CUSD: 1,\""):len(rep)-7]
        try:
            int(rep1, 16)
            #print ('it is hex')
            rep1 = bytes.fromhex(rep1).decode('latin1', "replace")
            rep1 = rep1.replace(chr(00), "")
            #print(rep1)
            rep=rep1
        except ValueError:
            #print ('it is not hex')
            #print(rep1)
            rep=rep1
    elif rep.find("+CUSD: 2") >=0:
        #print("2")
        rep="Not supported by network"
    return rep

phone = serial.Serial("COM14",  115200, timeout=2)
phone.flushInput()
phone.flushOutput()

phone.write(b'AT')
time.sleep(1)

phone.write(b'AT+CFUN=1')
time.sleep(1)

rep = phone.read_until('\n').decode('latin1', "replace")
#print(rep)
try:
    phone.write(b'ATZ\r')
    time.sleep(0.5)
    rep = phone.read_until('\n').decode('latin1', "replace")
    print(rep)
    
    phone.write(b'ATE1\r')
    time.sleep(0.5)
    rep = phone.read_until('\n').decode('latin1', "replace")
    print(rep)
    
    phone.write(b'AT+CUSD=1,"*123#",15\r')
    time.sleep(2)
    print(USSDDecode())
    phone.flush()
    
    phone.write(b'AT+CUSD=1,"*101#",15\r')
    time.sleep(2)
    print(USSDDecode())
    phone.flush()

    phone.write(b'AT+CUSD=1,"*110#",15\r')
    time.sleep(2)
    print(USSDDecode())
    phone.flush()
    
    phone.write(b'AT+CUSD=1,"2"\r')
    time.sleep(2)
    print(USSDDecode())
    phone.flush()
    
    phone.write(b'AT+CUSD=1,"1"\r')
    time.sleep(2)
    print(USSDDecode())
    phone.write(bytes([26]))
    
    phone.flushInput()
    phone.flushOutput()
    time.sleep(.5)

finally:
    phone.close()

'''Revision History

    Initial Release : 25 Mars 2020

License

    Py-SIM800L is distributed under the LGPL version 3 license.
'''

Credits

Mortadha Dahmani

Mortadha Dahmani

1 project • 2 followers

Comments