【问题标题】:Logging rainfall with Python用 Python 记录降雨量
【发布时间】:2020-10-12 11:13:51
【问题描述】:

第一次发帖,我遇到了这个问题。

(一些背景) 我有一个树莓派 PiZero,我正在开发一个气象站,到目前为止它记录温度、湿度和压力,并将数据发送到 windy.com API。最近我添加了一个翻斗式雨量计。 这有 2 根电线连接到 GPIO,当水桶倾斜时,它会立即与电路竞争,基本上是按下按钮!

这里的目标是每小时计算一次小费,然后重新设置。在重置之前,将此数据发送到日志文件 + Windy API。这是我正在努力解决的部分。

我很擅长 python,但我正处于真正的写作障碍时刻,这是我从 sn-ps 拼凑而成的一个小程序,它计算了测试技巧

/usr/bin/python3
import requests
from gpiozero import Button
import time

rain_sensor = Button(27)
bucket_size = 0.2794
count = 0

    def bucket_tipped():
    global count
    count = count + 1
    print(count * bucket_size)

def reset_rainfall():
    global count
    count = 0


#display and log results
def timed_loop():
reset_rainfall
timeout = time.monotonic() + 3600   # 1 hour from now
while True:
    if time.monotonic() > timeout:  # break if timeout time is reached
        rain_sensor.when_pressed = bucket_tipped
        time.sleep(1)          # Short sleep so loop can be interupted
        continue
print count


  # close the log file and exit nicely
 GPIO.cleanup()

【问题讨论】:

  • 您的代码每 5 秒只记录/打印一次。 GPI add_event_detect 可能会增加,但您每 5 秒只记录一次。我不确定的一件事是“按钮按下”是如何工作的。可能是您正在监听一个从未发生过的事件。如果 add_event_detect 回调从未发生,那么您将每 5 秒记录一次 0。
  • 谢谢,如果我在 t 运行时手动操作存储桶,我会收到一个值。然而,它然后重置回 0。我如何在特定的人身上累积这个然后重置?
  • 我建议不要预先计算每小时总和或类似的东西。我建议存储原始数据并从该数据中得出降雨率。您可以使用带有 groupby 的 pandas 来计算每小时小费。

标签: python weather raspberry-pi4 raspberry-pi-zero


【解决方案1】:

您似乎在while True: 循环中不断将rain 设置为0

编辑: 为你的循环尝试这样的事情。

def timed_loop():
    rain = 0
    timeout = time.monotonic() + 3600   # 1 hour from now
    while True:
        if time.monotonic() > timeout:  # break if timeout time is reached
            # You place your code here that you want to run every hour. 
            # After that the loop restarts
            rain = 1  
            time.sleep(1)          # Short sleep so loop can be interupted
            continue

编辑 3:

使用以下代码,您可以记录指定时间内的按钮按下情况。

import time

def bucket_tip_counter():
    recording_time_timeout = 3600  # Amount of seconds you want to have the timer run
    recording_time = time.monotonic() + recording_time_timeout
    button_timeout = 1  # This timeout is here so the button doesnt trigger the count more then once for each trigger
                        # You have to modify this to your needs. If the button stays activated for a few seconds you need to set the timer accordingly.
    count = 0           # Sets the counter to 0 at the start
    button = 0          # Here you need to replace the 0 with the GPIO pin that returns True if the button is pressed
    while True:         # starts the loop

        if button:         # if button gets pressed/bucket tipped
            count += 1     # up count by one
            time.sleep(button_timeout)  # wait specified timeout to make sure button isnt pressed anymore

        if time.monotonic() > recording_time:  # If the recording_time is reached the loop triggers this if condition
            print(count)   # print count
                           # Here you can also place code that you want to run when the hour is over
                           # Note that the counter wont start back up until that code is finished.

            count = 0      # set count back to 0
            recording_time = time.monotonic() + recording_time_timeout  # Set a new timer so the hour can start anew
            continue   # restart the loop

        time.sleep(1)  # small sleep to stop CPU hogging.

【讨论】:

  • 谢谢,有没有办法在循环之外重置它。例如,中断 1 小时后循环重置并再次执行?
  • 完美,这看起来是我所追求的。所以在 > timeout 之后运行我的代码块:所以就在 timed_loop() 发送数据到日志之前,它会重新运行?
  • 是的。 if 条件中的所有内容都在设定的时间之后运行,在这种情况下,每小时运行一次,然后重新开始。
  • 谢谢,我会试一试并报告。
  • 到目前为止我有这个,到目前为止似乎只是立即打破循环而不是计时。 def timed_loop(): reset_rainfall timeout = time.monotonic() + 3600 # 1 hour from now while True: if time.monotonic() > timeout: # break if timeout time is reached rain_sensor.when_pressed = bucket_tipped time.sleep(1) # Short sleep so loop can be interupted continue print count
猜你喜欢
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
相关资源
最近更新 更多