python 有没有办法检查值的变化然后触发一个函数?
is there a way for python to check for value change and then trigger a function?
我正在做一个学校项目,该项目使用 raspberry pi 模型 3b 和 dht11 来测量温度。我希望仅当温度值增加或减少时将温度数据发送到云服务(Beebotte)。我需要为此使用观察者模式吗?下面是我学校项目的代码:
import pygame
import Adafruit_DHT #Import DHT Library for sensor
import time
import datetime
from time import sleep, strftime
import Adafruit_CharLCD as LCD
import requests
from beebotte import *
_accesskey = 'My accesskey'
_secretkey = 'My secretkey'
bbt = BBT( _accesskey, _secretkey)
GPIO.setwarnings(False)
lcd_rs = 27
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_backlight = 4
lcd_columns = 16
lcd_rows = 2
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
ReedSwitchPin = 17
LED = 21
sensor_name = Adafruit_DHT.DHT11 #we are using the DHT11 sensor
sensor_pin = 16 #The sensor is connected to GPIO16 on Pi
RawValue = 0
timer = time.time()
#buzzer = 5
#Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(ReedSwitchPin, GPIO.IN)
GPIO.setup(LED, GPIO.OUT)
#GPIO.setup(buzzer,GPIO.OUT)
#SFX
pygame.init()
pygame.mixer.init()
Alarm = pygame.mixer.Sound("/home/pi/python_games/bomb.wav")
try:
while True:
RawValue = GPIO.input(ReedSwitchPin)
if (RawValue == 0):
lcd.clear() #Clear the LCD screen
GPIO.output(LED,True)
if time.time() - timer > 30:
def telegram_bot_sendtext(bot_message):
bot_token = 'my bot_token' #token that can be generated talking with @BotFather on telegram
bot_chatID = 'my bot_chatID'
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
GPIO.output(LED,True)
Alarm.play()
test = telegram_bot_sendtext("The refrigerator door is left open! Please close it immediately") # the message to send
else:
GPIO.output(LED, False)
Alarm.stop()
humidity, temperature = Adafruit_DHT.read_retry(sensor_name, sensor_pin) #read from sensor & save respective values in temperature and humidity variable
lcd.clear() #Clear the LCD screen
lcd.message ('Temp = %.1f C' % temperature) # Display the value of temperature
previous_temperature = None
while True:
if temperature != previous_temperature:
bbt.write("MonitoringTemperature", "Temperature", temperature)
previous_temperature = temperature
break
# 0 is off, 1 is activated by magnet
#print(" ,RawValue:",RawValue)
time.sleep(1)
except KeyboardInterrupt:
pass
print("Exiting Reed Switch Test ...")
GPIO.cleanup()
您可以使用 previous_temperature
变量来检查值是否已更改:
previous_temperature = None
while True:
temperature = input("Give me the new temperature :")
if temperature != previous_temperature:
print("trigger something !")
previous_temperature = temperature
** 编辑 **
在我的示例中,我使用 input()
来模拟您的新温度,但在您的情况下,您使用 humidity, temperature = Adafruit_DHT.read_retry(...)
。所以你必须把这段代码放在 while 循环中。我还添加了一个 time.sleep()
以便系统在读取新温度值之前等待 10 秒。
试试这个:
else:
GPIO.output(LED, False)
Alarm.stop()
lcd.clear() #Clear the LCD screen
previous_temperature = None
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor_name, sensor_pin) #read from sensor & save respective values in temperature and humidity variable
lcd.message ('Temp = %.1f C' % temperature) # Display the value of temperature
if temperature != previous_temperature:
bbt.write("MonitoringTemperature", "Temperature", temperature)
break
previous_temperature = temperature
time.sleep(10) # wait 10 seconds before reading the new temperature
我正在做一个学校项目,该项目使用 raspberry pi 模型 3b 和 dht11 来测量温度。我希望仅当温度值增加或减少时将温度数据发送到云服务(Beebotte)。我需要为此使用观察者模式吗?下面是我学校项目的代码:
import pygame
import Adafruit_DHT #Import DHT Library for sensor
import time
import datetime
from time import sleep, strftime
import Adafruit_CharLCD as LCD
import requests
from beebotte import *
_accesskey = 'My accesskey'
_secretkey = 'My secretkey'
bbt = BBT( _accesskey, _secretkey)
GPIO.setwarnings(False)
lcd_rs = 27
lcd_en = 22
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18
lcd_backlight = 4
lcd_columns = 16
lcd_rows = 2
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
ReedSwitchPin = 17
LED = 21
sensor_name = Adafruit_DHT.DHT11 #we are using the DHT11 sensor
sensor_pin = 16 #The sensor is connected to GPIO16 on Pi
RawValue = 0
timer = time.time()
#buzzer = 5
#Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(ReedSwitchPin, GPIO.IN)
GPIO.setup(LED, GPIO.OUT)
#GPIO.setup(buzzer,GPIO.OUT)
#SFX
pygame.init()
pygame.mixer.init()
Alarm = pygame.mixer.Sound("/home/pi/python_games/bomb.wav")
try:
while True:
RawValue = GPIO.input(ReedSwitchPin)
if (RawValue == 0):
lcd.clear() #Clear the LCD screen
GPIO.output(LED,True)
if time.time() - timer > 30:
def telegram_bot_sendtext(bot_message):
bot_token = 'my bot_token' #token that can be generated talking with @BotFather on telegram
bot_chatID = 'my bot_chatID'
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
GPIO.output(LED,True)
Alarm.play()
test = telegram_bot_sendtext("The refrigerator door is left open! Please close it immediately") # the message to send
else:
GPIO.output(LED, False)
Alarm.stop()
humidity, temperature = Adafruit_DHT.read_retry(sensor_name, sensor_pin) #read from sensor & save respective values in temperature and humidity variable
lcd.clear() #Clear the LCD screen
lcd.message ('Temp = %.1f C' % temperature) # Display the value of temperature
previous_temperature = None
while True:
if temperature != previous_temperature:
bbt.write("MonitoringTemperature", "Temperature", temperature)
previous_temperature = temperature
break
# 0 is off, 1 is activated by magnet
#print(" ,RawValue:",RawValue)
time.sleep(1)
except KeyboardInterrupt:
pass
print("Exiting Reed Switch Test ...")
GPIO.cleanup()
您可以使用 previous_temperature
变量来检查值是否已更改:
previous_temperature = None
while True:
temperature = input("Give me the new temperature :")
if temperature != previous_temperature:
print("trigger something !")
previous_temperature = temperature
** 编辑 **
在我的示例中,我使用 input()
来模拟您的新温度,但在您的情况下,您使用 humidity, temperature = Adafruit_DHT.read_retry(...)
。所以你必须把这段代码放在 while 循环中。我还添加了一个 time.sleep()
以便系统在读取新温度值之前等待 10 秒。
试试这个:
else:
GPIO.output(LED, False)
Alarm.stop()
lcd.clear() #Clear the LCD screen
previous_temperature = None
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor_name, sensor_pin) #read from sensor & save respective values in temperature and humidity variable
lcd.message ('Temp = %.1f C' % temperature) # Display the value of temperature
if temperature != previous_temperature:
bbt.write("MonitoringTemperature", "Temperature", temperature)
break
previous_temperature = temperature
time.sleep(10) # wait 10 seconds before reading the new temperature