I have ever discussed about developing a simple SMS gateway system using Gammu.
But it was only able to send message without ability in receiving message.
And now, I will present the complement of the last article. This is still about SMS gateway
using Gammu, yet I will use python-gammu instead of Gammu. Beside that I will use
wammu and gammu to help generating a configuration and testing the connection.
The first step is installing all the necessary programs such as Geany(Programming Environment),
Python Compiler, Python-gammu, Wammu, Gammu, MySQL, python-mysqldb
(for saving messages into database) and Cron (it’s default
installed by Linux). If you are using Ubuntu, all this programs can be found in Synaptic Package Manager.
None of those programs is difficult to install but if you are facing problems in installation,
you can find the solution from their official website.
The second is attempting to check the conection between the computer and the handphone.
To do this step, you firstly open wammu, and choose automatic detection choice. If this goes correctly,
than you will find a file named .gammurc in the home folder. If not so, then you can use Bash command which is
‘locate gammurc’ to track where the configuration file goes. Then, place the file to home folder!
Try to test your connection to your phone by executing ‘gammu –identify’ in console. There is much posibility
in unsuccessfully connection caused by the absence of the configuration file so you have to
consider it carefully. Here is some samples.
[gammu]
port=/dev/ttyACM0
connection=at19200
name=Sony Ericsson K610i
After those steps have gone well, you are ready to start to make the program.
This is for example
#!/usr/bin/env python
import gammu
import MySQLdb
def validitas_mesg(x): #function for checking message validity, if this return false then the message will not be processed
h = 0
mesg = “”
while h < 6:
mesg = mesg + x[h]
h = h + 1
if mesg == ‘Kosong’: #if first six letters compose “Kosong”
h = 6
mesg = x[h]
if mesg == ‘#’: #if sixth character is ‘#’
h = 17
mesg = x[h]
if mesg == ‘#’: #if seventieth character is ‘#’
h = 23
mesg = x[h]
if mesg == ‘#’: #if 23rd character is ‘#’
return True
else:
return False
else:
return False
else:
return False
else:
return False
def ambil_tgl(x): //function for receiving date
i = 7
tgl =”"
while i < 17:
tgl = tgl + x[i]
i = i + 1
return tgl
def ambil_jam(x): //function for receiving hour
j = 18
jam = “”
while j < 23:
jam = jam + x[j]
j = j + 1
return jam
def ambil_ruang(x): //function for receiving compartment information
k = 24
ruang = “”
while k < 28:
ruang = ruang + x[k]
k = k + 1
return ruang
sm = gammu.StateMachine()
sm.ReadConfig()
sm.Init()
status = sm.GetSMSStatus()
conn = MySQLdb.connect (host = “localhost”,
user = “root”,
passwd = “root”,
db = “smsbolos”
cursor = conn.cursor()
remain = status['SIMUsed'] + status['PhoneUsed']
start = True
while remain > 0:
if start:
sms = sm.GetNextSMS(Start = True, Folder = 0)
start = False
else:
sms = sm.GetNextSMS(Location = sms[0]['Location'], Folder = 0)
remain = remain – len(sms)
for m in sms:
if str(m['State']) == “UnRead”:
if validitas_mesg(m['Text']):
tgl = “”
jam = “”
dt = m['Text']
tgl = ambil_tgl(dt)
jam = ambil_jam(dt)
ruang = ambil_ruang(dt)
cursor.execute(“INSERT INTO msg VALUES (now(), \”%s\”, \”%s\”, \”%s\”, \”%s\”
” % (m['Number'],tgl, jam, ruang))
else:
x = 1;
maks = len(m['Number'])
no = ”
while x < maks:
print m['Number'][x]
no = no + m['Number'][x]
x = x + 1
print no
message = {‘Text’: ‘Kosong#yyyy-mm-dd#hh:mm#U205′, ‘SMSC’: {‘Location’: 1}, ‘Number’: no}
sm.SendSMS(message)
print “null”
The first 3 lines indicate that the program is using python compiler and including the MySQLdb and Gammu
library.
#!/usr/bin/env python
import gammu
import MySQLdb
The next following code are functions which are explained by the comments on each.
Gammu starts used in the following codes which mean creating gammu object, reading the Gammu’s config file,
and starting Gammu communication to the phone.
sm = gammu.StateMachine()
sm.ReadConfig()
sm.Init()
Gammu will give the information about SMS status by calling this command.
status = sm.GetSMSStatus()
If you want to know what are the results of GetSMSStatus function, you can try to print the status variable.
Connection to MySQL is conducted by this code.
conn = MySQLdb.connect (host = “localhost”,
user = “root”,
passwd = “root”,
db = “smsbolos”
cursor = conn.cursor()
saved SMS in the inbox will be read when program execute this code
sms = sm.GetNextSMS(Location = sms[0]['Location'], Folder = 0)
the code showed above only reads our message from inbox folder. sms is an array which one of the element
is indexed with ‘State’. This element consists of UnRead or Read value.
if str(m['State']) == “UnRead”:
the above code is used to recognize while the message fetched has Read status or UnRead.
This is all my explanation regarding to the program. If you want to know it more, you can
try to print all the variables until you know its values precisely.