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()
readTemp = MessageThread()
readTemp.start()
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()
本文介绍了一个使用Python编写的简单程序,该程序通过多线程定时读取设备的温度信息,并将其记录到日志文件中。读取间隔设置为每分钟一次。
8118

被折叠的 条评论
为什么被折叠?



