Particle Location with Google Integration
var cfenv = require( 'cfenv' ); | |
var express = require( 'express' ); | |
var jsonfile = require( 'jsonfile' ); | |
var parser = require( 'body-parser' ); | |
var Particle = require( 'particle-api-js' ); | |
var request = require( 'request' ); | |
// External configuration | |
config = jsonfile.readFileSync( __dirname + '/config.json' ); | |
// Application | |
var app = express(); | |
// Middleware | |
app.use( parser.json() ); | |
app.use( parser.urlencoded( { | |
extended: false | |
} ) ); | |
// Per-request actions | |
app.use( function( req, res, next ) { | |
// Configuration | |
req.config = config; | |
// Just keep swimming | |
next(); | |
} ); | |
// Static for main files | |
app.use( '/', express.static( 'public' ) ); | |
// Bluemix | |
var env = cfenv.getAppEnv(); | |
// Listen | |
var server = app.listen( env.port, env.bind, function() { | |
// Debug | |
console.log( 'Started on: ' + env.port ); | |
} ); | |
// Socket | |
var io = require( 'socket.io' )( server ); | |
// Paricle | |
var particle = new Particle(); | |
// Login | |
particle.login( { | |
username: config.particle_username, | |
password: config.particle_password | |
} ).then( | |
function( data ) { | |
// Listen to event stream | |
// Specific to my devices | |
// Can use device ID if known | |
particle.getEventStream( { | |
auth: data.body.access_token, | |
deviceId: 'mine', | |
} ).then( function( stream ) { | |
// Stream event arrived | |
stream.on( 'event', function( evt ) { | |
// Look for location-specific event | |
if( evt.name.startsWith( 'hook-response/' + config.event_name ) ) { | |
// Parse out location details | |
var parts = evt.data.split( ',' ); | |
// Assemble message | |
var msg = JSON.stringify( { | |
id: evt.name.split( '/' )[2], | |
published: evt.published_at, | |
position: { | |
lat: parseFloat( parts[0] ), | |
lng: parseFloat( parts[1] ), | |
}, | |
accuracy: parseInt( parts[2] ) | |
} ); | |
// Send to clients | |
io.emit( 'location', msg ); | |
} | |
} ); | |
} ); | |
}, | |
function( err ) { | |
console.log( err ); | |
} | |
); |
{ | |
"location_api_key": "AIzaSyBLfo4g4C8Wzo2gqZy6rFkYLORIBAXakPw", | |
"map_api_key": "AIzaS2AVdZUkJcTZlcBr3xdidBgCBA55q4jjbXg", | |
"event_name": "deviceLocator", | |
"particle_username": "_YOUR_PARTICLE_USERNAME_", | |
"particle_password": "_YOUR_PARTICLE_PASSWORD_", | |
"particle_device": "240023000f4d343436313031" | |
} |
<html> | |
<head> | |
<title>Location</title> | |
<!-- Styles --> | |
<!-- Just fill with map --> | |
<style> | |
body { | |
margin: 0; | |
overflow: hidden; | |
padding: 0; | |
} | |
#map { | |
height: 100%; | |
width: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<!-- Map placeholder --> | |
<div id="map"></div> | |
<!-- Google Maps --> | |
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCHSq9HBd3Q-ar2zbCUnB4HwdD64IgNl3M"></script> | |
<!-- Socket IO --> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.2/socket.io.js"></script> | |
<!-- Application --> | |
<script> | |
class Location { | |
constructor() { | |
// Create the map instance | |
// Set default location | |
// Set default zoom level | |
this._map = new google.maps.Map( document.querySelector( '#map' ), { | |
center: {lat: 41.1106266, lng: -73.7248718}, | |
zoom: 14 | |
} ); | |
// Marker showing location | |
// Wait for first location event | |
this._marker = null; | |
// Socket | |
// Listen for location events | |
this._socket = io(); | |
this._socket.on( 'location', evt => this.doLocation( evt ) ); | |
} | |
// Location event | |
// Position marker and map | |
doLocation( evt ) { | |
// Parse JSON | |
var data = JSON.parse( evt ); | |
console.log( data ); | |
// First location event | |
// Instantiate marker | |
if( this._marker == null ) { | |
this._marker = new google.maps.Marker( { | |
map: this._map | |
} ); | |
} | |
// Position marker | |
// Center map | |
this._marker.setPosition( data.position ); | |
this._map.setCenter( data.position ); | |
} | |
}; | |
// Here we go! | |
let app = new Location(); | |
</script> | |
</body> | |
</html> |
#include <google-maps-device-locator.h> | |
// Google Maps | |
GoogleMapsDeviceLocator locator; | |
// Setup | |
void setup() { | |
// Scan for visible networks | |
// Publish to the cloud every thirty seconds | |
locator.withSubscribe( locationCallback ).withLocatePeriodic( 30 ); | |
} | |
void locationCallback( float lat, float lng, float accuracy ) { | |
// Do stuff if you want | |
} | |
// Loop | |
void loop() { | |
// Keep on looping | |
locator.loop(); | |
} |
{ | |
"name": "Locator", | |
"version": "0.1.0", | |
"description": "Listen for geolocation from Particle Cloud.", | |
"private": true, | |
"scripts": { | |
"start": "node app.js" | |
}, | |
"dependencies": { | |
"body-parser": "^1.15.2", | |
"cfenv": "^1.0.3", | |
"express": "^4.14.0", | |
"jsonfile": "^2.4.0", | |
"particle-api-js": "^6.5.0", | |
"request": "^2.78.0", | |
"socket.io": "^1.5.1" | |
}, | |
"repository": {}, | |
"engines": { | |
"node": "4.x" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment