我目前正试图通过MQTT协议发送一条消息,这条消息是有效的。我使用一个树莓派和一个振动传感器作为触发消息发送的手段。在开始的时候,我会触摸传感器,它将一次发送大量的消息,这是我不希望发生的。但现在它检测到一个振动,然后不会检测到另一个振动,但不会停止文件的运行,我可以让它再次检测振动的唯一方法是再次运行文件。
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). 我是不是把这个放在了错误的地方?
你还没有开始MQTT客户端网络循环,所以它将无法接收消息或发送大的MTU的消息。
你也会在保持生命期后被断开连接。
添加 client.loop_start()
临行前 client.connect()
或插入 client.loop()
之后 time.sleep(5000)
在main的循环中,最好让它变成1000,让客户端循环至少每秒运行一次。
在回调中添加睡眠通常是个坏主意,因为它不会阻止事件被触发,只是会延迟它们,因为它们会一直排队直到睡眠时间结束。
你应该在发送MQTT消息后的第一个事件上设置一个标志,然后你可以使用main中的While True循环使其失效。如果一个新的事件进来的时候标志还在设置,那么你就不要再发送MQTT消息了。