Demo: https://wiaio.github.io/wia-location-tracker-example-web/
Project Requirements
- Node.js and NPM must be installed. How to install Node.js on a Raspberry Pi: http://blog.wia.io/installing-node-js-on-a-raspberry-pi-3
- A Wia account. You can create one for free here: https://www.wia.io/signup
Setting up the Wia Node.js SDK
- First thing you will want to do is to create a folder on your location device. Let’s call it
wia-location-device
.
- Go into the folder and initialize the project by creating a package.json file using the command “
npm init
” (use defaults for everything).
- Install the Wia SDK using the command “
npm install --save wia
“.
- Your
package.json
file should look something like the code below.
{
"name": "wia-location-device",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Your Name Here",
"license": "ISC",
"dependencies": {
"wia": "^1.3.3"
}
}
Creating your Location Tracking Device
- Go to the Wia dashboard and select Devices.
- From here, click on the + symbol and the Add New Device modal will appear.
- Enter ‘Location Device’ as the name and click Add Device.
- Take a note of the device’s secret key, we’ll need this later.
Write code for the location tracking device
- We need to create a file for our program, let’s call it
index.js
- Copy the code from the example below add to the
index.js
file.
- Replace the
device-secret-key
on line 3 with your device’s secret key that you made a note of earlier.
- Now to test that you have done your setup correctly, simply run
node index.js
- Your device should have updated in the dashboard under the Location tab, with your sensor readings. You can also view in in real-time in the Debugger tab.
'use strict'
var wia = require('wia')('Your secret key');
// collection of cities
var cities = [{"city":"Dublin","lat":"53.349805","lng":"-6.260310"},
{"city":"London","lat":"51.5013673","lng":"-0.1440787"},
{"city":"Paris","lat":"48.8922431","lng":"2.2380753"},
{"city":"Toronto","lat":"43.6426731","lng":"-79.3928402"}];
// Using the MQTT stream
wia.stream.on('connect', function() {
console.log('Stream connected!');
// randomly choosing city location to publish
var random = Math.floor(Math.random() * 4);
var randomcity = cities[random];
// setting the interval at which to publish location i.e. every second
setInterval(function() {
// function to publish location data
wia.locations.publish(
{
"latitude": randomcity.lat,
"longitude": randomcity.lng,
"altitude": 2
Read More: Build a Real-time Location Tracker Using Wia