1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
| ##### Config #####
sms_password = 'mypassword'
##### Constants #####
TRUE = 1
FALSE = 0
##### Modules #####
#Use serial
import SER
#Use build in module
import MOD
#Use AT command interface
import MDM
#Use GPS
import GPS
###### General Functions ######
#Debug message
def debugmsg(msgtext):
msgtext = msgtext.replace('\r', '\\r')
msgtext = msgtext.replace('\n', '\\n')
print msgtext
SER.send(msgtext + '\r\n')
#f = open('log.txt','ab')
#f.write(msgtext + '\n')
#f.close()
#GPS status
def gps_status(gpspos):
debugmsg('Retrieving GPS status')
gpspos_parts = gpspos.split(',')
if ( (gpspos_parts[5] == '2') or (gpspos_parts[5] == '3') ): #2D or 3D fix
debugmsg('GPS fix "' + gpspos_parts[5] + '" ie valid');
status = TRUE
else:
debugmsg('GPS fix "' + gpspos_parts[5] + '" ie not valid');
status = FALSE
return status
###### SMS Process Functions ######
#Process SMS messages
def sms_proceses(gpspos):
debugmsg('Process SMS')
#List SMS
smsmsgs = sms_list()
totalsms = len(smsmsgs)
#debugmsg('SMS total: %d' % totalsms)
#Go throguh SMS messages
for smsmsgindex in range(0, totalsms):
debugmsg('Message: %d' % smsmsgindex)
#Delete SMS (it is being processed now)
status = sms_delete(smsmsgs[smsmsgindex]['id'])
#If status deleted ok
if (status == TRUE):
#debugmsg('List SMS id: %d, from: %s, msg: %s' % (smsmsgs[smsmsgindex]['id'], smsmsgs[smsmsgindex]['from'], smsmsgs[smsmsgindex]['msg']))
#Split SMS message into parts
smsmsgparts = smsmsgs[smsmsgindex]['msg'].split(' ')
#If at least 2 parts
if (len(smsmsgparts) > 1):
#Check if password valid
if (smsmsgparts[0].lower() == sms_password):
debugmsg('Password valid')
#If position requested
if (smsmsgparts[1].lower() == 'pos') or (smsmsgparts[1].lower() == 'position'):
debugmsg('Action: Position requested')
#If GPS position fix valid
if (gps_status(gpspos) == TRUE):
gpsdataparts = gpspos.split(',')
senddata = 'Lat: ' + gpsdataparts[1] + ', Lon: ' + gpsdataparts[2] + ', Heading: ' + gpsdataparts[6] + ', Speed: ' + gpsdataparts[7] + ' km/hr'
else:
senddata = 'No GPS Fix'
#Send SMS
sms_send(smsmsgs[smsmsgindex]['from'], senddata)
else:
debugmsg('Action: Unknown')
else:
debugmsg('Password invalid')
else:
debugmsg('Unable to delete')
###### SMS Library Functions ######
#Setup SMS
def sms_setup():
debugmsg('Setting up SMS')
MDM.send('AT+CMGF=1\r', 0)
res = MDM.receive(50)#5 sec
MOD.sleep(1)#wait 0.1sec
debugmsg('SMS setup: ' + res)
#List SMS
def sms_list():
#Note: Command will not return anything if SIM is not ready
debugmsg('List SMS')
#MDM.send('AT+CMGL="REC UNREAD"\r', 0) #ALL REC UNREAD STO SENT
MDM.send('AT+CMGL="REC UNREAD"\r', 0)
#smslist = ''
#res = MDM.receive(50)#5 sec
#smslist = smslist + res
#while (res.find('\r\nOK\r\n') == -1):
# res = MDM.receive(10)
# smslist = smslist + res
smslist = ''
res = ''
timeout = MOD.secCounter() + 60
while ( (res.find('\r\nOK\r\n') == -1) and (MOD.secCounter() < timeout) ):
debugmsg('Timeout now: %d timeout: %d' % (MOD.secCounter(), timeout))
res = MDM.receive(50)
smslist = smslist + res
#debugmsg('get %s' % smslist)
MOD.sleep(1)#wait 0.1sec
smsparts = smslist.split('\r\n')
smspartslen = len(smsparts)
#debugmsg('SMS Parts %d' % smspartslen)
smsmsgs = []
if (smspartslen-2 > 0) and (smsparts[smspartslen-2] == 'OK'):
#If there is at least 1 message
if (smspartslen >= 6):
#Go through all messages
for partno in range(1, smspartslen):
#Find out if this is an cmgl line
cmglparts = smsparts[partno].split('CMGL: ')
#If at least 2 parts, leading cmgl and data
if (len(cmglparts) > 1) and (cmglparts[0] == '+') and (partno+1 <= smspartslen):
#debugmsg('Found CMGL %d: %s' % (partno, smsparts[partno]))
#debugmsg('Text %d: %s' % (partno+1, smsparts[partno+1]))
#Split msg info line into parts
msginfoparts = cmglparts[1].split(',')
#Check have all parts
#if (len(msginfoparts) > 5):
if (len(msginfoparts) > 4):
#int() may fail
try:
#Convert id to integer
msginfoparts[0] = int(msginfoparts[0])
#Remove quotes
msginfoparts[2] = msginfoparts[2].replace('"', '')
#msginfoparts[4] = msginfoparts[4].replace('"', '')
#msginfoparts[5] = msginfoparts[5].replace('"', '')
smsmsgs.append({'id': msginfoparts[0], 'from': msginfoparts[2], 'msg': smsparts[partno+1]})
debugmsg('SMS id: %d, from: %s, msg: %s' % (msginfoparts[0], msginfoparts[2], smsparts[partno+1]))
except Exception:
debugmsg('Error CMGL %d: %s' % (partno, smsparts[partno]))
debugmsg('SMS total: %d' % len(smsmsgs))
else:
debugmsg('No SMS messages available')
return smsmsgs
#SMS Delete
def sms_delete(delindex):
debugmsg('SMS Delete: %d' % delindex)
MDM.send('AT+CMGD=' + str(delindex) + '\r', 0)
res = MDM.receive(50)#5 sec
MOD.sleep(1)#wait 0.1sec
if (res == '\r\nOK\r\n'):
status = TRUE
else:
status = FALSE
return status
#SMS Send
def sms_send(to, text):
debugmsg('Send SMS to: %s, MSG: %s' % (to, text))
MDM.send('AT+CMGS="' + to + '"\r', 0)
res = MDM.receive(50)#5 sec
MOD.sleep(1)#wait 0.1sec
#Check for SMS prompt
if (res == '\r\n> '):
#Send SMS message text
MDM.send(text, 0)
#End SMS
MDM.sendbyte(0x1A, 0)
res2 = MDM.receive(180)#5 sec
MOD.sleep(1)#wait 0.1sec
if (res2.find('\r\nOK\r\n') != -1):
debugmsg('SMS sent')
status = TRUE
else:
debugmsg('SMS Send: ' + res2)
debugmsg('Did not get SMS sent confirmation')
status = FALSE
else:
debugmsg('SMS Send: ' + res)
debugmsg('Did not receive SMS prompt')
#Abort SMS (just in case)
MDM.sendbyte(0x1B, 0)
MOD.sleep(1)#wait 0.1sec
status = FALSE
return status
##################################################################################
###### Init ######
SER.set_speed('115200','8N1')
SER.send('\r\n--------------------\r\n\r\n')
debugmsg('Running...');
#Set verbose error reporting
MDM.send('AT+CMEE=2\r', 0)
MDM.receive(50)#5 sec
MOD.sleep(1)#wait 0.1sec
#May be required in North America / Canada to switch the unit to 900/1900MHz frequency (uncomment if in those areas)
#MDM.send('AT#BND=1\r', 0)
#MDM.receive(1)#0.1sec
#MOD.sleep(1)#wait 0.1sec
gpspos_lastvalid = ''
#Setup SMS
sms_setup()
#Main loop
while 1:
debugmsg('Entering loop')
#Retrieve current position
gpspos = GPS.getActualPosition()
debugmsg('Position: %s' % gpspos)
#Retrieve GPS fix status
gps_statusnow = gps_status(gpspos)
#Save last valid position
#If position fix valid, or none recorded already, use last retrieved
if ( (gps_statusnow == TRUE) or (gpspos_lastvalid == '') ):
gpspos_lastvalid = gpspos
#Check for / process new SMS messages
sms_proceses(gpspos_lastvalid)
debugmsg('Powersave for 10 seconds')
#Powersave for 10 seconds
MOD.powerSaving(10) |
Partager