Created
December 8, 2013 17:56
-
-
Save evilmachina/7861148 to your computer and use it in GitHub Desktop.
Electric imp door lock.
Read more at http://slickstreamer.info
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 doorState = "open"; | |
local page = "<html>" + | |
"<head></head>" + | |
"<body>" + | |
"<form action='" + http.agenturl() +"' method='post'>" + | |
"<input type='password' autocomplete='on' name='pin' placeholder='pin'></input> <br>" + | |
"<button name='command' value='open' type='submit'>Open</button>" + | |
"<button name='command' value='lock' type='submit'>Lock</button>" + | |
"</form>" + | |
"</body>" + | |
"</html>"; | |
function getState(){ | |
if(device.isconnected()){ | |
local json = {"doorState":doorState}; | |
return http.jsonencode({"doorState":doorState}); | |
}else{ | |
local json = {"doorState":"notConnected"}; | |
return http.jsonencode(json); | |
} | |
} | |
function requestHandler(request, response) { | |
//server.log(request.method); | |
/* foreach (i,val in request) | |
{ | |
server.log(i); | |
}*/ | |
if("/state" == request.path){ | |
response.header("Content-Type", "application/json"); | |
response.send(200, getState()); | |
}else if("POST" == request.method){ | |
local data = http.urldecode(request.body); | |
if ("command" in data && data["pin"] == "1111") { //set code | |
server.log(data["command"]); | |
if(data["command"] == "open" ){ | |
device.send("command", 1); | |
}else if(data["command"] == "lock"){ | |
device.send("command", 0); | |
} | |
} | |
//response.send(200, page); | |
response.send(200, page); | |
}else{ | |
response.send(200, page); | |
} | |
}; | |
device.on("doorState", function(value){ | |
if(value != doorState){ | |
server.log(value); | |
} | |
doorState = value; | |
}); | |
// your agent code should only ever have ONE http.onrequest call. | |
http.onrequest(requestHandler); | |
device.on("ping", function(startTime) { | |
// immediatly send a reply | |
device.send("pong", startTime); | |
}); |
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 doorState = "open"; | |
local doorLockState = "unlock"; | |
function reset(){ | |
hardware.pin9.write(0.04 + (0.45 * 0.09)); | |
}; | |
function updateDoorState(){ | |
local state = hardware.pin7.read(); | |
if(state == 0){ | |
doorState = "closed"; | |
}else{ | |
doorState = "open"; | |
} | |
agent.send("doorState", doorState); | |
}; | |
agent.on("command", function (value) { | |
local v =0.04 + (value * 0.09); | |
server.log(format("servo position set to %.2f", v)); | |
try { | |
hardware.pin9.write(v); | |
imp.sleep(1); | |
reset(); | |
}catch (err) { | |
server.error("Invalid Frame Input"); | |
} | |
}); | |
hardware.pin7.configure(DIGITAL_IN_PULLUP, updateDoorState); | |
hardware.pin8.configure(ANALOG_IN); | |
hardware.pin9.configure(PWM_OUT, 0.020, 0.045); | |
server.log("Hardware Configured"); | |
reset(); | |
updateDoorState(); | |
// when we get a "pong" message from the agent | |
agent.on("pong", function(startMillis) { | |
local endMillis = hardware.millis(); | |
local diff = endMillis - startMillis; | |
//server.log("Round trip took: " + diff + "ms"); | |
}); | |
// pings the server | |
function doreState() { | |
local state = hardware.pin8.read(); | |
//server.log(state); | |
if(state > 40000){ | |
doorLockState = "locked"; | |
agent.send("doorState", doorLockState); | |
}else{ | |
doorLockState = "unlock"; | |
agent.send("doorState", doorState); | |
} | |
agent.send("ping", hardware.millis()); | |
imp.wakeup(2.0, doreState); | |
} | |
doreState(); | |
imp.configure("door lock", [], []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment