Skip to content
New issue

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

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

Already on GitHub? # to your account

Client ID is not detected and therefore a random ID is used #76

Closed
skrapi opened this issue Sep 16, 2020 · 4 comments
Closed

Client ID is not detected and therefore a random ID is used #76

skrapi opened this issue Sep 16, 2020 · 4 comments

Comments

@skrapi
Copy link
Contributor

skrapi commented Sep 16, 2020

Hi,

I am attempting to connect to the Thingstream MQTT broker which requires that you use specific Client IDs, I have been able to do this using the paho mqtt library, but I am running into an issue with the Flask-MQTT library.

I set the id as shown below:
app.config['MQTT_CLIENT_ID'] = "device:69f2xxxxdaad"

and print it out to be sure:
print(app.config['MQTT_CLIENT_ID'])

but when looking at the logs I see this:

2020-09-16T09:21:37.184078+00:00 app[web.1]: device:69f2xxxxdaad
2020-09-16T09:21:38.613395+00:00 app[web.1]: device:69f2xxxxdaad
2020-09-16T09:21:38.624120+00:00 app[web.1]: 16 Received CONNACK (0, 2), attempting to use non-empty CID
2020-09-16T09:21:38.692237+00:00 app[web.1]: 16 Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k3600) client_id=5qkeclEQ4fL9bef6E4CpRq
2020-09-16T09:21:38.775075+00:00 app[web.1]: 16 Received CONNACK (0, 2), attempting to use non-empty CID
2020-09-16T09:21:38.778689+00:00 app[web.1]: 16 Received CONNACK (0, 4)
2020-09-16T09:21:38.877357+00:00 app[web.1]: 16 Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k3600) client_id=6JygjUHZfvIEUmfdIXcFai
2020-09-16T09:21:38.972989+00:00 app[web.1]: 16 Received CONNACK (0, 4)
2020-09-16T09:21:39.852414+00:00 app[web.1]: 16 Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k3600) client_id=5qkeclEQ4fL9bef6E4CpRq
2020-09-16T09:21:39.941388+00:00 app[web.1]: 16 Received CONNACK (0, 4)
2020-09-16T09:21:40.044825+00:00 app[web.1]: 16 Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k3600) client_id=6JygjUHZfvIEUmfdIXcFai
2020-09-16T09:21:40.135002+00:00 app[web.1]: 16 Received CONNACK (0, 4)

The device ID is not detected and a random one is used. Any help would be greatly appreciated.

Thanks,
Sylvan

@kim-acmehi
Copy link

I found this problem a while back and have been testing a fix. It looks like the current code is mishandling the "MQTT_CLIENT_ID" coming in from the environment. I have highlighted two lines below that seem to fix the problem. You can try this and see if it fixes the problem for you. It does for me. If so I can submit a pull request.

From flask_mqtt/init.py:

def init_app(self, app: Flask) -> None:
    """Init the Flask-MQTT addon."""
    if "MQTT_CLIENT_ID" in app.config:  # <-- check this early
        self.client_id = app.config["MQTT_CLIENT_ID"]  # <-- must get this here for next lines to work

    if isinstance(self.client_id, unicode):
        self.client._client_id = self.client_id.encode("utf-8")
    else:
        self.client._client_id = self.client_id

    self.client._transport = app.config.get("MQTT_TRANSPORT", "tcp").lower()
    self.client._protocol = app.config.get("MQTT_PROTOCOL_VERSION", MQTTv311)
    self.client._clean_session = self.clean_session
    self.client.on_connect = self._handle_connect
    self.client.on_disconnect = self._handle_disconnect

    if "MQTT_CLIENT_ID" in app.config:
        self.client_id = app.config["MQTT_CLIENT_ID"]

@skrapi
Copy link
Contributor Author

skrapi commented Sep 17, 2020

This change of simply moving the If statement to the first line works for me. A PR would be greatly appreciated as I am running this on Heroku and can't have locally modified packages.

@stlehmann
Copy link
Owner

Fixed by #77

@stlehmann
Copy link
Owner

Thanks @skrapi for fixing this issue

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

No branches or pull requests

3 participants