0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ラズパイ 赤外線でエアコンの消し忘れを自動で消す、人がいる時にエアコンの暖房自動ONを実現 ジャンプ

Last updated at Posted at 2025-01-10

1号機で設定

1.人が居る時にエアコンの暖房自動ONを実現。 heater_on_check.pyを作成。
2号機での人感センサーで人が検知されたら、1号機で情報を取得して、エアコンをオンにする。heater_on.pyで起動。(11度未満であればエアコンをオンにする。)

!/usr/bin/ python3
from datetime import datetime
import time
import subprocess
import mysql.connector

print(datetime.now().strftime('%Y/%m/%d %H:%M:%S'))

cmd = "sudo python3 /home/pi/heater_on.py"

# db connect
def main():     
# mysql connect 
    cnt = mysql.connector.connect(
    host='miyamodb',
    port='3306',
    db='miyamodb',
    user='miyamo',
    password='miyamoXXXX',
    charset='utf8'
    )

    db = cnt.cursor(buffered=True)   

#room_env select
#    sql = 'select kenchisw from jinkan_kenchi where id = 1'

    db.execute('select kenchisw from jinkan_kenchi where id = 1')

# display & check
    for record in db:
        print('kenchisw=', record[0])
    if record[0] == '1':
        print("heater_ON")
        subprocess.check_call(cmd, shell=True) 

# db.close
    db.close()
# cnt.close
    cnt.close()

if __name__ == "__main__":
    main()

2.仕上げ crontab登録

sudo crontab -e

中身は以下
0 7 * * * cd /home/pi/ && /usr/bin/python3 /home/pi/heater_on_check.py > /tmp/cronh.log 2>&1

完成

せっかく図書館で借りた、動画編集の本、読んでいないので、明日から勉強しよう。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?