Monday, July 4, 2016

Tracking GPS data with the Tessel 2

The Tessel 2 is a JavaScript-focused microcontroller that has a range of pre-built modules you can attach to extend its functionality. In this article, we will explore what we can do when attaching a GPS module to a Tessel 2.

If you are new to working with the Tessel 2, I covered the basics of getting started with the Tessel 2 earlier this year. Have a read of that first to get a grip on the basics of setting up the Tessel on your Wi-Fi and pushing code to it. It also teaches you how to make the LEDs on your Tessel blink like crazy. Very valuable skills to know!

Connecting the GPS Module

To bring some GPS functionality to your Tessel, connect it up to port A on your Tessel 2 — this is the one closest to the USB power connector:

Tessel 2 Port A

As you can see above, you'll want to connect it with the large bulky rectangle bit and electrical components facing up. If you look at the pin connectors, you will see one which says GND — that should match with the GND on the Tessel 2's port A. Basically, there are plenty of telltale signs if you are connecting it incorrectly!

Starting Our GPS Tessel App

Create a folder for your Tessel app called "gps" (or whatever name you'd prefer). Go to that folder in your Terminal/Command Prompt and type the following to initialize a new project:

[code language="bash"]
t2 init
[/code]

Then, run the following command in npm to install the GPS module:

[code language="bash"]
npm install gps-a2235h
[/code]

gps-a2235h should match the name on your GPS module (this is important to note, in case future GPS modules are a little different).

If you find you get an error message like this:

[code language="bash"]
> cd examples ; pakmanager build || echo 'Could not build pakmanager package. Please make sure pakmanager is globally installed'

sh: pakmanager: command not found
Could not build pakmanager package. Please make sure pakmanager is globally installed
[/code]

You'll want to install that globally first like so (and then attempt the gps module installation again):

[code language="bash"]
npm install pakmanager -g
[/code]

Our Tessel's JavaScript

Our Tessel 2 JavaScript code is relatively simple and looks like so:

[code language="javascript"]
var tessel = require("tessel"),
gpsLib = require("gps-a2235h"),
gps = gpsLib.use(tessel.port["A"]),
WebSocket = require('ws'),
ws = new WebSocket('ws://192.168.0.30:5000'),
latestCoords;

gps.setCoordinateFormat({
'format': 'deg-dec'
});

gps.on('ready', function() {
console.log('GPS module now searching for satellites...');

gps.on('coordinates', function(coords) {
console.log('Lat:', coords.lat, '\tLon:', coords.lon, '\tTimestamp:', coords.timestamp);
latestCoords = coords.lat + ',' + coords.lon;
});

gps.on('fix', function(data) {
console.log(data.numSat, 'fixed.');
});

gps.on('dropped', function(){
console.log('GPS signal dropped');
});
});

gps.on('error', function(err){
console.log('GPS Error: ', err);
});

ws.on('open', function() {
setInterval(function() {
if (latestCoords !== undefined) {
console.log('Trying to send coords of ' + latestCoords);

try {
ws.send(latestCoords, function ack(error) {
console.log('Error detected while sending: ' + error);
});
} catch (e) {
console.log('Error caught while sending: ' + error);
}
} else {
console.log('No coords coming through');
}
}, 10000);
});
[/code]

Let's go over what is actually happening here. We start by requiring the Tessel module and our GPS' module:

[code language="javascript"]
var tessel = require("tessel"),
gpsLib = require("gps-a2235h"),
[/code]

We then set up the GPS module by telling it which port our Tessel's physical GPS module is located in. I placed mine in port A, which I defined like so:

[code language="javascript"]
gps = gpsLib.use(tessel.port["A"]),
[/code]

In order to send data back and forth between our Tessel and our server, we will be using WebSockets. Due to the wonderful fact that the Tessel 2 runs JavaScript and npm modules, we can run the commonly used ws WebSocket module on the Tessel. We add in the ws module and set it up to watch for our server location. I ran this all locally, with my Mac running the Node server connected to my 4G router and my Tessel also connected to the same 4G router. This allowed me to directly use an IP address to refer to the server.

[code language="javascript"]
WebSocket = require('ws'),
ws = new WebSocket('ws://192.168.0.30:5000'),
[/code]

If you wanted to have this run over the web, you could host this on a publicly accessible server and change the WebSocket set up to:

[code language="javascript"]
ws = new WebSocket('ws://www.myfancynodeserver.com'),
[/code]

Continue reading %Tracking GPS data with the Tessel 2%


by Patrick Catanzariti via SitePoint

No comments:

Post a Comment