Hi gents,
My wife asked me if i can make automated blinds in our living room and kitchen. I finished the project in the living room and kitchen is next. I scrambled a code for device and the agent with a little security. If you guys can take a look and improve it or help with my next version of it that would be awesome! here is my device code:
const SERVO_MIN = 0.06; const SERVO_MAX = 0.11;hardware.pin9.configure(PWM_OUT, 0.02, SERVO_MIN);
hardware.pin8.configure(PWM_OUT, 0.02, SERVO_MIN);
hardware.pin7.configure(PWM_OUT, 0.02, SERVO_MIN);agent.on(“value”, function (value) {
local value = value * (SERVO_MAX-SERVO_MIN) + SERVO_MIN; //0.01 + (value * 0.06); try { hardware.pin9.write(value); hardware.pin8.write(value); hardware.pin7.write(value); imp.sleep(1); }catch (err) { server.error("Error!"); }
});
And the Agent Code:
local blindsState = "open"; local page = "" + "" + "" + "" + "
" + "Open" + "Close" + "" + "" + "";function requestHandler(request, response) {
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 ("value" in data && data["pin"] == "") { //set code server.log(data["value"]); if(data["value"] == "open"){ device.send("value", 1); }else if(data["value"] == "close"){ device.send("value", 0); } } //response.send(200, page); response.send(200, page);
}else{
response.send(200, page);
}
};http.onrequest(requestHandler);
My next goal is to add:
- Battery percentage if possible at all
- And a “Check Status” to check if blinds are Opened/Closed
Thanks for your time and help