Skip to content

Instantly share code, notes, and snippets.

View ersatzavian's full-sized avatar

Tom Buttner ersatzavian

View GitHub Profile
@ersatzavian
ersatzavian / gist:6708177
Created September 26, 2013 00:25
electric imp: Drive a hardware event with an HTTP request
/* AGENT CODE ---------------------------------------------------------------*/
/* Define a handler for incoming HTTP requests */
http.onrequest(function(request, res) {
server.log("Got new HTTP request");
// read the body of the HTTP request into a local variable
local data = request.body;
@ersatzavian
ersatzavian / handle_disconnect.nut
Created December 21, 2013 00:04
Setting up reconnect attempts in an electric imp onunexpecteddisconnect handler
server.onunexpecteddisconnect(function(err) {
setStatusLight(STATUS_DISCONNECTED);
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, WIFI_TIMEOUT);
// this is also a good time to set up some reconnection attempts
imp.wakeup((60*RECONNECT_PERIOD), function() {
imp.wakeup((60*RECONNECT_PERIOD), this);
server.connect(function() {
setStatusLed(STATUS_CONNECTED);
rtc.sync();
fetchSchedule();
@ersatzavian
ersatzavian / seconds_til.nut
Created December 21, 2013 00:05
Calculate seconds til the next time the clock strikes n
function secondsTil(now,targetTime) {
local data = split(targetTime,":");
local target = { hour = data[0].tointeger(), min = data[1].tointeger() };
if ((target.hour < now.hour) || (target.hour == now.hour && target.min < now.min)) {
target.hour += 24;
}
local secondsTill = 0;
secondsTill += (target.hour - now.hour) * 3600;
function uartRead() {
local readValue = "";
// pause to let partner device finish writing command
// at 9600 baud, it takes about 1.04ms to write a byte
// (8 bits plus start bit and stop bit = 10 bits)
// so, it'll take about 4.16 ms to write a 4-byte word
// we'll wait even longer for demonstration purposes here
imp.sleep(0.005);
@ersatzavian
ersatzavian / gist:9552161
Created March 14, 2014 17:09
LED Breather
// Pulse the LED
// Adapted from http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ by Sean Voisen
function pulse_led(led) {
imp.wakeup(0.05, function() {
pulse_led(led);
});
// Work out the next PWM level
local led_level = (math.exp(math.sin(hardware.millis()/2000.0*PI)) - 0.36787944) * 0.423529412;
led.write(led_level);
@ersatzavian
ersatzavian / imp003-evb-readtemp.agent.nut
Created July 7, 2014 16:57
Read imp003 EVB temp sensor and show value on web page
// Copyright (c) 2014 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
body <- "No value yet";
http.onrequest(function(request,res){
local html = @"<html><meta http-equiv=""refresh"" content=""5"">
<body>" + body + "</body></html>";
res.send(200,html);
});
@ersatzavian
ersatzavian / imp003-evb-thermostat.agent.nut
Created July 7, 2014 18:09
Build a basic "thermostat" with the imp003 EVB
// Copyright (c) 2014 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
// Agent Code
target <- 30.0;
current <- "Unkown";
device.send("target", target);
@ersatzavian
ersatzavian / imp003-evb-blinkblue.device.nut
Created July 7, 2014 18:13
Blink Blue LED on imp003 EVB
// Device Code
led <- hardware.pinK;
led.configure(DIGITAL_OUT);
state <- 0;
function loop(){
server.log("Blink: "+state);
led.write(state);
state = 1 - state;
imp.wakeup(1, loop);
@ersatzavian
ersatzavian / imp001-blink.device.nut
Created July 7, 2014 18:40
Blink LED on imp001 pin 1
@ersatzavian
ersatzavian / imp001-readtemp.agent.nut
Last active August 29, 2015 14:03
Read temperature with Thermistor on imp001, show on small agent webpage
// agent code:
body <- "No value yet";
http.onrequest(function(request,res){
local html = @"<html><meta http-equiv=""refresh"" content=""5"">
<body>" + body + "</body></html>";
res.send(200,html);
});
device.on("temp", function(t){
body = format("Temperature: %0.1f C", t );