martin-ger / uMQTTBroker

MQTT Broker library for ESP8266 Arduino
MIT License
442 stars 105 forks source link

ECONNRESET (Connection reset by peer) #5

Open mree58 opened 6 years ago

mree58 commented 6 years ago

Thanks for this great work firstly, but I have some problem with it. Let me try to explain it please. I can connect successfully to the broker, and with latest release I can successfully disconnect I suppose. But after a couple of connect-disconnect sequence (usually 4-6) ESP is not accepts new connections. It throws this immediatelly when I try to connect :

java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

Until I restart (power off then on) the ESP, I cannot connect to the mqtt broker from any client on any platform

using the latest commit of this library as a broker

using the paho.mqtt.android as a client (tried on other platforms as well, client is not problem I think)

martin-ger commented 6 years ago

Cannot reproduce the error. Using mosquitto_sub and mosquitto_pub I can connect and disconnect (from my laptop) without any obvious problem.

What exactly is your test-setup with paho.mqtt.android (or another client with problems)?

mree58 commented 6 years ago

Thanks for your reply. I builded the example project that they provided (only disconnect() function called on onDestroy method, ip and port changed also ofcourse) :

https://github.com/eclipse/paho.mqtt.android/tree/master/paho.mqtt.android.example/src/main/java/paho/mqtt/java/example

The log output from client side was written in first message. I can record video or prepare sample mobile app for you in case you need.

MasyaShu commented 6 years ago

Hello. I also encountered such a problem. uMQTTBroker is installed on one ESP. To another PubSubClient. If the client connects to the server for the first time, then everything is in order. When the client is rebooted, the connection returns an error: -4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time

The error is saved before the uMQTTBroker reboot.

MasyaShu commented 6 years ago

Solved a problem: client.disconnect (); // Wait 15 seconds before retrying delay (15000);

MasyaShu commented 6 years ago

It did not work long :( https://image.prntscr.com/image/bPfHInkuQ-2mRddt9dAIFg.png

martin-ger commented 6 years ago

Could you send me the client's code? Guess, it has to do with session handling. For some reason the server rejects the CONN request (probably, because the session is still there?)

MasyaShu commented 6 years ago

include

include

// Update these with values suitable for your network.

const char ssid = "AU_01_WiFi"; const char password = "";

long lastReconnectAttempt = 0;

WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; int value = 0; IPAddress local_IP(192,168,4,22);

void setup() { pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(9600); setup_wifi(); client.setServer(local_IP, 1602); client.setCallback(callback); }

void setup_wifi() {

delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid);

WiFi.begin(ssid);

while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); }

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

// Switch on the LED if an 1 was received as first character if ((char)payload[0] == '1') { digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is acive low on the ESP-01) } else { digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH } lastReconnectAttempt = 0; }

void reconnect() { client.disconnect(); // Wait 9 seconds before retrying delay(30000); while (!client.connected()) { Serial.print("Attempting MQTT connection..."); //client.disconnect(); //delay(15000); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("outTopic", "hello world"); // ... and resubscribe client.subscribe("inTopic"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 17 seconds"); //client.disconnect(); // Wait 9 seconds before retrying // delay(30000); } } }

void loop() { //client.disconnect();
if (!client.connected()) { reconnect(); } else {

client.loop();

long nowT = millis(); if (nowT - lastMsg > 2000) { lastMsg = nowT; ++value; snprintf (msg, 75, "hello world #%ld", value); Serial.print("Publish message: "); Serial.println(msg); client.publish("outTopic", msg); }

} }

mree58 commented 6 years ago

I think, it could be the same problem. What do you think, is it the same problem ? Here is the video and the sample app :

https://drive.google.com/open?id=1HeRfEC9yxq0SSGp-Px8SQujcflGwReoN

martin-ger commented 6 years ago

Thanks - probably it is the same problem - no time today, will try to look into it over the weekend.

MasyaShu commented 6 years ago

Thank you. I would be very grateful if I can get rid of this problem.

martin-ger commented 6 years ago

Think, I found the problem. Introduced it, when trying to fix issue #3

It failed to clean up old sessions from the same client. Should be fixed now...

MasyaShu commented 6 years ago

Thank you, it's getting better. Now after reboot it is stably connected from the second time: https://image.prntscr.com/image/8HFba0bRQaauzAEEUYBQoQ.png

If the client uses "client.disconnect ();", then the connection does not exist.

martin-ger commented 6 years ago

Dies Not existiert means does not work?

Am 22. Januar 2018 05:58:15 schrieb MasyaShu notifications@github.com:

Thank you, it's getting better. Now after reboot it is stably connected from the second time: https://image.prntscr.com/image/8HFba0bRQaauzAEEUYBQoQ.png

If the client uses "client.disconnect ();", then the connection does not exist.

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/martin-ger/uMQTTBroker/issues/5#issuecomment-359325483

MasyaShu commented 6 years ago

Now the client turns on, sends a message and falls asleep, after 5 seconds wakes up, sends a message again. After he wakes up the connection to the broker occurs at the second attempt. I tried to disconnect from the broker before going to sleep: "client.disconnect ()", in this version it could not connect a second time.

MasyaShu commented 6 years ago

After it increased the sleep time (30 seconds), it started from the first time: https://image.prntscr.com/image/calH1rC2SZaT7mbgo8P1Lw.png

mree58 commented 6 years ago

Same result for me. Same error occurred, which could be seen in the video that I send.

martin-ger commented 6 years ago

Let me try to understand the setup:

mree58 commented 6 years ago
martin-ger commented 6 years ago

Tried to reproduce the problem, somewhat hard...

With the ESP as AP and IoT MQTT Dashboard (https://play.google.com/store/apps/details?id=com.thn.iotmqttdashboard) as sample client on Android it just works fine.

Pubsubclient on the AP for me doesn't even try to connect... (I don't see any TCP connect request on the ESP).

Could you help me an try the following on you setup: in "mqtt_server.c" in the library please uncomment the two lines:

40: #undef MQTT_INFO 41: #define MQTT_INFO os_printf

And add Serial.setDebugOutput(true); in your Arduino code?

This enables debug output in the MQTT server. The output might give some insight into what's going on...

MasyaShu commented 6 years ago

Starting MQTT broker Starting MQTT server on port 1602 MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 33 bytes of data received MQTT: TCP: data received 33 bytes (State: 15) MQTT: total_len: 33 MQTT: message_type: 1 MQTT: Connect received, message_len: 33 MQTT: Connect flags 2 MQTT: Keepalive 60 MQTT: Client id "gghdrj1516683805700" MQTT: Server task activated - state 20 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 15 bytes of data received MQTT: TCP: data received 15 bytes (State: 20) MQTT: total_len: 15 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 15 MQTT: Message id 1 MQTT: Subscribed topic /DVP/123 QoS 0 MQTT: Subscribe successful MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0001 MQTT_ClientCon_recv_cb(): 96 bytes of data received MQTT: TCP: data received 96 bytes (State: 20) MQTT: total_len: 17 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 17 MQTT: Message id 2 MQTT: Subscribed topic outTopic/# QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 13 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 13 MQTT: Message id 3 MQTT: Subscribed topic /DVP/# QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 15 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 15 MQTT: Message id 4 MQTT: Subscribed topic mybroker QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 12 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 12 MQTT: Message id 5 MQTT: Subscribed topic ggggg QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 18 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 18 MQTT: Message id 6 MQTT: Subscribed topic mybroker/sv QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 21 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 21 MQTT: Message id 7 MQTT: Subscribed topic MyBroker/count QoS 0 MQTT: Subscribe successful MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683805700 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0002 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683805700 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0003 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683805700 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0004 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683805700 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0005 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683805700 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0006 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683805700 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0007 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT_ClientCon_discon_cb(): client disconnected MQTT: DeleteClientCon MQTT: Broker already disconnecting MQTT: Server task activated - state 9 MQTT: Client disconnected MQTT: DeleteClientCon MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 33 bytes of data received MQTT: TCP: data received 33 bytes (State: 15) MQTT: total_len: 33 MQTT: message_type: 1 MQTT: Connect received, message_len: 33 MQTT: Connect flags 2 MQTT: Keepalive 60 MQTT: Client id "gghdrj1516683823586" MQTT: Server task activated - state 20 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 15 bytes of data received MQTT: TCP: data received 15 bytes (State: 20) MQTT: total_len: 15 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 15 MQTT: Message id 1 MQTT: Subscribed topic /DVP/123 QoS 0 MQTT: Subscribe successful MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0001 MQTT_ClientCon_recv_cb(): 96 bytes of data received MQTT: TCP: data received 96 bytes (State: 20) MQTT: total_len: 17 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 17 MQTT: Message id 2 MQTT: Subscribed topic outTopic/# QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 13 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 13 MQTT: Message id 3 MQTT: Subscribed topic /DVP/# QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 15 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 15 MQTT: Message id 4 MQTT: Subscribed topic mybroker QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 12 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 12 MQTT: Message id 5 MQTT: Subscribed topic ggggg QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 18 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 18 MQTT: Message id 6 MQTT: Subscribed topic mybroker/sv QoS 0 MQTT: Subscribe successful MQTT: Get another received message MQTT: total_len: 21 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 21 MQTT: Message id 7 MQTT: Subscribed topic MyBroker/count QoS 0 MQTT: Subscribe successful MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683823586 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0002 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683823586 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0003 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683823586 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0004 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683823586 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0005 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683823586 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0006 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516683823586 MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0007 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 12 MQTT: receive MQTT_MSG_TYPE_PINGREQ MQTT: Server task activated - state 20 MQTT: Sending, type: 13, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 12 MQTT: receive MQTT_MSG_TYPE_PINGREQ MQTT: Server task activated - state 20 MQTT: Sending, type: 13, id: 0000 MQTT_ClientCon_sent_cb(): Data sent

На клиенте: WiFi connected IP address: 192.168.4.121 Attempting MQTT connection... pm open,type:2 0 failed, rc=-4 try again in 5 seconds Attempting MQTT connection...connected Publish message: hello world #1 16870 state: 5 -> 0 (0) rm 0 pm close 7 del if0 usl

MasyaShu commented 6 years ago

If ESP does not go to sleep, and you use "client.disconnect ()", then on the client: 192.168.4.121 Attempting MQTT connection ... connected Publish message: hello world # 1 19705 Attempting MQTT connection ... pm open, type: 2 0 failed, rc = -4 try again in 10 seconds Attempting MQTT connection ... failed, rc = -4 try again in 10 seconds Attempting MQTT connection ... failed, rc = -4 try again in 10 seconds Attempting MQTT connection ... failed, rc = -4 try again in 10 seconds Attempting MQTT connection ... failed, rc = -4 try again in 10 seconds

On the broker: Starting MQTT server on port 1602 add 1 aid 1 station: 5c: cf: 7f: da: 01: 36 join, AID = 1 MQTT_ClientCon_connected_cb (): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb (): 27 bytes of data received MQTT: TCP: data received 27 bytes (State: 15) MQTT: total_len: 27 MQTT: message_type: 1 MQTT: Connect received, message_len: 27 MQTT: Connect flags 2 MQTT: Keepalive 15 MQTT: Client id "ESP8266Client" MQTT: Server task activated - state 20 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb (): Data sent MQTT_ClientCon_recv_cb (): 23 bytes of data received MQTT: TCP: data received 23 bytes (State: 20) MQTT: total_len: 23 MQTT: message_type: 3 MQTT: Publish received, message_len: 23 MQTT: Published topic "outTopic" MQTT: Matches to: MQTT: Client: LOCAL Topic: "#" QoS: 0 received topic 'outTopic' with data 'hello world' outTopic MQTT_ClientCon_recv_cb (): 14 bytes of data received MQTT: TCP: data received 14 bytes (State: 20) MQTT: total_len: 14 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 14 MQTT: Message id 2 MQTT: Subscribed topic inTopic QoS 0 MQTT: Subscribe successful MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0002 MQTT_ClientCon_recv_cb (): 33 bytes of data received MQTT: TCP: data received 33 bytes (State: 20) MQTT: total_len: 33 MQTT: message_type: 3 MQTT: Publish received, message_len: 33 MQTT: Published topic "outTopic / qqqqqq" MQTT: Matches to: MQTT: Client: LOCAL Topic: "#" QoS: 0 received topic 'outTopic / qqqqqq' with data 'hello world # 1' outTopic / qqqqqq MQTT_ClientCon_sent_cb (): Data sent MQTT_ClientCon_recv_cb (): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: Server task activated - state 7 MQTT: Disconnect MQTT_ClientCon_discon_cb (): client disconnected MQTT: DeleteClientCon MQTT: Broker already disconnecting MQTT_ClientCon_connected_cb (): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb (): 27 bytes of data received MQTT: TCP: data received 27 bytes (State: 15) MQTT: total_len: 27 MQTT: message_type: 1 MQTT: Connect received, message_len: 27 MQTT: Connect flags 2 MQTT: Keepalive 15 MQTT: Disconnect client: ESP8266Client MQTT: Client id "ESP8266Client" MQTT: Server task activated - state 8 MQTT: Disconnect MQTT_ClientCon_discon_cb (): client disconnected MQTT: DeleteClientCon MQTT_ClientCon_connected_cb (): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb (): 27 bytes of data received MQTT: TCP: data received 27 bytes (State: 15) MQTT: total_len: 27 MQTT: message_type: 1 MQTT: Connect received, message_len: 27 MQTT: Connect flags 2 MQTT: Keepalive 15 MQTT: Disconnect client: ESP8266Client MQTT: Client id "ESP8266Client" MQTT: Server task activated - state 8 MQTT: Disconnect MQTT_ClientCon_discon_cb (): client disconnected MQTT: DeleteClientCon MQTT_ClientCon_connected_cb (): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb (): 27 bytes of data received MQTT: TCP: data received 27 bytes (State: 15) MQTT: total_len: 27 MQTT: message_type: 1 MQTT: Connect received, message_len: 27 MQTT: Connect flags 2 MQTT: Keepalive 15 MQTT: Disconnect client: ESP8266Client MQTT: Client id "ESP8266Client" MQTT: Server task activated - state 8 MQTT: Disconnect MQTT_ClientCon_discon_cb (): client disconnected MQTT: DeleteClientCon MQTT_ClientCon_connected_cb (): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb (): 27 bytes of data received MQTT: TCP: data received 27 bytes (State: 15) MQTT: total_len: 27 MQTT: message_type: 1 MQTT: Connect received, message_len: 27 MQTT: Connect flags 2 MQTT: Keepalive 15 MQTT: Disconnect client: ESP8266Client MQTT: Client id "ESP8266Client" MQTT: Server task activated - state 8 MQTT: Disconnect

MasyaShu commented 6 years ago

If you do not use "client.disconnect ();" and to fill the client for 30 seconds, it works without fail. It also works without fail if the device is connected to a broker and is no longer disconnected.

MasyaShu commented 6 years ago

Even when another client is connected, the second client can not connect: MQTT_ClientCon_sent_cb (): Data sent station: 5c: cf: 7f: da: 01: 36 leave, AID = 2 rm 2 err already associed! station: a0: 20: a6: 16: c7: 54 leave, AID = 1 rm 1 MQTT_ClientCon_discon_cb (): client disconnected MQTT: DeleteClientCon add 1 aid 1 station: a0:20:a6:16:c7:54 join, AID = 1 MQTT_ClientCon_discon_cb(): client disconnected MQTT: DeleteClientCon MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 12 MQTT: receive MQTT_MSG_TYPE_PINGREQ MQTT: Server task activated - state 20 MQTT: Sending, type: 3, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516693570140 MQTT: Server task activated - state 20 MQTT: Sending, type: 3, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT: Next message to client: gghdrj1516693570140 MQTT: Server task activated - state 20 MQTT: Sending, type: 13, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 12 MQTT: receive MQTT_MSG_TYPE_PINGREQ MQTT: Server task activated - state 20 MQTT: Sending, type: 13, id: 0000 MQTT_ClientCon_sent_cb(): Data sent

Error: -2: MQTT_CONNECT_FAILED - the network connection failed

mree58 commented 6 years ago

Thanks for your interest. Here is the log output :

0,Configuring access point... 0,Ready for client port:51042 0,Babil_29E6 0,Babil_29E6 0,AP IP address: 0,192.168.4.1 0,Starting MQTT broker Starting MQTT server on port 51042 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,0 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,1 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,2 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,3 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,4 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,5 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,6 add 1 aid 1 station: 54:dc:1d:32:d7:c4 join, AID = 1 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,7 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,8 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,9 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,10 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,11 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,12 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,13 MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 32 bytes of data received MQTT: TCP: data received 32 bytes (State: 15) MQTT: total_len: 32 MQTT: message_type: 1 MQTT: Connect received, message_len: 32 MQTT: Connect flags 2 MQTT: Keepalive 60 MQTT: Client id "emree1516718890163" MQTT: Server task activated - state 20 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 14 bytes of data received MQTT: TCP: data received 14 bytes (State: 20) MQTT: total_len: 14 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 14 MQTT: Message id 1 MQTT: Subscribed topic Ws2812B QoS 0 MQTT: Subscribe successful MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0001 MQTT_ClientCon_sent_cb(): Data sent MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,14 MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: Server task activated - state 7 MQTT: Disconnect MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,15 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,16 MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 32 bytes of data received MQTT: TCP: data received 32 bytes (State: 15) MQTT: total_len: 32 MQTT: message_type: 1 MQTT: Connect received, message_len: 32 MQTT: Connect flags 2 MQTT: Keepalive 60 MQTT: Client id "emree1516718895769" MQTT: Server task activated - state 20 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 14 bytes of data received MQTT: TCP: data received 14 bytes (State: 20) MQTT: total_len: 14 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 14 MQTT: Message id 1 MQTT: Subscribed topic Ws2812B QoS 0 MQTT: Subscribe successful MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0001 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: Server task activated - state 7 MQTT: Disconnect MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,17 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,18 MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 32 bytes of data received MQTT: TCP: data received 32 bytes (State: 15) MQTT: total_len: 32 MQTT: message_type: 1 MQTT: Connect received, message_len: 32 MQTT: Connect flags 2 MQTT: Keepalive 60 MQTT: Client id "emree1516718908346" MQTT: Server task activated - state 20 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 14 bytes of data received MQTT: TCP: data received 14 bytes (State: 20) MQTT: total_len: 14 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 14 MQTT: Message id 1 MQTT: Subscribed topic Ws2812B QoS 0 MQTT: Subscribe successful MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0001 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,19 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: Server task activated - state 7 MQTT: Disconnect MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 32 bytes of data received MQTT: TCP: data received 32 bytes (State: 15) MQTT: total_len: 32 MQTT: message_type: 1 MQTT: Connect received, message_len: 32 MQTT: Connect flags 2 MQTT: Keepalive 60 MQTT: Client id "emree1516718922054" MQTT: Server task activated - state 20 MQTT: Sending, type: 2, id: 0000 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,20 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 14 bytes of data received MQTT: TCP: data received 14 bytes (State: 20) MQTT: total_len: 14 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 14 MQTT: Message id 1 MQTT: Subscribed topic Ws2812B QoS 0 MQTT: Subscribe successful MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0001 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: Server task activated - state 7 MQTT: Disconnect MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 32 bytes of data received MQTT: TCP: data received 32 bytes (State: 15) MQTT: total_len: 32 MQTT: message_type: 1 MQTT: Connect received, message_len: 32 MQTT: Connect flags 2 MQTT: Keepalive 60 MQTT: Client id "emree1516718927890" MQTT: Server task activated - state 20 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 14 bytes of data received MQTT: TCP: data received 14 bytes (State: 20) MQTT: total_len: 14 MQTT: message_type: 8 MQTT: Subscribe received, message_len: 14 MQTT: Message id 1 MQTT: Subscribed topic Ws2812B QoS 0 MQTT: Subscribe successful MQTT: Server task activated - state 20 MQTT: Sending, type: 9, id: 0001 MQTT_ClientCon_sent_cb(): Data sent MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,21 MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 20) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: Server task activated - state 7 MQTT: Disconnect MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,22 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,23 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,24 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,25 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,26

And here is the screen capture that I uploaded from the program that you suggested :

https://drive.google.com/drive/folders/1HeRfEC9yxq0SSGp-Px8SQujcflGwReoN

martin-ger commented 6 years ago

Thanks for the input. I reworked the logic of disconnection. Now your sample works just fine, even with client.disconnect() for me...

mree58 commented 6 years ago

Thank you, I updated it with your latest committed source. But sadly result is same :(

MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,32 MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 40 bytes of data received MQTT: TCP: data received 40 bytes (State: 14) MQTT: total_len: 40 MQTT: message_type: 1 MQTT: Connect received, message_len: 40 MQTT: Connect flags c2 MQTT: Keepalive 60 MQTT: Client id "11516869545907" MQTT: Username izzet MQTT: Password den MQTT: Server task activated - state 19 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,33 MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 19) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: DeleteClientCon MQTT: Server task activated - state 6 MQTT: Disconnect MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 40 bytes of data received MQTT: TCP: data received 40 bytes (State: 14) MQTT: total_len: 40 MQTT: message_type: 1 MQTT: Connect received, message_len: 40 MQTT: Connect flags c2 MQTT: Keepalive 60 MQTT: Client id "11516869550456" MQTT: Username izzet MQTT: Password den MQTT: Server task activated - state 19 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 19) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: DeleteClientCon MQTT: Server task activated - state 6 MQTT: Disconnect MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,34 MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 40 bytes of data received MQTT: TCP: data received 40 bytes (State: 14) MQTT: total_len: 40 MQTT: message_type: 1 MQTT: Connect received, message_len: 40 MQTT: Connect flags c2 MQTT: Keepalive 60 MQTT: Client id "11516869554184" MQTT: Username izzet MQTT: Password den MQTT: Server task activated - state 19 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 19) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: DeleteClientCon MQTT: Server task activated - state 6 MQTT: Disconnect MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,35 MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 40 bytes of data received MQTT: TCP: data received 40 bytes (State: 14) MQTT: total_len: 40 MQTT: message_type: 1 MQTT: Connect received, message_len: 40 MQTT: Connect flags c2 MQTT: Keepalive 60 MQTT: Client id "11516869559435" MQTT: Username izzet MQTT: Password den MQTT: Server task activated - state 19 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 19) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: DeleteClientCon MQTT: Server task activated - state 6 MQTT: Disconnect MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,36 MQTT_ClientCon_connected_cb(): Client connected MQTT: InitClientCon MQTT_ClientCon_recv_cb(): 40 bytes of data received MQTT: TCP: data received 40 bytes (State: 14) MQTT: total_len: 40 MQTT: message_type: 1 MQTT: Connect received, message_len: 40 MQTT: Connect flags c2 MQTT: Keepalive 60 MQTT: Client id "11516869563851" MQTT: Username izzet MQTT: Password den MQTT: Server task activated - state 19 MQTT: Sending, type: 2, id: 0000 MQTT_ClientCon_sent_cb(): Data sent MQTT_ClientCon_recv_cb(): 2 bytes of data received MQTT: TCP: data received 2 bytes (State: 19) MQTT: total_len: 2 MQTT: message_type: 14 MQTT: receive MQTT_MSG_TYPE_DISCONNECT MQTT: ServerDisconnect MQTT: DeleteClientCon MQTT: Server task activated - state 6 MQTT: Disconnect MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,37 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,38 MQTT: Client: LOCAL Topic: "#" QoS: 0 0,Self_Msg,39

martin-ger commented 6 years ago

For me, it seems to work... Using this client, I can do as many (re-)connects as I like:

#include <PubSubClient.h>
#include <ESP8266WiFi.h>

// Update these with values suitable for your network.
const char* ssid = ".....";
const char* password = ".....";

long lastReconnectAttempt = 0;

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
IPAddress local_IP(192,168,4,1);

void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
Serial.setDebugOutput(true);
setup_wifi();
client.setServer(local_IP,  1883);
client.setCallback(callback);
}

void setup_wifi() {

delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

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

// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
lastReconnectAttempt = 0;
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    //clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {
//client.disconnect();
if (!client.connected()) {
reconnect();
}
else {

client.loop();

long nowT = millis();
if (nowT - lastMsg > 2000) {
lastMsg = nowT;
++value;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);

client.disconnect();
}

}
}
martin-ger commented 6 years ago

According to your log you are using user/password - maybe that is the reason here, why you get a reject of the connect request?

MasyaShu commented 6 years ago

Thanks, it works! I will describe three situations:

  1. client.disconnect ();    ESP.deepSleep (5e6); After the ESP wakes up, the connection happens the first time!

  2. //client.disconnect ();    ESP.deepSleep (5e6); After the ESP wakes up, the connection is from the second time!

  3. client.disconnect ();    //ESP.deepSleep(5e6); ESP is connected from the first time!

Another digression, for a long time struggled with the error rc = -2. It turned out that I did not have a line: WiFi.mode (WIFI_STA) before WiFi.begin (ssid);

Thanks again for your work!

mree58 commented 6 years ago

@martin-ger I removed username/password, but it was not the problem in this case. I didn't use android client for new tests. In one video used your code as untouched and for another video used a modified version of client. Tried both STA and AP modes. I uploaded the videos, and sample codes that I builded.

client in sta mode, broker in ap mode. Problem occured after a few connection.

client in sta mode, broker in sta mode, and used a modem. It didnt connect at first, at min 2:54 it established a connection somehow, but then connection failed.

https://drive.google.com/drive/folders/1Vr1rqzueW5nZ3B6VGBVFF3zW7HQDx_Is

I know you are trying to solve this issue for such a unique and brilliant library that no one ever tried to make one so far for ESP. I am grateful for this. Thanks again.