11
14

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.

macOS + google-home-notifier 超入門

11
Last updated at Posted at 2018-01-12

環境

  • OS: macOS High Sierra 10.13.1
  • anyenv
  • pyenv(on anyenv):2.7
  • ndenv(on anyenv)

anyenvの環境構築は別記事のanyenv + macOS環境構築を参考にしてみてください。

google-home-notifierのインストール

Pythonのバージョンでインストールエラーになるのでanyenvとpyenvなどのパッケージ管理ツールを使用してpythonのバージョンを切り替えれるように環境を構築しておくことをお勧めします。

以下anyenvとpyenv環境を前提で構築

作業ディレクトリを作成

$ mkdir myProject
$ cd myProject

npmのアップデート

npmを最新のものにアップデート

$ npm update -g npm

npmの初期化

npmのモジュールをインストールするためにまずはpackage.jsonを生成するために以下のコマンドを実行。対話ベースで諸々入力しますがあまり気にせず適宜入力しておけば良いと思います。

$ npm init

pyenvで作業ディレクトリをpython2.7.9に設定

google-home-notifiernpmでインストールする際にnode-gyp rebuildが実行されるのですがこれがPython2.7.9以前でなければエラーが出るので、もしPython3系などを使っている場合はpyenvなどのパッケージ管理ツールなどを使用してPython2.7.9で動作する環境を事前に整えておきます。

pyenvでインストールしているバージョンを確認

$ pyenv versions

ここに2.7.9がなければpyenv2.7.9をインストール

$ pyenv install 2.7.9

pyenvで作業ディレクトリを2.7.9に設定

$ pyenv local 2.7.9

pythonのバージョンがきちんと設定されているか確認

$ python -V
Python 2.7.9

OK

npmでgoogle-home-notifierをインストール

$ npm install --save google-home-notifier

pythonのバージョンを2.7.9に設定しているので無事node-gyp rebuildが実行される。
※Pythonのバージョンが3系だとここでエラーが出てしまう。

Google Homeで発話させIPアドレスを表示させる

index.jsの作成

index.js
const googlehome = require('google-home-notifier');
const lang = 'ja';

googlehome.device('Google Home', lang);
googlehome.notify('こんにちは、グーグルホームです。', function(res){
	console.log(res);
});

コマンドを実行してGoogle HomeのIPを表示させる(発話も同時)

$ node index.js
Device "Google-Home-xxxxxxxxxxxxxxxxxxx" at 192.168.31.86:8009
Device notified

Google-Home-xxxxxxxxxxxxxxxxxxxの部分がデバイスのID
192.168.31.31の部分がGoogle HomeのIPアドレス
8009の部分がGoogle Homeのポート番号

デバイスが複数ある場合はここに複数のDevice "Google-Home-xxxx" at 192.168.xx.xx:xxxxが表示される

上記でIPアドレスがわかれば恣意的にIPを設定することも可能で、複数デバイスがある場合にはIPを指定してあげると良い。

※この方法でなくともスマホのGoogle HomeアプリでデバイスのIPを確認することも可能

IPアドレスを指定するコード

index.js
const googlehome = require('google-home-notifier');
const lang = 'ja';

googlehome.ip('192.168.31.86'); //ここで指定
googlehome.device('Google-Home', lang);
googlehome.notify('こんにちは、グーグルホームです。', function(res){
	console.log(res);
});

まとめ

スマートホームハックの手始めとしてgoogle-home-notifierの入門でした。
Respberry Pi 3を使用して構築することもできるので、今後色々といじっていきたいと思います。
Firebase realtime databaseを利用した記事もアップしていきたい。

11
14
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
11
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?