-
Notifications
You must be signed in to change notification settings - Fork 3
/
barnowl-baby-steps.html
204 lines (199 loc) · 11.8 KB
/
barnowl-baby-steps.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE HTML>
<html>
<head>
<title>diyActive: Barnowl Baby Steps</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Learn how to use barnowl, reelyActive's free middleware package for Node.js which is central to all our software.">
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style/main.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.4/ui-bootstrap-tpls.min.js"></script>
<script type="text/javascript" src="js/diyactive.js"></script>
</head>
<body ng-app="diyActive">
<div ng-controller="InteractionCtrl">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle"
ng-init="navCollapsed = true"
ng-click="navCollapsed = !navCollapsed">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="https://www.reelyactive.com">
<strong>reely</strong>Active
</a>
</div>
<div class="collapse navbar-collapse" ng-class="!navCollapsed && 'in'"
ng-click="navCollapsed = true">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="index.html"> diyActive </a></li>
<li class="dropdown" dropdown on-toggle="toggled(open)">
<a href class="dropdown-toggle" dropdown-toggle role="button"
data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false">
Quick links <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>
<a href="https://github.com/reelyactive/" target="_blank">
reelyActive on GitHub
</a>
</li>
<li>
<a href="https://www.npmjs.com/~reelyactive" target="_blank">
reelyActive on npmjs
</a>
</li>
<li role="separator" class="divider"></li>
<li>
<a href="https://www.reelyactive.com" target="_blank">
reelyActive website
</a>
</li>
<li>
<a href="https://getpareto.com" target="_blank">
Pareto by reelyActive
</a>
</li>
<li role="separator" class="divider"></li>
<li>
<a href="https://shop.reelyactive.com" target="_blank">
Our online store
</a>
</li>
<li>
<a href="https://reelyactive.com/blog/" target="_blank">
Our blog
</a>
</li>
</ul>
</li>
</ul>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-xs-0 col-sm-1 col-md-2 col-lg-3"></div>
<div class="col-xs-12 col-sm-10 col-md-8 col-lg-6">
<h1> Barnowl Baby Steps </h1>
<p> Here we'll present how to write simple JavaScript programs that do potentially interesting and useful things with <a href="https://www.npmjs.org/package/barnowl" target="_blank">barnowl</a>, our free middleware package for Node.js. </p>
<img src="images/barnowlBabySteps.jpg"
class="img-responsive center-block">
<h2> Installing Node.js </h2>
<p> If you don't already have Node.js installed, be sure to do so first! Installation is easy on all platforms. </p>
<p class="text-center">
<a class="btn btn-primary" href="https://nodejs.org"
role="button" target="_blank"> Let's do it </a>
</p>
<h2> Installing barnowl </h2>
<p> The easy way is via npm. Create a new directory called <b>baby-barnowl</b>, then from that directory run: <br> <code> npm install barnowl </code> </p>
<p> Yeah, that's it, you're done! </p>
<hr>
<p> If you prefer to install from github, instead clone the <a href="https://github.com/reelyactive/barnowl" target="_blank">barnowl repository</a>. </p>
<h2> Running barnowl and telling it where to listen </h2>
<p> It's as simple as three lines of code! First you load barnowl, second, you create an instance of barnowl and, third, you tell that instance where to listen for packets. Create a new file called <b>baby-barnowl.js</b> in the same directory and paste in the following lines of code: </p>
<pre>
var barnowl = require('barnowl');
var middleware = new barnowl();
middleware.bind({ protocol: 'test',
path: 'default' }); </pre>
<p> If you've already completed the <a href="install-a-starter-kit.html" target="_blank">Install a Starter Kit</a> tutorial, you'll likely want to update the <i>protocol</i> and <i>path</i> to listen for your hardware. The 'test' protocol specified above will initiate a packet generator that can be used in the absence of hardware. </p>
<p> If you run this code, it will listen for and process any received packets. In order to do something useful we'll need to handle the output as we'll show next. </p>
<h2> Printing packets to the console </h2>
<p> Barnowl outputs <i>visibilityEvents</i> which our code can listen for. To listen for these events and output the data to the console (something potentially useful), paste in these additional lines of code: </p>
<pre>
middleware.on('visibilityEvent', function(tiraid) {
console.log(tiraid); // Useful code goes here
}); </pre>
<p> Now run your code as follows to see data packets output to the console: <br> <code> node baby-barnowl.js </code> </p>
<h2> Wait, what's a tiraid? </h2>
<p> A tiraid combines three elements:
<ul>
<li> <b>ti</b>mestamp
<li> <b>ra</b>dio decodings
<li> <b>id</b>entifier
</ul>
</p>
<p> These are in fact the three core elements of hyperlocal context (which you can learn more about <a href="http://context.reelyactive.com/context.html" target="_blank">on the reelyActive website</a>). Radio decodings and location are effectively equivalent if the location of the decoding devices is known (as is typically the case for fixed infrastructure). </p>
<p> The data produced by barnowl is always structured as these three elements. In the following sections, we'll see how we can filter based on these criteria. </p>
<h2> Filtering packets by signal strength </h2>
<p> Perhaps we're only interested in outputting data to the console for devices that are physically close to a reelceiver. In other words, we only display the data when the signal strength is above a threshold. To do so, we simply wrap the <i>console.log(tiraid);</i> from the previous example in an if statement which tests the signal strength or rssi (Received Signal Strength Indication): </p>
<pre>
var rssi = tiraid.radioDecodings[0].rssi;
if(rssi > 160) {
console.log(tiraid);
} </pre>
<p> Now run your code again and try moving the detected wireless device(s) closer and farther away from the reelceiver(s). </p>
<h2> Filtering packets by identifier </h2>
<p> Perhaps we're only interested in outputting data to the console for a specific device. In other words, we only display the data when it is associated with a given device identifier. To do so, we again wrap the <i>console.log(data);</i> from the previous example in an if statement which tests the identifier: </p>
<pre>
var identifier = tiraid.identifier.value;
if(identifier === '001bc50940100000') {
console.log(tiraid);
} </pre>
<p> Now run your code again and confirm that only packets from the given device are being output to the console. </p>
<h2> Going one step further with reelib </h2>
<p> The <a href="https://www.npmjs.com/package/reelib" target="_blank">reelib</a> package is a library for common reelyActive methods. Next we'll show how it simplifies printing the data stream to the console or to a file. Install reelib as follows: <br> <code> npm install reelib </code> </p>
<h3> Prettier printing to the console </h3>
<p> Create a new file called <b>baby-barnowl-print.js</b> and paste in the following code: </p>
<pre>
var barnowl = require('barnowl');
var reelib = require('reelib');
var middleware = new barnowl();
middleware.bind({ protocol: 'test',
path: 'default' });
middleware.on('visibilityEvent', function(tiraid) {
console.log(reelib.tiraid.toShortString(tiraid));
}); </pre>
<p> Now run your code as follows to see data packets output to the console in a single line: <br> <code> node baby-barnowl-print.js </code> </p>
<h3> Logging data to a file </h3>
<p> Create a new file called <b>baby-barnowl-log.js</b> and paste in the following code: </p>
<pre>
var barnowl = require('barnowl');
var reelib = require('reelib');
var fs = require('fs');
var middleware = new barnowl();
middleware.bind({ protocol: 'test',
path: 'default' });
fs.appendFile('log.csv', reelib.tiraid.CSV_HEADER + '\r\n');
middleware.on('visibilityEvent', function(tiraid) {
var csv = reelib.tiraid.toCSVString(tiraid);
fs.appendFile('log.csv', csv + '\r\n');
}); </pre>
<p> Now run your code as follows for the data to be written as comma-separated values to the file <b>log.csv</b>: <br> <code> node baby-barnowl-log.js </code> </p>
<p> You can easily analyze and graph the data by opening <b>log.csv</b> in a spreadsheet program such as Excel or Google Sheets. </p>
<p> </p>
<h2> Don't give a hoot about writing code? </h2>
<img src="images/pareto-by-reelyactive.jpg"
class="img-responsive center-block"><br>
<p> Our Pareto platform combines even more features and functionality with the convenience of SaaS. Leave the coding to us. </p>
<p class="text-center">
<a class="btn btn-primary" href="https://getpareto.com"
target="_blank" role="button"> Visit getpareto.com </a>
</p>
<h2> What's next? </h2>
<p> Read <a href="mascot-stories.html#barnowl">the barnowl story</a> and learn more about our software stack. <a href="build-web-apps-with-beaver.html">Build web apps with beaver.js</a> to do much the same but in your web browser rather than the command line. </p>
<p class="text-center">
<a class="btn btn-default" href="mascot-stories.html#barnowl"
role="button"> The barnowl story </a>
<a class="btn btn-default" href="build-web-apps-with-beaver.html"
role="button"> Build web apps with beaver.js </a>
<a class="btn btn-success" href="/"
role="button"> Return to diyActive </a>
</p>
</div>
<div class="col-xs-0 col-sm-1 col-md-2 col-lg-3"></div>
</div>
</div>
<footer class="footer">
<a href="index.html"> diyActive </a> |
<a href="https://www.reelyactive.com">
© reelyActive 2014-2018
</a>
</footer>
</div>
</body>
</html>