Popular Posts

Thursday, December 15, 2011

GETTING BUS TIMING UPDATE THROUGH SMS(USING PYTHON)


    In this modernized world SMS is one thing  which is useful in getting regular updates and information easily.
This simple project is used to get update of  bus timing through SMS.

GSM Modem (Global System for Mobile communication):
     GSM modem is a device in mobile communication for data transfer(SMS, Cal). Many GSM Modems available in  market, for our project, We use GSM Modem with serial port for sending and receiving SMS in PC using AT commands.

Requirements:

Hardware:
 *  A GSM Modem with serial port cable.
 *  PC.
 *  A Valied SIM card for communication.

Software:
 * Python 2.7,  Pyserial module for serial communication, MySQLdb module for (MYSQL data base   connectivity)
 * MySQL Database


Sample Testing:
 * Create a database(DB) named 'bus' in MySQL and in that DB create a table names 'route'
     with unnecessary fields route, time1, time2, time3 as varchar type.
 * insert values into table 'route'.
 * insert SIM card in modem and connect to the PC.
 * Run the below python code in our PC.
 * Send SMS in format, TIME<space> <route> from another mobile to the SIM which you insert in
    GSM   Modem.
  *  If you send correct route the code fetch data from the 'route' table those match from the received route
     and send back to you.
 * In case the format is wrong or wrong route it send info accordingly.

Code:
import serial
import MySQLdb


def send_sms(ser,no,sms):
    ser.write("AT+CMGS="+no+"\r")
    ch = ser.readline()
    ch = ch.strip()
    while ch != ">":
        ch = ser.readline()
        ch = ch.strip()
    ser.write(sms+"\x1A")
    ser.readline()
    sucess = ser.readline()
    if sucess == "OK":
        print "SMS send:" + sms
    else:
        print "Failed to send"
              
def delete_sms(ser,loc):
    ser.write("AT+CMGD="+str(loc)+",4\r")
    ser.readline()
    while True:
        if ser.readline() == "OK":
            break

    print "SMS Deleted sucessfully"

def open_db(dbname):
    conn = MySQLdb.Connection( host="localhost", user="root", passwd="password", db=dbname)
    print "connection object ready"
    mysql = conn.cursor()
    print "cursor object ready"
    return mysql

def modem_init():
    ser = open_serial("/dev/ttyUSB0")
    while True:
        ser.write("AT+CMGF=1\r")
        ser.readline()
        if ser.readline() == "OK":
            break
    while True:    
        ser.write("AT+CNMI=1,1,0,0,0\r")
        ser.readline()
        if ser.readline() == "OK":
            break
       
    print "modem ready for communication"
    return ser

def open_serial(port):
    ser = serial.Serial(port,9600,timeout=2)
    print "serial port opend"
    return ser

   
try:
    ser = modem_init()
    mysql = open_db("bus")
    while True:
        ms = ser.readline()
        m = ms.split(":")       
        if m[0] == "+CMTI":
            print "SMS received"
            m = ms.split(",")
            ser.write("AT+CMGR="+m[1]+"\r")
            ser.readline()
            ms = ser.readline()
            content = ser.readline()
            ser.readline()
            m = ms.split(",")
            no = m[1]
            print "Mobile NO:"+ no
            content = content.upper()
            content = content.strip()
            print "Content:"+ content
            time = content.split(" ")
            if time[0] == "TIME":
                if time[1] != " ":
                    query = "select * from route where route = '"+ time[1] +"'"
                    mysql.execute(query)
                    resultset = mysql.fetchall()
                    if resultset == ():
                        sms = "wrong route"
                        send_sms(ser, no, sms)
                        delete_sms(ser, 1)
                       
                    else:
                        sms = "%s %s %s %s"% resultset[0]
                        print sms
                        send_sms(ser, no, sms)
                        delete_sms(ser, 1)
                else:
                    sms = "Wrong format TIME<space>ROUTE no route"
                    send_sms(ser, no, sms)
                    delete_sms(ser, 1)
                   
            else:
                sms = "Wrong format TIME<space>ROUTE"
                send_sms(ser, no, sms)
                delete_sms(ser, 1)
finally:
    ser.close()
   

   

2 comments: