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.
When I turn off the Gtimer, the problem of disconnection occurs after about 10 minutes.
I hope someone can help.
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?
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?
Hi @AlanWL
You are using mosquuitto as the broker is it?
Any log from the BW16 side?
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.
Thanks for your prompt reply, will get back to you once there is update
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.
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
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.
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.
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.
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();
}
}
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.
Thanks for letting us know!
I was wondering how come I can’t reproduce your error on my end haha~
@xidameng
I attach my code here. Maybe you can try it.
/*
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();
}
}
Hi @AlanWL,
The source of the disconnection has been found. You can refer to this pull request 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.
Hi @daphwl ,
I will test it soon.
This solution helped me a lot, Thanks for your help!
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.
@daphwl
When I change the publish freq. to 1 Hz, you can see the publish would delay 3s sometimes.