Last active
March 6, 2017 07:51
-
-
Save bonbombs/e38c9b0fe3fe51fbf963f94afaf1f29e to your computer and use it in GitHub Desktop.
Scans for networks within the area and sends the number of networks it sees over an I2C connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i2c = require('i2c'); | |
var sudo = require("sudo"); | |
var address = 0x04; //FLORA Board device address | |
var wire = new i2c(address, {device: '/dev/i2c-1'}); //Begin I2C connection with FLORA board | |
//Scan networks every 5 seconds | |
setInterval(ScanForNetwork, 5 * 1000); | |
function ScanForNetwork() { | |
iwlistScan(function(err, results){ | |
//Print out results of scan | |
console.log(results); | |
//Write to FLORA how many devices we saw | |
wire.writeByte(results.length, function(err) { | |
console.log("Error: " + err); | |
}); | |
}); | |
} | |
//maxogden's scan call from https://github.com/maxogden/iwlist/blob/master/index.js | |
//modified so that it uses sudo calls with elevated privileges | |
function iwlistScan(callback) { | |
var ps = sudo(['iwlist', 'wlan0', 'scan']); | |
var line = ''; | |
ps.stdout.on('data', function ondata(buf) { | |
for(var i = 0; i < buf.length; i++) { | |
if(buf[i] === 10) { | |
parseLine(line); | |
line = ''; | |
} | |
else line += String.fromCharCode(buf[i]); | |
} | |
}); | |
var stderr = ''; | |
ps.stderr.on('data', function (buf) {stderr += buf }); | |
ps.on('close', function() { | |
if (code !== 0) return callback('Code = ' + stderr); | |
ap.sort(function(a, b) { | |
var x = a['signal']; var y = b['signal']; | |
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); | |
}).reverse(); | |
callback(null, ap); | |
}); | |
var code; | |
ps.on('exit', function(c) { | |
code = c; | |
}); | |
var ap = []; | |
var current = null; | |
function parseLine(line) { | |
var m; | |
if(m = /^\s+Cell \d+ - Address: (\S+)/.exec(line)) { | |
current = {address : m[1] }; | |
ap.push(current); | |
return; | |
} | |
if(!current) return; | |
if(m = /^\s+ESSID:"(.+)"/.exec(line)) { | |
current.essid = m[1]; | |
} | |
if(m = /^\s+Encryption key:(.+)/.exec(line)) { | |
current.encrypted = m[1] !== 'off'; | |
} | |
if(m = /^\s+ESSID:(.+?)\//.exec(line)) { | |
current.signal = +m[1]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment