アラームをスケジュールで起動する仕組みを考えてみた。
wavplay.py**********************
import os
import sys
import time
import subprocess
import RPi.GPIO as GPIO
RELAY_PIN = 27
def play_wav(wav_file):
file_path = os.path.join("/home/pi", wav_file)
if not os.path.isfile(file_path):
return f"File not found: {file_path}"
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
try:
GPIO.output(RELAY_PIN, GPIO.HIGH)
time.sleep(0.2)
proc = subprocess.Popen(["aplay", file_path])
proc.wait()
return f"Played {wav_file}"
except Exception as e:
return f"Error: {e}"
finally:
GPIO.output(RELAY_PIN, GPIO.LOW)
GPIO.cleanup(RELAY_PIN)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python script.py <waveFilename>")
sys.exit(1)
filename = sys.argv[1]
result = play_wav(filename)
print(result)
import sys
import time
import subprocess
import RPi.GPIO as GPIO
RELAY_PIN = 27
def play_wav(wav_file):
file_path = os.path.join("/home/pi", wav_file)
if not os.path.isfile(file_path):
return f"File not found: {file_path}"
GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAY_PIN, GPIO.OUT)
try:
GPIO.output(RELAY_PIN, GPIO.HIGH)
time.sleep(0.2)
proc = subprocess.Popen(["aplay", file_path])
proc.wait()
return f"Played {wav_file}"
except Exception as e:
return f"Error: {e}"
finally:
GPIO.output(RELAY_PIN, GPIO.LOW)
GPIO.cleanup(RELAY_PIN)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python script.py <waveFilename>")
sys.exit(1)
filename = sys.argv[1]
result = play_wav(filename)
print(result)
crontab -e******************
0 17-19 10 11-12 * /usr/bin/python3 /home/pi/wavplay.py PurchaseKerosene.wav
ちなみに、現在のcrontabの一部
5 6 * * * /home/pi/update-cloudflare-ufw.sh >> /home/pi/tmp/ufw-cron.log 2>&1
*/10 * 13-29 2 * curl -s http://localhost:8080/py/collect >/dev/null
0 8 13-29 2 * curl "http://192.168.1.30/setConfig?setA1=21.5&setA2=21.4&setA3=21.5"
1行目cloudflare関連、2,3行目が苗床ヒーター管理
0 件のコメント:
コメントを投稿