将同一个变量与自身进行比较?
Comparing the same variable against itself?
提前感谢您阅读并花时间与我一起解决此问题!
让我先从重要的事情开始:
目标设备:
- Raspberry Pi4B
- 电子沙龙接力帽
开发环境:
- macOS 卡特琳娜
- PyCharm
- Python 2.7.10
在我家,我有一个 spring 为我家供水。我想出的解决方案是关闭一个阀门,等待大约 12 小时让水恢复原状,以防止恶劣的雨天造成的脏水松动地面土壤进入我的水箱。然后我打开阀门,清水流入我的水箱,为我家提供用水,这个解决方案非常有效。
我最近得出结论,我想用一个常开的螺线管来自动化这个过程。我购买了 Raspberry Pi、中继帽和环境气象站。
我想用 Python 2.7.10 做的是在指定时间后检查同一个变量自身。在这个例子中,我正在检查自身的相对气压,我想寻找该变量的显着负变化。
即“变量 A 有什么?好的,现在等待 3 秒。A 现在有什么?它改变了多少?”
这是我目前总结的,我该如何改进?谢谢。
起初我在想也许我应该用数据绘制图表并比较两个绘图点之间的差异,但我不确定如何使用 Matplotlib。
# This is the executing script for the Smart Valve.
# This project is powered by raspberry pi and 120 angry pixies.
import time,imp
from ambient_api.ambientapi import AmbientAPI
# This code will pull the data from the weather computer
api = AmbientAPI()
devices = api.get_devices()
device = devices[0]
time.sleep(1) #pause for a second to avoid API limits
# The code below is meant for telling the python interpreter to behave normally whether or not it's in a RPi env or a
# developer env
try:
imp.find_module('RPi.GPIO')
import RPi.GPIO as GPIO
except ImportError:
"""
import FakeRPi.GPIO as GPIO
OR
import FakeRPi.RPiO as RPiO
"""
import FakeRPi.GPIO as GPIO
# this code compares the rate of change for the barometric pressure over time and checks if rate is negative
a1 = None
a2 = None
while True:
weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
wind = data[u'windspeedmph']
rain = data[u'hourlyrainin']
a1 = pressure
time.sleep(30)
a2 = pressure
print("A1 is equal to " + str(a1))
print("A2 is equal to " + str(a2))
if a1 > a2:
print("we should close the valve, it'll rain soon")
continue
elif a1 == a2:
print("It's all hunky dory up here!")
break
一般的设计模式是:
oldval = 0
while 1:
newval = read_the_value()
if oldval != newval:
take_action()
oldval = newval
sleep(xxx)
我认为这对你有用。
首先我会避免 Python 2. 2020 年是它的最后一年。现在Python3一路。你也不需要 u'text'
因为一切都是 unicode。
您的代码存在问题,您引用了同一个压力变量两次。
原因如下。等待后你什么都不做改变。它有效:
pressure = 123.4
first = pressure # 123.4
thread.sleep(30)
second = pressure # 123.4
second == first
# True
我会避免一次设置两个测量值。我将使用 previous 和 current 作为名称,因为它比 a1 或 first 更有意义。
没有循环。
prev = None
# Pass 1
weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure # e.g. 123.4
if prev is None or curr > prev:
print("Pressure is rising!")
# Current iteration's current pressure will become the next iteration's previous pressure. You could even then see curr = None because we about to overwrite on next iteration.
prev = curr # 123.4
time.sleep(30)
# Pass 2
weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure # e.g. 234.5
if prev is None or curr > prev:
print("Pressure is rising!")
# 234.5 > 123.4
time.sleep(30)
带循环:
WAIT = 30
prev = None
while True:
weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure
if prev is None or curr > prev:
print("Pressure is rising!")
break
print(f"Nothing usual. Waiting {WAIT} seconds...")
time.sleep(WAIT)
提前感谢您阅读并花时间与我一起解决此问题!
让我先从重要的事情开始:
目标设备:
- Raspberry Pi4B
- 电子沙龙接力帽
开发环境:
- macOS 卡特琳娜
- PyCharm
- Python 2.7.10
在我家,我有一个 spring 为我家供水。我想出的解决方案是关闭一个阀门,等待大约 12 小时让水恢复原状,以防止恶劣的雨天造成的脏水松动地面土壤进入我的水箱。然后我打开阀门,清水流入我的水箱,为我家提供用水,这个解决方案非常有效。
我最近得出结论,我想用一个常开的螺线管来自动化这个过程。我购买了 Raspberry Pi、中继帽和环境气象站。
我想用 Python 2.7.10 做的是在指定时间后检查同一个变量自身。在这个例子中,我正在检查自身的相对气压,我想寻找该变量的显着负变化。
即“变量 A 有什么?好的,现在等待 3 秒。A 现在有什么?它改变了多少?”
这是我目前总结的,我该如何改进?谢谢。
起初我在想也许我应该用数据绘制图表并比较两个绘图点之间的差异,但我不确定如何使用 Matplotlib。
# This is the executing script for the Smart Valve.
# This project is powered by raspberry pi and 120 angry pixies.
import time,imp
from ambient_api.ambientapi import AmbientAPI
# This code will pull the data from the weather computer
api = AmbientAPI()
devices = api.get_devices()
device = devices[0]
time.sleep(1) #pause for a second to avoid API limits
# The code below is meant for telling the python interpreter to behave normally whether or not it's in a RPi env or a
# developer env
try:
imp.find_module('RPi.GPIO')
import RPi.GPIO as GPIO
except ImportError:
"""
import FakeRPi.GPIO as GPIO
OR
import FakeRPi.RPiO as RPiO
"""
import FakeRPi.GPIO as GPIO
# this code compares the rate of change for the barometric pressure over time and checks if rate is negative
a1 = None
a2 = None
while True:
weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
wind = data[u'windspeedmph']
rain = data[u'hourlyrainin']
a1 = pressure
time.sleep(30)
a2 = pressure
print("A1 is equal to " + str(a1))
print("A2 is equal to " + str(a2))
if a1 > a2:
print("we should close the valve, it'll rain soon")
continue
elif a1 == a2:
print("It's all hunky dory up here!")
break
一般的设计模式是:
oldval = 0
while 1:
newval = read_the_value()
if oldval != newval:
take_action()
oldval = newval
sleep(xxx)
我认为这对你有用。
首先我会避免 Python 2. 2020 年是它的最后一年。现在Python3一路。你也不需要 u'text'
因为一切都是 unicode。
您的代码存在问题,您引用了同一个压力变量两次。
原因如下。等待后你什么都不做改变。它有效:
pressure = 123.4
first = pressure # 123.4
thread.sleep(30)
second = pressure # 123.4
second == first
# True
我会避免一次设置两个测量值。我将使用 previous 和 current 作为名称,因为它比 a1 或 first 更有意义。
没有循环。
prev = None
# Pass 1
weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure # e.g. 123.4
if prev is None or curr > prev:
print("Pressure is rising!")
# Current iteration's current pressure will become the next iteration's previous pressure. You could even then see curr = None because we about to overwrite on next iteration.
prev = curr # 123.4
time.sleep(30)
# Pass 2
weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure # e.g. 234.5
if prev is None or curr > prev:
print("Pressure is rising!")
# 234.5 > 123.4
time.sleep(30)
带循环:
WAIT = 30
prev = None
while True:
weatherData = device.get_data()
data = dict(weatherData[0])
pressure = data[u'baromrelin']
curr = pressure
if prev is None or curr > prev:
print("Pressure is rising!")
break
print(f"Nothing usual. Waiting {WAIT} seconds...")
time.sleep(WAIT)