LoginSignup
1
2

More than 5 years have passed since last update.

RaspberryPi3を使ってSlackからエアコン操作を行う②

Posted at

こちらの記事は前回の続きとなります。
初めから読みたい方はこちらからお願いします。
RaspberryPi3を使ってSlackからエアコン操作を行う①

7.前回までと今回やることの確認

前回まででLチカをSlackから操作することができるようになりました。
今回からいよいよエアコンを操作していきます。

流れとしては、このようになります。
①赤外線受光モジュールを使って、エアコンのON/OFF時の赤外線パターンを覚える
②赤外線送信LEDを使って、覚えた赤外線パターンを送信する

8.エアコンのON/OFF時の赤外線を受信する(配線編)

ラズパイの電源を落として下図のように配線をします。
赤外線リモコン受信モジュールは
右側の足が5V電源のピン、真ん中の足がマイナス、左側の足がlircの入力ピン(今回はGPIO18)、マイナスからGNDにささっていることを確認してください。

スクリーンショット 2018-03-18 16.46.13.png

9.エアコンのON/OFF時の赤外線を受信する(ファイル設定編)

lircというLinuxで赤外線リモコンを受信したり発信したりする環境を使って赤外線の受発信を行いますので、まずはこちらをインストールしていきます。

terminal
$ sudo apt-get update  #一旦更新する
$ sudo apt-get install lirc

lircがバージョンアップの影響?で参考にしていた記事とコマンドが違かったり、confファイルが変わっていてかなり苦戦しました。
2018/02時点で私の環境では、lircのバージョンとフォルダ構成は以下となります。

terminal
$ lircd -v  #バージョン確認
lircd 0.9.4c
$ ls -lR /etc/lirc  #ファイル一覧
/etc/lirc:
合計 36
-rw-r--r-- 1 root root 1124  2月 17 14:30 irexec.lircrc
-rw-r--r-- 1 root root 1124  4月  6  2017 irexec.lircrc.dist
-rw-r--r-- 1 root root 1024  2月 17 15:03 lirc_options.conf
-rw-r--r-- 1 root root  988  4月  6  2017 lirc_options.conf.dist
-rw-r--r-- 1 root root  313  2月 17 14:30 lircd.conf
drwxr-xr-x 2 root root 4096  2月 20 22:23 lircd.conf.d
-rw-r--r-- 1 root root  313  4月  6  2017 lircd.conf.dist
-rw-r--r-- 1 root root  155  2月 17 14:30 lircmd.conf
-rw-r--r-- 1 root root  155  4月  6  2017 lircmd.conf.dist

/etc/lirc/lircd.conf.d:
合計 12
-rw-r--r-- 1 root root  615  4月  6  2017 README.conf.d
-rw-r--r-- 1 root root 5859  2月 20 22:23 lircd_Aircon.conf  #このファイルは初めはありません

まずはlirc_options.confファイルを変更します。

terminal
$ sudo vi /etc/lirc/lirc_options.conf

driver = devinput
device = auto
このように書かれている箇所を下記のように修正します。

/etc/lirc/lirc_options.conf
# These are the default options to lircd, if installed as
# /etc/lirc/lirc_options.conf. See the lircd(8) and lircmd(8)
# manpages for info on the different options.
#
# Some tools including mode2 and irw uses values such as
# driver, device, plugindir and loglevel as fallback values
# in not defined elsewhere.

[lircd]
nodaemon        = False
driver          = default     #←ここを修正
device          = /dev/lirc0  #←ここを修正
output          = /var/run/lirc/lircd
pidfile         = /var/run/lirc/lircd.pid
plugindir       = /usr/lib/arm-linux-gnueabihf/lirc/plugins
permission      = 666
allow-simulate  = No
repeat-max      = 600
#effective-user =
#listen         = [address:]port
#connect        = host[:port]
#loglevel       = 6
#uinput         = ...
#release        = ...
#logfile        = ...

[lircmd]
uinput          = False
nodaemon        = False

# [modinit]
# code = /usr/sbin/modprobe lirc_serial
# code1 = /usr/bin/setfacl -m g:lirc:rw /dev/uinput
# code2 = ...


# [lircd-uinput]
# release-timeout = 200

次に/boot/config.txtにGPIOの接続情報を記載します。

terminal
$ sudo vi /boot/config.txt

dtoverlayと記載がある箇所を下記のようにします。
dtoverlay=lirc-rpi,gpio_in_pin=18,gpio_out_pin=17

今回は赤外線受信を18番ピン、送信を17番ピンで行います。

/boot/config.txt
# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Uncomment this to enable the lirc-rpi module
dtoverlay=lirc-rpi,gpio_in_pin=18,gpio_out_pin=17 #←ここを修正

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

# NOOBS Auto-generated Settings:
hdmi_force_hotplug=1

設定を適用するために再起動します。

terminal
$ sudo reboot

再起動後にlircと/dev/lirc0の存在を確認します。

terminal
$ lsmod | grep lirc
lirc_rpi                9032  0
lirc_dev               10583  1 lirc_rpi
rc_core                24377  1 lirc_dev
$ ls -l /dev/lirc*
crw-rw---- 1 root video 244, 0  2月 17 14:53 /dev/lirc0

lircdを止めます。

terminal
$ sudo /etc/init.d/lircd stop
[....] Stopping lircd (via systemctl): lircd.serviceWarning: Stopping lircd.service, but it can still be activated by:
  lircd.socket
. ok 

mode2のバージョン確認

terminal
$ mode2 -v
mode2  0.9.4c

エアコンの受信信号を受信します。
下記コマンドを打った後、赤外線受信モジュールに向けてリモコンで運転ボタンを押す

terminal
$ mode2 -d /dev/lirc0
Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Using device: /dev/lirc0

受信できているとこうなる。

terminal
$ mode2 -d /dev/lirc0
Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Using device: /dev/lirc0
pulse 5937046
space 423
pulse 459
space 409
pulse 462
space 407
pulse 457
space 411
  :
  :

反応しない場合は、配線が間違っているか、「/boot/config.txt」のピンの指定を間違っている可能性が高いと思います。
(私は赤外線受信モジュールを間違って3Vに接続していて、受信できませんでした。。)

10.受信した赤外線のパターンファイルを作成する

先ほど受信ができたので、次にこのパターンをファイルにします。

まずはlircdを止めます。

terminal
$ sudo /etc/init.d/lircd stop
[....] Stopping lircd (via systemctl): lircd.serviceWarning: Stopping lircd.service, but it can still be activated by:
  lircd.socket
. ok 

エアコンをONにするボタンを押す → Ctrl+C

terminal
$ mode2 -d /dev/lirc0 | tee AIRON
Trying device: /dev/lirc0
Using device: /dev/lirc0
Using driver default on device /dev/lirc0
pulse 5937046
space 423
pulse 459
space 409
pulse 462
space 407
pulse 457
space 411
  :
  :

エアコンをOFFにするボタンを押す → Ctrl+C

terminal
$ mode2 -d /dev/lirc0 | tee AIROFF
Trying device: /dev/lirc0
Using device: /dev/lirc0
Using driver default on device /dev/lirc0
pulse 4167550
space 433
pulse 430
space 431
pulse 458
space 411
pulse 462
space 407
  :
  :

ファイルが作られている。

terminal
$ ls -l
合計 2
-rw-r--r-- 1 pi pi 6544  2月 19 20:13 AIROFF
-rw-r--r-- 1 pi pi 6541  2月 19 20:12 AIRON

次にこのファイルから赤外線送信ファイルを作成していきます。
下記ファイルを作成し、パターンファイルを整形します。

parse_pulse.rb
lines =  File.open(ARGV[0]).readlines #行ごとに配列として読み込み
lines_without_first = lines.slice(1..-1) #先頭の行は不必要なので省く
puts lines_without_first.map{|line|line.split(" ")[1]}.join(" ") #整形して表示

このスクリプトを実行することにより、パターンファイルから数字の部分のみを抜き出してくれます。

terminal
$ ruby parse_pulse.rb AIRON
5937046 423 459 409 462 407 457 411 458 411 458 412 25194 3480 1765 408 1326 412 457 411 458 411 458 411 1306 439 430 439 431 438 422 447 422 447 1305 433 430 439 1299 439 1299 439 422 446 1292 446 1291 447 1293 445 1291 447 1299 439 422 447 431 438 1291 447 430 439 430 439 423 446 422 447 430 438 431 438 431 438 423 446 431 438 431 438 1299 439 431 438 436 433 423 446 431 438 431 438 422 447 422 447 422 447 422 447 422 447 422 447 430 439 431 439 421 447 430 439 430 439 430 439 422 447 430 439 430 438 423 446 431 438 431 438 430 439 427 442 422 447 422 447 422 447 430 439 422 447 430 439 422 447 430 439 422 447 422 447 430 438 432 440 428 439 430 439 430 439 422 447 422 447 430 439 431 437 423 446 431 438 423 446 446 417 457 411 463 406 458 411 458 411 457 413 457 411 458 411 458 411 458 411 458 411 458 423 433 424 458 411 465 404 458 411 458 411 457 412 458 411 458 411 457 412 458 411 458 411 457 410 448 423 461 411 459 407 458 411 458 410 459 410 462 407 456 414 457 411 462 407 458 418 451 418 423 446 431 438 423 446 430 439 422 447 439 423 460 409 462 407 458 411 460 409 464 410 453 410 457 413 457 412 458 415 454 411 458 411 434 434 459 411 462 407 457 411 459 411 457 411 458 413 456 410 459 416 453 411 465 405 474 401 422 440 459 411 461 407 457 412 464 405 459 410 458 411 1327 411 1327 411 458 417 432 429 1328 421 448 412 459 410 457 412 34753 3486 1756 411 1326 411 458 411 459 410 462 407 1298 447 431 439 424 436 457 413 468 401 1330 408 458 411 1330 408 1327 411 458 411 1312 432 1292 456 1282 446 1301 437 1292 446 431 444 417 438 1332 407 448 421 458 411 458 411 458 411 457 412 458 410 459 411 458 411 458 411 446 430 424 445 439 430 430 439 430 439 438 430 423 446 422 448 444 425 1291 446 423 446 430 439 1299 439 430 439 425 444 1300 438 422 447 422 447 1291 447 1307 431 1299 439 422 439 1327 412 457 411 458 411 458 411 458 411 458 411 458 411 458 411 458 411 458 418 423 446 422 447 422 447 430 439 422 447 1299 439 1299 440 421 447 1299 438 423 447 430 438 423 446 423 446 423 446 423 446 431 438 422 447 423 439 458 411 462 407 458 411 458 411 458 411 458 411 458 411 458 411 1330 408 1309 436 422 447 422 447 422 440 458 412 461 407 468 401 459 412 455 413 456 423 446 412 1326 412 1327 417 431 438 426 442 452 411 462 407 461 408 458 411 461 408 458 411 458 411 458 411 466 405 456 412 457 418 422 453 416 447 422 447 422 447 1299 439 422 440 457 414 468 399 457 411 458 412 1331 406 1328 410 463 406 458 411 458 419 432 435 453 412 462 407 450 418 1321 416 465 406 459 408 458 413 455 420 456 404 459 417 423 446 431 431 462 407 463 406 461 408 458 411 463 406 1327 410 1335 405 1326 418
$ ruby parse_pulse.rb AIROFF
4167550 433 430 431 458 411 462 407 458 411 458 411 25203 3472 1765 408 1326 411 458 411 458 411 458 411 1307 438 430 439 422 447 430 439 422 447 1291 447 422 447 1301 437 1291 447 422 446 1292 446 1301 437 1291 447 1309 429 1300 438 430 439 430 445 1285 447 430 439 430 439 430 439 430 439 423 446 430 438 431 439 422 446 423 446 431 438 1291 447 423 446 430 439 423 446 430 440 422 446 422 447 422 447 422 447 432 434 454 414 466 404 457 408 458 411 463 406 458 411 457 412 458 411 460 409 458 411 464 405 429 451 418 440 458 409 464 407 458 411 457 412 457 410 467 403 465 404 461 408 458 411 458 412 457 418 432 437 423 446 423 446 431 431 456 417 458 407 458 410 457 413 458 411 458 411 465 404 458 411 458 411 457 412 460 416 430 438 423 447 422 447 430 439 450 412 462 407 462 409 455 410 459 419 454 409 456 411 457 412 461 407 459 411 459 410 439 430 458 418 455 407 457 412 458 413 462 405 458 411 458 411 458 411 457 412 458 411 461 415 422 447 430 432 458 418 455 407 457 411 458 416 453 412 456 412 458 419 451 408 459 418 451 412 466 413 426 433 458 411 463 406 458 411 459 410 457 412 458 411 462 407 458 411 458 411 458 411 458 418 425 437 457 412 466 403 458 411 457 411 458 412 461 408 458 410 465 404 458 411 458 411 458 418 431 438 431 431 457 412 463 410 458 407 462 411 454 411 1331 407 1327 411 458 411 458 418 1291 447 431 432 456 412 462 411 34754 3482 1736 436 1300 438 431 433 464 403 463 407 1326 411 458 411 466 403 458 411 458 411 1335 403 459 410 1306 439 1299 438 426 443 1300 439 1291 455 1283 446 1292 446 1291 447 423 439 458 411 1326 412 464 405 458 410 467 403 461 408 458 411 458 411 458 412 460 415 422 447 422 447 422 446 432 438 422 446 423 439 458 419 455 406 458 412 457 411 458 411 458 411 458 411 1331 407 458 411 463 413 1300 438 430 439 422 447 1291 447 1291 447 1308 430 422 448 1289 447 423 446 423 446 423 446 431 438 422 447 428 441 430 439 423 446 422 447 428 441 422 447 422 447 422 447 422 447 1300 438 1291 447 422 447 1290 441 458 411 464 405 463 404 460 409 463 406 459 412 457 411 458 411 458 411 458 412 457 411 437 439 423 446 422 447 431 431 458 411 463 406 1327 411 1327 411 458 412 457 417 452 411 458 411 458 420 438 428 454 409 463 411 452 413 445 423 1326 411 1329 410 457 411 463 406 458 418 423 439 458 412 469 400 457 411 458 411 462 406 461 427 440 419 450 411 457 412 458 411 460 416 440 422 458 411 1331 407 458 411 460 409 458 415 454 411 458 410 1327 411 1307 431 458 416 457 407 458 411 458 411 462 407 458 417 452 411 1331 407 464 405 458 418 432 436 452 411 462 407 456 419 452 415 454 411 1327 410 1331 408 1326 419 1291 446 1307 431 431 432 1330 408 1330 407

数字の1番目はリモコンのスイッチを押すまでの時間なので消します。

続いてこの数字をlircd.confファイルに追加していきます。
バージョン0.9.2からlircd.confファイルは、lircd.conf.dフォルダ下にある*.confファイルをインクルードするようになったみたいです。

terminal
$ cat /etc/lirc/lircd.conf
# Populated config files can be found at http://sf.net/p/lirc-remotes. The
# irdb-get(1) and lirc-setup(1) tools can be used to search and download
# config files.
#
# From 0.9.2 config files could just be dropped as-is in the lircd.conf.d
# directory and be included by this file.

include "lircd.conf.d/*.conf"

なので、lircd.conf.dフォルダ下に新たにファイルを作成します。

terminal
$ sudo vi /etc/lirc/lircd.conf.d/lircd_Aircon.conf
/etc/lirc/lircd.conf.d/lircd_Aircon.conf
# Please make this file available to others
# by sending it to <lirc@bartelmus.de>
#
# this config file was automatically generated
# using lirc-0.9.0-pre1(default) on 2015
#
# contributed by (任意)
#
# brand: (任意)
# model no. of remote control: (任意)
# devices being controlled by this remote: (任意)
#

begin remote

  name  aircon
  flags RAW_CODES
  eps            30
  aeps          100

  gap          200000
  toggle_bit_mask 0x0

      begin raw_codes
    name off
433 430 431 458 411 462 407 458 411 458 411 25203 3472 1765 408 1326 411 458 
411 458 411 458 411 1307 438 430 439 422 447 430 439 422 447 1291 447 422 
447 1301 437 1291 447 422 446 1292 446 1301 437 1291 447 1309 429 1300 
438 430 439 430 445 1285 447 430 439 430 439 430 439 430 439 423 446 430
438 431 439 422 446 423 446 431 438 1291 447 423 446 430 439 423 446 430 
440 422 446 422 447 422 447 422 447 432 434 454 414 466 404 457 408 458 
411 463 406 458 411 457 412 458 411 460 409 458 411 464 405 429 451 418 
440 458 409 464 407 458 411 457 412 457 410 467 403 465 404 461 408 458 
411 458 412 457 418 432 437 423 446 423 446 431 431 456 417 458 407 458 
410 457 413 458 411 458 411 465 404 458 411 458 411 457 412 460 416 430 
438 423 447 422 447 430 439 450 412 462 407 462 409 455 410 459 419 454 
409 456 411 457 412 461 407 459 411 459 410 439 430 458 418 455 407 457 
412 458 413 462 405 458 411 458 411 458 411 457 412 458 411 461 415 422 
447 430 432 458 418 455 407 457 411 458 416 453 412 456 412 458 419 451 
408 459 418 451 412 466 413 426 433 458 411 463 406 458 411 459 410 457 
412 458 411 462 407 458 411 458 411 458 411 458 418 425 437 457 412 466 
403 458 411 457 411 458 412 461 408 458 410 465 404 458 411 458 411 458 
418 431 438 431 431 457 412 463 410 458 407 462 411 454 411 1331 407 1327 
411 458 411 458 418 1291 447 431 432 456 412 462 411 34754 3482 1736 436 
1300 438 431 433 464 403 463 407 1326 411 458 411 466 403 458 411 458 
411 1335 403 459 410 1306 439 1299 438 426 443 1300 439 1291 455 1283 
446 1292 446 1291 447 423 439 458 411 1326 412 464 405 458 410 467 403 
461 408 458 411 458 411 458 412 460 415 422 447 422 447 422 446 432 438 
422 446 423 439 458 419 455 406 458 412 457 411 458 411 458 411 458 411 
1331 407 458 411 463 413 1300 438 430 439 422 447 1291 447 1291 447 1308 
430 422 448 1289 447 423 446 423 446 423 446 431 438 422 447 428 441 430 
439 423 446 422 447 428 441 422 447 422 447 422 447 422 447 1300 438 1291 
447 422 447 1290 441 458 411 464 405 463 404 460 409 463 406 459 412 457 
411 458 411 458 411 458 412 457 411 437 439 423 446 422 447 431 431 458 
411 463 406 1327 411 1327 411 458 412 457 417 452 411 458 411 458 420 
438 428 454 409 463 411 452 413 445 423 1326 411 1329 410 457 411 463 
406 458 418 423 439 458 412 469 400 457 411 458 411 462 406 461 427 440 
419 450 411 457 412 458 411 460 416 440 422 458 411 1331 407 458 411 460 
409 458 415 454 411 458 410 1327 411 1307 431 458 416 457 407 458 411 
458 411 462 407 458 417 452 411 1331 407 464 405 458 418 432 436 452 411 
462 407 456 419 452 415 454 411 1327 410 1331 408 1326 419 1291 446 1307 
431 431 432 1330 408 1330 407
    name on
423 459 409 462 407 457 411 458 411 458 412 25194 3480 1765 408 1326 412 
457 411 458 411 458 411 1306 439 430 439 431 438 422 447 422 447 1305 433 
430 439 1299 439 1299 439 422 446 1292 446 1291 447 1293 445 1291 447 
1299 439 422 447 431 438 1291 447 430 439 430 439 423 446 422 447 430 
438 431 438 431 438 423 446 431 438 431 438 1299 439 431 438 436 433 
423 446 431 438 431 438 422 447 422 447 422 447 422 447 422 447 422 447 
430 439 431 439 421 447 430 439 430 439 430 439 422 447 430 439 430 438 
423 446 431 438 431 438 430 439 427 442 422 447 422 447 422 447 430 439 
422 447 430 439 422 447 430 439 422 447 422 447 430 438 432 440 428 439 
430 439 430 439 422 447 422 447 430 439 431 437 423 446 431 438 423 446 
446 417 457 411 463 406 458 411 458 411 457 413 457 411 458 411 458 411 
458 411 458 411 458 423 433 424 458 411 465 404 458 411 458 411 457 412 
458 411 458 411 457 412 458 411 458 411 457 410 448 423 461 411 459 407 
458 411 458 410 459 410 462 407 456 414 457 411 462 407 458 418 451 418 
423 446 431 438 423 446 430 439 422 447 439 423 460 409 462 407 458 411 
460 409 464 410 453 410 457 413 457 412 458 415 454 411 458 411 434 434 
459 411 462 407 457 411 459 411 457 411 458 413 456 410 459 416 453 411 
465 405 474 401 422 440 459 411 461 407 457 412 464 405 459 410 458 411 
1327 411 1327 411 458 417 432 429 1328 421 448 412 459 410 457 412 34753 
3486 1756 411 1326 411 458 411 459 410 462 407 1298 447 431 439 424 436 
457 413 468 401 1330 408 458 411 1330 408 1327 411 458 411 1312 432 1292 
456 1282 446 1301 437 1292 446 431 444 417 438 1332 407 448 421 458 411 
458 411 458 411 457 412 458 410 459 411 458 411 458 411 446 430 424 445 
439 430 430 439 430 439 438 430 423 446 422 448 444 425 1291 446 423 446 
430 439 1299 439 430 439 425 444 1300 438 422 447 422 447 1291 447 1307 
431 1299 439 422 439 1327 412 457 411 458 411 458 411 458 411 458 411 
458 411 458 411 458 411 458 418 423 446 422 447 422 447 430 439 422 447 
1299 439 1299 440 421 447 1299 438 423 447 430 438 423 446 423 446 423 
446 423 446 431 438 422 447 423 439 458 411 462 407 458 411 458 411 458 
411 458 411 458 411 458 411 1330 408 1309 436 422 447 422 447 422 440 
458 412 461 407 468 401 459 412 455 413 456 423 446 412 1326 412 1327 
417 431 438 426 442 452 411 462 407 461 408 458 411 461 408 458 411 458 
411 458 411 466 405 456 412 457 418 422 453 416 447 422 447 422 447 1299 
439 422 440 457 414 468 399 457 411 458 412 1331 406 1328 410 463 406 458 
411 458 419 432 435 453 412 462 407 450 418 1321 416 465 406 459 408 458 
413 455 420 456 404 459 417 423 446 431 431 462 407 463 406 461 408 458 
411 463 406 1327 410 1335 405 1326 418
      end raw_codes

end remote

数字の羅列の部分は先ほどのスクリプトで表示された数字の羅列を入れてください。
数字の行があまりに長いとうまく動かないらしいので、80文字程度ごとに改行する必要があるみたいです。(任意)と書いてある箇所は適宜記入してください。

11.エアコンに送信する(配線編)

ラズパイの電源を落として下図のように配線をします。
受光モジュールの配線に赤外線送信LEDの配線を横に追加しています。
以前の設定ファイルにより送信はGPIO17に接続します。

スクリーンショット 2018-03-18 17.47.53.png

12.エアコンに送信する(実行編)

配線が完了したら、いよいよ送信です。
lircdを起動します。

terminal
$ sudo /etc/init.d/lircd start
[ ok ] Starting lircd (via systemctl): lircd.service.

自動で起動したい場合は、update-rc.dで登録します。

terminal
$ sudo update-rc.d lircd defaults

下記コマンドを打ち、今回のコマンドが表示されればOK

terminal
$ irsend LIST '' ''

aircon

$ irsend LIST aircon ''

0000000000000001 off
0000000000000002 on

それではエアコンにONの制御を送信してみましょう。

terminal
$ irsend SEND_ONCE aircon on

ピピッとエアコンが動き出せば成功です。

(私はここでももちろん躓きまして、苦戦しました。)
以下うまくいかなかった時は参考にしてください。

原因①:エアコンに赤外線LEDが向いていない
エアコンの受光部にLEDが向いていないと、エアコンは受信してくれません。
エアコンの受光部に向けて実行しましょう。
irsendコマンドにオプション「-#10」をつけると、指定回数実行してくれます。(この場合10回)
実行しながらモジュールを動かして、エアコンの反応しそうな位置や距離を試してみましょう。

terminal
$ irsend -#10 SEND_ONCE aircon on

原因②:設定ファイルが正しく設定できていない
lircd_Aircon.confファイルの数字の羅列を80文字程度で改行し忘れている場合など。

原因③:配線が間違っている
(私はこれでした。。)
送信LEDがGPIO17に刺さっているかなど確認してみましょう。

13.Slackから操作する

ついに最後です。
Slackから指定した文字列でエアコンのON/OFFを操作できるようにします。

/var/www/index.phpを修正します。

/var/www/index.php
<?php
if (preg_match('/off/', $_POST['text'])):
    exec("irsend SEND_ONCE aircon off");?>
  {"text":"エアコンを消しました!"}
<?php 
elseif (preg_match('/on/', $_POST['text'])):
    exec("irsend SEND_ONCE aircon on");?>
  {"text":"エアコンをつけました!"}
<?php endif;?>

API「Outgoing WebHooks」の引き金となる言葉を「on,off」に設定します。
これで「on」「off」とSlackで入力したときにそれぞれの動作をしてくれるようになりました。

スクリーンショット 2018-03-18 18.17.47.png
スクリーンショット 2018-03-18 18.17.59.png

これで完成です。

14.さいごに

エアコン操作はうまくできましたでしょうか。

私自身は試行錯誤をかなり繰り返して、やっとできました。

多くのエアコン操作の記事を参考に進めましたが、
lircのバージョンの違いや購入した部品が違うものだったために配線で苦戦したりといろいろ躓きました。少しでも参考になればなと思っております。

またラズパイを使ってライフハックをした事例を紹介できればなと思っております。

最後まで読んでいただきありがとうございました。

1
2
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
1
2