TinyGPS and Spark Core

Here is a sample application using TinyGPS and Spark Core. The application uses TinyGPS library to access a GPS receiver. I used a local made MediaTek MT3329 receiver but it should work with most of the receivers. The application publishes GPS location of the Spark Core device every 15 minutes. The HTML page subscribe the event and display Core’s location in Map using Google Map V3 SDK. It is added to the library also.

10 Likes

just a few questions

will any receiver work with the tinyGPS?
can the tinyGPS render a live location of the spark device?

thank you very much!

@paolosofio01, I have tested with MT3329 chipset device and is working fine. I get the live location also.

wow thanks! do you think it will work on other gps receiver? btw, can i just ask how did you do it with the web part to get the live location? i know that you’ve used google maps, but i want it to be in a specific location only, like a university, it will just focus on that place. any suggestions? thanks!

1 Like

@paolosofio01, the TinyGPS library works with other GPS receivers also. The library is a NMEA GPS parser, the parse does not directly access the GPS module. As you can see from the below code snippet, we are reading from Serial and pass the data to the TinyGPS library for parsing:

// Check GPS data is available
while (Serial1.available()){
    char c = Serial1.read();
            
    // parse GPS data
    if (gps.encode(c))
        isValidGPS = true;
}

Any GPS Receiver that can output NMEA string can be used with this library.

About the plotting the location, the application do a publish whenever we have a GPS location update, if it is valid then the location otherwise just 0.0, 0.0. The client page subscribes this event and plot update the location on the Google Map. I am using Google Map V3 for this. The following code code is used:

function(e) {
	var parsedData = JSON.parse(e.data);
	var data = parsedData.data.split(",");

	console.log( parsedData.data);
	console.log(data[0]);
	console.log(data[1]);

	loc = new google.maps.LatLng(data[0], data[1]);
	
	marker.setMap(null);
	
	marker = new google.maps.Marker({position: loc, map: map});

	marker.setMap(null);
	map.panTo(loc);
	marker.setMap(map);

}

You can use this approach or any other to plot the location of your core, if you are planning to use more than one core you have to handle that too.

yeah im planning to use this with one or more cores, how is that so?

can i ask if you can post a tutorial of your project? im planning to use it as a foundation of my project. thanks!

1 Like

@paolosofio01, sure, I will update my sample.

1 Like

thanks! i’ll wait for it.

@krvarma can you just teach me how did you made the html page to listen to the location of the cores live using google maps?

@paolosofio01, let me explain what I did to plot location on Google Map, you can read the HTML file from the GitHub page (GitHub - krvarma/TinyGPS_SparkCore: TinyGPS for Spark Core).

OK, the sample application publishes an event in every 15 minutes with longitude and latitude separated by comma. If there is NO valid GPS location, the event data contains 0.0 for both longitude and latitude. The following code snippet does exactly the same:

// If we have a valid GPS location then publish it
    if (isValidGPS){
        float lat, lon;
        unsigned long age;
    
        gps.f_get_position(&lat, &lon, &age);
        
        sprintf(szInfo, "%.6f,%.6f", (lat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lat), (lon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : lon));
    }
    else{
        // Not a vlid GPS location, jsut pass 0.0,0.0
        // This is not correct because 0.0,0.0 is a valid GPS location, we have to pass a invalid GPS location
        // and check it at the client side
        sprintf(szInfo, "0.0,0.0");
    }
    
    Spark.publish("gpsloc", szInfo);

The sample HTML file (spark_location.html, which is also in the GitHub Repo) subscribes this event and parse the date when ever data is received. The following JavaScript function initialize(), initialize the Google Map and add a default marker.

function initialize() {
	var mapOptions = {
        	zoom: 18,
                minZoom: 10,
                maxZoom: 18,
                draggable: false,
                disableDefaultUI: true,
                center: new google.maps.LatLng(0.0, 0.0)
            };

	map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
	marker = new google.maps.Marker({position: new google.maps.LatLng(0.0, 0.0), map: map});

	marker.setMap(map);
}

When we receive the data and parsed, the old marker is removed and add a new marker with new location received. The following code does the location update.

var parsedData = JSON.parse(e.data);
var data = parsedData.data.split(",");

console.log( parsedData.data);
console.log(data[0]);
console.log(data[1]);

loc = new google.maps.LatLng(data[0], data[1]);

marker.setMap(null);

marker = new google.maps.Marker({position: loc, map: map});

marker.setMap(null);
map.panTo(loc);
marker.setMap(map);

For a detailed explanation of how Spark publish/subscribe works, refer to @bko’s excellent tutorial:

Regarding the multiple points, I didn’t get time work on it since I was so busy at work. May be this week end I can post it here.

Hope this clears your doubts, waiting for your awesome project to complete.

Cheers!

2 Likes

Hey @paolosofio01, about multiple cores publishing to a web page, while searching for something else, I found one excellent tutorial by @bko here.

This will be very helpful to you, take a look at this.

Cheers!

2 Likes

@krvarma can i change the map library instead of google maps i will replace it with open layers 3? i find it more flexible in terms of map usage however, can it still parse the location of the spark?

@paolosofio01, yes, you can use any Map to display the locations. The locations parsing us done using JavaScript, you can change the Google Map code and make it work with the other Map you are trying.

1 Like

@krvarma can you send me the link of the gps receiver you’ve used? i cant find any of like that.

cheers!

@paolosofio01, what I have is a locally made GPS Receiver based on MediaTek MT3329 Chipset. You can use the Adafruit GPS Breakout Board which us based on MTK3339 chipset. You can read the NMEA strings using Serial and send to TinyGPS for parsing. Or you can use Adafruit GPS Library (you have to port it to Spark Core, which I think is easy one) to get GPS locations.

@paolosofio01, I am converting the Adafruit Library now, may be this week end or early next week I can post it here.

@krvarma so it means if i will use the adafruit gps library, i may not need now the gps receiver? Am i correct?

@paolosofio01, you always need a GPS Receiver, you can use Adafruit Library or the TinyGPS library to parse the data from the GPS Receiver.

@krvarma i see, so any gps receiver will do for both libraries? Can you suggest what is the most effective gps receiver and gps libraries that we may use? Thanks!