Adafruit mqtt库中的mqtt.ping() [英] mqtt.ping() in Adafruit mqtt library
问题描述
以下代码来自Adafruit Mqtt文档:
Following code comes form the Adafruit Mqtt documentation:
// Adjust as necessary, in seconds. Default to 5 minutes (300 seconds).
#define MQTT_CONN_KEEPALIVE 300
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
if(! mqtt.ping()) {
mqtt.disconnect();
}
"MQTT_CONN_KEEPALIVE"实际上是做什么的?我无法弄清楚..如果我按上面所示的那样编写代码并将其放入循环中,则ping将不断执行,并且所有数据包都将被拒绝...我期望在ping中使用MQTT_CONN_KEEPALIVE变量()函数仅在经过300秒后才执行ping操作,但id似乎不是这样.我应该如何编写代码以便每隔几分钟仅ping一次?
What does the "MQTT_CONN_KEEPALIVE" actually do? I cannot figure it out.. If i write the code as shown above here and put it in my loop, then the ping is executed constantly, and all packets are rejected... I was expecting that the MQTT_CONN_KEEPALIVE variable is used in the ping() function to execute the ping only if the 300 seconds have passed, but id does not seem to be like that. How am I supposed to write the code in order to ping only once every few minutes?
推荐答案
MQTT保持活动状态是MQTT协议的一部分,用于维护代理与客户端之间的连接.您可以在
MQTT Keep Alive is part of MQTT protocol to maintain a connection between broker and clients. You can read more about it in the documentation.
MQTT使用通常由客户端保持打开状态的TCP/IP连接,以便可以随时发送和接收数据.为了检测连接失败,MQTT使用ping系统,如果在一定时间(KeepAlive)之后没有发送消息,则它会以预定的时间间隔将消息发送到代理.
MQTT uses a TCP/IP connection that is normally left open by the client so that is can send and receive data at any time. To detect a connection failure MQTT uses a ping system where it sends messages to the broker at a pre-determined interval if no messages have been sent after a certain period (KeepAlive).
特定于Adafruit_MQTT的实现,如果您发布数据并且确定要在MQTT_CONN_KEEPALIVE设置的时间段内发布数据,那么您就很好了.
Specific to Adafruit_MQTT implementation, if you publish data and you are sure that you will publish data within the time period set by MQTT_CONN_KEEPALIVE, then you are good to go.
如果服务器/经纪人未在MQTT_CONN_KEEPALIVE + MQTT_CONN_KEEPALIVE的额外50%范围内从客户端接收数据或PINGREQ,则代理将断开与网络的连接(超时),并且客户端将必须重新建立连接
If the server/broker didn't receive data or PINGREQ from client within the MQTT_CONN_KEEPALIVE + an extra of 50% of MQTT_CONN_KEEPALIVE, the broker will disconnect from the network(timeout) and the client will have to re-establish the connection.
因此,如果MQTT客户端仅订阅主题而不发布,则该客户端必须在每MQTT_CONN_KEEPALIVE秒内至少将ping(PINGREQ)发送给代理一次.但是,您不想持续对服务器执行ping操作.一种解决方法是仅每MQTT_CONN_KEEPALIVE秒发送一次mqtt.ping().
So if a MQTT client only subscribe to a topic without publishing, the client then must send the ping (PINGREQ) to the broker at least once in every MQTT_CONN_KEEPALIVE sec. However, you don't want to constantly ping the server. One way of doing it is only send the mqtt.ping() every MQTT_CONN_KEEPALIVE sec.
#define MQTT_KEEP_ALIVE 300
unsigned long previousTime = 0;
loop() {
if((millis() - previousTime) > MQTT_KEEP_ALIVE * 1000) {
previousTime = millis();
if(! mqtt.ping()) {
mqtt.disconnect();
}
}
// do something else
}
这篇关于Adafruit mqtt库中的mqtt.ping()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!