树莓派 实时温度监控

from threading import Thread
import time

class MessageThread(Thread):
    def run(self):
        while True:
            time.sleep(60)
            ReadTemp()

def ReadTemp():
    file = open("/sys/class/thermal/thermal_zone0/temp")
    # 读取结果,并转换为浮点数
    s = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 获取格式化的当前时间
    temp = float(file.read()) / 1000
    log_file = open('/home/pi/Desktop/temperature/temp_log',"a+")
    log_file.write(s + ' ' + str(temp) + '\n')
    # 关闭文件
    log_file.close()

    file.close()

if __name__ == "__main__":
    readTemp = MessageThread()
    readTemp.start()

猜你喜欢

转载自blog.csdn.net/alolf/article/details/80897846