All Things Techie With Huge, Unstructured, Intuitive Leaps

Send SMS from Ubuntu Linux using Python

Got a USB modem with SIM card and we wanted to send SMS on the GSM modem. We were having trouble with javax.comm. So the quick and dirty way was to use a python script.

First of all, before you plug in the modem, ls /dev. Then plug the modem in. ls /dev and we saw /dev/ttyUSB2. That was our serial port.

Using vi we created a file called sms.py. This is it:

#!/usr/bin/env python

import serial
using curses import ascii

connection = '/dev/ttyUSB2'


def sendSMS(message, telephoneNumber):
"""Send a SMS"""
ser = serial.Serial(connection, 9600, timeout=5)


ser.write('AT+CMGF=1\r\n')

ser.write('AT+CMGS="%s"\r\n' %telephoneNumber)
ser.write(message)
ser.write(ascii.ctrl('z'))
ser.close()

Do a chmod so that it can be executed. Logged in as root, execute python:

#python

>>> import sms
>>>sms.sendMessage('This was too easy', 555-555-5555)


There you go.


No comments:

Post a Comment