3
4

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.

Raspberry pi とNode-REDを使ってスマホをリモコンにするまでの話

Last updated at Posted at 2019-12-16

テレビやエアコン,シーリングライトなどのリモコンが増えてきて,使いたいときに手元になかったりすることが多くなったので,Raspberry piを使って家の中のリモコンを集約してみました.

PCやスマホのブラウザから操作できるようにすることを目的としています.
javascript→Node-Red→pythonの順でリモコン操作のコマンドが流れていく設計としてますが,Node-RedにUI全部持たせても良かったかも.

用意したもの

  • Raspberry pi 2 Model B+
  • 赤外線LEDとか赤外線受光素子とかNチャネルFETとか

→エアコンの操作した結果,温度下がったのとかを確認できると良いですよね.ということでTIのSensorTag(CC2650) を使って温度測定もやったので別途書きたい.その際の参考記事.

SensorTagで測った温度、湿度などをRaspberry Pi3経由でAmbientに送ってグラフ化する
https://qiita.com/AmbientData/items/9b74bc8627c79aeecb0a

RPiリモコン化までの話

格安スマートリモコンの作り方
https://qiita.com/takjg/items/e6b8af53421be54b62c9

ここ読んで諸々準備は終わる.
下記のようにコマンドラインから機器の操作ができるようになってるはず

$ python3 irrp.py -p -g17 -f codes light:on

なお,点灯回路については手元にあったNchの2SK2232を使ってこんな感じにした
image.png
(※)R1についてはLEDの負荷電流考えて適切な値にしてね

Node-Redを使ってirrp.pyを叩く

irrp.pyの引数のうち,下記の部分についてWebフロントエンドからPOSTで受け取って実行するフローを作る.

$ python3 irrp.py -p -g17 -f codes <このぶぶん!>

結果,こんな感じのフローになった

image.png

忙しい人向け書き出したフロー:https://github.com/nc30mtd/rpi-nodered-web-remotecontroller/blob/master/node-red-flow/flow.json

Webの画面からNode-Redのフローにリクエスト送る

RPiにApacheをセットアップしてWebサーバを起動しておく.
ボタンなどを配置しておいて,ボタン押下時にPOSTする関数への引数とする

<button type="button" class="btn btn-primary" onclick="send_data('tv:power');">Power</button>

POSTする関数についてはこんな感じ

function send_data(cmd)
{
    console.log("send_data("+cmd+")");
    //json
    var json = 
    {
        "cmd":cmd
    };

    
    //
    // POST
    //			
    var xhrReq = new XMLHttpRequest();
    xhrReq.onreadystatechange = function() 
    {
        if (xhrReq.readyState == 4 && xhrReq.status == 200) 
        {
            var retText = xhrReq.responseText;
            var ret = JSON.parse(retText);
            
            console.log(retText);
        }
        else if (xhrReq.readyState == 4 && xhrReq.status != 200) 
        {
            alert("失敗("+xhrReq.status+"");
        }
    };
    xhrReq.open("POST", "<RPiのIPアドレス>:1880/send", true);
    xhrReq.setRequestHeader("Content-Type", "text/json");
    xhrReq.send(JSON.stringify(json));
}

その他ソースなどはこちら↓
https://github.com/nc30mtd/rpi-nodered-web-remotecontroller

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?