# BW16 - MQTT disconnect issue

**URL:** <https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094>\
**Category:** Peripherals\
**Tags:** evb-bw16\
**Created:** [February 14, 2022, 3:31am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094 "2022-02-14T03:31:14Z")\
**Posts on this page:** 20\
**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:** [February 14, 2022, 3:31am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/1 "2022-02-14T03:31:15Z")

</div>

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.

---

<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:** [February 14, 2022, 3:35am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/2 "2022-02-14T03:35:44Z")

</div>

When I turn off the Gtimer, the problem of disconnection occurs after about 10 minutes.  
I hope someone can help.

---

<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:** [February 14, 2022, 3:45am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/3 "2022-02-14T03:45:29Z")

</div>

Hi @AlanWL

Can you provide the log? like what is the error code from the `PubSubClient` ?

And btw, you are using the latest Arduino SDK version right?

---

<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:** [February 14, 2022, 4:34am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/4 "2022-02-14T04:34:47Z")

</div>

Hi @xidameng  
It didnt give the error code. In my project, I will check the client status before publish the message.  
The current problem is that MQTT will disconnect once in about 4-15 minutes.  
And the Broker sees is that the Client is disconnected, so it can be ruled out the problem is the Broker.  
I’m using the 3.1.1 Version. Could it be caused by the this version?

---

<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:** [February 14, 2022, 5:53am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/5 "2022-02-14T05:53:47Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/1X/f454f9b30f0560fb5836dd9ffca8729ba88b0861.png)  
As shown in the picture, I will be disconnect once in about 4-10 minutes.

---

<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:** [February 14, 2022, 6:16am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/6 "2022-02-14T06:16:29Z")

</div>

Hi @AlanWL  
You are using mosquuitto as the broker is it?  
Any log from the BW16 side?

---

<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:** [February 14, 2022, 6:38am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/7 "2022-02-14T06:38:21Z")

</div>

Hi @xidameng  
Yes, I use the mosquitto as the broker. I still try the other public broker, the problem still have.  
When MQTT disconnect, I didnt see any log in LOG\_UART.

---

<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:** [February 14, 2022, 6:41am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/8 "2022-02-14T06:41:39Z")

</div>

Thanks for your prompt reply, will get back to you once there is update

---

<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:** [February 14, 2022, 8:09am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/9 "2022-02-14T08:09:21Z")

</div>

Hi @xidameng  
I try to remove the client.loop() in my loop(), then the disconnect problem was sloved.  
But the other problem is that the message sometimes accumulates several before being sent at one time, as the picture shown.  
This information is provided for you and may be corrected this function in the future.  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/1X/d873c2c0b0864d91dad1f014cb2f3d5dbf4eaa34.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:** [February 14, 2022, 9:40am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/10 "2022-02-14T09:40:41Z")

</div>

Hi @AlanWL

It’s generally NOT recommended to the comment off the `client.loop()` as this function does more than just **looping** the function, it help the client to stay connected and also check for any abnormallies. However, it’s still unclear as to which part of the network stack caused the problem, it could be from the MQTT lib, or the TCP connection

---

<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:** [February 15, 2022, 12:09am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/11 "2022-02-15T00:09:05Z")

</div>

Hi @xidameng  
OK, Thanks for your suggestion. I will try to find which part caused the problem.  
If you have any update, please let me know. 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:** [February 18, 2022, 6:16am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/12 "2022-02-18T06:16:28Z")

</div>

Hi @xidameng  
I still can’t solve this problem.  
The time of each disconnect is not regular.  
And before each disconnection, it will stop for about 3 seconds just like the picture shown.  
If anyone else has this problem?  
I hope this issue can be resolved as soon as possible so that my project can progress.  
Thanks.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/1X/0fddd642944e82c29677d48e1a42cf169ef7d429.png)

---

<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:** [February 18, 2022, 8:09am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/13 "2022-02-18T08:09:58Z")

</div>

Hi @xidameng  
This program is a test program I modified with the MQTT example, and it also has the problem of disconnection.  
When client.loop() is moved out of if(TimerTick), it will even cause continuous disconnection.  
Maybe you can help to see what’s wrong, I’d appreciate it.

```auto
uint8_t TimerTick = 0;
uint8_t TimerTick2 = 0;
void timer_tick(uint32_t data) {
  TimerTick = 1;
  mySerial.write(0xF5);
}
void timer_tick2(uint32_t data) {
  TimerTick2 = 1;
}

void setup() {
    mySerial.begin(1000000);

    while (status != WL_CONNECTED) {
        mySerial.print("Attempting to connect to SSID: ");
        mySerial.println(ssid);
        // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
        status = WiFi.begin(ssid, pass);

        // wait 10 seconds for connection:
        delay(10000);
    }

    client.setServer(mqttServer, 1883);
    client.setCallback(callback);
    
    GTimer.begin(0, (3 * 100 * 1000), timer_tick);
    GTimer.begin(1, (1 * 250 * 1000), timer_tick2);
    // Allow the hardware to sort itself out
    delay(1500);
}

void loop() {
    
    if (!(client.connected())) {
        reconnect();
    }
    
    if(TimerTick){
      TimerTick = 0;
      client.publish(publishTopic, publishPayload);
      client.loop();
    }
}

```

---

<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:** [March 3, 2022, 5:45am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/14 "2022-03-03T05:45:27Z")

</div>

hi @xidameng  
I found a reason for the disconnection.  
When using UART and MQTT in the program at the same time, it will lead to irregular disconnection.  
I hope there will be a solution to make it work.  
Thank you.

![image](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/1X/ba2310d9d8c4e1a5792803233aaa09cc98ca7d93.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:** [March 3, 2022, 7:25am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/15 "2022-03-03T07:25:20Z")

</div>

Thanks for letting us know!

I was wondering how come I can’t reproduce your error on my end haha~

---

<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:** [March 3, 2022, 8:02am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/16 "2022-03-03T08:02:11Z")

</div>

@xidameng  
I attach my code here. Maybe you can try it.

```auto
/*
 Basic MQTT example

 This sketch demonstrates the basic capabilities of the library.
 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.
 
*/

#include <WiFi.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>

// Update these with values suitable for your network.

char ssid[] = "CJS_5G"; // your network SSID (name)
char pass[] = "77777777"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

char mqttServer[] = "192.168.137.1";
char clientId[] = "amebaClient";
char publishTopic[] = "outTopic";
char publishPayload[] = "hello world";
char subscribeTopic[] = "inTopic";

/* UART */
SoftwareSerial mySerial(PB2, PB1); // RX, TX

void callback(char* topic, byte* payload, unsigned int length) {
    mySerial.print("Message arrived [");
    mySerial.print(topic);
    mySerial.print("] ");
    for (int i = 0; i < length; i++) {
        mySerial.print((char)(payload[i]));
    }
    mySerial.println();
}

WiFiClient wifiClient;
PubSubClient client(wifiClient);

void reconnect() {
    // Loop until we're reconnected
    while (!(client.connected())) {
        mySerial.print("Attempting MQTT connection...");
        // Attempt to connect
        if (client.connect(clientId)) {
            mySerial.println("connected");
        } else {
            mySerial.print("failed, rc=");
            mySerial.print(client.state());
            mySerial.println(" try again in 5 seconds");
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}

void setup() {
    mySerial.begin(1000000);

    while (status != WL_CONNECTED) {
        mySerial.print("Attempting to connect to SSID: ");
        mySerial.println(ssid);
        // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
        status = WiFi.begin(ssid, pass);

        // wait 10 seconds for connection:
        delay(10000);
    }

    client.setServer(mqttServer, 1883);
    client.setCallback(callback);

    // Allow the hardware to sort itself out
    delay(1500);
}
long nowTime = 0;
long lastTime = 0;
void loop() {
    nowTime = millis();
    if (nowTime > (lastTime + 200))
    {
      mySerial.write(0x84);
      client.publish(publishTopic, publishPayload);
      lastTime = nowTime;
    }
    client.loop();
    if (!(client.connected())) {
        reconnect();
    }
}

```

---

<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 19, 2022, 6:14am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/17 "2022-07-19T06:14:24Z")

</div>

Hi @AlanWL,

The source of the disconnection has been found. You can refer to this [pull request](https://github.com/ambiot/ambd_arduino/pull/101) for the solution.

I have also tested it with different brokers (mosquitto, SIoT and HiveMQ) and the disconnection problem didn’t happen after making the changes.

---

<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 20, 2022, 1:28am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/18 "2022-07-20T01:28:46Z")

</div>

Hi @daphwl ,  
I will test it soon.  
This solution helped me a lot, Thanks for your help!

---

<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 28, 2022, 6:29am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/19 "2022-07-28T06:29:54Z")

</div>

Hi @daphwl ,  
I find the other problem.  
When I call client.loop() in main loop, sometimes I will get the 2~3s blocking.  
I don’t know if this has happened to you.  
I have a delay of around 5-15 seconds per minute with the publish frequency of 10Hz as the picture shown.  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/2X/c/c421c74c872d873768cabab03e11d5e71d4dd989.png)

---

<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 28, 2022, 7:32am UTC](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094/20 "2022-07-28T07:32:19Z")

</div>

@daphwl  
When I change the publish freq. to 1 Hz, you can see the publish would delay 3s sometimes.

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

[Next page](https://forum.amebaiot.com/t/bw16-mqtt-disconnect-issue/1094.md?page=2)
