3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Home / Slack を使って RM mini3 で家電制御

Last updated at Posted at 2019-02-09

システム構成

"Firebaseを使って RM mini3 で家電制御"と同様に、Google Homeに「部屋の明かりをつけて」と話しかけると赤外線リモコン対応シーリングライトを点灯させるシステムを実装してみます。
slack-home.png

必要なもの

  1. Google Home - IFTTTのGoogle Assistant - Slack連携をトリガーします。
  2. Raspberry Pi - Slackを監視して赤外線リモコンを制御するのに使います。
  3. RM mini3とかeRemote mini - 赤外線リモコンの制御可能なら他も可。

Google Assistant - Slack 連携

Google Home に話しかけるとSlackのチャンネルにリモコン制御コマンドを送るまで。

Slackの設定

ユーザ名 rm-mini3 で Bots を登録します。リモコン制御コマンドはIFTTTからrm-mini3 botへのdirect messageとして送ります。
slackbot.jpg

IFTTTの設定

Google Assistantに「部屋の明かりをつけて」と話しかけると、Slack botへDirect messageで制御コマンドを送るAppletを作成します。

slackリモコン制御コマンド

Slackに送るリモコン制御コマンドを以下のように定義します。

Command Device IR command Description
send light on on/off toggle
bright brighter
dark darker

DeviceとIR commandの間は「:」で区切り、複数のコマンドを「,」で連結可能とします。例えば、電気をつけて明るくする制御コマンドはこんな感じ。

send light:on, light:bright

テスト

Google Homeに「部屋の明かりをつけて」と話しかけるとIFTTTを経由してslackにdirect messageが送られます。
ifttt-slack.jpg

Slackの監視とIRコマンドの送信

Slack監視

Slack bot への direct message を監視するのにslackbotを使うことにします。

$ pip3 install slackbot

IFTTTのdirect messageを監視するためにはdispatcher.pyの修正・差し替えが必要です。IFTTTから送られてくるメッセージは 'text' property ではなく'attachements' の 'pretext' に入っているので、これを取り出すよう修正します。

IFTTTから送られてくるmessage
{
  'username': 'IFTTT',
  'attachments': [{
    'pretext': 'send light: on',
    'fallback': 'send light: on',
    'mrkdwn_in': ['text', 'pretext']}],
  ...
}

direct message の中から'send'で始まる message を取り出して、その中からIR commandを取り出します。

from slackbot.bot import respond_to
from libs import ir_code as ir
import re

@respond_to('send (.*)')
def send(message, command):
    #message.reply('accept command:' + command)
    commands = parse(command)
    for c in commands:
        ir.send_ir_command(c)

def parse(cmd_string):
    return [ x.strip() for x in cmd_string.split(',') ]

IRコマンド送信

IR commandの送信には python-bloadlinkを使います。direct messageから取り出したコマンド (light: on) をIR codeに変換してRM mini3に送ります。

$ pip3 install bloadlink
import broadlink

# IR codes
ceiling_light = {
    'on' : '2600580000012...',
    'bright' : '26005800000129...',
    'dark' : '26005000000...'
}
codes = {
    'light' : ceiling_light
}

def send_ir_command(cmd_string):
    if (discover()):
        op, val = [ x.strip() for x in cmd_string.split(':') ]
        code = codes[op][val]
        send(code)
    else:
        print('no devices')

def discover():
    global devices
    if (devices is None):
        devices = broadlink.discover(timeout=5)
        devices[0].auth()
    return (devices is not None)

def send(code):
    data = bytearray.fromhex(code)
    devices[0].send_data(data)

Integrate

これで、Google Homeに「部屋の明かりをつけて」と話しかけるとIFTTTを経由してslackにdirect messageが送られ、direct messageに指定されたIR制御コマンドをIRコードに変換してRM mini3に送る系が完成します。
あとは、IFTTT applet、制御コマンド、IRコードを拡張していけばOK.
例えば、TVを制御するのに以下のようなコマンドを定義して実装していけばよいでしょう。

Command Device IR command Description
send tv on on/off toggle
1-12 Channel
t Telestrial
bs BS
cs CS

サンプルコードは https://github.com/fnishio/rm-slack にas isで

References

3
5
0

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
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?