The All-in-1 solution for Btraced Track & Trace

Somehow the Btraced post I wrote some time ago was somewhat half-finished. That post explained how to use a Raspberry Pi as your private Btraced upload server and briefly shows what you can do with your GPS data. What I did not cover was how to get the Btraced information in a web page. Btraced on Google MapsFor me personally, getting the Btraced information to a MQTT broker was the only thing I needed to do to get it all working, because the rest was already covered – and I figured that creating a webpage that could receive the Btraced information and display it was a bit too trivial to mention. Nevertheless, last week I did a small ‘remake’ of the Btraced code I wrote, cause I thought it would be nice to also create an All-in-1 solution for Btraced for those that don’t use MQTT or don’t have a web-server (or both ;-)).

Combining the Btraced upload server, reverse geo-coding and a web-server in one ‘package’ running on a single Raspberry Pi, fully self-supporting, that was the idea. No need for another web-server anymore, nothing – just a Raspberry Pi and the Btraced app. And of course everything’s working real-time, cause nowadays you don’t have to settle for less anymore.

Btraced codeMost of the code was already finished, all that needed to be added was a web-server and a way to get the uploaded, parsed & extended Btraced information to the web clients (the browser(s)).

And of course some static content like a html page, style sheet and some Javascript.

And the result is great – a light-weight web page, served by the Raspberry Pi that gives you real-time access to the Btraced GPS information on any device with a web browser on board – and all with the power usage of a single Raspberry Pi. Saving the uploaded data in a database is not supported yet but should be very easy to accomplish. And there are more things that can be easily added once you have a Raspberry running..not just Btraced related; Pieter and I are currently discussing some options.

Interested? Let me know. In the mean time, happy tracking!

Btraced, Raspberry Pi, Node.js, MQTT to build your own GPS tracking system

Btraced-2

The Btraced app is a great tool, especially because it allows you to upload the GPS data to your own server. Once you’ve got this aspect covered, the things you can do with the GPS data is unlimited – you can do whatever you want with it!
Personally, I’m not that interested in the ‘trip’ functionality of Btraced; I’d rather use the app to allow me to do positioning: constantly letting my Domotica system know where I am. And not just my location, soon other family members will be added as well.

Although uploading the GPS data to your own server is very convenient, accomplishing this might sound scary to some people, cause “I’ve never done that”, “don’t know how”, “don’t have a server”, “too difficult”. This post will show you that it’s none of those..

During the last few days I’ve been playing with a Raspberry Pi, Node.js and a webpage on my web server. The situation before I started with this, was that I had an ASP.Net web form on my IIS web server; this page was used for uploading the Btrace GPS data to my server and storing it in my SQL Server. After a few days I started wondering why I stored all this GPS information in a database – all I did with it was SELECT-ing the newest record and display my last-known location. What a waste of disk space..

So I added MQTT publishing to the ASP.Net page and let the SQL stuff the way it was. A few days later I completely removed the SQL stuff, and realized that using an ASP.Net page on my IIS was a bit overkill for what I was doing. There must be an easier way! And there is: I’ll show you how to create your own Btraced-upload-server without a big web server and minimal cost.

Hardware

All you need is a Raspberry Pi (model B, the one with Ethernet interface) and a LAN cable to connect it to your router or LAN. I prefer to run my Raspberry’s without keyboard, video & mouse so you’ll need a way to connect to the RPi over the LAN – my favorite tool for that is Putty. Connect your RPi to the network, power it up and find out with which IP address the RPi is using.

Network

Your gateway to Internet (modem, router, firewall) should be able to do port forwarding, cause we’ll have to forward the Btraced data to the RPi. Choose a port (e.g. 8000, 8080, 8081 but preferably not 80 cause maybe you want to run a website on your RPi one day…) and forward that port to the RPi. In the Btraced App, you’ll have to change the setting for the upload server, like this: http://ww.xx.yy.zz:pppp (assuming ww.xx.yy.zz is your external IP address and pppp is the port number).

Software

You’ll need to install Node.js and an additional Node module: xml2js. Optional: mqtt. And some extra code, which will be provided below. Just follow one of those excellent guides on ‘How to install Node.js’ that can be found on the web. Also install npm, the Node Package Manager, just to make life easier. Not comfortable with using an editor on Linux? Install an Node.js and a FTP server on your PC, edit the files on your PC, test & debug them there, install an FTP client on the RPi and ‘ftp get’ the files to your RPi once the scripts are finished.

Some code

The Btraced app uploads the data as xml over http. “Oops, so I need a http server and work with XML?” Yes you do, but it only takes 75 lines of code, so don’t be scared! Keep on reading.. here’s the code that can ‘receive’ and parse the uploaded Btraced XML data:

http = require('http');
mqtt = require('mqtt');
var parseString = require('xml2js').parseString;
var ydate = '';
var position = {};

mqttClient = mqtt.createClient(process.env.DDMC_BROKER_PORT, process.env.DDMC_BROKER_HOST, {clientId: 'Btraced', keepalive: 30000});

function processPoint(p){
  if(p.date > ydate){
    ydate = p.date;
    position.date = parseFloat(p.date);
    position.latitude = parseFloat(p.lat);
    position.longitude = parseFloat(p.lon);

    position.speed = parseFloat(p.speed);
    if(position.speed > 0){
      position.speed = Math.round(position.speed * 36)/10;
    } else {
      position.speed = 0;
    }

    if(p.course.substring(0,1) == "-"){
      position.angle = 0;
    } else {
      position.angle = parseFloat(p.course);
    }
    position.batt = Math.round(parseFloat(p.bat)*100);
  }
  return parseInt(p.id);
}

http.createServer(function (req, res) {
  if(req.method == 'POST') {
    var body='';
    var travelid = '';
    var ids=[];

    req.on('data', function (data) {
      body +=data;
    });

    req.on('end',function(){
      // explicitArray false for NOT getting all nodes as arrays
      parseString(body, {explicitArray:false}, function (err, result) {
        bwiredtravel = result.bwiredtravel;
        username = bwiredtravel.username;
        travelid = parseInt(bwiredtravel.travel.id);
        ydate = '';
        position = {};
        position.username = username;
        var points = bwiredtravel.travel.point;
        if(typeof points.length === 'undefined')
        {
          ids.push(processPoint(points));
        } else {
          for(var i=0;i<points.length;i++){
            ids.push(processPoint(points[i]));
          };
        }
        console.log(JSON.stringify(position));
        mqttClient.publish('raw/'+username+'/location/gps', JSON.stringify(position));
      });
      body = '';

      response = {};
      response.id = 0;
      response.tripid = travelid;
      response.points = ids;
      response.valid = true;

      console.log(JSON.stringify(response));
      res.end(JSON.stringify(response));
    });
  }
}).listen(8000);

That’s it? Yep.. and all that needs to be done to get this code running is entering ‘node btraced’ @ the command prompt and it’s running… If you didn’t install the mqtt module mentioned earlier then just delete all the lines containing the string ‘mqtt’ and the script will keep on working, but now the information will only be displayed on the console and not published anymore – you’ll have to find yourself another way to get the information there where you want it.

With MQTT it’s very easy to create a web page that displays the information in text and also draws a map, all real-time.  I added some reverse geocoding to complement the information with an address, et voila:

Btraced-1

I haven’t tried it, but it cannot be hard to run the web page shown above on a RPi as well.

So there you have it: your own, private, fully customizable GPS tracking system for the price of a Raspberry Pi!

Btraced GPS App and geo-fencing

Btraced screenSince April this year I’ve been bèta-testing Btraced GPS Tracking App for Android; and since a week this Android version is finished too. I love it! In short, with Btraced (now available in the App Store as well as Google play) you can upload GPS coordinates, speed, course, altitude to your own web server (or to the free Btraced web server). Uploading the data can be done real-time, or afterwards (e.g. when you have a Wifi connection). Well, the feature list is too long, so go to Btraced.com for more information.

Last week I made an ASP.Net web page that enabled me to upload (post) the data to my own web server and store the data in a SQL Server table and I updated the touchscreen application that’s running in the livingroom so that all the family members can see where I am.

OK, this is nice, but I want to do more with this of course, so I added another feature to my Btraced upload web page: an MQTT client! Now I have my GPS location available on my MQTT broker:

GPS Location on MQTT brokerThat’s more like it 😉 Now I can really start doing something with the data instead of ‘just’ storing it!

Reverse geocoding was the first thing I tried. There’s a lot of code on the Internet for Node.js and I found a module (download the more recent code from GitHub for it to work) that does exactly what I needed – it uses the Google Maps API to find an (approximate) address for the GPS coordinates. Within  a quarter of an hour I got it working. Cool, now I can show an address as well, which is even more convenient then just displaying a marker on a map – although I could also combine the two of course.

Next: geo-fencing. There’s a (Dutch) TV commercial where someone turns on the central heating with his smart phone after landing at the airport – that won’t be necessary anymore with geo-fencing! When your system knows where you are and can determine whether you’re entering or leaving a geo-fence, it can do things like switching the house to Eco mode, booting the PC when coming home, starting the robot vacuum cleaner when nobody is at home – you name it and it’s possible!

All I need is a way to define my geo-fences with 1 or more GPS coordinates – either as a rectangle or as a circle with a center and radius. The rest should be easy!