Skip to content

Instantly share code, notes, and snippets.

View ElectricImpSampleCode's full-sized avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / crypto.verify.agent.nut
Last active September 10, 2018 14:47
Electric Imp crypto.verify() example
// CONSTANTS
// Full key omitted but can be generated with:
// $ openssl genrsa -out rsa.key 2048
const RSA_PRIVATE_KEY_PEM = @"
-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----";
// The public key must be extracted from the private key file:
// $ openssl rsa -pubout < rsa.key > rsa.pub
@ElectricImpSampleCode
ElectricImpSampleCode / crypto.sign.agent.nut
Last active September 10, 2018 14:45
Electric Imp imp API crypto.sign() PKCS#1 example
// CONSTANTS
// Full PKCS#1 key omitted but can be generated with:
// $ openssl genrsa -out rsa.key 2048
const RSA_PRIVATE_KEY_PEM = @"
-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----
";
function decodePrivatePem(pemString) {
@ElectricImpSampleCode
ElectricImpSampleCode / factory.activation.agent.nut
Last active March 25, 2019 15:01
Factory Activation Example Code
// ------------------------------------------------------------------------------
// File: factory.activation.agent.nut
// Version: 1.0.4
//
// 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
@ElectricImpSampleCode
ElectricImpSampleCode / audiokit.agent.nut
Last active September 12, 2018 11:27
imp004m Audio Kit Sample Code
// Set up the variables
local receiveBuffers = [];
local size = 0;
// Define the handler for storing audio from the device
device.on("store.audio.data", function(buffer) {
// Break the incoming data down into 1024-byte chunks
while (buffer.tell() < buffer.len()) {
local store = blob();
local bytesleft = buffer.len() - buffer.tell();
@ElectricImpSampleCode
ElectricImpSampleCode / impmonitor.agent.nut
Last active November 25, 2019 13:33
Example Code: development device status monitoring using the impCentral™ API
// impMonitor AGENT CODE
// Copyright (c) 2018, Electric Imp, Inc.
// Writer: Tony Smith
// Licence: MIT
// Version: 1.1.1
// IMPORTS
#require "Rocky.class.nut:2.0.2"
// If you are not using Squinter or an equivalent tool to combine multiple Squirrel files,
// you need to paste the contents of the accompanying files over the following four lines
@ElectricImpSampleCode
ElectricImpSampleCode / deviceinfo.device.nut
Last active September 10, 2018 14:48
Electric Imp imp API device.info() example code
local address = "";
function checkIP() {
local connectionData = device.info();
if (connectionData.isconnected) {
server.log(format("Device %s is connected", connectionData.id);
if (address.len() == 0) {
server.log(format("Device %s has connected with IP address %s", connectionData.id, connectionData.ipaddress));
} else if (connectionData.ipaddress != address)
@ElectricImpSampleCode
ElectricImpSampleCode / imppowersavedemo.agent.nut
Last active September 12, 2018 10:18
Electric Imp imp API imp.setpowersave() demo code
// Just respond to 'ping' messages by immmediately
// sending back a 'pong' message to the device
device.on("ping", function(dummy) {
device.send("pong", true);
});
@ElectricImpSampleCode
ElectricImpSampleCode / deviceinfo.device.nut
Last active August 27, 2019 09:18
Boot-time Device Information
/**
* Boot-time device information functions
*
* Code which logs impOS and network information. It is intended to be included early in the runtime (hence the name).
* Includes functions and code to trigger those functions
*
* @author Tony Smith (@smittytone)
* @copyright Electric Imp, 2017-18
* @licence MIT
* @version 2.2.2
@ElectricImpSampleCode
ElectricImpSampleCode / imp004m.ffd.nut
Last active March 2, 2018 12:14
imp004m fixedfrequencydac example
dac <- hardware.fixedfrequencydac;
dac.configure(hardware.pwmpairKD,
mySampleRate,
myBufferArray,
myBufferFunction,
audioOptions);
dac.start();
@ElectricImpSampleCode
ElectricImpSampleCode / bpsn.agent.nut
Last active October 16, 2017 10:49
BPSN Fridge Monitor Example
// Set up a table of static calendar functions
utilities <- {};
utilities.dayOfWeek <- function(d, m, y) {
local dim = [
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
];
local ad = ((y - 1) * 365) + _totalLeapDays(y) + d - 5;