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

How would i get all this to send to googlemaps and send back a link

Thu May 30, 2019 7:33 pm

So ongoing from my other threads hope there isn't too many!
The GPS part is working fine and parsing through pynmea module and i get a location fix which is very accurate

However now i want to be able to send this location data to googlemaps and have it pull that data and send me an SMS with the link of location but with the while true statement it will just keep looping GPS data. Don't want that i need it to end but allow me to carry on with the SMS commands after its done getting a location

Anyone know any sources for reading material to get this working ?

Everything i have seen is written for arduino and i tried to make it work through python but failed.

https://www.youtube.com/watch?v=qQnjlwf-8yQ

I have looked into possibly sending the data via googlemaps api through http but this modem only supports 2G which is 385kb/s max which i don't think would be enough plus its only a testing ground for now anyway so thats not important.





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('AT'+'\r') #Check to see if modem is responding
str = ser.readline()
print str
time.sleep(5)

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

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


ser.write('AT+CGPSOUT=32'+'\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)

Code: Select all

#!/usr/bin/python

import serial
import time

ser = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)

ser.write('ATE=0'+'\r\n')
ser.read()
print ser
time.sleep(.1)

ser.write('AT+CMGF=1'+'\r\n')
ser.read(10)
print ser
time.sleep(.1)
ser.write('AT+CNMI=2,1,0,0,0'+'\r\n')
ser.read(10)
print ser
time.sleep(.1)

ser.write('AT+CMGS="xxxxxxxxxxx"'+'\r\n')
ser.read(10)
print ser
time.sleep(.1)

ser.write('something http://googlemaps=loc etc etc'+'\r\n')
ser.read(10)
print ser
time.sleep(.1)

ser.write("\x1A")
for i in range(10):
    ser.read(10)
    print ser

Andyroo

Re: How would i get all this to send to googlemaps and send back a link

Thu May 30, 2019 8:13 pm

You could spin the map routine off into its own thread or use the multiprocessing functions in python.

That would let you continue with the serial read.

Unless you need to have the pin permanently on the map you can bypass the whole Google API and use the location as part of the URL in this format:
This results in this if you click here

Andyroo

Re: How would i get all this to send to googlemaps and send back a link

Thu May 30, 2019 8:15 pm

Sorry - should apologise for using the Foundation address as the pin ‘target’ :lol: :roll: :lol:

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

Re: How would i get all this to send to googlemaps and send back a link

Thu May 30, 2019 8:58 pm

Andyroo wrote:
Thu May 30, 2019 8:13 pm
You could spin the map routine off into its own thread or use the multiprocessing functions in python.

That would let you continue with the serial read.

Unless you need to have the pin permanently on the map you can bypass the whole Google API and use the location as part of the URL in this format:
This results in this if you click here
You lost me at You could spin :D

Do you mean manually input the data in a browser?

what i really want to know is how to grab the location data thats output and pass it into the SMS like you posted there i've seen it goes something like https://googlemaps.com/maps?=loc/ but i don't know how to achieve it yet!

This is the arduino code i gather its working by grabbing modules as python would.

Code: Select all

void loop() {
  sendTabData("AT+CGNSINF",1000,DEBUG);
  if (state !=0) {
    Serial.println("State  :"+state);
    Serial.println("Time  :"+timegps);
    Serial.println("Latitude  :"+latitude);
    Serial.println("Longitude  :"+longitude);

    sim808.print("AT+CMGS=\"");
    sim808.print(phone_no);
    sim808.println("\"");
    
    delay(300);

    sim808.print("http://maps.google.com/maps?q=loc:");
    sim808.print(latitude);
    sim808.print(",");
    sim808.print (longitude);
    delay(200);
    sim808.println((char)26); // End AT command with a ^Z, ASCII code 26
    delay(200);
    sim808.println();
    delay(20000);
    sim808.flush();
    
  } else {
    Serial.println("GPS Initialising...");

Andyroo

Re: How would i get all this to send to googlemaps and send back a link

Thu May 30, 2019 9:14 pm

Please do not take this the wrong way but you may do best to spend an hour or two looking at an intro to python. The first eight of these https://m.youtube.com/playlist?list=PL- ... FUiLCjGgY7 will help significantly to understand the basics.

Your code already shows that you have the latitude and longitude from the GPS so it’s a simple case of processing that into a string and then sending that as the text of the SMS. You also have the AT commands (the instructions for the GPRS module) listed.

As for the ‘spin’ statement - ignore that as you do not need to run the serial routine at the same time as the URL build as you are just sending a text via the AT commands.

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

Re: How would i get all this to send to googlemaps and send back a link

Thu May 30, 2019 9:20 pm

Andyroo wrote:
Thu May 30, 2019 9:14 pm
Please do not take this the wrong way but you may do best to spend an hour or two looking at an intro to python. The first eight of these https://m.youtube.com/playlist?list=PL- ... FUiLCjGgY7 will help significantly to understand the basics.

Your code already shows that you have the latitude and longitude from the GPS so it’s a simple case of processing that into a string and then sending that as the text of the SMS. You also have the AT commands (the instructions for the GPRS module) listed.

As for the ‘spin’ statement - ignore that as you do not need to run the serial routine at the same time as the URL build as you are just sending a text via the AT commands.
Not at all I welcome any learning. I’ve been watching a few beginner guides and I have a python for beginners book which I reference as I learn. I would like to find a good course where I can learn within a space of 2-3 months as right now a lot is confusing to me even though pycharm does help correct errors.

I am having fun learning. Thank you for the info

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

Re: How would i get all this to send to googlemaps and send back a link

Fri May 31, 2019 11:18 pm

Very messy but functional just need to know how i can call a function to grab the string from lat and long for pasting into googlemaps link.

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('AT' + '\r\n')  # Check to see if modem is responding
ser.read(10)
print ser.read(10)
time.sleep(.1)
ser.flushInput()
ser.flushOutput()


ser.write('AT+CGNSPWR=1,AT+CGPSSTATUS=?' + '\r')  # Turns on the GPS power
str = ser.read()
print str
time.sleep(15)
ser.flushInput()
ser.flushOutput()

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



ser.write('AT+CGNSTST=1' + '\r')  # Starts sending data to UART
str = ser.readline()
print str
time.sleep(.1)
ser.flushInput()
ser.flushOutput()

# 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)

    else:
        print("Waiting for Fix")


while True:
    str = ser.readline()
    parseGPS(str)
    print(str)
    break

ser.write('AT+CGPSPWR=0'+'\r\n')

ser.write('AT+CMGF=1'+'\r\n')
ser.read(10)
print ser
time.sleep(.1)
ser.write('AT+CNMI=2,1,0,0,0'+'\r\n')
ser.read(10)
print ser
time.sleep(.1)

ser.write('AT+CMGS="mobile.no"'+'\r\n')
ser.read(10)
print ser
time.sleep(.1)

ser.write('How do i grab lat and lon and paste into a googlemaplink??'+'\r\n')
ser.read(10)
print ser
time.sleep(.1)

ser.write("\x1A")
for i in range(10):
    ser.read(10)
    print ser

Andyroo

Re: How would i get all this to send to googlemaps and send back a link

Sat Jun 01, 2019 10:38 am

The simplest way to convert a number to a string is to use the ‘str()’ function like this

Code: Select all

str(data.latitude)
These can be joined to text (concatenation) by the the ‘+’ operator

Code: Select all

print('The latitude value is ' + str(data.latitude))
and assigned to variables as normal

Code: Select all

a_string_variable = str(data.latitude)

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

Re: How would i get all this to send to googlemaps and send back a link

Thu Jun 20, 2019 1:43 pm

Code: Select all

def parse_gps(incoming_data):

    if incoming_data.find("GGA") > 0:
        data = pynmea2.parse(incoming_data)
        # print("Timestamp: %s // Lat: %s  // Lon: %s // Satellites: %s  " % (data.timestamp, data.latitude,
        #                                                                     data.longitude, data.num_sats))
        return data

        # Debug
        #print("Satellites: %s " % data.num_sats)



while True:
    gps_fix = ser.readline().decode('ascii')
    parse_gps(gps_fix)
    d = data.num_sats
    if d > 7:
    break
I am having trouble with my while loop
i want to get data from the num_sats and say > 7:
break

But everything i try isn't working any tips?

Andyroo

Re: How would i get all this to send to googlemaps and send back a link

Thu Jun 20, 2019 1:58 pm

You need to indent the break beyond the IF:

Code: Select all

while True:
    gps_fix = ser.readline().decode('ascii')
    parse_gps(gps_fix)
    d = data.num_sats
    if d > 7:
       break

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

Re: How would i get all this to send to googlemaps and send back a link

Thu Jun 20, 2019 4:29 pm

Andyroo wrote:
Thu Jun 20, 2019 1:58 pm
You need to indent the break beyond the IF:

Code: Select all

while True:
    gps_fix = ser.readline().decode('ascii')
    parse_gps(gps_fix)
    d = data.num_sats
    if d > 7:
       break
That is what I thought and have previously tried however I can’t set d to the variable I want as it doesn’t know what data.num_sats is.

I know that it’s a call to pynmea2.parse so I have data.lat, data.lon etc and pynmea2 parses through those.

What could I do to make this work?

Andyroo

Re: How would i get all this to send to googlemaps and send back a link

Thu Jun 20, 2019 5:13 pm

My bad - I just looked at the break and not the code before :oops:

Try:

Code: Select all

def parse_gps(incoming_data):

    if incoming_data.find("GGA") > 0:
        data = pynmea2.parse(incoming_data)
        # Debug prints
        # print("Timestamp: %s // Lat: %s  // Lon: %s // Satellites: %s  " % (data.timestamp, data.latitude,
        #                                                                     data.longitude, data.num_sats))
        # print("Satellites: %s " % data.num_sats)
        return data


while True:
    gps_fix = ser.readline().decode('ascii')
    gps_data = parse_gps(gps_fix)
    if gps_data.num_sats > 7:
        break

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

Re: How would i get all this to send to googlemaps and send back a link

Thu Jun 20, 2019 5:23 pm

Andyroo wrote:
Thu Jun 20, 2019 5:13 pm
My bad - I just looked at the break and not the code before :oops:

Try:

Code: Select all

def parse_gps(incoming_data):

    if incoming_data.find("GGA") > 0:
        data = pynmea2.parse(incoming_data)
        # Debug prints
        # print("Timestamp: %s // Lat: %s  // Lon: %s // Satellites: %s  " % (data.timestamp, data.latitude,
        #                                                                     data.longitude, data.num_sats))
        # print("Satellites: %s " % data.num_sats)
        return data


while True:
    gps_fix = ser.readline().decode('ascii')
    gps_data = parse_gps(gps_fix)
    if gps_data.num_sats > 7:
        break
Thank you that looks like it could work will test it tomorrow when home. It was the logic of accessing a variable outside of a function I struggled with In terms of logic.

Will let you know!

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

Re: How would i get all this to send to googlemaps and send back a link

Fri Jun 21, 2019 2:45 am

Sadly that didn't work
I've been trying to learn as much as i can without throwing code in and hoping for the best but these conditional statements confuse the heck out of me.

I get this error

Traceback (most recent call last):
File "/tmp/tmp/pycharm_project_812/gps.py", line 41, in <module>
if gps_data.num_sats > 7:
AttributeError: 'NoneType' object has no attribute 'num_sats'

Process finished with exit code 1

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

Re: How would i get all this to send to googlemaps and send back a link

Fri Jun 21, 2019 2:45 am

Sadly that didn't work
I've been trying to learn as much as i can without throwing code in and hoping for the best but these conditional statements confuse the heck out of me.

I get this error

Traceback (most recent call last):
File "/tmp/tmp/pycharm_project_812/gps.py", line 41, in <module>
if gps_data.num_sats > 7:
AttributeError: 'NoneType' object has no attribute 'num_sats'

Process finished with exit code 1

I know (i think) that NoneType means empty. I don't know if this is an issue with the code or ser.readline not able to pick out num_sats which is part of the pynmea2 module.
?? ?? sigh...

Andyroo

Re: How would i get all this to send to googlemaps and send back a link

Fri Jun 21, 2019 11:47 am

So this is how I would work out what the issue is :D

First, think through the possible errors:
It could be nothing coming out of the serial read
It could be different data coming out of the GPS so data is not being set up
It could be the parse_gps routine not working at all so we need to see what is coming out of that
It could be the GGA data not having num_sats in it

(Hint - The issue will be due to the incoming data not having GGA in it. If this is not found in the string, the output in data will be empty rather than from parse_gps and as it does not have the num_sats element the 'if' command will error)

So lets put a bit of debugging print code in to find out what is coming out of the data stream and stop the error killing the program dead:

Code: Select all

def parse_gps(incoming_data):

    # Set data up to tell us if no GGA is found - if GGA is found it will be overwritten by the pynmea2 routine if thats returning anything
    data = 'Not a GGA output'
    
    if incoming_data.find("GGA") > 0:
        data = pynmea2.parse(incoming_data)
        
        # Check output from the pynmea routine
        print("Timestamp: %s // Lat: %s  // Lon: %s // Satellites: %s  " % (data.timestamp, data.latitude, data.longitude, data.num_sats))
        
        # I cannot remember if this is the correct command or syntax so I've put a try / except around it
        # This tries the command(s) and if an error occurs it does not tell you but goes to the except block of code and runs that
        # The except block just has the pass command and does nothing so any errors go on to the next line
    
        # If I have this correct it will list all the elements in gps_data  - the command may just be dir(gps_data) with no print
        try:
            print("GPS Data has:")
            print(dir(gps_data))
        except:
            pass
            
        # NOTE I'm now returning data even if the 'IF' command is not executed by dropping the indent back to the same as the start of the IF command
        # data will now have my text from the top OR the output of the parse routine
    return data
    
while True:
    gps_fix = ser.readline().decode('ascii')
    
    # Check what we got back from the GPS
    print("Serial read completed")
    print("GPS sent ", gps_fix)
    
    gps_data = parse_gps(gps_fix)
     
    # Use the try / pass command - checks if error occurs - if so just ignores it and will loop around again
    try:
        if gps_data.num_sats > 7:
            break
    except:
        pass
Now I hope this will give you lots of text till it gets a lock with 8+ satellites (may not happen indoors) and then end. The break may only take you out of the try / except rather than the while loop - I cannot remember :oops:

If it does not end - use ctrl-c a few times to stop it :lol: Make sure you save it first...

I'm not going to be around this afternoon or tonight - my daughter is coming home as she is setting off at 5AM tomorrow to sail from Aberdeen!

Hope this will keep you going :)


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

Re: How would i get all this to send to googlemaps and send back a link

Fri Jun 21, 2019 3:15 pm

Andyroo wrote:
Fri Jun 21, 2019 11:50 am
Have a look at this for if / else statements

http://anh.cs.luc.edu/handsonPythonTuto ... ments.html

Copy the text, run it and play with them :)
Thank You that put my brain into work and gave me the idea for the fix

Code: Select all

while True:
    gps_fix = ser.readline().decode('ascii')
    gps_data = parse_gps(gps_fix)
    if gps_data is not None and int(gps_data.num_sats) > 7:
        break

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

Re: How would i get all this to send to googlemaps and send back a link

Fri Jun 21, 2019 3:29 pm

YitEarp wrote:
Fri Jun 21, 2019 3:15 pm
Andyroo wrote:
Fri Jun 21, 2019 11:50 am
Have a look at this for if / else statements

http://anh.cs.luc.edu/handsonPythonTuto ... ments.html

Copy the text, run it and play with them :)
Thank You that put my brain into work and gave me the idea for the fix EDIT: I missed your code breakdown above that is something i will use in the future to debug seems logical! Thanks again.

Code: Select all

while True:
    gps_fix = ser.readline().decode('ascii')
    gps_data = parse_gps(gps_fix)
    if gps_data is not None and int(gps_data.num_sats) > 7:
        break

Andyroo

Re: How would i get all this to send to googlemaps and send back a link

Sat Jun 22, 2019 12:12 pm

Glad you got there.

My daughter has had second breakfast and lunch (think she is a hobbit by life choice) and should be in Bonny Scotland by now.

The other thing to think about is splitting the IF into the two parts as separate lines. To my mind this is easier to read as you have the dreaded ‘not none’ statement :D I get in a right twist when I read that esp. when it’s followed by a check of the same variable.

I honestly think it’s my age :lol:

User avatar
scruss
Posts: 3391
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: How would i get all this to send to googlemaps and send back a link

Sat Jun 22, 2019 1:24 pm

Once you get the format fully working, most mobile phones support geo: URIs, eg:
[url]geo:43.6478,-79.5821[/url]

Its support on anything but mobile can be a bit spotty, but it's a long-established standard - https://tools.ietf.org/html/rfc5870 - that can save a lot of data formatting hassle.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Return to “Other projects”