Skip to content

Instantly share code, notes, and snippets.

@hfiennes
Last active December 27, 2016 12:18
Show Gist options
  • Save hfiennes/8ed300b1d89e22e35dc593d0711c9484 to your computer and use it in GitHub Desktop.
Save hfiennes/8ed300b1d89e22e35dc593d0711c9484 to your computer and use it in GitHub Desktop.
Electric Imp client for FIND
// electric imp client for FIND
// [email protected] 20161225
const FIND_GROUP = "group";
const FIND_USERNAME = "username";
const FIND_LOCATION = "someplace";
device.on("location", function(scan) {
server.log(http.jsonencode(scan));
// Reformat MAC addresses to make the wifi list
local wifilist = [];
foreach(ap in scan) {
// Add colons to MAC address & add entry
local bssid_with_colon = ap.bssid.slice( 0, 2)+":"+
ap.bssid.slice( 2, 4)+":"+
ap.bssid.slice( 4, 6)+":"+
ap.bssid.slice( 6, 8)+":"+
ap.bssid.slice( 8,10)+":"+
ap.bssid.slice(10,12);
wifilist.append({ "mac":bssid_with_colon, "rssi":ap.rssi });
}
// Make the body
local body = { "group": FIND_GROUP,
"username": FIND_USERNAME,
"location": FIND_LOCATION,
"wifi-fingerprint": wifilist };
// Form POST request
local request = http.post("https://ml.internalpositioning.com/track",
{ "Content-Type": "application/json",
"Accept": "application/json"},
http.jsonencode(body));
// Send & check response in async callback
request.sendasync(function(res) {
server.log("Response: "+res.statuscode+" Body: "+res.body);
});
});
// electric imp client for FIND
// [email protected] 20161225
//
// Power consumption:
// ~3 seconds at ~80mA to connect, scan wifi networks, and send (TLS1.2 EDH connection)
// 60s sleep at ~50uA (6uA from imp, ~44uA from other things on impExplorer)
// With a 3+60 second cycle, average current draw is ~3.85mAh
//
// For shorter sleeps, you'd stay awake and use imp.enablepowersave(true) to reduce
// wifi power consumption. Average current should be <10mA on a quiet network with imp001
//
// Improvements: Could sleep on accelerometer interrupt and only wake when movement is detected
// for multi-year battery life.
//
// impExplorer board with 3x alkaline AA's (2500mAh):
// 60s sleep: 27 days
// 120s sleep: 52 days
// 300s sleep: 123 days
// 600s sleep: 232 days
//
// impExplorer can be found at http://a.co/8VHnzKD
// Scan networks, send results to agent
server.log("Sending location");
agent.send("location", imp.scanwifinetworks());
// Go to sleep when system has gone idle
imp.onidle(function() {
server.log("Sleeping");
imp.deepsleepfor(60);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment