Skip to content

Instantly share code, notes, and snippets.

@ersatzavian
Created July 7, 2014 18:48
Show Gist options
  • Select an option

  • Save ersatzavian/1c84c5f221fe9a2a91c7 to your computer and use it in GitHub Desktop.

Select an option

Save ersatzavian/1c84c5f221fe9a2a91c7 to your computer and use it in GitHub Desktop.
Basic "Thermostat" Example with imp001 with thermistor and LED
// agent code:
target <- 30.0;
current <- "Unkown";
device.send("target", target);
device.on("current", function(t){ current <- format("%0.1f C",t)});
http.onrequest(function(request,res){
if(request.method == "POST"){
local post = http.urldecode(request.body);
if("target" in post){
target = post.target.tofloat();
device.send("target", target);
}
}
local html = "<html><body><form method='post'>Current Temperature: "+current+"<input type='submit' value='Refresh'><br><form method='post'>Set Temperature: <input name='target' type='number' min='20' max='40' step='0.5' value='"+target+"'><input type='submit'></form></body></html>";
res.send(200,html);
});
temp_enable <- hardware.pin2;
temp_enable.configure(DIGITAL_OUT);
temp_enable.write(1);
temp_pin <- hardware.pin5;
temp_pin.configure(ANALOG_IN);
led <- hardware.pin1;
led.configure(DIGITAL_OUT);
target <- 30;
agent.on("target", function(t){target = t});
function read_temperature() {
local vdda = hardware.voltage();
local v_therm = temp_pin.read() * (vdda / 65535.0);
return (298.15 * 3380.0) /
(3380.0 - 298.15 * math.log(10000.0 / ((vdda - v_therm) *
(10000.0 / v_therm)))) - 273.15;
}
function loop(){
local t = read_temperature();
agent.send("current",t);
if(t < target){
led.write(1);
}else{
led.write(0);
}
imp.wakeup(1, loop);
}
loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment