2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ラズパイのSoC温度を定期的にチェックし閾値を超えたらメール通知する

Last updated at Posted at 2022-07-21

vcgencmdで温度をチェックしてsmtlibでメール通知する

vcgencmdとは?

コマンド 内容
get_camera Raspberry Piカメラの有効状態、検出状態を表示
measure_temp Raspberry Pi 4 の内部温度センサーで測定した SoC の温度を返す
measure_clock arm 指定されたクロックの現在の周波数を返す:ARM(CPU)
measure_clock core 指定されたクロックの現在の周波数を返す:CORE(GPU)
  • 温度取得例
$ vcgencmd measure_temp
temp=45.9'C

ユースケース

  • Raspberry Pi4は以前の機種より熱を持つらしいのでcronでvcgencmdをまわし温度チェックを定期実行する
  • Gmailを使ったメール送信は こちら の記事を参考にさせて頂いた
  • crontabで15分毎に定期実行する

モジュール1 [temp_check.py]

temp_check.py
import subprocess
import send_email

# Alert criteria temparature
criteria = 40.0

def temp_check():
    value0 = subprocess.run(['vcgencmd', 'measure_temp'], 
                            stdout=subprocess.PIPE, text=True)
    value1 = value0.stdout.rstrip('\n')
    value2 = value1.split('=')
    temp_data = value2[1][:-2]

    if float(temp_data) >= criteria:
        print(f'[ALERT] Raspi Temp : {temp_data}°C')
        body_message = '[ALERT] Raspi Temp : ' + temp_data + '°C'
        send_email.smtp_email(body_message)
    else:
        print(f'Raspi Temp : {temp_data}°C')


if __name__ == ('__main__'):
    temp_check()

モージュール2 [send_email.py]

send_email.py
from email.utils import formatdate
from email.mime.text import MIMEText
import smtplib

def smtp_email(body_message):
    sendAddress = 'xxx.xxx@gmail.com'
    password = 'passwordpasswprd'

    subject = 'Raspberry Pi Temparature Alert!'
    bodyText = body_message
    fromAddress = 'xxx.xxx@gmail.com'
    toAddress = 'xxx.xxx@gmail.com'

    # Connect to SMTP server
    smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
    smtpobj.starttls()
    smtpobj.login(sendAddress, password)

    # Create an email
    msg = MIMEText(bodyText)
    msg['Subject'] = subject
    msg['From'] = fromAddress
    msg['To'] = toAddress
    msg['Date'] = formatdate()

    # Send an email
    smtpobj.send_message(msg)
    smtpobj.close()


if __name__ == ('__main__'):
    smtp_email('test send!')

crontab

$ crontab -e

*/15 * * * * /home/mashcannu/.venv/bin/python /home/mashcannu/temp_check.py

Alert Email

alert_email.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?