Last active
August 29, 2015 13:56
-
-
Save azdle/9005856 to your computer and use it in GitHub Desktop.
A very simple example showing how to use the Electric Imp with Exosite.
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
// Class for Communication to Exosite - No Need to Edit | |
class Exosite{ | |
cik = null; | |
vendor = null; | |
model = null; | |
serial = null; // Consider hardware.getimpeeid() or imp.getmacaddress() | |
constructor(cikp = null, vendorp = null, modelp = null, serialp = null){ | |
if(cikp == null){ | |
server.log("No CIK, need to provision.") | |
if(vendorp != null, modelp != null, serialp != null){ | |
// Save Values for Later | |
vendor = vendorp; | |
model = modelp; | |
serial = serialp; | |
// Do Provision | |
local ret = this.provision(vendorp, modelp, serialp); | |
if(ret != null){ | |
cik = ret; | |
local data_table = server.load(); | |
datatable["cik"] = cik; | |
server.save(data_table); | |
} | |
}else{ | |
server.error("Vender, Model, and Serial must be set to provision."); | |
} | |
}else{ | |
cik = cikp; | |
} | |
} | |
function provision(vendorp, modelp, serialp){ | |
if(vendorp == null, modelp == null, serialp == null){ | |
server.error("Vender, Model, and Serial must be set to provision.") | |
return null; | |
} | |
local response = http.post( | |
"https://m2.exosite.com//provision/activate", | |
{"Content-Type": "application/x-www-form-urlencoded", | |
"X-Exosite-CIK": cik}, | |
http.urlencode({vendor = vendorp, | |
model = modelp, | |
sn = serialp}) | |
).sendsync(); | |
if(response.statuscode == 200 && response.headers("content-length" == 40)){ | |
server.log("Provisioned with CIK: "+response.body); | |
cik = response.body; | |
}else{ | |
server.error("Could not provision device on Exosite. HTTP Response Code: "+response.statuscode) | |
return null; | |
} | |
} | |
// Read from Exosite | |
function read(aliases, callback){ | |
local alias_url_format = ""; | |
foreach(alias in aliases){ | |
alias_url_format = alias_url_format + alias + "&"; | |
} | |
local response = http.get( | |
"https://m2.exosite.com/onep:v1/stack/alias?"+alias_url_format, | |
{"Accept": "application/x-www-form-urlencoded", | |
"X-Exosite-CIK": cik} | |
).sendsync(); | |
if(response.statuscode == 200 || response.statuscode == 204){ | |
server.log(response.body); | |
callback(http.urldecode(response.body)); | |
}else{ | |
server.error("Could not read from the OnePlatform. HTTP Response Code: "+response.statuscode) | |
return null; | |
} | |
} | |
// Write to Exosite | |
function write(data){ | |
local response = http.post( | |
"https://m2.exosite.com/onep:v1/stack/alias", | |
{ | |
"Content-Type": "application/x-www-form-urlencoded", | |
"X-Exosite-CIK": cik | |
}, | |
http.urlencode(data) | |
).sendsync(); | |
if(response.statuscode != 204 || response.statuscode == 200){ | |
server.error("Could not write to the OnePlatform. HTTP Response Code: "+response.statuscode) | |
} | |
} | |
} | |
// Setup Exosite Object | |
exoComms <- Exosite("8ce2d7b09a8058f43085aa01cdaddb00a79065ce"); | |
// Agent 'write' to datasource(s) call. | |
device.on("write", function(keyValuePairs) { | |
foreach(alias, value in keyValuePairs){ | |
server.log("Write to "+alias+": "+value); | |
} | |
exoComms.write(keyValuePairs); | |
}); | |
//Agent 'read' from datasource(s) call. | |
device.on("read", function(aliasArray) { | |
foreach(alias in aliasArray){ | |
server.log("Read from "+alias+"."); | |
} | |
local data = exoComms.read(aliasArray, function(data){ | |
device.send("recvvals", data); | |
}); | |
}); | |
function pushReceiveServer(request,res){ | |
server.log("Got Push: "+ request.body) | |
res.send(200, "okay"); | |
device.send("recvvals", http.urldecode(request.body)); | |
} | |
http.onrequest(pushReceiveServer); |
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
// | |
local loop_interval = 30.0 | |
local uptime = 0; | |
// Capture and log a temperature reading every 5m | |
function send_uptime() | |
{ | |
// Set timer for the next capture | |
imp.wakeup(30.0, send_uptime); | |
// Update our Uptime Variable | |
uptime = uptime + 30; | |
// Output the temperature | |
local packet = {}; | |
packet.uptime <- uptime; | |
agent.send("write", packet); | |
} | |
function recvvals(data){ | |
if(data.command != null){ | |
if(data.command == "ok" || data.command == "error"){ | |
// Do Nothing | |
} | |
else if(data.command == "reset uptime"){ | |
server.log("Reseting Uptime") | |
uptime = 0; | |
// Respond to Command | |
local packet = {}; | |
packet.command <- "ok"; | |
agent.send("write", packet); | |
} | |
else{ | |
server.error("Unknown Command: "+data.led) | |
// Respond to Command | |
local packet = {}; | |
packet.command <- "error"; | |
agent.send("write", packet); | |
} | |
} | |
} | |
// Register Read Response with Agent | |
agent.on("recvvals", recvvals); | |
// Start uptime() loop. | |
send_uptime(); | |
// End of code. |
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
-- This script simply waits until a new value gets written to a | |
-- datasource, then pushes that value to the Imp device. | |
--------------- Configure These Variables --------------------- | |
local datasource_alias = 'command' | |
local agent_url = 'https://agent.electricimp.com/ENTER URL HERE' | |
--------------------------------------------------------------- | |
local datasource = alias[datasource_alias] | |
while true do | |
local new_timestamp = datasource.wait() | |
local new_value = datasource[new_timestamp] | |
local body = string.format('%s=%s', datasource_alias, new_value) | |
debug(string.format('Sending Message: %s', body, date())) | |
dispatch.http(agent_url, | |
"post", | |
body, | |
"application/x-www-form-urlencoded" | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment