SMS over 3G and Bluetooth from Python
Posted by Diarmuid on June 26, 2008
I wrote a fairly well received blog post on using those nice 3G modems from Vodafone and O2 in Linux to send SMS. It was based on using the modems through minicom, but since minicom uses a straight serial connection it is possible to use the modems through code, in my case Python.
The following code shows how to send an SMS via a 3G USB modem.
import serial
def SendVia3G():
ser = serial.Serial(‘/dev/ttyUSB1’, 115200, timeout=1)
ser.write(‘ATZ\r’)
ser.write(‘AT+CMGF=1\r’)
ser.write(‘AT+CMGS=”+353868276XXX”\r’)
ser.write(‘SMS over 3G but from Python\n’)
ser.write(chr(26))
line = ser.readline() #read a ‘\n’ terminated line
print line
ser.close()
Obviously the serial extensions for python have to be installed. I use apt.
apt-get install python-serial
You can also use some similiar code to send the SMS via a mobile phone that is connected via bluetooth
apt-get install python-serial python-bluetooth
import bluetooth
import serial
def SendViaBluetooth():
sockfd = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sockfd.connect((’00:12:D2:7A:XX:XX’, 1)) # BT Address
sockfd.send(‘ATZ\r’)
sockfd.send(‘AT+CMGF=1\r’)
sockfd.send(‘AT+CMGS=”+353868276XXX”\r’) # TO PhoneNumber
sockfd.send(‘SMS over Bluetooth\n’)
sockfd.send(chr(26)) # CTRL+Z
sockfd.close(
You might need to mess around with python and bluetooth to make sure the channel is right for your phone (1 above in the connect line). 1 is for Nokias.
I’ve created a little class (although it’s a while since I wrote this and I may have got inspiration from somewhere as it looks too good to be my code!!!) that makes using the E220 a little easier in more complicated use cases.
import bluetooth
import serial
class HuaweiModem(object):
def __init__(self):
self.open()
def open(self):
self.ser = serial.Serial(‘/dev/ttyUSB2’, 115200, timeout=1)
self.SendCommand(‘ATZ\r’)
self.SendCommand(‘AT+CMGF=1\r’)
def close(self):
self.ser.close()
def SendSMS(self, address, message):
command = ‘AT+CMGS=”%s”\r’%address
self.SendCommand(command,getline=False)
command = ‘%s\n’%message
self.SendCommand(command,getline=False)
self.SendCommand(chr(26),getline=False)
def GetAllSMS(self):
self.ser.flushInput()
self.ser.flushOutput()
command = ‘AT+CMGL=”all”\r’
print self.SendCommand(command,getline=False)
self.ser.timeout = 2
data = self.ser.readline()
print data
while data !=”:
data = self.ser.readline()
if data.find(‘+cmgl’)>0:
print data
def SendCommand(self,command, getline=True):
self.ser.write(command)
data = ”
if getline:
data = self.ReadLine()
return data
def ReadLine(self):
data = self.ser.readline()
print data
return data
h = HuaweiModem()
h.GetAllSMS()
h.SendSMS(‘+353868276XXX’,’A Nice message from Bluekulu.com’)
h.close()
Happy texting!!! and check out http://www.Bluekulu.com. Bluetooth Marketing Specialists.
Apologies for the formating but WordPress is deleting the leading spaces from the code above. Use this link to get a text file.
Jonas said
Hi, i realy like your program, but i wonder if you could do a program that show the huawei e220 signal strength and if possible in percent. i would realy apreciate that. 🙂
Have a nice day!
/jonas
Ender said
SMS works with an Orange’ Huawei E270 USB 3G modem on openSUSE 11
Great work, thank you very much!
yonson said
i was wondering if it possible to receive sms using some kind of listener, i was working on a web application and i want it to receive SMS
Thanks
Myke said
Hi, I’m new with python about 3 days old using the program. I was wondering how I can create an application that will detect all BT device within range and allow it to send a file(a command maybe) and for the system to send message to them. I was able to do the detect but having hard time sending and receiving. Does a code require a specific modem to work? or it can work using any BT device?
If you have a guide or a working sample that would be very helpfull
Thanks,
Myke
septianw said
thanks for post this one.
it would be help me to develop more.
let me grab it.
David Faure said
Excellent stuff, exactly what I was looking for.
Please note that the blog software replaced all quotes and double-quotes with non-ascii characters, which python doesn’t like (and the indenting got messed up too). It would probably be helpful to provide a link to a plain-text version of it for proper download rather than copy/paste/reformatting by hand 😉
Also the “if data.find(’+cmgl’)>0:” seems wrong, here it’s +CMGL in uppercase, and it’s at offset 0, and anyway the text of the SMS is on the following lines. So I recommend the following while loop instead:
while data != ”:
data = self.ser.readline()
if data.find(‘+CMGL’)>=0:
print·
print string.rstrip(data)
I was wondering about writing a GUI app to do this. But maybe it would be simpler to just improve the python script so that it sends an email for each SMS it sees in GetAllSMS, so that one can get his incoming SMS messages in his email client, no need to monitor yet another application.
nml said
http://www.bluekulu.com/files/3GSMS.pyy
Jonas said
hi, anybody knows how to do arbitrary calls? i got a prepaid UMTS stick (huawei e1550), and in order to load vouchers you have to call *103*SPECIALNUMBER#. doing so via ATD*… will fail (NO CARRIER). have not tried to do it in my cellphone so far, but afaik it would also do an ATD?.
it seems that only ATD*98# and ATD*99# are possbile. then i get CONNECT as reply. (i even would get an ip etc., though i will be redirected to a page telling me that i have to pay first 😉 )
ok. to put it short: are there different ways of calling? any hint, why ATD*103*…# won’t work? or maybe, anybody knows how to check how “mobile partner” software acomplishes this?
devender said
thanks Diarmuid,
i was looking for a labview application, found your post very helpful. regards. DN.
Conductive Hearing Loss said
your works are really improving traffic then you can do some social media service, social bookmarking and press release distribution are very powerful for increase traffic to your website
funny white elephant gift said
What is it: Christmas, among other things, is a time for goodies.
It seems sometimes people are looking for that something extra
to pull the look of a room in their home together.
So, oft in theologic wars The disputants, I ween, Rail on in utter ignorance Of what each other mean; And prate about an Elephant Not one of them has seen.
http://www.incaradvancements.co.uk said
You should take part in a contest for one of the most useful
sites online. I will recommend this site!
Leisha Eichhorst said
Do you mind if I quote a several of your posts as long as I provide credit and sources back to your webpage: %BLOGURL%. I’m going to aslo be sure to give you the appropriate anchor text link using your webpage title: %BLOGTITLE%. Be sure to let me know if this is okay with you. With thanks
como fazer meu namorado feliz said
Aos 28 eu parei de tomar Belara e continuei, bela. https://parentingunion.co.uk/profile.php?a=28