【问题标题】:Strange failure while logging CPU-Temp with Python to a .csv-File (Rasbperri-Pi)使用 Python 将 CPU-Temp 记录到 .csv 文件 (Raspberry-Pi) 时出现奇怪的故障
【发布时间】:2022-01-15 21:11:18
【问题描述】:

我尝试用风扇设置我的树莓派。它应该每 5 分钟记录一次温度,并使用 Python 脚本将其写入 .csv 文件。如果温度高于 52 摄氏度,则应打开 USB 集线器,如果低于该值则将其关闭。现在,我创建了一个小网站,以查看我记录的数据。这是我的问题:正如您在屏幕截图中看到的那样,它有时会告诉我 [HOT] 有时 [OK] 对于相同的值?此外,它不会像希望它分离(超过/低于 52 摄氏度)那样分离数据。

Screenshot from my little website(这只显示我的 .csv 文件)

我的 .py 代码:

from gpiozero import CPUTemperature
from time import sleep, strftime, time
from datetime import datetime, timedelta
from threading import Timer
import matplotlib.pyplot as plt

v=datetime.today()
w = v.replace(day=v.day, hour=1, minute=0, second=0, microsecond=0) + timedelta(days=1)
delta_t=w-v

secs=delta_t.total_seconds()

cpu = CPUTemperature()

plt.ion()
x = []
y = []

def write_tempHot(temp):
        with open("/var/www/html/cpuTemp.csv", "a") as log:
                log.write("{0}{1}\n".format(strftime("%Y-%m-%d %H:%M:%S = [HOT] "),str(temp)))


def write_tempLow(temp):
        with open("/var/www/html/cpuTemp.csv", "a") as log:
                log.write("{0}{1}\n".format(strftime("%Y-%m-%d %H:%M:%S = [OK] "),str(temp)))



def clearTemp():
 filename = "/var/www/html/cpuTemp.csv"
        # opening the file with w+ mode truncates the file
 f = open(filename, "w+")
 f.close()

while True:
 temp = cpu.temperature
 if temp > 52:
  temp = cpu.temperature
  write_tempHot(temp)
  plt.pause(300)
  t = Timer(secs, clearTemp)
  t.start()
 else:
  temp = cpu.temperature
  write_tempLow(temp)
  plt.pause(300)
  t = Timer(secs, clearTemp)
  t.start()

(也不介意 clearTemp() 函数,我想每天清除一次 .csv 文件)

有人不知道,为什么结果会那么奇怪? 问候和非常感谢!

【问题讨论】:

    标签: python csv while-loop raspberry-pi temperature


    【解决方案1】:

    你的比较是

    if temp > 52
    

    应该是什么时候

    if temp >= 52.0
    

    因为否则你的温度 Hi 只有在 53C 或以上时才会匹配。

    我也会使用time.sleep() 而不是plt.pause()

    对于你的日志文件,你有两个函数可以写入你的日志文件,我会用一个来代替:

    
    def write_log(temp):
        fmt = """{when} = [{option}] {temp}"""
        datefmt = """%Y-%m-%d %H:%M:%S"""
        option = "OK"
        with open("/var/www/html/cpuTemp.csv", "a") as log:
            when = datetime.now().strftime(datefmt)
            if temp >= 52.0:
                option = "HOT"
            log.write(fmt.format(when=when, option=option, temp=temp))
    
    

    最后,我不明白你为什么要每 5 分钟截断一次日志文件。

    【讨论】:

    • 嘿,非常感谢!我会直接尝试这个解决方案!和截断:我不想每 5 分钟做一次,但它是我现在找到的唯一解决方案。但是这些天我会尝试不同的解决方案:)!你有一个想法如何轻松清除文件?
    • 忽略“你为什么要这样做”来截断日志文件,如何旋转文件名呢?以 5 分钟的间隔进行采样,一天可以获得 288 行。您可以更新代码以检查日期编号是否已更改,然后写入新文件名,可能会附加日期戳。或者只是不用担心。
    • 嘿,只是为了让您了解最新情况:我删除了 clear-function 并在网站上创建了一个按钮,这会在我的 .php-website 上打开一个 shellcommand 来删除文件。因此,如果免费内存越来越少,我可以手动执行 :) 我还添加了风扇并在 pythonscript 中添加了一个 shell 命令,它可以在您创建的“选项”值旁边激活和停用风扇。我用 uhubctl 做了这个。非常感谢您的支持 :) 如果有人对最终结果感兴趣,我可以将其上传到 github,只需给我发短信
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2020-12-11
    • 2018-04-19
    • 2020-06-16
    • 2014-01-20
    相关资源
    最近更新 更多