Skip to content

Instantly share code, notes, and snippets.

Avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
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
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) {
case WAKEREASON_POWER_ON: // 0
s += "Cold boot";
break;
case WAKEREASON_TIMER: // 1
s += "Timer woke the imp";
break;
@ElectricImpSampleCode
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
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
hardware.pin1.write(state);
@ElectricImpSampleCode
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
hardware.pin1.configure(DIGITAL_OUT);
hardware.pin1.write(0);
// Create an array of WiFi networks, each defined by a table
connections <- [
{ ssid = "SSID1", pw = "SSID1PW" },
@ElectricImpSampleCode
ElectricImpSampleCode / pi_logger.py
Last active Aug 29, 2015
Raspberry Pi Python code for unattended logging
View pi_logger.py
#!/usr/bin/env python
import serial
s = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
message = ""
while True:
logChar = s.read()
if logChar:
@ElectricImpSampleCode
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
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
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 = button.read();
if (state == 1) {
// The button is released
server.log("Release");
} else {
// The button is pressed
@ElectricImpSampleCode
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
pot.configure(ANALOG_IN);
// Define a function to poll the pin every 0.1 seconds
function poll() {
// Read the pin and log its value
server.log(pot.read());