Skip to content

Instantly share code, notes, and snippets.

@imp-guru
Created July 30, 2014 15:01
Show Gist options
  • Save imp-guru/ff8e3bea02a3f00f7211 to your computer and use it in GitHub Desktop.
Save imp-guru/ff8e3bea02a3f00f7211 to your computer and use it in GitHub Desktop.
Create a REST API for Electric Imp using Blocking device.on()
// ----------------------Agent Code-----------------------
function requestHandler(request, response) {
try {
// Send a read request to the device
device.send("getRequest", request);
// Blocking wait to receive response from the imp
device.on("getResponse", function(data) {
response.send(200,data);
});
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
// register the HTTP handler
http.onrequest(requestHandler);
// ----------------------Device Code----------------------
// Define test response data
local responseData = "Hello"
function getRequest(params) {
agent.send("getResponse", responseData)
}
agent.on("getRequest", getRequest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment