Is there a way to save the GPS Data to a variable that can be used in a string?

Was wondering if there’s a way to pull a node’s GPS data variables (Longitude, Latitude, Altitude) and store each of these in a string or int. I’m trying to get a node to poll the GPS details but then send this as a text to all other nodes using the sendText() class.

Closest thing I could find was in the sendPosition() class:

p = mesh_pb2.Position()
    if(latitude != 0.0):
        p.latitude_i = int(latitude / 1e-7)

    if(longitude != 0.0):
        p.longitude_i = int(longitude / 1e-7)

    if(altitude != 0):
        p.altitude = int(altitude)

but I’m not able to get this information into a text string that I can use in the sendText() class.

interface.nodes is a dictionary mapping from node ID to node infos (this is called “the node DB” on the devices). It always contains the last known info about each node in the mesh. So you can iterate that or lookup a node by using its id.

Once you have that nodeinfo (which is also a dict), the “position” will contain the full position info. So you could do:

p = inteface.nodes["!2345678"]
print(p[“latitude”])

That number can be put into a string of your choice. Does that help?

2 Likes

Thank you so much, it works!

First I had to determine the ID of the node by running the meshtastic executable on another ttbeam.

    p = interface.nodes["!123456789012"]
    print(str(p))

Which let me see the whole dictionary values.

{'num': , 
'user': {'id': ' ', 
'longName': ' ', 
'shortName': ' ', 
'macaddr': ' '}, 

'position': {'altitude':  , 
'latitudeI': , 
'longitudeI': , 
'time': , 
'latitude': , 
'longitude': }, 

'snr': }

Lots of good information in there including the position data. Then following your recommendation I am now able to pull the longitude and latitude data.

    p = interface.nodes["!123456789012"]
    print(p["position"]["latitude"])
    print(p["position"]["longitude"])
    print(p["position"]["altitude"])
    print(str(p))

And now I can see the dictionary values.

import meshtastic

interface = meshtastic.SerialInterface()

    p = interface.nodes["!123456789012"]
    print(p["position"]["latitude"])
    print(p["position"]["longitude"])
    print(p["position"]["altitude"])
    print(str(p))
1 Like

Btw. Based on your comment I just added a getMyNodeInfo() method to make this easier. It will be in the next python release.

2 Likes

Thank you so much :slight_smile:

Hi @geeksville hope all is well. Is it possible to set the GPS updating /polling frequency on a node to a more frequent setting? I know this will impact battery life but was trying to figure out how to set this to do some range testing.

When I use the Python script referenced earlier in thread the GPS position is pulled only the first time (or every 15 minutes after that first time). If I run it in a loop, the subsequent use of interface.nodes[], does not return the updated position data in the dictionary. Not sure if this is related to a GPS refresh threshold.

The main commands are discussed here: https://meshtastic.discourse.group/t/real-time-communication-and-location-update/

You should be able to find the needed information, by searching for those commands. :slightly_smiling_face:

2 Likes

Awesome will check it out. Thanks.

2 Likes

It works. Here’s what I updated using the meshtastic executable:

meshtastic --set position_broadcast_secs 15  --set gps_update_interval 5 --set send_owner_interval 1

I set the broadcast to 15 seconds because my script sends a message from the test node to the mesh every 15 seconds.

Thanks again.

1 Like