LoginSignup
20
3

More than 5 years have passed since last update.

IoTカノジョ 2.0

Posted at

この記事は、カノジョできないエンジニア Advent Calendar 2016 23日目の記事です。

1年前にこんな記事を書きました。

1周年なので、バージョンアップしてみたいと思います。

用意するもの

Arduino UNO

どこのご家庭にもある一般的なArduinoです。

illu-arduino-UNO.png

PIRセンサー

集電型赤外線センサー、いわゆる人感センサーです。
pir.jpg

LED

光ります。

thumbnail_led.jpg

Johnny-Five

友人です。
johnny-five-fb.png

その他

ジャンパー線、ブレッドボード、抵抗などはお好みのものをご用意ください。

IoTカノジョ 2.0

IMG_2785.JPG

kanojyo.js
var five = require('johnny-five');
var SlackNode = require('slack-node');

var board = new five.Board();
var slackToken = "<your-slack-token>";
var slackNode = new SlackNode(slackToken);
var freq = 1000;
var waitTime = 0;
var hasPostedWelcomeMessage;
var hasPostedAngryMessage;

board.on('ready', function() {
    var led = new five.Led(8);
    var angryLed = new five.Led(7);
    var sensor = new five.Sensor({
        pin: 'A0',
        freq: freq
    });
    sensor.on('data', function(){
        if (waitTime > 5000) {
            angryLed.on();
            if (!hasPostedAngryMessage) {
                postMessage("ちょっと遠くないかにゃ。");
                hasPostedAngryMessage = true;
            }
        } else {
            hasPostedAngryMessage = false;
        }

        if (this.value != 0) {
            led.on();
            if (!hasPostedMessage) {
                postMessage("おかえりにゃ!")
                hasPostedMessage = true;
                waitTime = 0;
                angryLed.off();
            }
        } else {
            led.off();
            hasPostedMessage = false;
            waitTime += freq;
            console.log("waitTime: " + waitTime);
        }
    });
});

function postMessage(message) {
    slackNode.api('chat.postMessage', {
        text: message,
        channel: '#iot-kanojyo',
        as_user: true
    }, function(err, response) {
        console.log(response);
    });
}

近づくと話しかけてきます。近づかないと話しかけてくれません。
スクリーンショット 2016-12-23 14.54.01.png

しばらく近づかないと、怒ります。
スクリーンショット 2016-12-23 15.04.50.png

怒っているので、赤く光ります。こわいですね。
IMG_2786.JPG

まとめ

良いお年を。

20
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
3