Myu
2026年2月22日日曜日
rapiでPCのタイマー起動
PCをraspiからタイマー起動、Offできるようにしてみた。
*********gpio_control.py import RPi.GPIO as GPIO import time import json from datetime import datetime, timedelta import subprocess IN_PIN = 22 OUT_PIN = 4 SCHEDULE_FILE = "/home/pi/schedule.json" GPIO.setmode(GPIO.BCM) GPIO.setup(IN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(OUT_PIN, GPIO.OUT, initial=GPIO.LOW) start_done = False end_done = False trigger_active_until = None PC_IP = "192.168.1.21" # ←PCのIP def is_pc_on(): try: result = subprocess.run( ["ping", "-c", "1", "-W", "1", PC_IP], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) return result.returncode == 0 except: return False def load_schedule(): try: with open(SCHEDULE_FILE) as f: s = json.load(f) start = datetime.strptime(s["start"], "%Y-%m-%d %H:%M:%S") end = datetime.strptime(s["end"], "%Y-%m-%d %H:%M:%S") return start, end except: return None, None def trigger(): global trigger_active_until GPIO.output(OUT_PIN, GPIO.HIGH) trigger_active_until = datetime.now() + timedelta(seconds=1) print("Trigger executed") try: while True: start, end = load_schedule() now = datetime.now() # ★追加:未来スケジュールならリセット if start and now < start: start_done = False if end and now < end: end_done = False # 開始トリガー if start and not start_done and start <= now <= start + timedelta(seconds=30): print("Start trigger") trigger() start_done = True # 終了トリガー if end and not end_done and end <= now <= end + timedelta(seconds=30): print("End trigger") trigger() end_done = True # trigger期間中は監視をスキップ if trigger_active_until and now < trigger_active_until: pass else: if trigger_active_until: GPIO.output(OUT_PIN, GPIO.LOW) trigger_active_until = None # 通常監視 if GPIO.input(IN_PIN) == GPIO.HIGH: GPIO.output(OUT_PIN, GPIO.HIGH) else: GPIO.output(OUT_PIN, GPIO.LOW) time.sleep(1) finally: GPIO.cleanup() ***********gpio_control.service Description=GPIO制御スクリプト After=network.target [Service] ExecStart=/usr/bin/python3 /home/gpio_control.py WorkingDirectory=/home StandardOutput=inherit StandardError=inherit Restart=always User=root [Install] WantedBy=multi-user.target ***********bottle #PC on off schedule SCHEDULE_FILE = "/home/pi/schedule.json" def load_schedule(): if not os.path.exists(SCHEDULE_FILE): return {} with open(SCHEDULE_FILE, "r") as f: return json.load(f) def save_schedule(s): tmp = SCHEDULE_FILE + ".tmp" with open(tmp, "w") as f: json.dump(s, f, indent=2) os.replace(tmp, SCHEDULE_FILE) # 現在のschedule取得 @app.get("/py/schedule") def get_schedule(): response.content_type = "application/json" return load_schedule() # schedule設定 @app.post("/py/schedule") def set_schedule(): data = request.json # 形式チェック&統一 start = datetime.strptime(data["start"], "%Y-%m-%d %H:%M:%S") end = datetime.strptime(data["end"], "%Y-%m-%d %H:%M:%S") schedule = { "start": start.strftime("%Y-%m-%d %H:%M:%S"), "end": end.strftime("%Y-%m-%d %H:%M:%S") } save_schedule(schedule) return {"status": "ok", "schedule": schedule} @app.get("/py/schedule/edit") def schedule_edit(): s = load_schedule() start = s.get("start", "") end = s.get("end", "") return f"""
スケジュール設定
開始:
終了:
""" @app.post("/py/schedule/edit") def schedule_edit_post(): start = request.forms.get("start") end = request.forms.get("end") # datetime-local → json形式へ変換 start = start.replace("T", " ") + ":00" end = end.replace("T", " ") + ":00" schedule = { "start": start, "end": end } save_schedule(schedule) return """ 保存しました
戻る
"""
すでに、22番号Pinをタッチセンサで起動させるようにしていたので、それとの絡みで少し複雑になってしまった。raspiからWeb画面で日時を設定できるようにしてみた。複数セットできると便利かもしれないが、今後、使う機能かどうか、様子見てからにしたいと思う。
0 件のコメント:
コメントを投稿
前の投稿
ホーム
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿