# BW16 keepalive Issue

**URL:** <https://forum.amebaiot.com/t/bw16-keepalive-issue/1272>\
**Category:** SDK\
**Tags:** rtl872xd, evb-bw16\
**Created:** [May 26, 2022, 8:48am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272 "2022-05-26T08:48:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlanWL](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@AlanWL](https://forum.amebaiot.com/u/AlanWL)\
**Post date:** [May 26, 2022, 8:48am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/1 "2022-05-26T08:48:29Z")

</div>

Hi,  
我正在使用MQTT相關應用於BW16上，  
然而在測試時，  
keepalive為600s，  
Publish間隔為10s，  
理論上持續Publish下不應該觸發PINGREQ封包，  
但每當過了600s，BW16仍然會發出一PINGREQ封包，並從而導致後續一筆封包的延遲，  
如圖所示，在時間為653554034時觸發了PINGREQ，而後當653554044封包送出後，  
653554054的封包便沒有正常傳出，後續直到下一筆653554064才同時出現，  
不知道是否有人遇過這種問題?  
還是我應該把keepalive設定為0關閉此功能呢  
謝謝。

 ![image](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/2X/8/8071ab386d1e359fbb778dab4df9818983db642a.png)

---

<div class="post-metadata">

**Author:** ![xidameng](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/xidameng/32/18_2.png) [@xidameng](https://forum.amebaiot.com/u/xidameng)\
**Post date:** [June 1, 2022, 3:50am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/2 "2022-06-01T03:50:53Z")

</div>

@daphwl will assist you on this

---

<div class="post-metadata">

**Author:** ![AlanWL](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@AlanWL](https://forum.amebaiot.com/u/AlanWL)\
**Post date:** [June 8, 2022, 9:37am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/3 "2022-06-08T09:37:12Z")

</div>

@xidameng 目前還沒收到提供的幫助，謝謝您。

---

<div class="post-metadata">

**Author:** ![daphwl](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/daphwl/32/756_2.png) [@daphwl](https://forum.amebaiot.com/u/daphwl)\
**Post date:** [June 9, 2022, 1:21am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/4 "2022-06-09T01:21:22Z")

</div>

Hi @AlanWL,

Sorry for the late reply, I’m currently still doing the testing. Will get back to you when I have some results. Thank you.

---

<div class="post-metadata">

**Author:** ![AlanWL](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@AlanWL](https://forum.amebaiot.com/u/AlanWL)\
**Post date:** [July 1, 2022, 1:07am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/5 "2022-07-01T01:07:50Z")

</div>

Hi @daphwl ,

Did you find this problem in testing?  
I hope there will be a solution to reslove it.  
Thank you.

---

<div class="post-metadata">

**Author:** ![daphwl](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/daphwl/32/756_2.png) [@daphwl](https://forum.amebaiot.com/u/daphwl)\
**Post date:** [July 1, 2022, 6:13am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/6 "2022-07-01T06:13:11Z")

</div>

Hi @AlanWL,

After testing, i discovered that the AmebaClient will closed its connection at random intervals, but i didnt see the PINGREQ message. I’m still trying to do more testing to see exactly what cause the disconnection.  
I have also tried setting `keepalive()` to 0, but the disconnection still occured.

---

<div class="post-metadata">

**Author:** ![AlanWL](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@AlanWL](https://forum.amebaiot.com/u/AlanWL)\
**Post date:** [July 1, 2022, 8:55am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/7 "2022-07-01T08:55:54Z")

</div>

Hi @daphwl ,

> [@BW16 - MQTT disconnect issue](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094):
>
> When I turn on Gtimer (4Hz), MQTT will be disconnected within 2-5 minutes. Does anyone have encountered this problem or know how to solve it? Thanks.

The closed issue is as mentioned in my previous article.  
This part really needs to be resolved.  
Please let me know if you have new test results, thanks.

---

<div class="post-metadata">

**Author:** ![daphwl](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/daphwl/32/756_2.png) [@daphwl](https://forum.amebaiot.com/u/daphwl)\
**Post date:** [July 4, 2022, 9:57am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/8 "2022-07-04T09:57:39Z")

</div>

Hi @AlanWL,

I was able to reproduce the PINGREQ packet issue.

**In PubSubClient.cpp**

```auto
        unsigned long t = millis();
        if ((t - lastInActivity > this->keepAlive*1000UL) || (t - lastOutActivity > this->keepAlive*1000UL)) {
            if (pingOutstanding) {
                this->_state = MQTT_CONNECTION_TIMEOUT;
                _client->stop();
                return false;
            } else {
                this->buffer[0] = MQTTPINGREQ;
                this->buffer[1] = 0;
                _client->write(this->buffer,2);
                lastOutActivity = t;
                lastInActivity = t;
                pingOutstanding = true;
            }
        }

```

Since you are only publishing , this only update `lastOutActivity`. `lastInActivity` will only be updated if you receive a packet from the server, which will only happen if you publish with QOS1 or QOS2.

You can either change the publish QOS value or change the code to:

`if ((t - lastInActivity > this->keepAlive*1000UL) && (t - lastOutActivity > this->keepAlive*1000UL))`

---

<div class="post-metadata">

**Author:** ![AlanWL](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@AlanWL](https://forum.amebaiot.com/u/AlanWL)\
**Post date:** [July 5, 2022, 12:47am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/9 "2022-07-05T00:47:40Z")

</div>

Hi @daphwl ,  
Thanks for your help, I’ll test this part again.  
In addition, after modifying this part, will there still be problems of closed its connection?

---

<div class="post-metadata">

**Author:** ![daphwl](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/daphwl/32/756_2.png) [@daphwl](https://forum.amebaiot.com/u/daphwl)\
**Post date:** [July 5, 2022, 1:21am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/10 "2022-07-05T01:21:59Z")

</div>

Yes have, still trying to find out what is causing the connection to closed

---

<div class="post-metadata">

**Author:** ![AlanWL](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@AlanWL](https://forum.amebaiot.com/u/AlanWL)\
**Post date:** [July 5, 2022, 1:41am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/11 "2022-07-05T01:41:03Z")

</div>

@daphwl  
OK.  
if there are new results, please let me know about this again.  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![AlanWL](https://avatars.discourse-cdn.com/v4/letter/a/bbce88/32.png) [@AlanWL](https://forum.amebaiot.com/u/AlanWL)\
**Post date:** [July 5, 2022, 4:28am UTC](https://forum.amebaiot.com/t/bw16-keepalive-issue/1272/12 "2022-07-05T04:28:02Z")

</div>

Hi @daphwl ,  
Here is my disconnect test result:  
When I use mosquitto as MQTT Broker, the problem of random disconnection will occur.  
And when I use [SIoT1.3](https://github.com/vvlink/SIoT) as Broker, the random disconnection problem will not occur.
