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="+353868276111"\r') # TO PhoneNumber sockfd.send('SMS over Bluetooth\n') sockfd.send(chr(26)) # CTRL+Z sockfd.close() def SendVia3G(): ser = serial.Serial('/dev/ttyUSB1', 115200, timeout=1) ser.write('ATZ\r') ser.write('AT+CMGF=1\r') ser.write('AT+CMGS="+353868276111"\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() 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('+353868276111','A nice message from Bluekulu') h.close() SendViaBluetooth() SendVia3G()