Adafruit mqtt 库中的 mqtt.ping() [英] mqtt.ping() in Adafruit mqtt library

查看:78
本文介绍了Adafruit mqtt 库中的 mqtt.ping()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码来自 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 Keep Alive 是 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 + 50% 的 MQTT_CONN_KEEPALIVE 中没有收到来自客户端的数据或 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆