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

Client disconected due protocol error when counting PUBLISH packets from 0 #203

Open
apsite opened this issue Nov 13, 2021 · 6 comments
Open

Comments

@apsite
Copy link

apsite commented Nov 13, 2021

  • Arduino board: ESP8266 (LoLin NodeMcu v3)
  • Adafruit_MQTT_library: 2.4.2
  • MQTT Server: Eclipse mosquitto 2.0.12

Hello, I discovered strange problem during publishing messages. Every first PUBLISH packet gets no response and additionally server closes connection with:

Client 7879f4a.... disconnected due to protocol error.

...when packets are counted from 0. When I set packet_id_counter variable initially to 1 in Adafruit_MQTT::Adafruit_MQTT, everything works OK

Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
                             const char *user, const char *pass) {
  servername = server;
  portnum = port;
  clientid = cid;
  username = user;
  password = pass;

  // reset subscriptions
  for (uint8_t i = 0; i < MAXSUBSCRIPTIONS; i++) {
    subscriptions[i] = 0;
  }

  will_topic = 0;
  will_payload = 0;
  will_qos = 0;
  will_retain = 0;

  keepAliveInterval = MQTT_CONN_KEEPALIVE;

  // changed code
  packet_id_counter = 1;
}

This is also related to QOS level. packet_id_counter can be set to 0 but only when QOS 0 is used.

I suspect that this is exact same case as described in MQTT docs:

SUBSCRIBE, UNSUBSCRIBE, and PUBLISH (in cases where QoS > 0) Control Packets MUST contain a non-zero 16-bit Packet Identifier [MQTT-2.3.1-1].

@apsite apsite changed the title Client disconected due protocol error when counting packets from 0 Client disconected due protocol error when counting PUBLISH packets from 0 Nov 13, 2021
@lvqier
Copy link

lvqier commented Jan 18, 2022

Same problem on sending SUBSCRIBE immediately after connect.

@Mollayo
Copy link

Mollayo commented Jan 24, 2022

I agree with you both. Actually, the other MQTT library (pubsubclient) is using a packet id counter which is always different from 0. See here:

@brentru : This bug requires modifying few lines. I can make the modifications myself and make a pull request.

@brentru
Copy link
Member

brentru commented Jan 26, 2022

@Mollayo Thank you for finding this, please make a pull request and tag me so I can review it.

@Mollayo
Copy link

Mollayo commented Jan 28, 2022

@brentru I have submitted the pull request. It is available for review. See here: #207

1 similar comment
@Mollayo
Copy link

Mollayo commented Jan 28, 2022

@brentru I have submitted the pull request. It is available for review. See here: #207

@einglis
Copy link

einglis commented Dec 4, 2022

@Mollayo @brentru This looks like it stalled, and all three (!) PRs were closed without explanation.
Is there anything you or I could do to move it forward?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants