Skip to content

Instantly share code, notes, and snippets.

View ElectricImpSampleCode's full-sized avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / crashreporter.nut
Last active August 27, 2019 09:57
Cookbook Recipe: Add A Crash Reporter
/**
* Generic development-oriented crash report service
*
* @author Tony Smith (@smittytone)
* @licence MIT
* @version 1.0.0
*/
crashReporter <- {
/**
@ElectricImpSampleCode
ElectricImpSampleCode / onidle.server.disconnect.device.nut
Last active January 31, 2019 12:20
imp API Cookbook ‘How To Disconnect And/or Deep-sleep Devices Correctly’ Sample Code
const RETRY_TIME = 120;
const CONNECT_TIMEOUT = 30;
// Set the reconnection/error trigger policy (RETURN_ON_ERROR) early
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 10);
function connectHandler(reason) {
if (reason != SERVER_CONNECTED) {
// Queue up a reconnection attempt
imp.wakeup(RETRY_TIME, reconnect);
@ElectricImpSampleCode
ElectricImpSampleCode / factory.firmware.dut.agent.nut
Last active August 6, 2020 09:23
New Factory Firmware sample for separate DUT and Fixture Device Groups
// ------------------------------------------------------------------------------
// File: factory.firmware.dut.agent.nut
// Version: 2.1.1
//
// Copyright 2020 Twilio
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@ElectricImpSampleCode
ElectricImpSampleCode / jsonencode.device.nut
Last active February 4, 2019 14:38
Squirrel example code: JSON encode for devices
// VERSION 1.0.1
function jsonencode(obj, opts = null, ins = 0) {
local cp = "compact" in opts ? opts.compact : false;
local es = cp ? "" : " ";
local sp = "";
if (!cp && ins > 0) {
for (local i = 0 ; i < ins ; i++) sp += " ";
}
// Branch on type of object being processed
switch (typeof obj) {
@ElectricImpSampleCode
ElectricImpSampleCode / losant.agent.nut
Last active November 7, 2018 12:47
Losant Example
// MIT License
// Copyright 2018 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// Zigbee Lamp Simulator - Gateway Agent Code
// Copyright Electric Imp, Inc. 2018
// IMPORTS
#require "Rocky.class.nut:2.0.1"
// CONSTANTS
const HTML_STRING = @"
<!DOCTYPE html>
<html lang='en-US'>
@ElectricImpSampleCode
ElectricImpSampleCode / imp.net.counters.device.nut
Created September 12, 2018 10:36
Electric Imp imp API imp.net.counters() example
local debug = true;
local infoTable = {};
local wifiData = imp.net.counters();
if ("wwd_wifi" in wifiData) {
// Get the raw WiFi data from the WICED WiFi profile and use it
// to populate a Squirrel table of WiFi information
local dataBlob = wifiData.wwd_wifi;
infoTable.version <- dataBlob.readn('w');
infoTable.length <- dataBlob.readn('w');
@ElectricImpSampleCode
ElectricImpSampleCode / imp.setrescuepin.device.nut
Created September 12, 2018 10:27
Electric Imp imp API imp.setrescuepin()
// To use a rescue pin, add the following function to your factory firmware
// and modify the pin and logic state as required
function setRescuePin() {
// Set up the rescue pin. We only need call this in our factory firmware
// if we are implementing a rescue pin, and this is why this function is
// not a part of our standard factory firmware example at
// https://developer.electricimp.com/examples/factoryfirmware
// Configure for impC001's pin G, set to go HIGH to trigger a rescue
imp.setrescuepin(hardware.pinG, 1);
@ElectricImpSampleCode
ElectricImpSampleCode / PowerEfficientRefrigeratorMonitor.agent.nut
Last active July 24, 2020 09:36
An Introduction To Squirrel Applications Example 8
// Power Efficient Refrigerator Monitor Agent Code
// -------------------------------------------------------
// CLOUD SERVICE LIBRARY
// -------------------------------------------------------
// Libraries must be required before all other code
// Initial State Library
#require "InitialState.class.nut:1.0.0"
// Library to manage agent/device communication
@ElectricImpSampleCode
ElectricImpSampleCode / RemoteMonitoringWithInterrupt.agent.nut
Last active July 24, 2020 09:30
An Introduction To Squirrel Applications Example 7
// Remote Monitoring Application With Interrupt Agent Code
// -------------------------------------------------------
// CLOUD SERVICE LIBRARY
// -------------------------------------------------------
// Libraries must be required before all other code
// Initial State Library
#require "InitialState.class.nut:1.0.0"
// Library to manage agent/device communication