Displaying my live GPS location on the site and also my battery status

Submitted by ravisagar on Mon, 04/02/2018 - 20:20

This Easter break was very fruitful for me. In fact I was quite satisfied with the work that I did in the last few days. In the past few weeks I am following IndieWeb principles and I implemented POSSE as well. I was quite satisfied with the new approach on my blog where I started posting small updates using notes. However I also wanted to implement checkins on the site and I shared the approach I took with my Drupal website and the modules I used to achieve it.

However I still wanted this ability to display my current location on my site. I was actually inspired by Aaron Parecki. I wanted to do the same thing and I first thought of using his code which he open sourced but I wanted to do it myself and also I don't have an iPhone plus my site runs on Drupal and I was more comfortable implementing it myself.


Introducing my live GPS location

If you go to the status page you will be able to check my last known location and the battery status coming directly from my phone.


How I did it?

I want to share the work that I did to achieve this. In case it is not clear or you need more information. Please contact me and I will be more than happy to share more details.

Step 1: Using GPSLogger app on Blackberry to track my location

Yes I love Blackberry and I use it in 2018 as well. It is great and don't ask me to change it. I use this android app running on my Blackberry Passport. This app captures the GPS location and creates a GPX and CSV file which I also send to my server using secure connection.

To save battery of my Blackberry, which is anyways great as compared to Android phones but I still wanted to make it last longer, I did some settings in the app as follows.

Distance filter: 50 meters (More is better for battery)
Accuracy Filter: 100 meters (More is better for battery)
Duration to match accuracy: 60 seconds (Less is better for battery)
Absolute time to GPS fix: 30 seconds (Less is better for battery)
Don't log if I'm not moving: On (Good for battery, uses accelerometer)

Anything which is better for the battery is not good for the GPS. I can log more frequently and make GPS very very accurate but it will drain the phone battery quickly.

Step 2: Shell script to read the CSV file and make a REST call to my Drupal site

This is my favourite part in the whole process. Lot of Linux skills and it was good to brush up my bash skills after a long time. I made a script that can read the last line of the CSV file and then sending the Timestamp, Latitude, Longitude and the Battery status, there are other things like elevation also in the CSV but I don't care about that (unless I am hiking, in that case I will use Garmin).

Then in the same shell script I am making a REST call to send these things to my site. I of course enabled the REST API modules that now come with the core and I also installed REST UI.

Step 3: Running the Shell script using Cron

I always get nervous when I use crontab. It is a pain to use it or may be I am not a good Linux Administrator. Well anyways after few hits and trials and way past my bedtime I was able to successfully run the shell script after every minute.

So every minute the shell script will update the last known location from the CSV file to my website. Of course it also depends whether my location has be updated or not. When I am not moving the location is not sent. Pretty cool stuff.


What's next - Querying my location database using REST API

I do have lot of GPX files store somewhere but I realised using flat CSV files works fine for me but I still want this ability to query my location database. Aaron Parecki mentioned on his site about Compass which he wrote using Lumen framework. I did try using it but was unable to set it up. I was able to get the Compass up and running but I was unable to login using my Known website. I also enable Indie Auth on this site but after few trials I gave up.

I am now planning to write my own code to query the flat CSV files. In Compass Aaron mentioned that he can query his location database using API calls which is exactly what I wanted to do. I will definitely write code for this, still not sure which language to use. May be good old PHP? Or Go? Don't know. May be I need couple of days to do this.

I hope this was useful and you can try something similar. To be honest I did this with the sole intention to learn something and I really had very, very good Easter break.

Have a nice day!

Ravi Sagar Location

Add new comment

Want to contact me?

Life begins outside your comfort zone and that is where your courage is developed". Write to me by filling this form. For living I am an Atlassian Consultant who love both Jira and Drupal. I am the author of "Mastering JIRA" and "Mastering JIRA 7" books. I Love both JIRA and Drupal (a bit more). You can Write To Me and I will get back to you as soon as I can. Have a nice day!

Social Links

Subscribe to my mailing list.

Enter your email address: