Home IoT Kit Example
This makes your room lamp smarter.
It turn on automatically when it detect human. And It also record the time at a file on Dropbox.
First, Attach a servo motor to a switch on the wall.
The way you attach depends on your switch.
And connect a PIR sensor and a servo motor to an obniz. Then connect to a power.
Write and Run a program.
This example program has a manual method.
Turn a switch to the left, Your servo will move.
Tune the value on the example program to work correctly.
And Please specify dropbox API key. You can see how to on Lessons.
Let's test.
Your servo will move when you close to a sensor.
And you will see a file which contain the time on your dropbox.
This time I use HTML But nodejs will work more sufficiently. It works persistently.
Also you might want to turn off automatically or record more information to a file. It seems fun
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
<script src="https://unpkg.com/obniz@2.0.2/obniz.js"></script>
<script src="https://unpkg.com/dropbox@4.0.3/dist/Dropbox-sdk.min.js"></script>
</head>
<body>
<div id="obniz-debug"></div>
<script>
var dbx = new Dropbox.Dropbox({ accessToken: '<YOUR ACCESS TOKEN HERE>' });
var obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
var servo = obniz.wired("ServoMotor", {signal:2, vcc:1, gnd:0});
servo.angle(90.0);
var sensor = obniz.wired("HC-SR505", { vcc:11, signal:10, gnd:9});
sensor.onchange = function(detected){
if (detected) {
turnon();
}
}
obniz.switch.onchange = function(state) {
if (state === "left") {
turnon();
}
}
async function turnon() {
await obniz.wait(500);
servo.angle(40.0);
await obniz.wait(500);
servo.angle(90.0);
dbx.filesUpload({path: '/obniz.txt', contents: "[ON] " + new Date() + "\n", mode: 'overwrite' })
.catch(function(error) {
alert(error);
});
}
}
</script>
</body>
</html>