This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Device Code | |
led <- hardware.pin1; | |
led.configure(DIGITAL_OUT); | |
state <- 0; | |
function loop(){ | |
server.log("Blink: "+state); | |
led.write(state); | |
state = 1 - state; | |
imp.wakeup(1, loop); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ); |
OlderNewer