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
// Define an HTTP request handler | |
function requestHandler(request, response) { | |
try { | |
if ("setting" in request.query) { | |
// 'setting' is a URL-encoded parameter, ie. '/setting=4' | |
local settingValue = request.query.setting.tointeger(); | |
// Use the 'response' object to acknowledge reception of the request | |
// to the request's source. '200' is HTTP status code for 'OK' | |
response.send(200, "Setting received and applied"); |
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
// Set up outgoing request object as a global (we may need to cancel it, so we need a reference to it) | |
request <- http.get(webServiceURL, webServiceHeaders); | |
// Define the response handler | |
function handleResponse(responseTable) { | |
// Called when the imp receives an immediate acknowledgement from the remote service | |
if (responseTable.statuscode == 200) { | |
// Remote service has responded with 'OK' so decode | |
// the response's body 'responseTable.body' and headers 'responseTable.headers' | |
// Code omitted for clarity... |
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
// Load the data from persistent storage | |
settings <- server.load(); | |
// It the load is null, it tells us this is the first time the agent is running | |
if (settings.len() == 0) { | |
// Set and save defaults | |
settings = { "colour" : { "r" : 255, | |
"g" : 0, | |
"b" : 0 }, | |
"state" : 0 }; |
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
function startup() { | |
// Check if nv table exists and if the key 'count' is in the nv table | |
if (!("nv" in getroottable() && "count" in nv)) { | |
// Create the nv table as a global variable with the key 'count' | |
nv <- { "count" : 0 }; | |
// Add a second nv-persisted key, 'previous' | |
nv.previous <- ""; | |
} |
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
// EARLY CALL CODE | |
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30.0); | |
// GLOBALS | |
local disconnectedFlag = false; | |
local disconnectedCount = 0; | |
// FUNCTIONS | |
function mainProgramLoop() { | |
// We'll fill this in below |
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
// EARLY CALL CODE | |
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30.0); | |
// GLOBALS | |
local disconnectedFlag = false; | |
local disconnectedCount = 0; | |
// FUNCTIONS | |
function mainProgramLoop() { | |
// Ensure the program loops safely every second |
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
// EARLY CALL CODE | |
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30.0); | |
// GLOBALS | |
local downTime = time(); | |
local reconnectAttemptFlag = false; | |
local sensorData = {}; | |
// FUNCTIONS | |
function mainLoop() { |
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
// Set the reconnection policy as early as possible in the code | |
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30.0); | |
// Disable the BlinkUp LED (enforced 60s after boot) | |
imp.enableblinkup(false); | |
local willNeedBlinkupFlag = false; | |
local netData = imp.net.info(); | |
if ("active" in netData) { |
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
// Set the deep sleep trigger to take place only when the imp is idle | |
imp.onidle(function() { | |
// Inform the agent that the device will now sleep for one hour | |
// NOTE time specified in seconds | |
server.sleepfor(3600); | |
}); |
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
// Set the deep sleep trigger to take place only when the imp is idle | |
imp.onidle(function { | |
// Tell the agent that the device will now sleep until quarter past midnight on Friday | |
server.sleepuntil(0, 15, 0, 5); | |
}); |