【发布时间】:2020-05-01 15:27:30
【问题描述】:
我目前正在尝试通过有效的 MQTT 协议发送消息。我使用树莓派和振动传感器作为触发消息发送的手段。一开始我会触摸传感器,它会立即发送大量消息,这是我不希望发生的。所以我试着让它在检测到振动后休眠 5 秒钟。但是现在它检测到一种振动,然后不会检测到另一种振动,但不会停止文件的运行。我能让它再次检测到振动的唯一方法是再次运行文件。这是我尝试的两种方法:
import time
from grove.gpio import GPIO
import paho.mqtt.client as mqttClient
class GrovePiezoVibrationSensor(GPIO):
def __init__(self, pin):
super(GrovePiezoVibrationSensor, self).__init__(pin, GPIO.IN)
self._on_detect = None
@property
def on_detect(self):
return self._on_detect
@on_detect.setter
def on_detect(self, callback):
if not callable(callback):
return
if self.on_event is None:
self.on_event = self._handle_event
self._on_detect = callback
def _handle_event(self, pin, value):
if value:
if callable(self._on_detect):
self._on_detect()
time.sleep(5000)
Grove = GrovePiezoVibrationSensor
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected #Use global variable
Connected = True #Signal connection
else:
print("Connection failed")
Connected = False #global variable for the state of the connection
broker_address= "hairdresser.cloudmqtt.com"
port = 15767
user = "kprpjfue"
password = "1fIq2_CIwHZj"
client = mqttClient.Client("Python") #create new instance
client.username_pw_set(user, password=password) #set username and password
client.on_connect= on_connect
client.loop_start()
client.connect(broker_address, port=port)
def main():
from grove.helper import SlotHelper
sh = SlotHelper(SlotHelper.GPIO)
pin = sh.argv2pin()
pir = GrovePiezoVibrationSensor(pin)
def callback():
print('Detected.')
value = 'detected'
client.publish("sensor/Temp", value)
pir.on_detect = callback
while True:
time.sleep(5000)
if __name__ == '__main__':
main()
while Connected != True: #Wait for connection
time.sleep(0.1)
import time
from grove.gpio import GPIO
import paho.mqtt.client as mqttClient
class GrovePiezoVibrationSensor(GPIO):
def __init__(self, pin):
super(GrovePiezoVibrationSensor, self).__init__(pin, GPIO.IN)
self._on_detect = None
@property
def on_detect(self):
return self._on_detect
@on_detect.setter
def on_detect(self, callback):
if not callable(callback):
return
if self.on_event is None:
self.on_event = self._handle_event
self._on_detect = callback
def _handle_event(self, pin, value):
if value:
if callable(self._on_detect):
self._on_detect()
time.sleep(5000)
Grove = GrovePiezoVibrationSensor
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected #Use global variable
Connected = True #Signal connection
else:
print("Connection failed")
Connected = False #global variable for the state of the connection
broker_address= "hairdresser.cloudmqtt.com"
port = 15767
user = "kprpjfue"
password = "1fIq2_CIwHZj"
client = mqttClient.Client("Python") #create new instance
client.username_pw_set(user, password=password) #set username and password
client.on_connect= on_connect
client.connect(broker_address, port=port)
def main():
from grove.helper import SlotHelper
sh = SlotHelper(SlotHelper.GPIO)
pin = sh.argv2pin()
pir = GrovePiezoVibrationSensor(pin)
def callback():
print('Detected.')
value = 'detected'
client.publish("sensor/Temp", value)
pir.on_detect = callback
while True:
time.sleep(5000)
client.loop()
if __name__ == '__main__':
main()
while Connected != True: #Wait for connection
time.sleep(0.1)
正如您在句柄事件方法中的 if callable 下看到的,我告诉过我说的是 time.sleep(5000)。我是不是放错地方了?
【问题讨论】:
-
不,你应该有一个或另一个。
client.loop_start()或client.loop(),不能同时使用。 -
它们似乎都不起作用。我更新了我的问题,以确保我做的都是正确的。
-
无论系统的其他部分是否按照您的意愿行事,您都需要其中之一才能让您的 MQTT 客户端正常工作。
标签: python raspberry-pi mqtt sensors