MacでIRKitとSiriを使って日立のエアコンを動かす！

## Macにhomebridgeインストールまでは良かったが。。。
[ワタナベ書店](http://senyoltw.hatenablog.jp/entry/2016/02/22/080000)の記事を参考に、Macにhomebridgeをインストールしたまでは良かったけど、肝心のエアコンが動かなかったので備忘録。

## 赤外線信号がメーカーによって違う
IRKitは反応するものの、エアコンが赤外線信号を拾ってないぽいので、問題はそのあたりにあるだろうと予想。
我が家は日立のエアコンを使っていて、他のメーカーに比べると赤外線信号が複雑ということをIRKit購入時にWebで目にしていた。

## まずは日立の赤外線信号をゲットする
<img width="375" alt="IMG_5691.PNG" src="https://qiita-image-store.s3.amazonaws.com/0/175672/63f07681-2f4b-9469-baec-d42f7b1f72bb.png">
IRKitの[iPhone用App](https://itunes.apple.com/jp/app/irkit%E3%82%B7%E3%83%B3%E3%83%97%E3%83%AB%E3%83%AA%E3%83%A2%E3%82%B3%E3%83%B3/id778790928?mt=8)を立ち上げ、「ボタンを追加」→「リモコン信号の学習」で、日立のエアコンのリモコンをIRKit本体に向けてON。
しばらくしてIRKitのLEDが明滅したら、homebridgeを稼働させるMacのTerminalで

```
% curl -i "http://irkitxxxxx.local/messages/messages" -H "X-Requested-With: curl"
```

と叩いてやると、以下のようにえらく長い信号が帰ってくる。
**※ONの後でOFFの信号もゲットすることを忘れずに。**

```
HTTP/1.0 200 OK
Access-Control-Allow-Origin: *
Server: IRKit/2.1.3.13.gbe33d36
Content-Type: text/plain
{"format":"raw","freq":38,"data":[6648,3341,843,2451,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2451,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2451,843,843,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,843,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2451,843,2451,843,843,843,843,843,2451,843,2451,843,2451,843,2451,843,843,843,843,843,2451,843,2451,843,843,843,843,843,843,843,2451,843,843,843,843,843,2451,761,904,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,904,761,904,761,2537,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,2537,761,904,761,904,761,2537,761,2537,761,904,735,2537,761,2537,761,904,761,2537,761,2537,761,904,761,904,761,2537,761,904,761,904,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,873,787,873,787,873,787,873,787,873,787,873,787,873,787,873,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,873,787,873,787,873,787,873,787,873,787,2537,787,873,787,873,787,2537,787,2537,787,2537,787,2537,787,2537,787,873,787,2537,843,2537,843,843,843,843,843,843,843,2537,843,843,843,2537,843,2537,843,843,843,2537,843,2537,843,2537,843,843,843,2537,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843]}
```

## 拾った赤外線信号をhomebridge設定ファイルの該当部分に記述
これが日立エアコンの赤外線信号なので、homebridge設定ファイルの該当部分に記述する。

```config.json
{
    "bridge": {
        "name": "Homebridge",
        "username": "CD:22:3D:E3:CE:30",
        "port": 51826,
        "pin": "031-45-156"
    },
    
    "description": "The SAMPLE",

    "platforms": [],

    "accessories": [
        {
            "accessory": "IRKit",
            "name": "エアコン",
            "irkit_host": "irkitxxxxx.local",
            "on_form": {"format":"raw","freq":38,"data":[6648,3341,843,2451,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2451,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2451,843,843,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,843,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,2451,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2451,843,2451,843,843,843,843,843,2451,843,2451,843,2451,843,2451,843,843,843,843,843,2451,843,2451,843,843,843,843,843,843,843,2451,843,843,843,843,843,2451,761,904,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,904,761,904,761,2537,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,2537,761,904,761,904,761,2537,761,2537,761,904,735,2537,761,2537,761,904,761,2537,761,2537,761,904,761,904,761,2537,761,904,761,904,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,873,787,873,787,873,787,873,787,873,787,873,787,873,787,873,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,873,787,873,787,873,787,873,787,873,787,2537,787,873,787,873,787,2537,787,2537,787,2537,787,2537,787,2537,787,873,787,2537,843,2537,843,843,843,843,843,843,843,2537,843,843,843,2537,843,2537,843,843,843,2537,843,2537,843,2537,843,843,843,2537,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843,2537,843]},
            "off_form": {"format":"raw","freq":38,"data":[6648,3341,873,2451,873,873,873,873,873,873,873,873,873,787,787,787,787,787,787,787,787,787,787,787,787,787,787,2451,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,2451,873,873,873,2451,873,2451,873,2451,873,2451,873,2451,873,2451,873,873,873,2451,873,2451,873,2451,873,2451,873,2451,873,2451,873,2451,873,2451,873,2451,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,2451,873,2451,873,873,873,873,873,2451,873,2451,873,2451,873,2451,873,873,873,873,873,2451,873,2451,873,873,873,873,873,873,873,2451,873,873,873,873,873,2451,873,873,873,873,873,2451,873,2451,873,873,873,2451,873,2451,873,873,873,2451,873,2451,873,873,873,2451,873,2451,873,873,873,873,873,2451,787,873,787,873,787,873,787,873,787,873,787,2537,787,2537,787,873,787,2537,787,2537,787,2537,787,873,787,873,787,2537,787,2537,787,873,787,2537,787,2537,787,873,787,2537,787,2537,787,873,787,873,787,2537,787,873,787,873,787,2537,787,873,787,873,787,873,787,873,787,873,787,873,787,873,787,873,787,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,2537,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,2537,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,904,761,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787,2537,787]}
        }
    ]
}
```
## homebridge起動
で、改めてTerminalでhomebridgeを起動。

```
homebridge
Loaded plugin: homebridge-irkit
Registering accessory 'homebridge-irkit.IRKit'
---
Loaded config.json with 1 accessories and 0 platforms.
---
Loading 0 platforms...
Loading 1 accessories...
[エアコン] Initializing IRKit accessory...
Scan this code with your HomeKit App on your iOS device to pair with Homebridge:
                       
    ┌────────────┐     
    │ 031-45-156 │     
    └────────────┘     
                       
Homebridge is running on port 51826.
```
この状態で、Siriに「エアコンをつけて！」とお願いすると、IRKitがLチカして見事に日立エアコンが起動。
めでたしめでたし。

※iPhoneのhomekitで、エラーが出たり、反応しない場合は一旦persistを削除してから、homebridgeを起動すると幸せになれるかも:relaxed:

```
rm -R /Users/[ユーザー名]/.homebridge/persist/
```

以上、初Qiita投稿でした！:v: