Skip to content

Instantly share code, notes, and snippets.

View ElectricImpSampleCode's full-sized avatar

Electric Imp, Inc. ElectricImpSampleCode

View GitHub Profile
@ElectricImpSampleCode
ElectricImpSampleCode / analogTherm.device.nut
Last active August 29, 2015 14:15
Example code to sample a thermistor connected to an imp. It makes use of the imp’s ANALOG_IN GPIO pin setting.
// Assign hardware.pin5 to a global variable, therm
therm <- hardware.pin5;
// Configure pin5/therm for analog input
therm.configure(ANALOG_IN);
// These constants are particular to the thermistor we're using.
// Check your datasheet for what values you should be using
const B_THERM = 3977.0;
const T0_THERM = 298.15;
@ElectricImpSampleCode
ElectricImpSampleCode / pwmLed.device.nut
Last active May 12, 2017 09:36
Example code to control an LED connected to an imp. It makes use of the imp’s PWM_OUT GPIO pin setting.
// Assign a global variable, led, to the imp001 pin to which the LED is connected
led <- hardware.pin9;
// Configure it for PWM output
led.configure(PWM_OUT, 1.0 / 400.0, 0.0);
// Record the state and delta of the LED's cycle
ledState <- 0.0;
ledChange <- 0.05;
@ElectricImpSampleCode
ElectricImpSampleCode / pwmServo.device.nut
Last active August 29, 2015 14:15
Example code to control a servo motor connected to an imp. It makes use of the imp’s PWM_OUT GPIO pin setting.
// These constants may be different for your servo
const SERVO_MIN = 0.03;
const SERVO_MAX = 0.1;
// Create global variable for the pin to which the servo is connected
// then configure the pin for PWM
servo <- hardware.pin7;
servo.configure(PWM_OUT, 0.02, SERVO_MIN);
// Define a function to control the servo.
@ElectricImpSampleCode
ElectricImpSampleCode / weather.agent.nut
Last active April 8, 2016 13:37
Code for the Wireless Weather Station project
// WEATHER UPDATE CLASS
class WeatherUpdate {
// Simple Squirrel class for interaction with Forecast.io
url = "https://api.forecast.io/forecast/";
apikey = "YOUR API KEY HERE";
function forecastRequest(longitude = 52.205082, latitude = 0.130209) {
// Parameters: longitude and latitude of location for which a forecast is required
// Return: configured HTTP request
@ElectricImpSampleCode
ElectricImpSampleCode / twitter.agent.nut
Last active April 8, 2016 14:12
Code for the Scrolling Twitter Display project
#require "Twitter.class.nut:1.2.1"
// CONSTANTS
// Twitter keys
const API_KEY = "YOUR API KEY";
const API_SECRET = "YOUR API SECRET";
const AUTH_TOKEN = "YOUR AUTH TOKEN";
const TOKEN_SECRET = "YOUR TOKEN SECRET";
// GLOBAL VARIABLES
@ElectricImpSampleCode
ElectricImpSampleCode / lightshow.agent.nut
Last active April 8, 2016 13:51
Code for the 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");
return;
}
@ElectricImpSampleCode
ElectricImpSampleCode / disconnect.device.nut
Last active September 10, 2018 10:30
This example shows a typical imp disconnection handler flow.
// Set the disconnection policy
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 10);
// Constants
const RECONNECT_TIME = 900;
const RECONNECT_TIMEOUT = 60;
// Globals
disData <- "";
disFlag <- false;
@ElectricImpSampleCode
ElectricImpSampleCode / datapersist.device.nut
Last active September 10, 2018 10:26
This example shows how the imp API's nv table is used to preserve a single value across periodic deep sleeps.
// On wake from deep sleep or cold boot, Squirrel starts here
// Check if nv table doesn't exist or, if it does, whether it lacks the key 'count'
if (!("nv" in getroottable()) || !("count" in nv)) {
// Either the nv table hasn't been instantiated or it has but lacks a 'count' slot,
// so create them. If nv table exists, this code just adds 'count' to it. If nv
// doesn't exist yet, impOS will automatically create it when we add 'count'
nv <- { "count": 0 };
}
@ElectricImpSampleCode
ElectricImpSampleCode / latency.agent.nut
Last active September 10, 2018 10:31
This example shows device-agent and agent-device messaging in action.
function startTime(time) {
// Send the device a 'pong' message immediately
device.send("pong", time);
}
// When we get a 'ping' message from the device, call startTime()
device.on("ping", startTime);
@ElectricImpSampleCode
ElectricImpSampleCode / httpgetasync.agent.nut
Last active September 10, 2018 10:33
This example shows the creation and transmission of a simple HTTP GET request.
// Set up outgoing request object
local request = http.get(webServiceURL, webServiceHeaders)
// Define the response handler
function handleResponse(responseTable) {
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200) {
// Remote service has responded with 'OK' so decode
// the response's body 'responseTable.body' and headers 'responseTable.headers'
// Code omitted for clarity...