2018年1月10日水曜日

GPIO割り込み[python]

GPIOの立ち上がり立下りで割り込みを行います。
この設定だと入力設定したどのピンでも反応します。

#イベントの取得
GPIO.add_event_detect(GPIO_SENSOR, GPIO.FALLING)
#コールバック関数設定
GPIO.add_event_callback(GPIO_SENSOR, callback)

コールバックの内容を記述します。
#callback処理内容
def callback(pin):
    #ここに割り込み処理内容を記述

以上です。

2018年1月9日火曜日

pythonでwavを再生する

pygameを使用します。

1.pygame.mixer.Sound で再生
500KbまでのWAVファイルを変数で再生できます。

    #おまじない
    pygame.mixer.init()
    #ロード
    sound1 = pygame.mixer.Sound("/home/pi/Music/sound1.wav")
    #再生
    sound1.play()
 
2.pygame.mixer.music で再生
500Kbの縛りなく再生できます。

    #おまじない
    pygame.mixer.init()
    #ロード
    pygame.mixer.music.load("/home/pi/Music/sound2.wav")
    #再生
    pygame.mixer.music.play(1)
    ※(1)とは再生回数
    #停止
    pygame.mixer.music.stop()

以上です。

python上で時間を設定する方法(オフライン)

-------------------------------------------------------------------------
cmd = "date -s '2017/11/06 23:10:00'"
subprocess.call( cmd ,shell=True)
-------------------------------------------------------------------------
cmd:コマンド
subprocess:コマンド実行

自動起動したプログラムを終了させる方法

前の内容で自動起動したプログラムを終了させる方法です。

起動中のプロセスが一覧で表示させます。
[コマンド]
~$ ps aux

終了したいプログラムを探します。
見つけたら下記コマンドで終了したいプログラムのPIDを指定します。

[コマンド]
~$ sudo kill (PID)

以上です。

Raspberry Piの起動時にコマンドを自動起動する方法

時代遅れらしい。
起動時にプログラムを自動で実行させます。

rc.localを編集します。

コマンド画面にて
$ sudo nano /etc/rc.local
と入力します。
sudoはroot権限で実行、nanoはエディタです。

--------------------------------------------------------------------------------------------
!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

exit 0
-------------------------------------------------------------------------------------------

ファイル内容は上記のようになっています。
"exit0" と"fi"の間に実行したいコマンドを記述します。

今回は"/home/pi/hoge.py"というプログラムを実行させたいので下記の一文を追加します。
”$sudo python /home/pi/hoge.py”

下記の状態で保存してます。

--------------------------------------------------------------------------------------------
!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

$sudo python /home/pi/hoge.py

exit 0
-------------------------------------------------------------------------------------------

以上です。