Banner

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. 

No comments: