4.Audio出力設定
参考URL:
https://heavymoon.org/2022/11/12/rpi-zero-pwm-audio/
設定変更:
$sudo raspi-config
「Advanced Opstions」→ 「Audio」→ 「Force 3.5mm (Headphone)」
/boot/config.txt に追加:
dtoverlay=audremap,pins_18_19
audio_pwm_mode=2
$reboot
$aplay -l
でHeadphonesのcard Xを確認。次の/.asoundrcに反映させる。
~/.asoundrc を追加:
pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm "output"
}
capture.pcm {
type plug
slave.pcm "input"
}
}
pcm.output {
type hw
card 0 # 出力先に card 0 を指定
}
ctl.!default {
type hw
card 0
}
ノイズが気になる場合はRC回路追加
5.Radish-play設定
参考URL:
https://min117.hatenablog.com/entry/2021/05/04/125614
https://github.com/jg1uaa/radish-play
聴取希望の放送局を受信するシェルスクリプトを作って、実行権を与えておく
例:
$chmod +x radiko_bayfm78.sh
6.制御用Pythonスクリプト作成
コード例:
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import sys, os
# call program when buttons are pushed
if __name__ == "__main__":
# intialize variables
count1 = 0
count2 = 0
count3 = 0
count4 = 0
count5 = 0
makeShortTH = 5
makeLongTH = 500
# setting GPIO by board
pin1 = 3
pin2 = 7
pin3 = 15
pin4 = 21
out1 = 8
out2 = 10
out3 = 16
out4 = 18
out5 = 22
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin1, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(pin2, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(pin3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(pin4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(out1, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(out2, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(out3, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(out4, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(out5, GPIO.OUT, initial=GPIO.LOW)
# ボタンクリック待機中
print("Program start!")
while True:
button1 = GPIO.input(pin1)
button2 = GPIO.input(pin2)
button3 = GPIO.input(pin3)
button4 = GPIO.input(pin4)
cmd = ""
if button1 == False:
# counter-up & judge Long
count1 += 1
if count1 >= makeLongTH:
count1 = 0
break
else:
# judge Short
if count1 >= makeShortTH:
count1 = 0
cmd = "./radiko_bayfm78"
print("Button1")
if button2 == False:
# counter-up
count2 += 1
else:
# judge Short
if count2 >= makeShortTH:
count2 = 0
cmd = "./radiko_TFM"
print("Button2")
if button3 == False:
# counter-up
count3 += 1
else:
# judge Short
if count3 >= makeShortTH:
count3 = 0
cmd = "./radiko_nipponHousou"
print("Button3")
if button4 == False:
# counter-up
count4 += 1
else:
# judge Short
if count4 >= makeShortTH:
count4 = 0
cmd = "./radiko_luckyFM"
print("Button4")
# 実行
if cmd != "":
#os.system(cmd)
#ret = os.popen(cmd).readline().strip()
#print(ret)
time.sleep(1) # ボタンを押した後のチャタリング防止のため
time.sleep(0.016)
GPIO.cleanup()
print("Shutdown...")
os.system("sudo shutdown -h now")
7.Pythonスクリプトの自動実行設定
参考URL:
https://qiita.com/karaage0703/items/ed18f318a1775b28eab4
autorun設定
$ mkdir -p ~/.config/lxsession/LXDE-pi
$ cp /etc/xdg/lxsession/LXDE-pi/autostart ~/.config/lxsession/LXDE-pi/
~/.config/lxsession/LXDE-pi/autostart
に追加。
python /home/pi/work/netradio.py &
※コメント投稿者のブログIDはブログ作成者のみに通知されます