Tuesday 3 June 2014

Syncing data from your SPOT Satellite Messenger to CartoDB

In my last blog post, we looked at how you can track your trips using the open API of the SPOT Satellite Messenger. A limitation is that you can only get your locations from the last 7 days, and the web service has request limits (500 request within 15 minutes). To avoid these limitations we can sync the data to a CartoDB account using a PHP cron job. CartoDB is a great choice as it allows us to both visualize and to query our tracking data.

A cron job is a script on the server running periodically at fixed times, dates, or intervals. I've created my cron job in PHP, but you can choose the language you want. What you need is a server which allows to run scripts repeatedly. I'm using the cron job support from my hosting provider, Bluehost. As I'm tracking my position every 10 minutes, the cron job is running at 10 minutes interval to sync the data.

CartoDB is providing a simple PHP library to make use of the CartoDB API as easy as possible. The library allows us to authenticate (using OAuth) and run SQL queries on our CartoDB account. You don't have to authenticate to read data from the SPOT API, but you need to create a shared page to obtain your feed id.

In my PHP script, I'm first reading the JSON feed from SPOT, and the last timestamp from my CartoDB table. Then I'm looping through the tracking points from SPOT, and if the timestamp is newer, I'm adding the point to CartoDB.


You need to add your feed id from SPOT and your CartoDB credentials. Remember to create the table in CartoDB before you run the script! The PHP script is available on Github. Feel free to improve it!

In the next blog post, we'll display the tracking data on a map. 

2 comments:

danieltakayama said...

Hi! I found Your blog while searching for solutions to show spot messenger positions. I really like it and think You do some good stuff! :-) I'm a beginner so this might be a bad question. I doesn't understand if Your Github should have included a config.php? And should this config.php specify the spot feedid and maybe more? I would appreciate if You have time to give me a headsup! Kepp up the good work! Best regards, Daniel

Peter Ribe said...

Hi Bjørn!

I have recently made a site for a friend of mine, http://roferd.no, that monitors a 3300km one-man rowing expedition around the Norwegian coastline. I am a novice at this but, inspired by your blog, I have managed to prepare a Leaflet map and store the mapping data in CartoDB. I will probably give the syncing part a go very soon too. Maybe you could tell me how I can make Leaflet focus on the active marker only, instead of all the markers (fitBounds)? Best regards, Peter