Sometimes the GPS signal will not be ready so i end up with gibberish i have tried to put a time wait to let it catch a signal first. Sometimes it works sometimes not i tried an if statement for str.find ("location not fix") but then i couldn't get my head around what to do after it prints that message.
Thanks!
Code: Select all
#!/usr/bin/python
import serial, time, pynmea2
#Enable Serial Communication
ser = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
#Transmitting AT Commands to the Modem
#\r indicates the Enter key
ser.write('AT'+'\r') #Check to see if modem is responding
str = ser.readline()
print str
time.sleep(.1)
ser.write('AT+CGNSPWR=1'+'\r') #Turns on the GPS power
str = ser.readline()
print str
time.sleep(.1)
ser.write('AT+CGPSSTATUS?'+'\r')
str = ser.readline()
print str
time.sleep(.1)
if str.find ("Location Not Fix") > 1:
print ("Waiting For Fix....")
ser.flushInput()
ser.flushOutput()
time.sleep(15)
ser.write('AT+CGPSOUT=0'+'\r') #Outputs raw NMEA data
str = ser.readline()
print str
time.sleep(3)
ser.write('AT+CGNSTST=1'+'\r') #Starts sending data to UART
str = ser.readline()
print str
time.sleep(5)
#Parsing Raw NMEA data through pynmea2
ser.flushInput()
ser.flushOutput()
def parseGPS(str):
if str.find("GGA") > 0:
data = pynmea2.parse(str)
print "Timestamp: %s -- Lat: %s %s -- Lon: %s %s " % (data.timestamp, data.latitude, data.lat_dir, data.longitude, data.lon_dir)
while True:
str = ser.readline()
parseGPS(str)
break
ser.write('AT+CGPSPWR=0''\r')
ser.readline()
print str
ser = serial.Serial("/dev/ttyS0", baudrate=115200, timeout=1)
ser.readline()
print str
time.sleep(.1)
ser.write('AT+CMGF=1'+'\r')
ser.readline()
print str
time.sleep(5)
ser.write('AT+CMGS="xxxxxxxxxxx":"https://googlemaps.com=loc'+'\^Z') # This means nothing for now till i figure out how to send map data
ser.readline()
print str
time.sleep(.1)