Sohil.Mehta
Posts: 57
Joined: Thu Feb 01, 2018 6:45 am

Not able to send GPS data (lat, lon and alt) to journal (systemd)

Wed Aug 22, 2018 11:34 am

Working on a data acquisition project where I am trying to send GPS data to cloud:
Board: Customised board based on the schematics of Raspberry Pi Compute Module 3 Lite and I/O baord. Have added GSM and GPS modules to it for my requirements.
OS : Raspbian Stretch Lite (June 2018 release, latest one)
GPS Module used: Ublox EVAM8M

My code:

Code: Select all

from systemd import journal
import gps
import time
import threading
import datetime

# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

while True:
    try:
    	report = session.next()
		# Wait for a 'TPV' report and display the current time
		# To see all report data, uncomment the line below
		#	print report
        if report['class'] == 'TPV':
            if hasattr(report, 'time'):
                timestamp = report.time
                print timestamp
                journal.send(
                channel='gps',
                priority=journal.Priority.INFO,
                timestamp="%f" % (time.time()*1000),
                )

        if report['class'] == 'TPV':
            if hasattr(report, 'lat'):
                lat = report.lat
                print lat
                journal.send(
                channel='gps',
                priority=journal.Priority.INFO,
                lat='lat',
                )  
    
        if report['class'] == 'TPV':
            if hasattr(report, 'lon'):
                long = report.lon
                print long
                journal.send(
                channel='gps',
                priority=journal.Priority.INFO,
                long='long',
                )

        if report['class'] == 'TPV':
            if hasattr(report, 'alt'):
                altitude = report.alt
                print altitude
                journal.send(
                channel='gps',
                priority=journal.Priority.INFO,
                altitude='altitude',
                )
        

    except KeyError:
		pass
    except KeyboardInterrupt:
		quit()
    except StopIteration:
		session = None
		print "GPSD has terminated"


I got this code from an Adafruit tutorial: https://learn.adafruit.com/adafruit-ult ... i?view=all and I installed systemd from pypi: https://pypi.org/project/systemd/ . While I am able to perfectly display timestamp, lat, lon and alt on the terminal, I am not able to send this data to the cloud. I know this because I receive the timestamp alone on the journal but not the other values. I also have another python code which does the exact same thing, and I am able to see all the data on the website. I am using python3 and not python to run that code. But the problem with that code is that, it causes the CPU to get really hot (that's another story altogether) hence I cannot use that code.

Coming back to the code here, what am I missing? I am not getting any error. Just that, I see nothing on the website.

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Not able to send GPS data (lat, lon and alt) to journal (systemd)

Wed Aug 22, 2018 12:39 pm

Sohil.Mehta wrote:
Wed Aug 22, 2018 11:34 am
While I am able to perfectly display timestamp, lat, lon and alt on the terminal, I am not able to send this data to the cloud.
Please clarify what you are trying to do. You are using a module to write to the log on the local Pi.
So what do you mean by sending it to the cloud? Where exactly do you want your data to go?
I am using python3 and not python to run that code.
Your code will not run with python 3; all print statements are not compatible

Looking at your code, e.g.

Code: Select all

       if report['class'] == 'TPV':
            if hasattr(report, 'alt'):
                altitude = report.alt
                print altitude
                journal.send(
                channel='gps',
                priority=journal.Priority.INFO,
                altitude='altitude',
                )
Looks to me that you send the literal text 'altitude' to the journal, instead of the value.
If you want to use text + value then you could use something like this (untested)

Code: Select all

                journal.send(channel='gps', priority=journal.Priority.INFO, altitude='altitude {}'.format(altitude))
and change the format as you like

Sohil.Mehta
Posts: 57
Joined: Thu Feb 01, 2018 6:45 am

Re: Not able to send GPS data (lat, lon and alt) to journal (systemd)

Wed Aug 22, 2018 4:01 pm

Hey Dirk problem solved. I wasn't seeing any data on the website because, I need to send the entire data packet together. What I am trying to do is send all the data to the journal and from there I have a pipeline that takes all the data to an interactive tool that displays it in the form of graphs and all. So this is what I modified in my code:

Code: Select all

while True:
    try:
    	report = session.next()
		# Wait for a 'TPV' report and display the current time
		# To see all report data, uncomment the line below
		#print report
        if report['class'] == 'TPV':
            if hasattr(report, 'time'):
                timestamp = report.time
                print timestamp
            
            if hasattr(report, 'lat'):
              	latitude = report.lat
                print latitude
            
            if hasattr(report, 'lon'):
                longitude = report.lon
                print longitude    

            if hasattr(report, 'alt'):
                altitude = report.alt
                print altitude

            journal.send(
            channel = 'gps',
            priority = journal.Priority.INFO,
            timestamp = "%f" % (time.time()*1000),
            latitude = "%f" % (latitude),
            longitude = "%f" % (longitude), 
            altitude = "%f" % (altitude),
            )
        

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Not able to send GPS data (lat, lon and alt) to journal (systemd)

Wed Aug 22, 2018 4:41 pm

Well, that's pretty much what I posted (you're just using 'old' functions).
But are you sure you're running that code in Python 3? The print statements are still not compatible...

Sohil.Mehta
Posts: 57
Joined: Thu Feb 01, 2018 6:45 am

Re: Not able to send GPS data (lat, lon and alt) to journal (systemd)

Wed Aug 22, 2018 4:55 pm

No I am using Python to run this code. Python3 is for another code, which I have not posted here.
What do you mean by 'old' functions? Is it not okay to use these functions?

DirkS
Posts: 10371
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Not able to send GPS data (lat, lon and alt) to journal (systemd)

Wed Aug 22, 2018 5:13 pm

Sohil.Mehta wrote:
Wed Aug 22, 2018 4:55 pm
No I am using Python to run this code. Python3 is for another code, which I have not posted here.
What do you mean by 'old' functions? Is it now okay to use these functions?
In future you should be a bit more careful about the wording of your posts. Leave out any irrelevant bits (the whole bit about the 'other code' for example)
One of the irrelevant bits made me think that you're using Python 3

And re 'old' functions you can google for 'python format vs percent'.
'%' is not deprecated but certainly discouraged.

Sohil.Mehta
Posts: 57
Joined: Thu Feb 01, 2018 6:45 am

Re: Not able to send GPS data (lat, lon and alt) to journal (systemd)

Thu Aug 23, 2018 12:58 pm

I also have another python code which does the exact same thing, and I am able to see all the data on the website. I am using python3 and not python to run that code.
I tried to make that as clear as possible. But yeah, I get your point. Okay, will keep that in mind. Thanks DirkS !

Return to “Python”