Skip to content

Instantly share code, notes, and snippets.


Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
ElectricImpSampleCode / serverconnect.example.nut
Last active Jan 29, 2019
An example of how to deal with multiple proximate calls to server.connect(), which maintains only a single callback reference.
View serverconnect.example.nut
// Establish threshold trigger flags for all your sensors
local sensorOneTriggerFlag = false;
local 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) {
if (reason == SERVER_CONNECTED) {
// Server is online. Because we can't say which sensor
// triggered the server.connect() we check both trigger
ElectricImpSampleCode / wakereason.example.nut
Last active Mar 18, 2021
A simple function which uses hardware.wakereason() to reveal why an imp restarted.
View wakereason.example.nut
function logWokenReason() {
local s = "Device restarted. Reason: ";
local c = hardware.wakereason();
switch (c) {
s += "Cold boot";
s += "Timer woke the imp";
ElectricImpSampleCode / logger.class.nut
Last active Nov 25, 2019
A sample class available for logging via UART while an imp is disconnected from WiFi.
View logger.class.nut
// UART Logger
// Copyright (c) 2014-19, Electric Imp, Inc.
// Licence: MIT
class Logger {
// Code version
static VERSION = "1.2.0";
// Instance properties
ElectricImpSampleCode / WaitForAck.example.nut
Last active Sep 10, 2018
A demonstration of server.setsendtimeoutpolicy()’s WAIT_FOR_ACK and WAIT_TIL_SENT options.
View WaitForAck.example.nut
// Configure imp001 pin 1
hardware.pin1.configure(DIGITAL_OUT, 0);
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_FOR_ACK, 10);
data <- blob(4096);
state <- 0;
function blink() {
// Turn LED on or off
ElectricImpSampleCode / cycleConnections.example.nut
Last active Sep 10, 2018
Demonstrates how you can use server.setsendtimeoutpolicy()’s RETURN_ON_ERROR option by using a server.onexpecteddisconnect() callback.
View cycleConnections.example.nut
// Set the timeout policy to RETURN_ON_ERROR, ie. to continue running at disconnect
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 10);
// Configure Pin 1 to drive the connection status LED
// Create an array of WiFi networks, each defined by a table
connections <- [
{ ssid = "SSID1", pw = "SSID1PW" },
ElectricImpSampleCode /
Last active Aug 29, 2015
Raspberry Pi Python code for unattended logging
#!/usr/bin/env python
import serial
s = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
message = ""
while True:
logChar =
if logChar:
ElectricImpSampleCode / Logger.class.nut
Last active Aug 29, 2015
Squirrel class for logging via UART
View Logger.class.nut
class Logger {
_serial = null;
constructor (impUART) {
if (impUART != null) {
_serial = impUART;
_serial.configure(115200, 8, PARITY_NONE, 1, NO_RX);
ElectricImpSampleCode / digitalout-led.device.nut
Last active Aug 29, 2015
Example code to blink an LED connected to an imp. It makes use of the imp’s DIGITAL_OUT GPIO pin setting.
View digitalout-led.device.nut
// Assign pin9 to a global variable
led <- hardware.pin9;
// Configure LED pin for DIGITAL_OUTPUT, and set initial state to off
led.configure(DIGITAL_OUT, 0);
// Assign a global variable to track current state of LED pin
state <- 0;
// Function to blink LED
ElectricImpSampleCode / digitalin-led.device.nut
Last active Aug 29, 2015
Example code to sample a button connected to an imp. It makes use of the imp’s DIGITAL_IN GPIO pin setting.
View digitalin-led.device.nut
// Alias the GPIO pin as 'button'
button <- hardware.pin1;
function buttonPress() {
local state =;
if (state == 1) {
// The button is released
} else {
// The button is pressed
ElectricImpSampleCode / analoginPotentio.device.nut
Last active Aug 29, 2015
Example code to sample a potentiometer connected to an imp. It makes use of the imp’s ANALOG_IN GPIO pin setting.
View analoginPotentio.device.nut
// Alias pin 2 as pot
pot <- hardware.pin2;
// Configure the pin for analog input
// Define a function to poll the pin every 0.1 seconds
function poll() {
// Read the pin and log its value