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 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.