前回の続き。
Raspberry Piにもどって、FirebaseとSuperAgentのNode.jsモジュールをインストールする。
メールでやったのを、Raspberry Piから、firebase経由で動かしてみる。
■firebaseのインストール
以下の要領で、firebaseのフォルダを作り、インストールする。
$ mkdir firebase
$ cd firebase
$ npm install firebase
■firebaseを操作するjsを作成
前回の操作で取得した、firebaseで取得したconfig以降の情報を貼り付けて、以下のindex.jsを作成する。
var firebase = require("firebase");
//firebase config
var config = {
apiKey: "ZZZZZZZZZZZZZZZZZZZZZZZ",
authDomain: "xxxxx-yyyyy.firebaseapp.com",
databaseURL: "https://xxxxx-yyyyy.firebaseio.com",
projectId: "xxxxx-yyyyy",
storageBucket: "xxxxx-yyyyy.appspot.com",
messagingSenderId: "wwwwwwwwwwww"
};
firebase.initializeApp(config);
//RM-mini3
const broadlink = require("./getDevice")
const rmlist = require("./rmlist")
//RM mini3 Device Set
const rmMac = "xx:xx:xx:xx:xx:xx"
let rm = {}
const timer = setInterval(function() {
rm = broadlink({host: rmMac})
if (rm) {clearInterval(timer)}
}, 100)
//RM mini3 ir send
const rmSend = (command) => {
const hexDataBuffer = new Buffer(rmlist[command], "hex")
console.log(command);
rm.sendData(hexDataBuffer)
}
//database更新時
const path = "/googlehome";
const key = "word";
const db = firebase.database();
db.ref(path).on("value", function(changedSnapshot) {
//値取得
const value = changedSnapshot.child(key).val();
if (value) {
console.log("解析開始");
console.log(value);
let commandlist = [
[ "冷房", "cool" ],
[ "暖房", "heat" ],
[ "除湿", "dehumidify" ],
[ "つけて", "aircon" ],
[ "エアコン", "aircon" ],
[ "消し" , "acoff" ],
[ "けし" , "acoff" ],
[ "止め" , "acoff" ],
[ "とめ" , "acoff" ],
[ "停止" , "acoff" ],
[ "default" , "" ]
];
let n = -1;
for( i = 0 ; i
if( commandlist[i].indexOf( value.split(" ")[1] ) >-1 ){
n=i;
}
}
console.log( n );
//コマンド実行
if( n>=0 ){
const command = commandlist[n][1];
rmSend(command)
//firebase clear
db.ref(path).set({[key]: ""});
}
}
});
前回作ったファイル「rmlist.js」とコピーした「getDevide.js」も、firebaseにコピーするのを忘れない。
node index.js
とすると、この状態で、タイトルが「暖房」のメールを送ると、暖房がつくはず。
■IFTTTの設定
では、最後にGoogle Homeの音声からエアコンを制御してみる。
やり方は、前回のThisの部分をGoogle Assistantにするだけ。
IFTTTのMy Appletsから、NewAppletを選択。
Thisでは、[Google Assistant]から、「Say a phrase with a text ingredient」を選択
Thatでは、[Webhooks]から、 「Make a web request」を選択
Thisには
What do you want to say? : エアコン $
What do you want the Assistant to say in response? : エアコンを制御します
Language : Japanese
Thatは以下のように入力。
URL:https://xxxxx-yyyyy.firebaseio.com/googlehome/word.json
Method:PUT
Content Type:application/json
Body:"aircon {{TextField}}"
Bodyへの入力には、「"」を忘れないこと。
以上で、「エアコン 暖房」と言うと、エアコンがつくはず。
■自動起動
最後に、Raspberry Pi を起動した時に自動でコマンドが走るよう
/etc/rc.local
のファイルにnode index.jsを入れておく。
後でプロセスを消したい場合は、
ps aux | grep node
で、nodeのプロセスを探し、killする。