Include technologies covered - Node.js | ORDS | Spatial
Have you ever wanted to track the location of a physical object and trigger an action based on the proximity of that object? The problem is, how to do this without spending a small fortune?
In this session, Blaine walks through using Raspberry Pi to track a small Bluetooth LE beacon. After a short explanation of BLE beacons, he talks about configuring the Pi and adding the software.
Beacon tracking generates lots of data for analysis which needs a home, ie, a database. Using REST APIs, Blaine safely stores the data in a cloud database and demonstrates using spatial queries to find the location of the beacon.
Attendees will come away from this session with the tools to build their own beacon scanning system that won't break the bank.
8. Update Raspbian
1. Put the SD card in your Raspberry Pi
2. Boot it up
3. SSH to pi
4. Change password (default password: raspberry)
5. Run an update
ssh pi@192.168.0.2
passwd
sudo apt-get update
Install Bluetooth Libraries
sudo apt-get install -y bluetooth bluez libbluetooth-dev libudev-dev
10. Setup Environment Variables
SCANNER_ID – The ID for this scanner.
BEACON_UUID – UUID of the beacon you’re tracking.
IFTTT_KEY – your account key for IFTTT
ORDS_URI – http://example.com/ords/beacon/beacon/alert/
Scan For a Beacon
var bu = require('./beaconUtil.js');
var bleacon = require('bleacon');
var beaconId = process.env.BEACON_UUID;
11. bleacon.startScanning(beaconId);
bleacon.on('discover', function(bleacon) {
var uuid = bleacon.uuid,
major = bleacon.major,
minor = bleacon.minor,
rssi = bleacon.rssi,
accuracy = bleacon.accuracy.toFixed(2),
proximity = bleacon.proximity,
distance = 0;
distance = bu.calculateDistance(uuid, rssi).toFixed(2);
console.log("UUID: " + uuid + "nMajor:" + major + "nMinor:" + minor +
"nrssi:" + bleacon.rssi + "naccuracy:" + accuracy +
"nProximity:" + proximity + "nDistance:" + distance + "nn");
});
Trigger an Ac on
var bu = require('./beaconUtil.js');
var bleacon = require('bleacon');
var beaconId = process.env.BEACON_UUID;
var scannerID = process.env.SCANNER_ID;
bleacon.startScanning(process.env.BEACON_UUID);
bleacon.on('discover', function(bleacon) {
var uuid = bleacon.uuid,
major = bleacon.major,
minor = bleacon.minor,
rssi = bleacon.rssi,
accuracy = bleacon.accuracy.toFixed(2),
proximity = bleacon.proximity,
distance = 0;
distance = bu.calculateDistance(bleacon.uuid, bleacon.rssi).toFixed(2);
//Process the alert action
if (distance < 2) {
bu.iftttNotify('LeftRoom', scannerID, beaconId, distance);
}
});
12. Home Automa on
Messages - IFTTT (If This Then That) - h ps://i .com
Home Assistant - h ps://home-assistant.io
Deadbolt
Sonos
Oracle REST Data Services
begin
ords.enable_schema(
p_enabled => true,
p_schema => 'BEACON',
p_url_mapping_type => 'BASE_PATH',
p_url_mapping_pattern => 'beacon',
p_auto_rest_auth => false);
ords.define_module(
p_module_name => 'Beacon',
p_base_path => '/beacon/',
p_items_per_page => 25,
p_status => 'PUBLISHED');
ords.define_template(
p_module_name => 'Beacon',
p_pattern => 'scan',
p_priority => 0,
p_etag_type => 'HASH',
p_etag_query => null);
ords.define_handler(
p_module_name => 'Beacon',
p_pattern => 'scan',
p_method => 'GET',
p_source_type => 'json/query',
p_items_per_page => 25,
13. p_mimes_allowed => '',
p_source => 'select *
from reading
order by created_on');
commit;
end;
Consume Rest API
var bu = require('./beaconUtil.js');
var bleacon = require('bleacon');
var beaconId = process.env.BEACON_UUID;
var scannerID = process.env.SCANNER_ID;
var request = require('request');
var ordsUri = process.env.ORDS_URI;
//get the alerts for this scanner
var alerts = [];
request.get({
url: ordsUri + 'alert/' + scannerID,
json: true
}, function(err, res, body) {
if (err) {
console.log(err);
}
alerts = body;
bleacon.startScanning(process.env.BEACON_UUID);
console.log('Scanning:', alerts);
});
bleacon.on('discover', function(bleacon) {
var uuid = bleacon.uuid,
major = bleacon.major,
minor = bleacon.minor,
i bl i
My beaconScan Repository
h ps://github.com/bcarter/beaconScan
14. There's more we could do
What if you want to map the beacon loca on?
Table Data
scanner_id beacon_id distance created_on
5 myBeacon 4.72 27-AUG-1704.19.58.082247PM
7 myBeacon 6.57 27-AUG-1704.19.58.780495PM
15. 6 myBeacon 4.64 27-AUG-1704.19.58.780855PM
7 myBeacon 5.84 27-AUG-1704.19.59.270639PM
6 myBeacon 4.8 27-AUG-1704.19.59.279716PM
5 myBeacon 4.8 27-AUG-1704.19.59.489140PM
7 myBeacon 6.65 27-AUG-1704.19.59.538878PM
5 myBeacon 5.36 27-AUG-1704.20.00.187193PM
6 myBeacon 4.71 27-AUG-1704.20.00.239919PM
6 myBeacon 3.95 27-AUG-1704.20.00.240297PM
7 myBeacon 6.41 27-AUG-1704.20.00.908366PM
5 myBeacon 5.12 27-AUG-1704.20.00.949043PM
6 myBeacon 4.21 27-AUG-1704.20.01.072711PM
7 myBeacon 5.92 27-AUG-1704.20.01.630422PM
7 myBeacon 6.85 27-AUG-1704.20.01.667588PM
6 myBeacon 4.69 27-AUG-1704.20.02.347791PM
5 myBeacon 5.5 27-AUG-1704.20.02.348201PM
7 myBeacon 5.88 27-AUG-1704.20.02.349190PM
Round to the nearest X
Round mestamps to the nearest 5 seconds
1. (cast(created_on as date) - date '1970-01-01')
2. *24*60*60
3. round((...)/5)
4. * 5
h ps://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:13946369553642
select round(((cast(created_on as date) - date '1970-01-01')
*24*60*60)
/5)
* 5
from reading
order by 2;
20. Draw a circle using x, y, radius
Convert x, y + radius into 3 points on the circle
mdsys.sdo_geometry(
2003,
null,
null,
mdsys.sdo_elem_info_array(1, 1003, 4),
mdsys.sdo_ordinate_array(
(x - rad), y,
x, (y + rad),
(x + rad), y
));
Intersec ng Polygons
21. SDO_GEOM.SDO_INTERSECTION(sdo_geometry(2003, null, null,
mdsys.sdo_elem_info_array(1, 1003, 3),
mdsys.sdo_ordinate_array(0,0, 10,10)),
beacon_tools.circle(0, 0, 5),
0.005)
Beacon Enters a Pre-Defined Zone
Define zones using polygons.
select zone
from restricted_areas
where SDO_OVERLAPBDYINTERSECT(zone,
beacon_tools.location('beacon1','someTimestamp'));
Recap