玩转树莓派——001 温度监控,微信推送,开机自启动

5月份入手树莓派 raspberry Pi4 8G版本,随手记录一些好玩的

树莓派4性能很强,同时也是发热大户。树莓派内置了一个温度传感器,可以通过python脚本实现温度读取,并在超限时通过server酱来微信推送,及时提醒。

# -*- coding:utf-8 -*-
import requests
import time
import os

# Server酱的推送地址,其中SendKey要换成你自己申请的key
fangtang_url = 'https://sctapi.ftqq.com/[SendKey].send'
data = {"text": "", "desp":""}

while True:
    t = int(open("/sys/class/thermal/thermal_zone0/temp").read())
    if t > 65000:
        desp = os.popen("ps -ef").read()
        data['text'] = f'{time.strftime("%m%d %H:%M:%S")} 温度:{t}'
        data['desp'] = desp.replace("\n","\n\n")
        requests.post(url=fangtang_url, data= data)

    time.sleep(60)

代码核心是读取 /sys/class/thermal/thermal_zone0/temp ,注意读取到的数值除以1000才是温度,比如 39400代表39.4℃,上述代码门限是65000 表示65度

把上述代码存成一个cpu_thermal_monitor.py文件,放在pi目录下

编辑 /etc/rc.local 文件,实现开机自启动

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# 在 exit之前加入下面这句

su pi python3 /root/py/cpu_thermal_monitor/cpu_thermal_monitor.py &

exit 0

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值