Created
February 4, 2015 00:46
-
-
Save cat-haines/3b7b24aca3f5863a6a68 to your computer and use it in GitHub Desktop.
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
local html = @"<!doctype html> | |
<html lang=""en""> | |
<head> | |
<title>Humidity Control</title> | |
<link rel=""stylesheet"" href=""https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"" /> | |
<link rel=""stylesheet"" href=""//d2c5utp5fpfikz.cloudfront.net/2_3_1/css/bootstrap.min.css"" > | |
<link rel=""stylesheet"" href=""//d2c5utp5fpfikz.cloudfront.net/2_3_1/css/bootstrap-responsive.min.css"" > | |
</head> | |
<body> | |
<div class='well' style='max-width: 320px; margin: 0 auto 10px; height:480px; font-size:22px;'> | |
<div class='row' style='margin-left:40px;'> | |
<div class='col-sm-4'> | |
<div class='panel panel-default'> | |
<div class='panel-heading'> | |
<h3 class='panel-title'>Humidity Control</h3> | |
</div> | |
<div class='panel-body'> | |
<label for='pan'>Pan:</label> | |
<input type='text' id='pan' style='width:30px; border: 0; color: #f6931f; font-weight: bold;' /> | |
<div id='slider-horizontal' style='width: 200px;'></div> | |
</div> | |
</div> | |
</div> | |
</div> | |
<script src=""https://code.jquery.com/jquery-1.9.1.js""></script> | |
<script src=""https://code.jquery.com/ui/1.10.3/jquery-ui.js""></script> | |
<script type=""text/javascript""> | |
function sendToImp(value) { | |
if (window.XMLHttpRequest) { | |
devInfoReq=new XMLHttpRequest(); | |
} | |
else { | |
devInfoReq=new ActiveXObject(""Microsoft.XMLHTTP""); | |
} | |
try { | |
devInfoReq.open('POST', document.URL, false); | |
devInfoReq.send(value); | |
} catch (err) { | |
console.log('Error parsing device info from imp'); | |
} | |
} | |
function pan(value) { | |
sendToImp('{""pan"":""' + value +'""}'); | |
} | |
$(function() { | |
$( '#slider-horizontal' ).slider({ | |
orientation: 'horizontal', | |
range: 'min', | |
min: 0, | |
max: 100, | |
value: 50, | |
step: 5, | |
slide: function( event, ui ) { | |
$( '#pan' ).val( ui.value ); | |
pan(ui.value); | |
} | |
}); | |
$( '#pan' ).val( $( '#slider-horizontal' ).slider( 'value' ) ); | |
}); | |
</script> | |
</body> | |
</html>"; | |
http.onrequest(function(request,res){ | |
if (request.body == "") { | |
res.send(200, html); | |
}else { | |
local json = http.jsondecode(request.body); | |
if("pan" in json){ | |
server.log("Setting Humidity to: " + json.pan); | |
device.send("humid", json.pan); | |
} | |
res.send(200, ""); | |
} | |
}); |
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
class Servo { | |
_pin = null; | |
_min = 0.0; | |
_max = 1.0; | |
constructor(pin, min = 0.0, max = 1.0, period=0.02, dutycycle=0.5) { | |
_min = min; | |
_max = max; | |
_pin = pin; | |
_pin.configure(PWM_OUT, period, dutycycle); | |
} | |
// Sets the minimum and maximum of the output scale. Both should be between 0.0 and 1.0. | |
function scale(min, max) { | |
_min = min; | |
_max = max; | |
} | |
// val: 0.0 <= val <= 1.0 | |
function write(val) { | |
if (val <= 0.0) val = 0.0; | |
else if (val >= 1.0) val = 1.0; | |
local last_write = val.tofloat(); | |
local f = 0.0 + _min + ( last_write.tofloat() * (_max - _min)); | |
return _pin.write(f); | |
} | |
} | |
// chagne to whatever pin you're using | |
servo <- Servo(hardware.pin9); | |
// may need to tweak these values, should be between 0.0 and 1.0 | |
servo.scale(0.0, 1.0); | |
agent.on("humid", function(humidity) { | |
local val = humidity.tofloat() / 100.0; | |
servo.write(val); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment