使用 Python 记录降雨量
Logging rainfall with Python
首先 post,我在这个问题上走投无路了。
(一些背景)
我有一个树莓派 PiZero,我正在用它开发一个气象站,到目前为止,它记录温度、湿度和压力,并将数据发送到 windy.com API。最近我加了一个翻斗式雨量计。
这有 2 条连接到 GPIO 的电线,当水桶倾倒时它会瞬间竞争电路,本质上是按下按钮!
这里的目标是每小时统计一次小费,然后重置。在重置之前,将此数据发送到日志文件 + Windy API。这是我正在努力的部分。
我很擅长 python 但我正处于真正的写作障碍时刻,这是我从片段中拼凑的一个小程序,其中包含测试技巧
/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()
您似乎在 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.
首先 post,我在这个问题上走投无路了。
(一些背景) 我有一个树莓派 PiZero,我正在用它开发一个气象站,到目前为止,它记录温度、湿度和压力,并将数据发送到 windy.com API。最近我加了一个翻斗式雨量计。 这有 2 条连接到 GPIO 的电线,当水桶倾倒时它会瞬间竞争电路,本质上是按下按钮!
这里的目标是每小时统计一次小费,然后重置。在重置之前,将此数据发送到日志文件 + Windy API。这是我正在努力的部分。
我很擅长 python 但我正处于真正的写作障碍时刻,这是我从片段中拼凑的一个小程序,其中包含测试技巧
/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()
您似乎在 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.