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.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 / 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 / products.js
Last active January 17, 2020 11:38
impCentral API Example Code: Retrieve Products and their Device Groups
// CONSTANTS
// Replace the 'USERNAME' and 'PASSWORD' values with your own
const USERNAME = '...'
const PASSWORD = '...'
const API_URL = 'api.electricimp.com'
const API_VER = '/v5/'
// GLOBALS
var https = require('https');
var products = [];
@ElectricImpSampleCode
ElectricImpSampleCode / flush.device.nut
Last active December 17, 2019 15:02
Electric Imp imp API uart.flush() example
// Assign parallel aliases for the UART according to role
arduino <- hardware.uart57;
computer <- hardware.uart57;
function relay() {
// UART initially configured for Arduino input
local byte = arduino.read();
if (byte != -1) {
// We have a valid byte to relay so reconfigure the UART
@ElectricImpSampleCode
ElectricImpSampleCode / hmacsha1.agent.nut
Last active December 11, 2019 15:30
Electric Imp imp API hash.hmacsha1() example code
helper <- {
function encode(str) {
return http.urlencode({ s = str }).slice(2);
}
}
class TwitterClient {
consumerKey = null;
consumerSecret = null;
accessToken = null;
@ElectricImpSampleCode
ElectricImpSampleCode / hmacsha256.agent.nut
Last active December 11, 2019 15:26
Electric Imp imp API hash.hmacsha256() example code
function blobToHexString(data) {
local s = "0x";
foreach (b in data) s += format("%02X", b);
return s;
}
function testHash() {
local data = "The quick brown fox jumps over the lazy dog";
local key = "key";
local hash = http.hash.hmacsha256(data, key);
@ElectricImpSampleCode
ElectricImpSampleCode / hmacsha512.agent.nut
Last active December 11, 2019 15:23
Electric Imp imp API hash.hmacsha512() example code
function blobToHexString(data) {
local s = "0x";
foreach (b in data) s += format("%02X", b);
return s;
}
function testHash() {
local data = "The quick brown fox jumps over the lazy dog";
local key = "key";
local hash = http.hash.hmacsha512(data, key);
@ElectricImpSampleCode
ElectricImpSampleCode / md5.agent.nut
Last active December 11, 2019 15:20
Electric Imp imp API hash.md5() example code
function blobToHexString(data) {
local s = "0x";
foreach (b in data) s += format("%02X", b);
return s;
}
function testHash() {
local data = "The quick brown fox jumps over the lazy dog";
local hash = http.hash.md5(data);