TRUE = 1 FALSE = 0 import SER import MOD import MDM import GPS import GPIO global nofix_count global status global status1 global log_count track_count = 0 log_count = 0 nofix_count = 0 maxalt = 0 def reset_gps(): MDM.send('AT$GPSR=1\r', 0) temp = MDM.receive(1) def debugmsg(msgtext): msgtext = msgtext.replace('\r', '\\r') msgtext = msgtext.replace('\n', '\\n') SER.send(msgtext + '\r\n') #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 def gps_nofix(gpspos): global nofix_count gpspos_parts = gpspos.split(',') if ((gpspos_parts[5] == '2') or (gpspos_parts[5] == '3') ): #2D or 3D fix nofix_count = 0 else: nofix_count = nofix_count + 1 if (gpspos_parts[10] == '00'): nofix_count = nofix_count + 1 else: nofix_count = 0 if (nofix_count > 20): debugmsg('Resetting GPS') reset_gps() nofix_count = 0 debugmsg('No Fix Count: %d ' % nofix_count) #SMS Send def sms_send(to, text): global status1 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') status1 = TRUE else: debugmsg('SMS Send: ' + res2) debugmsg('Did not get SMS sent confirmation') status1 = 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 status1 = FALSE return status1 #Setup SMS def sms_setup(): MDM.send('AT+CMGF=1\r', 0) res = MDM.receive(50)#5 sec MOD.sleep(1)#wait 0.1sec def cutdown(alt): debugmsg('Check Altitude') alt_temp = alt.split('.') debugmsg(alt_temp[0]) alt_int = int(alt_temp[0]) if (alt_int > 5000): debugmsg('Cutdown') GPIO.setIOvalue(4, 1) MOD.sleep(30) GPIO.setIOvalue(4, 0) else: debugmsg('Still Below 5000m') def camera(): GPIO.setIOvalue(6, 1) MOD.sleep(10) GPIO.setIOvalue(6, 0) def radio_on_of(on_off): if (on_off == 'on'): debugmsg('Radio on') GPIO.setIOvalue(7, 0) elif (on_off == 'off'): debugmsg('Radio off') GPIO.setIOvalue(7, 1) def dit(): MOD.sleep(2) GPIO.setIOvalue(3, 1) MOD.sleep(1) GPIO.setIOvalue(3,0) def dash(): MOD.sleep(2) GPIO.setIOvalue(3, 1) MOD.sleep(2) GPIO.setIOvalue(3,0) def morse_code(number): MOD.sleep(3) if (number == '.'): debugmsg('Dot') elif (number == 'N'): debugmsg('N') elif (number == 'S'): debugmsg('S') elif (number == 'E'): debugmsg('E') elif (number == 'W'): debugmsg('W') elif (number == '-'): debugmsg('-') dit() dash() dit() else: int_number = int(number) debugmsg('%d' % int_number) if (int_number == 0): dash() dash() elif (int_number == 1): dit() elif (int_number == 2): dit() dit() elif (int_number == 3): dit() dit() dit() elif (int_number == 4): dit() dit() dit() dit() elif (int_number == 5): dash() elif (int_number == 6): dash() dit() elif (int_number == 7): dash() dit() dit() elif (int_number == 8): dash() dit() dit() dit() elif (int_number == 9): dash() dit() dit() dit() dit() else: debugmsg('Not between 0 and 9') def morse_splitter(coord): debugmsg(coord) j = 0 while j < len(coord): morse_code(coord[j]) j = j + 1 def log_data(data): global log_count if(log_count > 5): debugmsg('writing to log file') file = open('data.txt','a') file.write(data) file.close() log_count = 0 else: debugmsg('no writing to log file') log_count = log_count + 1 debugmsg('Running...'); gpspos_lastvalid = '' sms_setup() #Main loop while 1: debugmsg('Entering loop') debugmsg('Click') camera() #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 debugmsg('3') gps_nofix(gpspos) #If GPS position fix valid if (gps_status(gpspos) == TRUE): GPIO.setIOdir(1, 1, 1) gpsdataparts = gpspos.split(',') senddata = 'T:' + gpsdataparts[0] + ',Lat:' + gpsdataparts[1] + ',Lon:' + gpsdataparts[2] + ',Alt:' + gpsdataparts[4] + ',H:' + gpsdataparts[6] + ',S:' + gpsdataparts[7] + 'km/hr' cutdown(gpsdataparts[4]) #Morse code radio_on_of("on") MOD.sleep(10) morse_splitter(gpsdataparts[1]) MOD.sleep(10) morse_splitter(gpsdataparts[2]) MOD.sleep(10) morse_splitter(gpsdataparts[4]) radio_on_of("off") if(gpsdataparts[4] > maxalt): maxalt = gpsdataparts[4] debugmsg(maxalt) else: GPIO.setIOdir(1, 0, 1) radio_on_of("on") GPIO.setIOvalue(3, 1) MOD.sleep(50) GPIO.setIOvalue(3,0) radio_on_of("off") senddata = 'NF' + gpspos_lastvalid debugmsg(senddata) debugmsg('Sending Text') sms_send('07748628528', senddata) if(track_count > 3): senddata_bot = 'track@natrium42.com N:firefly,' + senddata sms_send('07766404142', senddata_bot) debugmsg(senddata_bot) track_count = 0 else: track_count = track_count + 1 log_data(senddata) debugmsg('Powersave for 45 seconds') MOD.powerSaving(45)