You are on page 1of 3

import

import
import
import

MDM
SER
MOD
SER2

print '\n initialize\r'


a = MDM.send('AT+CMGF=1\r', 2)
print 'Setting Text Mode' #AT+CMGF mode 1 = text mode 0 = PDU mode (Default)
#clear Receive buffer
res = MDM.receive(5)
#clear Receive buffer
res = MDM.receive(5)
a = MDM.send('AT+CNMI=2,1,0,0,0\r', 2)
#clear Receive buffer
res = MDM.receive(5)
SER.set_speed('115200','8N1')
def getSMSnumber(SMSpos):
#setup command for reading message
SMS_read_command = 'AT+CMGR='+str(SMSpos)+'\r'
#clear receive buffer
MDM.receive(10)
MDM.send(SMS_read_command,5)
#send the read message command
SMScontent = MDM.receive(15)
#collect the contents of the message
b = SMScontent.find('+91')
#Identify the start of the number
c = b+13
SMSnumber = SMScontent[b:c]
print 'Number Found\n \r'
print SMSnumber
#Extract the number
return(SMSnumber)
def getSMSmessage(SMSpos):
#setup command for reading message
newSMSread_command = 'AT+CMGR='+str(SMSpos)+'\r'
#clear receive buffer
MDM.receive(10)
MDM.send(newSMSread_command,5)
#read the new SMS arrived
SMScontent = MDM.receive(15)
#collect the full message with heade
r
a = SMScontent.find('\n',4) + 1
#Find the end of the first line
b = len(SMScontent)
SMSmessage = SMScontent[a:b]
print 'Message Text Captured\n '
print SMSmessage
#extract message from response
return(SMSmessage)
def sendSMS( number, smstext):
#Send command for sending message
a = MDM.send('AT+CMGS="' + number + '"\r', 2)
#clear receive buffer
res = MDM.receive(10)
TEXT= 'Received Command'+"\""+smstext+"\""
a = MDM.send(TEXT, 2)
#Send body of message
#this terminates the message and sends it
a = MDM.sendbyte(0x1A, 2)
print 'Message Sent\n \r'
#terminating the message require
ctrl-z
return()
def gprsTurnOFF():
MDM.send('AT#GPRS=0\r',2)
res=MDM.receive(20)

def gprsTurnON():
a = SER.set_speed('115200','8N1')
a = MDM.send('AT+CGDCONT=1,"IP",'+APN,0)
a = MDM.send('"\r',0)
s = MDM.receive(20)
MOD.sleep(10)
def getGPS():
SER.send("\n \r GPS data \n \r")
SER2.set_speed('4800','8N1')
gpsbuf = SER2.receive(10)
return(gpsbuf)
def findNetwork():
a = MDM.send('AT#MONI\r',2)
res = MDM.receive(10)
SER.send("\n \r")
b = res.find('airtel')
c = res.find('vodafone')
if (b != -1):
return('airtel')
if(c != -1):
return('vodafone')
while 1:
print 'waiting for SMS\r'
#SMS Processing
SER.send("\n \n \n \r Initlize\n\r")
#Identify if a SMS was received
a = res.find('+CMTI: "SM",')
#Process Received SMS Message
if (a != -1):
print '\n NEW SMS RECEIVED\r'
SER.send('NEW SMS')
#Identify the position of memory location
firstdigit_pos = a + 12
#Extract Memory location as string
pos = res[firstdigit_pos:-2]
#Retrieve Sending Number
SMSnumber = getSMSnumber(pos)
SER.send(SMSnumber)
#Retrieve Message
SMSmessage = getSMSmessage(pos)
#Message processing
sendSMS(SMSnumber, SMSmessage)
SER.send(SMSmessage)
MOD.sleep(2)
#End of SMS Processing
#GPS Processing
SER.send('Finding Network \n \r')
network=findNetwork()
SER.send("Registered network :"+network)
gpsdata=getGPS()
c=gpsdata.find('GPRMC')
gpsdata=gpsdata[c:c+38]
SER.send(gpsdata+"\n \r")
MOD.sleep(20)
#SER.send("\r\n*********************************\n\r")
#END of GPS Processing

#HTTP Processing
#END of HTTP Processing
#end of while