Hi - i’m utilizing a reed switch to sense a door status on pin9 and would like to display agent side. A simple open or closed status would be perfect. What is the simplest way to do this?
Agent Code:
const html = @"
<link rel='stylesheet' href='https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css'>
Garage Door
</div>
<input type='password' class='form-control' style='margin-top:15px; text-align:center;' id='pw' placeholder='Password'>
</div>
<div class='text-left'>
<h2>Log:</h2>
<div id='logs'></div>
</div>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js'></script>
<script>
function setState(s) {
var pw = $('#pw').val();
var url = document.URL + '?pw=' + pw + '&power=' + s;
if (pw) {
$.get(url)
.done(function(data) {
$('#logs').prepend('<span style=\\'color: green;\\'>' + new Date().toLocaleString() + '</span><span> - Turned power ' + data + '</span><br />');
})
.fail(function(data) {
alert('ERROR: ' + data.responseText);
});
} else {
alert('Please enter a password and try again');
}
}
</script>
"
const PASSWORD = “0000”
// agent code:
function httpHandler(request, response) {
try {
// if they passed a power parameter
if (“power” in request.query) {
// add the ajax header
response.header(“Access-Control-Allow-Origin”, “*”);
// password variable
local pw = null;
// if they passed a password
if ("pw" in request.query) {
// grab the pw parameter
pw = request.query["pw"];
}
// if the password was wrong
if (pw != PASSWORD) {
// send back an angry message
response.send(401, "UNAUTHORIZED");
return;
}
// grab the power parameter
local powerState = request.query["power"].tointeger();
if (powerState == 0 || powerState == 1) {
// send it to the device
device.send("power", powerState);
// finally, send a response back to whoever made the request
response.send(200, powerState == 0 ? "Off" : "On");
return;
} else {
// if powerState isn't valid, send back an error message
response.send(500, "Invalid power parameter. Please pass 1 or 0 and try again.");
return;
}
} else {
// if power wasn't specified, send back the HTML page
response.send(200, html);
return;
}
}
catch (ex) {
// if there was an error, send back a response with the error
response.send(500, ex);
return;
}
}
// run httpHandler whenever a request comes into the Agent URL
http.onrequest(httpHandler);
Device Code
//device code
hardware.pin9.configure(DIGITAL_OUT); // configure pin as digital output
function powerHandler(state) {
server.log(“got a power message from the agent”); // log something
hardware.pin9.write(state); // set state of pin
//kris code
// Wait 50 ms
imp.sleep(1.00);
// Set pin low again
hardware.pin9.write(0);
}
// whenever we get a “power” message, run the powerHandler function
agent.on(“power”, powerHandler);
Device Code
//device code
hardware.pin9.configure(DIGITAL_OUT); // configure pin as digital output
function powerHandler(state) {
server.log(“got a power message from the agent”); // log something
hardware.pin9.write(state); // set state of pin
//kris code
// Wait 50 ms
imp.sleep(1.00);
// Set pin low again
hardware.pin9.write(0);
}
// whenever we get a “power” message, run the powerHandler function
agent.on(“power”, powerHandler);
You may get chattering or bouncing of the switch, which will toggle it many times during the “make” or “break” of the contact. If you do, you’ll have to “debounce” it. There are existing imp discussions about that, and code to try. Because the contacts are probably sealed nicely, they may be ‘clean’ enough so you don’t see bouncing.
The best way to deal with bouncing is to use the Button library.