projects:main4.py
TRUE = 1 FALSE = 0 import SER import MOD import MDM import GPS import GPIO nofix_count = 0 cutdown_count = 0 status = FALSE #Misc Functions 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 nofix_count = 0 else: debugmsg('GPS fix "' + gpspos_parts[5] + '" ie not valid'); status = FALSE nofix_count = nofix_count + 1 debugmsg('2') if (gpspos_parts[10] == '00'): debugmsg('GPS sats "' + gpspos_parts[10] + '" ie not valid'); debugmsg('0') nofix_count = nofix_count + 1 debugmsg('1') return status #SMS Send def sms_send(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): status = TRUE else: status = FALSE MDM.receive(1); # may prevent +CMS ERROR: 331 crash else: #Abort SMS (just in case) MDM.sendbyte(0x1B, 0) MOD.sleep(1)#wait 0.1sec status = FALSE return status #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 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 if (nofix_count > 10): debugmsg('Resetting GPS') res = GPS.resetMode(1) #Cold resets GPS module if (res == '-1'): debugmsg('Error unable to reset GPS module') res = GPS.resetMode(0) #Hardware restart nofix_count = 0 debugmsg('No Fix Count: %d ' % nofix_count) #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") 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 = 'No GPS Fix' + gpspos_lastvalid debugmsg(senddata) #Send SMS debugmsg('Sending Text') #sms_send('xxxxxxxxx', senddata) senddata_bot = 'track@natrium42.com N:firefly,' + senddata #sms_send('07766404142', senddata_bot) debugmsg(senddata_bot) debugmsg('Powersave for 45 seconds') MOD.powerSaving(45)
projects/main4.py.txt · Last modified: 2008/07/19 23:33 by 127.0.0.1