ラズパイ3で赤外線リモコンの操作
Raspberry Pi 3 で赤外線リモコン&温度センサーを試す
を参考に実践
まず
irMagician を ラズパイ3にUSB ケーブルで接続
使用するのはAndroid で使っているものでOK
irMagician の黄色LEDが点滅していれば認識している
また
1 | ls -l /dev/ttyACM0 |
で
/dev 以下に ttyACM0 が存在していれば認識しているのを
確認できる
次に
ptyhon-pip と pyserial のインストール
1 2 3 |
制御プログラムを git で取得
1 | git clone https: //github .com /netbuffalo/irmcli .git |
1 | cd irmcli/ |
で移動してから赤外線センサーの学習
1 | udo python irmcli.py -c |
を実行したけど
1 2 3 4 | File "irmcli.py" , line 15 print "Capturing IR..." ^ SyntaxError: Missing parentheses in call to 'print' . Did you mean print( "Capturing IR..." )? |
となる
SyntaxError: Missing parentheses in call to ‘print’.
で検索してみると
どうやら
python3 から print の構文が変わっていて
によれば
Python3からはprint()で実行しないとエラーになる
とのこと
15行目の
print “Capturing IR…”
を
print(“Capturing IR…”)
として
実行したら
1 2 3 4 | File "irmcli.py" , line 19 print msg ^ SyntaxError: Missing parentheses in call to 'print' . Did you mean print(msg)? |
となるので
さすがに毎回変更は面倒なので
1 | vim irmcli.py |
でファイルを開き
1 | :%s /print msg /print (msg) /g |
で一度に変更
vim の置き換えについては
vim置換
を参考に
いくつか修正するところがあるので
/print ”
で検索し
print() になるように修正
1 2 | File "irmcli.py" , line 113 print ir_serial.readline().rstrip() |
については
1 | print (ir_serial.readline().rstrip()) |
とすると
1 2 3 4 5 6 7 8 9 10 | Traceback (most recent call last): File "irmcli.py" , line 142, in <module> captureIR(args. file ) File "irmcli.py" , line 16, in captureIR ir_serial.write( "c\r\n" ) File "/home/pi/.pyenv/versions/3.6.10/lib/python3.6/site-packages/serial/serialposix.py" , line 532, in write d = to_bytes(data) File "/home/pi/.pyenv/versions/3.6.10/lib/python3.6/site-packages/serial/serialutil.py" , line 63, in to_bytes raise TypeError( 'unicode strings are not supported, please encode to bytes: {!r}' . format ( seq )) TypeError: unicode strings are not supported, please encode to bytes: 'c\r\n' |
となってしまう
irmagician ラズパイ エラー
で検索し
JSONに保存した赤外線データを使って赤外線照射したいのですが、エラーが出てできません。
によれば
python3 だと
“n,%d\r\n” % recNumberはstr型なので提示エラーになる
~.encode()とbyte型に変換するとよい
とのこと
python3 pySerial TypeError: unicode strings are not supported, please encode to bytes:
も参考にするように書いてある
少しわかりにくかったので
ser.write
で検索し
をみると
write() で
文字列の前に
b
をつければOKらしい
:%s/write(“/write(b”/g
で置き換えて実行したが
リモコンの学習ができない
仕方ないので
コードを書き換えるより
ディレクトリ単位で python 環境を構築することに
pyenv-virtualenvでディレクトリ単位のpython環境構築
ラズパイにpyenv・Python3.7.0をインストールしてpyenv-virtualenvで仮想環境の構築!
を参考に
なお pyenv-virtualenv のインストールに関しては
pyenv と pyenv-virtualenv で環境構築
を参考に
1 | git clone https: //github .com /yyuu/pyenv-virtualenv .git ~/.pyenv /plugins/pyenv-virtualenv |
でファイルを取得
1 | sudo vim .bash_profile |
でファイルを開き
最終行に
1 | eval "$(pyenv virtualenv-init -)" |
を追記して保存
1 | source ~/.bash_profile |
でファイルをリロード
次に仮想環境の構築
1 | cd irmcli/ |
で irmagician のディレクトリに移動
次に python 2.7.9 をインストール
1 | pyenv install 2.7.9 |
しかし
1 | ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib? |
となる
エラーを検索
RaspberryPiにpyenvを入れて複数のPython動かすよ
を参考に
1 | sudo apt install -y libssl1.0-dev |
Debian 9 Stretchでは、libssl-devは
OpenSSL 1.1.xベースになっているので
あったlibsslを入れろとの事
再度
1 | pyenv install 2.7.9 |
を実行しインストール成功
pyenv は
global なら全体のバージョンを設定
local なら
カレントディレクトリに反映となるので
1 | pyenv local 2.7.9 |
として
irmcli ディレクトリのみ
python を 2.7.9 にする
次に
仮想環境の保存と再構築のため
1 | pyenv exec pip install wheel |
で wheel をインストール
freeze コマンドで
requirements ファイルを作成し
wheel ファイルを出力
1 2 | pyenv exec pip freeze > ~ /pyp_list .txt pyenv exec pip wheel --wheel- dir =~ /wheelhouse -r ~ /pyp_list .txt |
としておく
次に
1 | python --version |
で ver 2.7.9 になっているのを確認
1 2 3 4 5 6 7 8 | python get-pip.py pip install pyserial [ /shell で必要なものをインストール vim irmcli.py |
で変更した部分を元に戻しておく
元に戻したら
1 | sudo python irmcli.py -c |
で部屋のリモコンの全灯を押し学習させる
1 | sudo python irmcli.py -s -f light_on.json |
でファイルへ書き出し
リモコンの消灯を押して照明を消す
1 | sudo python irmcli.py -p -f light_on.json |
を実行すると全灯になって照明が点灯する
なお距離は2mでも動作するが
リモコンと異なり
赤外線センサーの向きなどがあっていないと
操作できないので注意