-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeacon.js
137 lines (117 loc) · 3.46 KB
/
beacon.js
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
var tessel = require('tessel');
var blelib = require('ble-ble113a');
var Beacon = require('./lib/beacon');
var http = require('http');
var ble = blelib.use(tessel.port['A']);
var led2 = tessel.led[0].low();
// tessel push beacon.js -l -a http://www.athega.se/chrille -a sill -a false
var DEBUG = process.argv.length > 2 && process.argv.pop() || false;
var trackItem = process.argv.length > 2 && process.argv.pop() || 'sill';
var physicalWebUrl = process.argv.length > 2 && process.argv.pop() || 'http://www.athega.se/chrille';
debug("Initializing with physical web url", physicalWebUrl, "and item", trackItem);
var ready = function() {
debug('I\'m ready!');
// Physical Web Beacon
var beacon = new Beacon();
beacon.advertiseUrl(physicalWebUrl);
debug('Advertising', physicalWebUrl);
// connect wifi now, if not already connected
if (!wifi.isConnected()) {
connect();
} else {
// Device scanner
ble.startScanning();
}
};
var trackPeripheral = function(peripheral) {
debug("Discovered peripheral!", peripheral.address.toString(), peripheral.rssi);
track();
};
ble.on('ready', ready);
ble.on('discover', trackPeripheral);
function track() {
debug("Going to send track request");
led2.high();
var req = http.get({
host: 't-track.herokuapp.com',
path: '/t/' + trackItem
}, function(response) {
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
debug(body);
});
});
req.on('error', function(err) {
debug("Tracking error:", err);
});
req.end();
setTimeout(function led2ToLow() { led2.low(); }, 750);
}
/* the wifi-cc3000 library is bundled in with Tessel's firmware,
* so there's no need for an npm install. It's similar
* to how require('tessel') works.
*/
var wifi = require('wifi-cc3000');
var network = 'Tobbe och hans Mats'; // put in your network name here
var pass = 'athegacodebase'; // put in your password here, or leave blank for unsecured
var security = 'wpa2'; // other options are 'wep', 'wpa', or 'unsecured'
var timeouts = 0;
wifi.on('connect', function(data){
// you're connected
debug("connect emitted", data);
// Device scanner
ble.startScanning();
});
wifi.on('disconnect', function(data){
// wifi dropped, probably want to call connect() again
debug("disconnect emitted", data);
})
wifi.on('timeout', function(err){
// tried to connect but couldn't, retry
debug("timeout emitted");
timeouts++;
if (timeouts > 2) {
// reset the wifi chip if we've timed out too many times
powerCycle();
} else {
// try to reconnect
connect();
}
});
wifi.on('error', function(err){
// one of the following happened
// 1. tried to disconnect while not connected
// 2. tried to disconnect while in the middle of trying to connect
// 3. tried to initialize a connection without first waiting for a timeout or a disconnect
debug("error emitted", err);
});
// reset the wifi chip progammatically
function powerCycle(){
// when the wifi chip resets, it will automatically try to reconnect
// to the last saved network
wifi.reset(function(){
timeouts = 0; // reset timeouts
debug("done power cycling");
// give it some time to auto reconnect
setTimeout(function(){
if (!wifi.isConnected()) {
// try to reconnect
connect();
}
}, 20 *1000); // 20 second wait
})
}
function connect(){
wifi.connect({
security: security
, ssid: network
, password: pass
, timeout: 30 // in seconds
});
}
function debug(msg, arg1, arg2, arg3) {
if (DEBUG) console.log(msg, arg1, arg2, arg3);
}