YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Help me complete this python GPS code for sending SMS with googlemaps data

Tue May 28, 2019 7:59 pm

I could use some info for how to get out of the break statement and continue. I am super new to python and i ripped and tweaked this code as best i could from various sources and i am still putting in the work to learn. Just need to figure a way to append the location data to googlemaps and then send it in an SMS to the number.

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)















pcmanbob
Posts: 7743
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help me complete this python GPS code for sending SMS with googlemaps data

Wed May 29, 2019 12:04 pm

If you change your if to a while then that part of the code will loop until the output is less than one, then it will execute the rest of your program.

Code: Select all


ser.write('AT+CGPSSTATUS?'+'\r')
str = ser.readline()
print str
time.sleep(.1)

while 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)
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help me complete this python GPS code for sending SMS with googlemaps data

Wed May 29, 2019 2:02 pm

pcmanbob wrote:
Wed May 29, 2019 12:04 pm
If you change your if to a while then that part of the code will loop until the output is less than one, then it will execute the rest of your program.

Thanks i tried that but it still carries on when there is no GPS data to read from "Location Not Fix" i'll keep going with these python courses and see what i can learn as just a week is probably not enough time

pcmanbob
Posts: 7743
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Help me complete this python GPS code for sending SMS with googlemaps data

Wed May 29, 2019 4:16 pm

Have you tried adding a print statement in the lines after the while line, to actually print out the variable so you can see what's happening with the variable you are testing.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Andyroo

Re: Help me complete this python GPS code for sending SMS with googlemaps data

Wed May 29, 2019 5:12 pm

Try this as your while loop:

Code: Select all

while str.find ("Location Not Fix") > 1:
    print ('Waiting for a fix - ',str)
    ser.flushInput()
    ser.flushOutput()
    time.sleep(15)
    str = ser.readline()
I think you need to be repeatedly reading the serial port in the while loop.

YitEarp
Posts: 45
Joined: Mon May 13, 2019 7:44 pm

Re: Help me complete this python GPS code for sending SMS with googlemaps data

Wed May 29, 2019 6:09 pm

This is my code at the moment its a little bit messy whilst i am still a "Beginner" with python i am only just learning the basics this is just hashed together i found on the internet

What i would really like to do is parse the data and append it to googlemaps then send an SMS i've seen the code for it but its written for arduino so i need to learn what is the equal in python.

But for the most part this gets an accurate fix of location.

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.flushOutput()
ser.flushInput()

ser.write('ATE'+'\r') #Check to see if modem is responding
str = ser.readline()
print str
time.sleep(5)


ser.write('AT+CGPSOUT=32'+'\r') #Outputs raw NMEA data
str = ser.readline()
print str
time.sleep(3)


ser.write('AT+CGNSPWR=1,AT+CGPSSTATUS=?'+'\r') #Turns on the GPS power

while str.find ("Location Not Fix") > 1:
    print ('Waiting for a fix - ',str)
    ser.flushInput()
    ser.flushOutput()
    time.sleep(15)
    str = ser.readline(100)



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)

irishgeoff
Posts: 4
Joined: Sun Apr 07, 2019 8:38 pm

Re: Help me complete this python GPS code for sending SMS with googlemaps data

Fri Jun 07, 2019 11:10 pm

import requests
requests.post('https://textita.com/text', {
'phone': '9087938341',
'message': 'Sample SMS Text',
'key': 'textita',
})

Return to “Other projects”