Skip to content

Instantly share code, notes, and snippets.

View ElectricImpSampleCode's full-sized avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / imp006.impt.correct.device.nut
Last active July 13, 2020 10:29
imp006 Breakout Board impt tutorial correct version
// APPLICATION LIBRARIES
// Include the temperature/humidity sensor Library
#require "HTS221.device.lib.nut:2.0.2"
// APPLICATION CONSTANTS
// The I2C address for the sensor
const I2C_ADDR = 0xBE;
// APPLICATION GLOBALS
// Set up the imp I2C bus the sensor is connected to
@ElectricImpSampleCode
ElectricImpSampleCode / imp006.bb.gs2.device.nut
Last active June 30, 2020 09:40
imp006 Breakout Board Getting Started #2 — Device Code
// Set up global variables
// Reference the imp006 pins connected two of the user LED's colors
red_pin <- hardware.pinR;
// Define the LED flash function
function setLed(state) {
// Set the user LED control pins' state
red_pin.write(state ? 1 : 0);
}
@ElectricImpSampleCode
ElectricImpSampleCode / imp006.bb.gs2.agent.nut
Created June 24, 2020 11:41
imp006 Breakout Board Getting Started #2 — Agent Code
// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + "?led=1");
server.log("Turn LED Off: " + http.agenturl() + "?led=0");
// Define the function called when a request is sent to the agent
function requestHandler(request, response) {
try {
// Check if the user sent 'led' as a query parameter
if ("led" in request.query) {
// If they did, and led = anything or 0, set our state variable
@ElectricImpSampleCode
ElectricImpSampleCode / imp006.bb.gs1.device.nut
Last active June 30, 2020 09:38
imp006 Breakout Board Getting Started #1
// Set up global variables
// Reference the imp006 pin connected the user LED's blue light
blue_pin <- hardware.pinXB;
// Track the LED flash state
state <- false;
// Define the LED flash function
function flash() {
// Set the user LED control pin's state
state = !state;
@ElectricImpSampleCode
ElectricImpSampleCode / bindenv.device.nut
Created July 2, 2019 00:14
Squirrel bindenv() function example
function stateChangeCallback(state) {
// Define a callback function, which we will bind to the interface record
// in 'getAvailableInterfaces()' so that we know which interface's change
// of state actually triggered the callback
// Is the interface available for use? It is if it's connected
this.isAvailable = (state == imp.net.CONNECTED);
// When we reach a terminal state consider the interface reported
if (state == imp.net.CONNECTED ||
@ElectricImpSampleCode
ElectricImpSampleCode / network.interface.check.device.nut
Last active November 28, 2019 10:19
impOS 42 local networking interface availability checker
// The 'interfaces' variables holds references to available interfaces
local interfaces = {};
// Set flags that the app can check to see if an interface can be used
local availableForLocal = { "wifi" : false,
"ethernet" : false,
"cell" : false };
// These globals are used for reporting
local interfaceCount = 0;
@ElectricImpSampleCode
ElectricImpSampleCode / UDP_Hub_secure.agent.nut
Last active April 22, 2022 10:00
impOS 42 UDP Local Networking Example
// ********** Imports **********
#require "Rocky.class.nut:2.0.2"
// ********** Web UI **********
const HTML_STRING = @"<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>UDP Demo</title>
@ElectricImpSampleCode
ElectricImpSampleCode / imp.cancelwakeup.byname.agent.nut
Last active March 20, 2019 14:28
Code demonstrating the use of names in imp.cancelwakeup() on the agent
// Set up a timer...
imp.wakeup(60,
function() { server.log("Timer \"wakeup_timer\" woke up"); },
"wakeup_timer");
// ...but cancel it before it fires...
local timer = imp.wakeup(40, function() {
server.log("Timer \"wakeup_timer\" cancelled");
imp.cancelwakeup("wakeup_timer");
});
@ElectricImpSampleCode
ElectricImpSampleCode / factory.activation.dut.agent.nut
Last active November 23, 2020 14:29
Factory Activation Example Code 2.1.1
// ------------------------------------------------------------------------------
// File: factory.activation.dut.agent.nut
// Version: 2.1.2
//
// 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 / simple.factory.firmware.device.nut
Created February 28, 2019 16:50
Example Code: Simple Factory Firmware
// ------------------------------------------------------------------------------
// File: simple.factory.firmware.device.nut
// Version: 2.0.0
//
// Copyright 2015-19 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