Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with MQTT publish / ESP32 #1210

Closed
nFeno opened this issue Mar 26, 2019 · 11 comments
Closed

Issue with MQTT publish / ESP32 #1210

nFeno opened this issue Mar 26, 2019 · 11 comments

Comments

@nFeno
Copy link

nFeno commented Mar 26, 2019

Hi,
Seem to have same issue @sislakd in #1198 (comment)_

I have following line in mosquitto.log :

1553609007: New connection from 10.228.5.126 on port 1883.
1553609007: New client connected from 10.228.5.126 as arduinoClient (c1, k15).
1553609007: Client arduinoClient disconnected.
1553609007: New connection from 10.228.5.126 on port 1883.
1553609007: New client connected from 10.228.5.126 as arduinoClient (c1, k15).
1553609007: Client arduinoClient disconnected.
1553609007: New connection from 10.228.5.126 on port 1883.
1553609007: New client connected from 10.228.5.126 as arduinoClient (c1, k15).
1553609007: Client arduinoClient disconnected.

My mosquitto.conf was very simple :

pid_file /var/run/mosquitto.pid
log_dest file /var/log/mosquitto/mosquitto.log
allow_anonymous true
listener 1883

the sketch is very simple :

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

IPAddress server(10, 228, 5, 91);

void callback(char* topic, byte* message, unsigned int length) {
  // handle message arrived
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;

  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();
}

WiFiClient ethClient;
PubSubClient client(ethClient);

long lastReconnectAttempt = 0;

boolean reconnect() {
  if (client.connect("arduinoClient")) {
    client.publish("outTopic", "hello world");
    client.subscribe("inTopic");
  }
  return client.connected();
}

void setup() {
  Serial.begin(9600);

  WiFi.begin("...", "...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

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

  delay(1500);
  lastReconnectAttempt = 0;
}

void loop() {
  if (!client.connected()) {
    long now = millis();
    if (now - lastReconnectAttempt > 50000) {
      lastReconnectAttempt = now;
      // Attempt to reconnect
      if (reconnect()) {
        lastReconnectAttempt = 0;
      }
    }
  } else {
    client.loop();
  }
}

My question is why do I have a disconnect right after the connection?

@sislakd
Copy link

sislakd commented Mar 26, 2019

My issue has to be related to some change in authentication of newer Mosquitto version because it is working with PubSubClient from ESP8266 if I allow anonymous connections and remove acls in Mosquitto. But otherwise it looked very similar. MQTT connection was dropped by client (by ESP8266) after short timeout as there was no response packet from Mosquitto server. I verified this using tcpdump while TLS was disabled on MQTT.

@ralight
Copy link
Contributor

ralight commented Mar 26, 2019

@sislakd Is there any chance you could share a tcpdump file (no TLS)? We can do that privately if you would prefer.

@nFeno
Copy link
Author

nFeno commented Mar 27, 2019

I install mosquitto on my windows laptop and check if I have the same issue :-(

@sislakd
Copy link

sislakd commented Mar 27, 2019

@ralight When I've tried to capture new tcpdump with the latest version (mosquitto_1.5.8-0mosquitto1_armhf), I found that PubSubClient connection from ESP8266 is working well again. The issue was observed on some previous version for which I haven't kept pcap recording. As it is already working, there is no more issues from my side.

@ralight
Copy link
Contributor

ralight commented Mar 27, 2019

@sislakd Thanks for checking and replying.

@pfeno Let me know if you find anything else out.

@KurabiyeCanavari
Copy link

KurabiyeCanavari commented Mar 28, 2019

@pfeno Is your broker located in Raspberry Pi, if so, I had the same issue with ESP_IDF with older versions of Mosquitto.What is your version? OH WRONG: I remember just now, I also changed my ESP_IDF version to current master.
I switched to 1.5.6, then it was never disconnected but had a different issue: #1208

@nFeno
Copy link
Author

nFeno commented Mar 28, 2019

@KurabiyeCanavari I have version 1.4.10-3+deb9u4 for mosquitto and 1.0.1 of Arduino package

@sislakd need to continue to check (seems to work with anonymous but some difficulty with user credential)

@sislakd
Copy link

sislakd commented Mar 28, 2019

@pfeno Add mosquitto repository into your raspbian and try the latest version mosquitto 1.5.8 . This one is working well with user credentials, acls and TLS as well.

@nFeno
Copy link
Author

nFeno commented Mar 28, 2019

@sislakd need to return home because at work, some trouble with proxy ;)

@ralight
Copy link
Contributor

ralight commented Apr 11, 2019

Can this be closed? It sounds very much like a network problem rather than a mosquitto problem.

@nFeno
Copy link
Author

nFeno commented Apr 12, 2019 via email

@nFeno nFeno closed this as completed Apr 16, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants