Skip to content

Instantly share code, notes, and snippets.

@electricimp
electricimp / serverconnect.device.nut
Created January 29, 2015 09:36
Handle multiple server.connect() calls code snippet
// Establish threshold trigger flags for all your sensors
sensorOneTriggerFlag <- false
sensorTwoTriggerFlag <- false
// Define the function that will be called when the device has connected
// to the server (or the connection attempt times out)
function connectResponseCallback(reason)
{
@electricimp
electricimp / persistent.agent.nut
Created January 27, 2015 15:56
Persistent Storage Example
function sendPrefs(value)
{
// Clock has requested the current set-up data
device.send("clock.set.prefs", clockPrefs)
}
. . .
// Register device-sent request for settings
@electricimp
electricimp / factory.device.nut
Created January 23, 2015 10:50
Factory firmware stub
const FACTORY_IMP_MAC = "0c2a690130b5"
if (imp.getmacaddress() == FACTORY_IMP_MAC)
{
// Factory firmware running on factory imp,
// so run BlinkUp code
}
else
{
// Factory firmware runnong on target device.
@electricimp
electricimp / setcookie.agent.nut
Last active August 29, 2015 14:13
Sample code for httpresponse.headers() usage with 'set-cookie'
http.onrequest(function(request, response){
// This issues cookies with path=/, which means that the browser
// sends them to any page on agent.electricimp.com;
// this means that other agents can read it.
local url = http.agenturl()
// url is https://agent.electricimp.com/agentID, so use 'split' to
// generate an array of sub-strings separated by /
@electricimp
electricimp / button.agent.nut
Last active August 29, 2015 14:10
Gists for the Shelf Life blog post
// Screen Change Agent Code:
local day = true
function changeday(data){
if (day) day = false;
else day = true;
}
device.on("changeday, changeday)
@electricimp
electricimp / lightshow.agent.nut
Created November 27, 2014 14:19
Micro Disco Light Show Project
// AGENT CODE
function requestHandler(request, response)
{
try
{
if ("setcolor" in request.query)
{
device.send("setcolor", request.query.setcolor)
response.send(200, "Color Set")
@electricimp
electricimp / lcd.class.nut
Created November 11, 2014 17:00
Squirrel class to drive a character LCD driven Hitachi HD44780 controller via Adafruit I2C backpack
class LCD
{
// A Squirrel class to drive a 16 x 2 to 20 x 4 character LCD driven by a Hitachi HD44780 controller
// via an MCP23008 interface chip on an Adafruit I2C backpack [http://www.adafruit.com/product/292]
// Communicates with any imp I2C bus
// Written by Tony Smith (@smittytone) October 2014
// Version 1.0
@electricimp
electricimp / ssd1306a.class.nut
Last active December 22, 2017 06:17
Squirrel Class to control Adafruit SSD1306-based 0.96in 128 x 32/64 OLED
class SSD1306OLED
{
// Squirrel Class for Solomon SSD1306 OLED controller chip
// [http://www.adafruit.com/datasheets/SSD1306.pdf]
// As used on the Adafruit SSD1306 I2C breakout board
// [http://www.adafruit.com/products/931]
// Bus: I2C
// Code by Tony Smith (@smittytone) June 2014
// Version 1.0.2
@electricimp
electricimp / max7219.class.nut
Created November 11, 2014 15:45
Squirrel class for 8x8 LED matrix displays driven by the MAX7219 controller
class MAX7219
{
// Squirrel class for 8x8 LED matrix displays driven by the MAX7219 controller
// For example: https://www.sparkfun.com/products/11861
// Communicates with any imp GPIO (three pins)
// Written by Tony Smith (@smittytone) Jume 2014
// Version 1.0
MAX7219_REGISTER_BCD = 0x09
@electricimp
electricimp / ds3234rtc.class.nut
Last active August 29, 2015 14:09
Squirrel class for the Dallas/Maxim DS3234 real time clock as used on the SparkFun Dead On board
class DS3234RTC
{
// Squirrel class for the Dallas/Maxim DS3234 real time clock used on the
// SparkFun DeadOn breakout board https://www.sparkfun.com/products/10160
//
// Bus: SPI
//
// Written by Tony Smith, September 2014
// Version 1.0