Skip to content

Instantly share code, notes, and snippets.

View ElectricImpSampleCode's full-sized avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / http.agent.nut
Last active September 10, 2018 10:34
This example shows a minimal HTTP Request handler function with the recommended structure.
// 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");
@ElectricImpSampleCode
ElectricImpSampleCode / longpoll.agent.nut
Last active September 10, 2018 10:32
This example shows the creation of a simple HTTP GET request with a long-polling function to a remote service that delivers push notifications.
// 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...
@ElectricImpSampleCode
ElectricImpSampleCode / datapersist.agent.nut
Last active September 10, 2018 10:28
This example shows a typical flow for the persistence of data across agent restarts.
// 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 };
@ElectricImpSampleCode
ElectricImpSampleCode / getroottable.device.nut
Last active September 10, 2018 14:25
This example shows the use of Squirrel’s getroottable() function to test for the presence of the imp API nv table object at start-up.
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 <- "";
}
@ElectricImpSampleCode
ElectricImpSampleCode / disconnectTrap.device.nut
Last active March 25, 2019 12:41
A code snippet showing when and how to trap unexpected disconnection events.
// 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
@ElectricImpSampleCode
ElectricImpSampleCode / reconnect.device.nut
Last active March 25, 2019 12:48
A code snippet showing a way to manage attempts to reconnect to the imp’s agent.
// 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
@ElectricImpSampleCode
ElectricImpSampleCode / deliberateDisconnect.device.nut
Last active March 25, 2019 12:52
This example shows how device code can deliberately disconnect from the server yet remain operational.
// 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() {
@ElectricImpSampleCode
ElectricImpSampleCode / offlineStart.device.nut
Last active March 25, 2019 12:58
This code snippet shows how you might set a device to boot (warm or cold) into an offline state.
// 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) {
@ElectricImpSampleCode
ElectricImpSampleCode / deepSleepDuration.device.nut
Last active September 10, 2018 10:51
A code snippet that demonstrates server.sleepfor().
// 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);
});
@ElectricImpSampleCode
ElectricImpSampleCode / deepSleepUntil.device.nut
Last active September 10, 2018 10:51
A code snippet that demonstrates server.sleepuntil().
// 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);
});